MCPcopy Create free account
hub / github.com/numpy/numpy / notmasked_edges

Function notmasked_edges

numpy/ma/extras.py:1788–1836  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

1786
1787
1788def 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
1839def flatnotmasked_contiguous(a):

Callers 1

test_edgesMethod · 0.90

Calls 7

flatnotmasked_edgesFunction · 0.85
getmaskarrayFunction · 0.85
asarrayFunction · 0.70
arrayFunction · 0.70
compressedMethod · 0.45
minMethod · 0.45
maxMethod · 0.45

Tested by 1

test_edgesMethod · 0.72