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

Function _ezclump

numpy/ma/extras.py:1973–1999  ·  view source on GitHub ↗

Finds the clumps (groups of data with the same values) for a 1D bool array. Returns a series of slices.

(mask)

Source from the content-addressed store, hash-verified

1971
1972
1973def _ezclump(mask):
1974 """
1975 Finds the clumps (groups of data with the same values) for a 1D bool array.
1976
1977 Returns a series of slices.
1978 """
1979 if mask.ndim > 1:
1980 mask = mask.ravel()
1981 idx = (mask[1:] ^ mask[:-1]).nonzero()
1982 idx = idx[0] + 1
1983
1984 if mask[0]:
1985 if len(idx) == 0:
1986 return [slice(0, mask.size)]
1987
1988 r = [slice(0, idx[0])]
1989 r.extend((slice(left, right)
1990 for left, right in zip(idx[1:-1:2], idx[2::2])))
1991 else:
1992 if len(idx) == 0:
1993 return []
1994
1995 r = [slice(left, right) for left, right in zip(idx[:-1:2], idx[1::2])]
1996
1997 if mask[-1]:
1998 r.append(slice(idx[-1], mask.size))
1999 return r
2000
2001
2002def clump_unmasked(a):

Callers 2

clump_unmaskedFunction · 0.85
clump_maskedFunction · 0.85

Calls 2

nonzeroMethod · 0.80
ravelMethod · 0.45

Tested by

no test coverage detected