8000 ENH: Avoid memory peak when creating a MaskedArray with mask=True/Fal… · jaimefrio/numpy@edfe890 · GitHub
[go: up one dir, main page]

Skip to content

Commit edfe890

Browse files
saimnjaimefrio
authored andcommitted
ENH: Avoid memory peak when creating a MaskedArray with mask=True/False (numpy#6732).
When the `mask` parameter is set to True or False, create directly a `ndarray` of boolean instead of going inside `np.resize` which was causing of memory peak of ~15 times the size of the mask.
1 parent 8cf7c44 commit edfe890

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

numpy/ma/core.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,13 +2758,19 @@ def __new__(cls, data=None, mask=nomask, dtype=None, copy=False,
27582758
_data._sharedmask = True
27592759
else:
27602760
# Case 2. : With a mask in input.
2761-
# Read the mask with the current mdtype
2762-
try:
2763-
mask = np.array(mask, copy=copy, dtype=mdtype)
2764-
# Or assume it's a sequence of bool/int
2765-
except TypeError:
2766-
mask = np.array([tuple([m] * len(mdtype)) for m in mask],
2767-
dtype=mdtype)
2761+
# If mask is boolean, create an array of True or False
2762+
if mask is True:
2763+
mask = np.ones(_data.shape, dtype=mdtype)
2764+
elif mask is False:
2765+
mask = np.zeros(_data.shape, dtype=mdtype)
2766+
else:
2767+
# Read the mask with the current mdtype
2768+
try:
2769+
mask = np.array(mask, copy=copy, dtype=mdtype)
2770+
# Or assume it's a sequence of bool/int
2771+
except TypeError:
2772+
mask = np.array([tuple([m] * len(mdtype)) for m in mask],
2773+
dtype=mdtype)
27682774
# Make sure the mask and the data have the same shape
27692775
if mask.shape != _data.shape:
27702776
(nd, nm) = (_data.size, mask.size)

0 commit comments

Comments
 (0)
0