Skip to content

Commit

Permalink
MAINT: arrayprint.py formatters no longer require ravel'd data
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaldane committed Sep 26, 2017
1 parent 95e9bfa commit 6bfef27
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
28 changes: 12 additions & 16 deletions numpy/core/arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

import numpy as np
from . import numerictypes as _nt
from .umath import maximum, minimum, absolute, not_equal, isnan, isinf
from .umath import absolute, not_equal, isnan, isinf
from . import multiarray
from .multiarray import (array, format_longfloat, datetime_as_string,
datetime_data, dtype, ndarray)
Expand Down Expand Up @@ -312,8 +312,7 @@ def _get_format_function(data, **options):
if dtype_.fields is not None:
format_functions = []
for field_name in dtype_.names:
field_values = ravel(data[field_name])
format_function = _get_format_function(field_values, **options)
format_function = _get_format_function(data[field_name], **options)
if dtype_[field_name].shape != ():
format_function = SubArrayFormat(format_function)
format_functions.append(format_function)
Expand Down Expand Up @@ -386,10 +385,7 @@ def _array2string(a, options, separator=' ', prefix=""):
data = _leading_trailing(a)
else:
summary_insert = ""
if a.shape == () and options.get('sign', '') == 'legacy':
data = asarray(a)
else:
data = ravel(asarray(a))
data = asarray(a)

# find the right formatting function for the array
format_function = _get_format_function(data, **options)
Expand Down Expand Up @@ -626,7 +622,7 @@ def __init__(self, data, precision, suppress_small, sign=False):
self.exp_format = False
self.large_exponent = False
try:
self.fillFormat(ravel(data))
self.fillFormat(data)
except (NotImplementedError):
# if reduce(data) fails, this instance will not be called, just
# instantiated in formatdict.
Expand All @@ -637,16 +633,16 @@ def fillFormat(self, data):
hasinf = isinf(data)
special = isnan(data) | hasinf
valid = not_equal(data, 0) & ~special
non_zero = data.compress(valid)
non_zero = data[valid]
abs_non_zero = absolute(non_zero)
if len(non_zero) == 0:
max_val = 0.
min_val = 0.
min_val_sgn = 0.
else:
max_val = maximum.reduce(abs_non_zero)
min_val = minimum.reduce(abs_non_zero)
min_val_sgn = minimum.reduce(non_zero)
max_val = np.max(abs_non_zero)
min_val = np.min(abs_non_zero)
min_val_sgn = np.min(non_zero)
if max_val >= 1.e8:
self.exp_format = True
if not self.suppress_small and (min_val < 0.0001
Expand Down Expand Up @@ -724,8 +720,8 @@ def __call__(self, x, strip_zeros=True):
class IntegerFormat(object):
def __init__(self, data):
try:
max_str_len = max(len(str(maximum.reduce(data))),
len(str(minimum.reduce(data))))
max_str_len = max(len(str(np.max(data))),
len(str(np.min(data))))
self.format = '%' + str(max_str_len) + 'd'
except (TypeError, NotImplementedError):
# if reduce(data) fails, this instance will not be called, just
Expand Down Expand Up @@ -855,8 +851,8 @@ def __init__(self, data):
v = int_view[not_equal(int_view, nat_value.view(int_dtype))]
if len(v) > 0:
# Max str length of non-NaT elements
max_str_len = max(len(str(maximum.reduce(v))),
len(str(minimum.reduce(v))))
max_str_len = max(len(str(np.max(v))),
len(str(np.min(v))))
else:
max_str_len = 0
if len(v) < len(data):
Expand Down
6 changes: 6 additions & 0 deletions numpy/core/tests/test_arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ def test_sign_spacing(self):
assert_equal(repr(a), 'array([ 0., 1., 2., 3.])')
assert_equal(repr(np.array(1.)), 'array(1.)')

def test_sign_spacing_structured(self):
a = np.ones(2, dtype='f,f')
assert_equal(repr(a), "array([(1., 1.), (1., 1.)],\n"
" dtype=[('f0', '<f4'), ('f1', '<f4')])")
assert_equal(repr(a[0]), "(1., 1.)")


def test_unicode_object_array():
import sys
Expand Down

0 comments on commit 6bfef27

Please sign in to comment.