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)
| 1923 | |
| 1924 | |
| 1925 | def notmasked_edges(a, axis=None): |
| 1926 | """ |
| 1927 | Find the indices of the first and last unmasked values along an axis. |
| 1928 | |
| 1929 | If all values are masked, return None. Otherwise, return a list |
| 1930 | of two tuples, corresponding to the indices of the first and last |
| 1931 | unmasked values respectively. |
| 1932 | |
| 1933 | Parameters |
| 1934 | ---------- |
| 1935 | a : array_like |
| 1936 | The input array. |
| 1937 | axis : int, optional |
| 1938 | Axis along which to perform the operation. |
| 1939 | If None (default), applies to a flattened version of the array. |
| 1940 | |
| 1941 | Returns |
| 1942 | ------- |
| 1943 | edges : ndarray or list |
| 1944 | An array of start and end indexes if there are any masked data in |
| 1945 | the array. If there are no masked data in the array, `edges` is a |
| 1946 | list of the first and last index. |
| 1947 | |
| 1948 | See Also |
| 1949 | -------- |
| 1950 | flatnotmasked_contiguous, flatnotmasked_edges, notmasked_contiguous |
| 1951 | clump_masked, clump_unmasked |
| 1952 | |
| 1953 | Examples |
| 1954 | -------- |
| 1955 | >>> import numpy as np |
| 1956 | >>> a = np.arange(9).reshape((3, 3)) |
| 1957 | >>> m = np.zeros_like(a) |
| 1958 | >>> m[1:, 1:] = 1 |
| 1959 | |
| 1960 | >>> am = np.ma.array(a, mask=m) |
| 1961 | >>> np.array(am[~am.mask]) |
| 1962 | array([0, 1, 2, 3, 6]) |
| 1963 | |
| 1964 | >>> np.ma.notmasked_edges(am) |
| 1965 | array([0, 6]) |
| 1966 | |
| 1967 | """ |
| 1968 | a = asarray(a) |
| 1969 | if axis is None or a.ndim == 1: |
| 1970 | return flatnotmasked_edges(a) |
| 1971 | m = getmaskarray(a) |
| 1972 | idx = array(np.indices(a.shape), mask=np.asarray([m] * a.ndim)) |
| 1973 | return [tuple(idx[i].min(axis).compressed() for i in range(a.ndim)), |
| 1974 | tuple(idx[i].max(axis).compressed() for i in range(a.ndim)), ] |
| 1975 | |
| 1976 | |
| 1977 | def flatnotmasked_contiguous(a): |
searching dependent graphs…