summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Conseil <contact@saimon.org>2015-11-26 15:04:37 +0100
committerSimon Conseil <contact@saimon.org>2015-11-26 15:04:37 +0100
commit531f2ad34d006585281b9416fa30992dd37cf2af (patch)
tree25a00196d33d06455ae4ff99166ca579aaf76ae5
parent53e658f80da6a50d371c74f49d3bb81fabf20d1b (diff)
downloadnumpy-531f2ad34d006585281b9416fa30992dd37cf2af.tar.gz
ENH: Avoid memory peak when creating a MaskedArray with mask=True/False (#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.
-rw-r--r--numpy/ma/core.py22
1 files changed, 14 insertions, 8 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index b9f7da092..75c7c001e 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -2756,13 +2756,19 @@ class MaskedArray(ndarray):
_data._sharedmask = True
else:
# Case 2. : With a mask in input.
- # Read the mask with the current mdtype
- try:
- mask = np.array(mask, copy=copy, dtype=mdtype)
- # Or assume it's a sequence of bool/int
- except TypeError:
- mask = np.array([tuple([m] * len(mdtype)) for m in mask],
- dtype=mdtype)
+ # If mask is boolean, create an array of True or False
+ if mask is True:
+ mask = np.ones(_data.shape, dtype=mdtype)
+ elif mask is False:
+ mask = np.zeros(_data.shape, dtype=mdtype)
+ else:
+ # Read the mask with the current mdtype
+ try:
+ mask = np.array(mask, copy=copy, dtype=mdtype)
+ # Or assume it's a sequence of bool/int
+ except TypeError:
+ mask = np.array([tuple([m] * len(mdtype)) for m in mask],
+ dtype=mdtype)
# Make sure the mask and the data have the same shape
if mask.shape != _data.shape:
(nd, nm) = (_data.size, mask.size)
@@ -4690,7 +4696,7 @@ class MaskedArray(ndarray):
See Also
--------
numpy.ma.dot : equivalent function
-
+
"""
return dot(self, b, out=out, strict=strict)