latools/filtering/filters.py
def defrag(filt, threshold=3, mode='include'):
    """
    'Defragment' a filter.

    Parameters
    ----------
    filt : boolean array
        A filter
    threshold : int
        Consecutive values equal to or below this threshold
        length are considered fragments, and will be removed.
    mode : str
        Wheter to change False fragments to True ('include')
        or True fragments to False ('exclude')

    Returns
    -------
    defragmented filter : boolean array
    """
    if bool_2_indices(filt) is None:
        return filt

    if mode == 'include':
        inds = bool_2_indices(~filt) + 1
        rep = True
    if mode == 'exclude':
        inds = bool_2_indices(filt) + 1
        rep = False

    rem = (np.diff(inds) <= threshold)[:, 0]

    cfilt = filt.copy()
    if any(rem):
        for lo, hi in inds[rem]:
            cfilt[lo:hi] = rep

    return cfilt
def defrag(filt, threshold=3, mode='include'):
    """
    'Defragment' a filter.

    Parameters
    ----------
    filt : boolean array
        A filter
    threshold : int
        Consecutive values equal to or below this threshold
        length are considered fragments, and will be removed.
    mode : str
        Wheter to change False fragments to True ('include')
        or True fragments to False ('exclude')

    Returns
    -------
    defragmented filter : boolean array
    """
    if bool_2_indices(filt) is None:
        return filt

    if mode == 'include':
        inds = bool_2_indices(~filt) + 1
        rep = True
    if mode == 'exclude':
        inds = bool_2_indices(filt) + 1
        rep = False

    rem = (np.diff(inds) <= threshold)[:, 0]

    cfilt = filt.copy()
    if any(rem):
        for lo, hi in inds[rem]:
            cfilt[lo:hi] = rep

    return cfilt
'Defragment' a filter.

    Parameters
    ----------
    filt : boolean array
        A filter
    threshold : int
        Consecutive values equal to or below this threshold
        length are considered fragments, and will be removed.
    mode : str
        Wheter to change False fragments to True ('include')
        or True fragments to False ('exclude')

    Returns
    -------
    defragmented filter : boolean array