Find the indices of the first and last unmasked values along an axis. If all values are masked, return None. Otherwise, return a list of two tuples, corresponding to the indices of the first and last unmasked values respectively. Parameters ---------- a : array_like
(a, axis=None)
| 1786 | |
| 1787 | |
| 1788 | def notmasked_edges(a, axis=None): |
| 1789 | """ |
| 1790 | Find the indices of the first and last unmasked values along an axis. |
| 1791 | |
| 1792 | If all values are masked, return None. Otherwise, return a list |
| 1793 | of two tuples, corresponding to the indices of the first and last |
| 1794 | unmasked values respectively. |
| 1795 | |
| 1796 | Parameters |
| 1797 | ---------- |
| 1798 | a : array_like |
| 1799 | The input array. |
| 1800 | axis : int, optional |
| 1801 | Axis along which to perform the operation. |
| 1802 | If None (default), applies to a flattened version of the array. |
| 1803 | |
| 1804 | Returns |
| 1805 | ------- |
| 1806 | edges : ndarray or list |
| 1807 | An array of start and end indexes if there are any masked data in |
| 1808 | the array. If there are no masked data in the array, `edges` is a |
| 1809 | list of the first and last index. |
| 1810 | |
| 1811 | See Also |
| 1812 | -------- |
| 1813 | flatnotmasked_contiguous, flatnotmasked_edges, notmasked_contiguous |
| 1814 | clump_masked, clump_unmasked |
| 1815 | |
| 1816 | Examples |
| 1817 | -------- |
| 1818 | >>> a = np.arange(9).reshape((3, 3)) |
| 1819 | >>> m = np.zeros_like(a) |
| 1820 | >>> m[1:, 1:] = 1 |
| 1821 | |
| 1822 | >>> am = np.ma.array(a, mask=m) |
| 1823 | >>> np.array(am[~am.mask]) |
| 1824 | array([0, 1, 2, 3, 6]) |
| 1825 | |
| 1826 | >>> np.ma.notmasked_edges(am) |
| 1827 | array([0, 6]) |
| 1828 | |
| 1829 | """ |
| 1830 | a = asarray(a) |
| 1831 | if axis is None or a.ndim == 1: |
| 1832 | return flatnotmasked_edges(a) |
| 1833 | m = getmaskarray(a) |
| 1834 | idx = array(np.indices(a.shape), mask=np.asarray([m] * a.ndim)) |
| 1835 | return [tuple([idx[i].min(axis).compressed() for i in range(a.ndim)]), |
| 1836 | tuple([idx[i].max(axis).compressed() for i in range(a.ndim)]), ] |
| 1837 | |
| 1838 | |
| 1839 | def flatnotmasked_contiguous(a): |