8000 ENH: Avoid memory peak and useless computations when printing a MaskedArray. by saimn · Pull Request #6748 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Avoid memory peak and useless computations when printing a MaskedArray. #6748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 1, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow to change the maximum width with a class variable.
  • Loading branch information
saimn committed Dec 1, 2015
commit b5c456e84dc87521a476ff51e3a2ab55f8c5c29f
10 changes: 6 additions & 4 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -3712,14 +3714,14 @@ def __str__(self):
if names is None:
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)
if data.shape[axis] > self._print_width:
ind = np.int(self._print_width / 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do ind = self._print_width // 2 to avoid the integer cast.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that we always use the Python 3 meaning of / in order to be able to use the same source code for both Python 2 and 3.

arr = np.split(data, (ind, -ind), axis=axis)
data = np.concatenate((arr[0], arr[2]), axis=axis)
arr = np.split(mask, (nval, -nval), 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
Expand Down
0