diff options
author | Simon Conseil <contact@saimon.org> | 2015-11-27 23:12:32 +0100 |
---|---|---|
committer | Simon Conseil <contact@saimon.org> | 2015-11-28 00:11:49 +0100 |
commit | 593345a75fdd3de103e3e50969fbca2ed752f08d (patch) | |
tree | 884a858f6b16967634556ca8e298349d28f801f7 | |
parent | 53e658f80da6a50d371c74f49d3bb81fabf20d1b (diff) | |
download | numpy-593345a75fdd3de103e3e50969fbca2ed752f08d.tar.gz |
ENH: Avoid memory peak and useless computations when printing a MaskedArray.
Ref #3544.
When printing a `MaskedArray`, the whole array is converted to the object dtype,
whereas only a few values are printed to screen. So the approach here is to cut
the array and keep only a subset that it used for the string conversion. This
way the output should not change.
-rw-r--r-- | numpy/ma/core.py | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index b9f7da092..3c1f8210d 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -3710,8 +3710,19 @@ class MaskedArray(ndarray): # convert to object array to make filled work names = self.dtype.names if names is None: - res = self._data.astype("O") - res.view(ndarray)[m] = f + data = self._data + mask = m + nval = 50 + # For big arrays, to avoid a costly conversion to the + # object dtype, extract the corners before the conversion. + for axis in range(self.ndim): + if data.shape[axis] > 2 * nval: + arr = np.split(data, (nval, -nval), axis=axis) + data = np.concatenate((arr[0], arr[2]), axis=axis) + arr = np.split(mask, (nval, -nval), axis=axis) + mask = np.concatenate((arr[0], arr[2]), axis=axis) + res = data.astype("O") + res.view(ndarray)[mask] = f else: rdtype = _recursive_make_descr(self.dtype, "O") res = self._data.astype(rdtype) @@ -4690,7 +4701,7 @@ class MaskedArray(ndarray): See Also -------- numpy.ma.dot : equivalent function - + """ return dot(self, b, out=out, strict=strict) |