def defrag(filt, threshold=<NUM_LIT:3>, mode='<STR_LIT>'):
if bool_2_indices(filt) is None:<EOL><INDENT>return filt<EOL><DEDENT>if mode == '<STR_LIT>':<EOL><INDENT>inds = bool_2_indices(~filt) + <NUM_LIT:1><EOL>rep = True<EOL><DEDENT>if mode == '<STR_LIT>':<EOL><INDENT>inds = bool_2_indices(filt) + <NUM_LIT:1><EOL>rep = False<EOL><DEDENT>rem = (np.diff(inds) <= threshold)[:, <NUM_LIT:0>]<EOL>cfilt = filt.copy()<EOL>if any(rem):<EOL><INDENT>for lo, hi in inds[rem]:<EOL><INDENT>cfilt[lo:hi] = rep<EOL><DEDENT><DEDENT>return cfilt<EOL>
'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