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.
(mask)
| 1758 | |
| 1759 | |
| 1760 | def flatten_mask(mask): |
| 1761 | """ |
| 1762 | Returns a completely flattened version of the mask, where nested fields |
| 1763 | are collapsed. |
| 1764 | |
| 1765 | Parameters |
| 1766 | ---------- |
| 1767 | mask : array_like |
| 1768 | Input array, which will be interpreted as booleans. |
| 1769 | |
| 1770 | Returns |
| 1771 | ------- |
| 1772 | flattened_mask : ndarray of bools |
| 1773 | The flattened input. |
| 1774 | |
| 1775 | Examples |
| 1776 | -------- |
| 1777 | >>> mask = np.array([0, 0, 1]) |
| 1778 | >>> np.ma.flatten_mask(mask) |
| 1779 | array([False, False, True]) |
| 1780 | |
| 1781 | >>> mask = np.array([(0, 0), (0, 1)], dtype=[('a', bool), ('b', bool)]) |
| 1782 | >>> np.ma.flatten_mask(mask) |
| 1783 | array([False, False, False, True]) |
| 1784 | |
| 1785 | >>> mdtype = [('a', bool), ('b', [('ba', bool), ('bb', bool)])] |
| 1786 | >>> mask = np.array([(0, (0, 0)), (0, (0, 1))], dtype=mdtype) |
| 1787 | >>> np.ma.flatten_mask(mask) |
| 1788 | array([False, False, False, False, False, True]) |
| 1789 | |
| 1790 | """ |
| 1791 | |
| 1792 | def _flatmask(mask): |
| 1793 | "Flatten the mask and returns a (maybe nested) sequence of booleans." |
| 1794 | mnames = mask.dtype.names |
| 1795 | if mnames is not None: |
| 1796 | return [flatten_mask(mask[name]) for name in mnames] |
| 1797 | else: |
| 1798 | return mask |
| 1799 | |
| 1800 | def _flatsequence(sequence): |
| 1801 | "Generates a flattened version of the sequence." |
| 1802 | try: |
| 1803 | for element in sequence: |
| 1804 | if hasattr(element, '__iter__'): |
| 1805 | yield from _flatsequence(element) |
| 1806 | else: |
| 1807 | yield element |
| 1808 | except TypeError: |
| 1809 | yield sequence |
| 1810 | |
| 1811 | mask = np.asarray(mask) |
| 1812 | flattened = _flatsequence(_flatmask(mask)) |
| 1813 | return np.array([_ for _ in flattened], dtype=bool) |
| 1814 | |
| 1815 | |
| 1816 | def _check_mask_axis(mask, axis, keepdims=np._NoValue): |