Skip to content

Commit 701f0a6

Browse files
ENH: add ndenumerate specialization for masked arrays
1 parent 79a5000 commit 701f0a6

File tree

6 files changed

+88
-7
lines changed

6 files changed

+88
-7
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
`ndenumerate` specialization for masked arrays
2+
----------------------------------------------
3+
The masked array module now provides the `numpy.ma.ndenumerate` function,
4+
an alternative to `numpy.ndenumerate` that skips masked values.

doc/source/reference/routines.ma.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Finding masked data
190190
.. autosummary::
191191
:toctree: generated/
192192

193+
ma.ndenumerate
193194
ma.flatnotmasked_contiguous
194195
ma.flatnotmasked_edges
195196
ma.notmasked_contiguous

numpy/ma/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ from numpy.ma.extras import (
216216
masked_all_like as masked_all_like,
217217
median as median,
218218
mr_ as mr_,
219+
ndenumerate as ndenumerate,
219220
notmasked_contiguous as notmasked_contiguous,
220221
notmasked_edges as notmasked_edges,
221222
polyfit as polyfit,

numpy/ma/extras.py

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
"""
1111
__all__ = [
1212
'apply_along_axis', 'apply_over_axes', 'atleast_1d', 'atleast_2d',
13-
'atleast_3d', 'average', 'clump_masked', 'clump_unmasked',
14-
'column_stack', 'compress_cols', 'compress_nd', 'compress_rowcols',
15-
'compress_rows', 'count_masked', 'corrcoef', 'cov', 'diagflat', 'dot',
16-
'dstack', 'ediff1d', 'flatnotmasked_contiguous', 'flatnotmasked_edges',
17-
'hsplit', 'hstack', 'isin', 'in1d', 'intersect1d', 'mask_cols', 'mask_rowcols',
18-
'mask_rows', 'masked_all', 'masked_all_like', 'median', 'mr_',
13+
'atleast_3d', 'average', 'clump_masked', 'clump_unmasked', 'column_stack',
14+
'compress_cols', 'compress_nd', 'compress_rowcols', 'compress_rows',
15+
'count_masked', 'corrcoef', 'cov', 'diagflat', 'dot', 'dstack', 'ediff1d',
16+
'flatnotmasked_contiguous', 'flatnotmasked_edges', 'hsplit', 'hstack',
17+
'isin', 'in1d', 'intersect1d', 'mask_cols', 'mask_rowcols', 'mask_rows',
18+
'masked_all', 'masked_all_like', 'median', 'mr_', 'ndenumerate',
1919
'notmasked_contiguous', 'notmasked_edges', 'polyfit', 'row_stack',
2020
'setdiff1d', 'setxor1d', 'stack', 'unique', 'union1d', 'vander', 'vstack',
2121
]
@@ -1520,6 +1520,51 @@ def __init__(self):
15201520
#---- Find unmasked data ---
15211521
#####--------------------------------------------------------------------------
15221522

1523+
def ndenumerate(a):
1524+
"""
1525+
Multidimensional index iterator.
1526+
1527+
Return an iterator yielding pairs of array coordinates and values of
1528+
elements that are not masked.
1529+
1530+
Parameters
1531+
----------
1532+
a : array_like
1533+
An array with (possibly) masked elements.
1534+
1535+
See Also
1536+
--------
1537+
numpy.ndenumerate : Equivalent function ignoring any mask.
1538+
1539+
Examples
1540+
--------
1541+
>>> a = np.ma.arange(9).reshape((3, 3))
1542+
>>> a[1, 0] = np.ma.masked
1543+
>>> a[1, 2] = np.ma.masked
1544+
>>> a[2, 1] = np.ma.masked
1545+
>>> a
1546+
masked_array(
1547+
data=[[0, 1, 2],
1548+
[--, 4, --],
1549+
[6, --, 8]],
1550+
mask=[[False, False, False],
1551+
[ True, False, True],
1552+
[False, True, False]],
1553+
fill_value=999999)
1554+
>>> for index, x in np.ma.ndenumerate(a):
1555+
... print(index, x)
1556+
(0, 0) 0
1557+
(0, 1) 1
1558+
(0, 2) 2
1559+
(1, 1) 4
1560+
(2, 0) 6
1561+
(2, 2) 8
1562+
"""
1563+
for it, masked in zip(np.ndenumerate(a), getmaskarray(a).flat):
1564+
if not masked:
1565+
yield it
1566+
1567+
15231568
def flatnotmasked_edges(a):
15241569
"""
15251570
Find the indices of the first and last unmasked values.

numpy/ma/extras.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class mr_class(MAxisConcatenator):
7474

7575
mr_: mr_class
7676

77+
def ndenumerate(a): ...
7778
def flatnotmasked_edges(a): ...
7879
def notmasked_edges(a, axis=...): ...
7980
def flatnotmasked_contiguous(a): ...

numpy/ma/tests/test_extras.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
ediff1d, apply_over_axes, apply_along_axis, compress_nd, compress_rowcols,
2929
mask_rowcols, clump_masked, clump_unmasked, flatnotmasked_contiguous,
3030
notmasked_contiguous, notmasked_edges, masked_all, masked_all_like, isin,
31-
diagflat, stack, vstack
31+
diagflat, ndenumerate, stack, vstack
3232
)
3333

3434

@@ -1648,6 +1648,35 @@ def test_shape_scalar(self):
16481648
assert_equal(b.mask.shape, b.data.shape)
16491649

16501650

1651+
class TestNDEnumerate:
1652+
1653+
def test_ndenumerate_nomasked(self):
1654+
ordinary = np.ndarray(6).reshape((1, 3, 2))
1655+
empty_mask = np.zeros_like(ordinary, dtype=bool)
1656+
with_mask = masked_array(ordinary, mask=empty_mask)
1657+
assert_equal(list(np.ndenumerate(ordinary)),
1658+
list(ndenumerate(ordinary)))
1659+
assert_equal(list(ndenumerate(ordinary)),
1660+
list(ndenumerate(with_mask)))
1661+
1662+
def test_ndenumerate_allmasked(self):
1663+
a = masked_all(())
1664+
b = masked_all((100,))
1665+
c = masked_all((2, 3, 4))
1666+
assert_equal(list(ndenumerate(a)), [])
1667+
assert_equal(list(ndenumerate(b)), [])
1668+
assert_equal(list(ndenumerate(c)), [])
1669+
1670+
def test_ndenumerate_mixedmasked(self):
1671+
a = masked_array(np.arange(12).reshape((3, 4)),
1672+
mask=[[1, 1, 1, 1],
1673+
[1, 1, 0, 1],
1674+
[0, 0, 0, 0]])
1675+
items = [((1, 2), 6),
1676+
((2, 0), 8), ((2, 1), 9), ((2, 2), 10), ((2, 3), 11)]
1677+
assert_equal(list(ndenumerate(a)), items)
1678+
1679+
16511680
class TestStack:
16521681

16531682
def test_stack_1d(self):

0 commit comments

Comments
 (0)