def flatten_mask(mask):
<EOL>def _flatmask(mask):<EOL><INDENT>"<STR_LIT>"<EOL>mnames = mask.dtype.names<EOL>if mnames:<EOL><INDENT>return [flatten_mask(mask[name]) for name in mnames]<EOL><DEDENT>else:<EOL><INDENT>return mask<EOL><DEDENT><DEDENT>def _flatsequence(sequence):<EOL><INDENT>"<STR_LIT>"<EOL>try:<EOL><INDENT>for element in sequence:<EOL><INDENT>if hasattr(element, '<STR_LIT>'):<EOL><INDENT>for f in _flatsequence(element):<EOL><INDENT>yield f<EOL><DEDENT><DEDENT>else:<EOL><INDENT>yield element<EOL><DEDENT><DEDENT><DEDENT>except TypeError:<EOL><INDENT>yield sequence<EOL><DEDENT><DEDENT>mask = np.asarray(mask)<EOL>flattened = _flatsequence(_flatmask(mask))<EOL>return np.array([_ for _ in flattened], dtype=bool)<EOL>
Returns a completely flattened version of the mask, where nested fields
are collapsed.

Parameters
----------
mask : array_like
    Input array, which will be interpreted as booleans.

Returns
-------
flattened_mask : ndarray of bools
    The flattened input.

Examples
--------
>>> mask = np.array([0, 0, 1], dtype=np.bool)
>>> flatten_mask(mask)
array([False, False,  True], dtype=bool)

>>> mask = np.array([(0, 0), (0, 1)], dtype=[('a', bool), ('b', bool)])
>>> flatten_mask(mask)
array([False, False, False,  True], dtype=bool)

>>> mdtype = [('a', bool), ('b', [('ba', bool), ('bb', bool)])]
>>> mask = np.array([(0, (0, 0)), (0, (0, 1))], dtype=mdtype)
>>> flatten_mask(mask)
array([False, False, False, False, False,  True], dtype=bool)