diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2015-12-01 16:18:31 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-12-01 16:18:31 -0700 |
commit | 11f809219458973d73018ad2438cb8514b61c7a6 (patch) | |
tree | 620e8fb6761f8b30ce83120b89792493f08f016d /numpy | |
parent | 433e6691113c4656cd939f94da4a837ed08e59a4 (diff) | |
parent | d0e9d98b2aa126bb2654c4c5966a4034c4bb99fc (diff) | |
download | numpy-11f809219458973d73018ad2438cb8514b61c7a6.tar.gz |
Merge pull request #6748 from saimn/ma-repr-memory
ENH: Avoid memory peak and useless computations when printing a MaskedArray.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/ma/core.py | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index b7ee4a797..f6e445c2f 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -2684,6 +2684,8 @@ class MaskedArray(ndarray): _defaultmask = nomask _defaulthardmask = False _baseclass = ndarray + # Maximum number of elements per axis used when printing an array. + _print_width = 100 def __new__(cls, data=None, mask=nomask, dtype=None, copy=False, subok=True, ndmin=0, fill_value=None, @@ -3710,8 +3712,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 + # 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] > self._print_width: + ind = self._print_width // 2 + arr = np.split(data, (ind, -ind), axis=axis) + data = np.concatenate((arr[0], arr[2]), axis=axis) + arr = np.split(mask, (ind, -ind), 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 +4703,7 @@ class MaskedArray(ndarray): See Also -------- numpy.ma.dot : equivalent function - + """ return dot(self, b, out=out, strict=strict) |