diff options
author | Allan Haldane <allan.haldane@gmail.com> | 2017-09-19 14:34:38 -0400 |
---|---|---|
committer | Allan Haldane <allan.haldane@gmail.com> | 2017-09-25 21:23:02 -0400 |
commit | 710e0327687b9f7653e5ac02d222ba62c657a718 (patch) | |
tree | 5cb32e3c86a67dafe247df1f0aabf49ca291e236 | |
parent | 6bfef271d8df4c5aa4af9075defd672b11f1cbc0 (diff) | |
download | numpy-710e0327687b9f7653e5ac02d222ba62c657a718.tar.gz |
MAINT: fixups to legacy spacing in FloatFormat
-rw-r--r-- | doc/release/1.14.0-notes.rst | 20 | ||||
-rw-r--r-- | numpy/core/arrayprint.py | 35 | ||||
-rw-r--r-- | numpy/core/tests/test_arrayprint.py | 8 |
3 files changed, 24 insertions, 39 deletions
diff --git a/doc/release/1.14.0-notes.rst b/doc/release/1.14.0-notes.rst index e315d6f67..c9f1cec76 100644 --- a/doc/release/1.14.0-notes.rst +++ b/doc/release/1.14.0-notes.rst @@ -310,25 +310,15 @@ This option controls printing of the sign of floating-point types, and may be one of the characters '-', '+' or ' ', or the string 'legacy'. With '+' numpy always prints the sign of positive values, with ' ' it always prints a space (whitespace character) in the sign position of positive values, and with '-' it -will omit the sign character for positive values. The new default is '-'. +will omit the sign character for positive values, and with 'legacy' it will +behave like ' ' except no space is printed in 0d arrays. The new default is '-'. -Setting ``sign='legacy'`` will behave like ' ' except that no space is printed -in 0d arrays, and also includes a space before ``True`` values in size-1 bool -arrays. This approximates the behavior of numpy 1.13 and before. - -Unneeded whitespace in float and bool array printing removed ------------------------------------------------------------- +Unneeded whitespace in float array printing removed +--------------------------------------------------- The new default of ``sign='-'`` (see last note) means that the ``repr`` of float arrays now often omits the whitespace characters previously used to display the sign. This new behavior can be disabled to mostly reproduce numpy -1.13 behavior by calling: - - >>> np.set_printoptions(sign='legacy') - -Additionally, the ``repr`` of bool arrays with only one element now omits the -whitespace before a ``True`` value, so that ``repr(array([True]))`` now returns -``'array([True])'`` instead of ``'array([ True])'``. This is disabled by -setting ``sign='legacy'``. +1.13 behavior by calling ``np.set_printoptions(sign='legacy')``. ``threshold`` and ``edgeitems`` options added to ``np.array2string`` ----------------------------------------------------------------- diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 9e118217b..e0da9f81e 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -119,8 +119,7 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, print the sign of positive values. If ' ', always prints a space (whitespace character) in the sign position of positive values. If '-', omit the sign character of positive values. If 'legacy', print a - space for positive values except in 0d arrays, and also add a space for - 'True' values in size-1 bool arrays. (default '-') + space for positive values except in 0d arrays. (default '-') formatter : dict of callables, optional If not None, the keys should indicate the type(s) that the respective formatting function applies to. Callables should return a string. @@ -263,7 +262,7 @@ def _get_formatdict(data, **opt): prec, supp, sign = opt['precision'], opt['suppress'], opt['sign'] # wrapped in lambdas to avoid taking a code path with the wrong type of data - formatdict = {'bool': lambda: BoolFormat(data, legacy=(sign == 'legacy')), + formatdict = {'bool': lambda: BoolFormat(data), 'int': lambda: IntegerFormat(data), 'float': lambda: FloatFormat(data, prec, supp, sign), 'longfloat': lambda: LongFloatFormat(prec), @@ -379,7 +378,6 @@ def _recursive_guard(fillvalue='...'): # gracefully handle recursive calls, when object arrays contain themselves @_recursive_guard() def _array2string(a, options, separator=' ', prefix=""): - if a.size > options['threshold']: summary_insert = "..., " data = _leading_trailing(a) @@ -401,7 +399,6 @@ def _array2string(a, options, separator=' ', prefix=""): return lst - def array2string(a, max_line_width=None, precision=None, suppress_small=None, separator=' ', prefix="", style=np._NoValue, formatter=None, threshold=None, @@ -471,8 +468,7 @@ def array2string(a, max_line_width=None, precision=None, print the sign of positive values. If ' ', always prints a space (whitespace character) in the sign position of positive values. If '-', omit the sign character of positive values. If 'legacy', print a - space for positive values except in 0d arrays, and also add a space for - 'True' values in size-1 bool arrays. + space for positive values except in 0d arrays. Returns ------- @@ -613,7 +609,9 @@ class FloatFormat(object): if isinstance(sign, bool): sign = '+' if sign else '-' + self._legacy = False if sign == 'legacy': + self._legacy = True sign = '-' if data.shape == () else ' ' self.precision = precision @@ -621,12 +619,8 @@ class FloatFormat(object): self.sign = sign self.exp_format = False self.large_exponent = False - try: - self.fillFormat(data) - except (NotImplementedError): - # if reduce(data) fails, this instance will not be called, just - # instantiated in formatdict. - pass + + self.fillFormat(data) def fillFormat(self, data): with errstate(all='ignore'): @@ -653,8 +647,9 @@ class FloatFormat(object): self.large_exponent = 0 < min_val < 1e-99 or max_val >= 1e100 signpos = self.sign != '-' or any(non_zero < 0) - # for back-compatibility with np 1.13, add extra space if padded - signpos = signpos if self.sign != ' ' else 2 + # for back-compatibility with np 1.13, use two spaces + if self._legacy: + signpos = 2 max_str_len = signpos + 6 + self.precision + self.large_exponent conversion = '' if self.sign == '-' else self.sign @@ -739,15 +734,9 @@ class IntegerFormat(object): class BoolFormat(object): def __init__(self, data, **kwargs): - # in legacy printing style, include a space before True except in 0d - if kwargs.get('legacy', False): - self.truestr = ' True' if data.shape != () else 'True' - return - # add an extra space so " True" and "False" have the same length and - # array elements align nicely when printed, but only for arrays with - # more than one element (0d and nd) - self.truestr = ' True' if data.size > 1 else 'True' + # array elements align nicely when printed, except in 0d arrays + self.truestr = ' True' if data.shape != () else 'True' def __call__(self, x): return self.truestr if x else "False" diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py index 90e82ed74..b03229447 100644 --- a/numpy/core/tests/test_arrayprint.py +++ b/numpy/core/tests/test_arrayprint.py @@ -274,7 +274,7 @@ class TestPrintOptions(object): assert_equal(repr(np.array([True, False])), 'array([ True, False], dtype=bool)') assert_equal(repr(np.array([True])), - 'array([True], dtype=bool)') + 'array([ True], dtype=bool)') assert_equal(repr(np.array(True)), 'array(True, dtype=bool)') assert_equal(repr(np.array(False)), @@ -282,20 +282,26 @@ class TestPrintOptions(object): def test_sign_spacing(self): a = np.arange(4.) + b = np.array([1.234e9]) + assert_equal(repr(a), 'array([0., 1., 2., 3.])') assert_equal(repr(np.array(1.)), 'array(1.)') + assert_equal(repr(b), 'array([1.23400000e+09])') np.set_printoptions(sign=' ') assert_equal(repr(a), 'array([ 0., 1., 2., 3.])') assert_equal(repr(np.array(1.)), 'array( 1.)') + assert_equal(repr(b), 'array([ 1.23400000e+09])') np.set_printoptions(sign='+') assert_equal(repr(a), 'array([+0., +1., +2., +3.])') assert_equal(repr(np.array(1.)), 'array(+1.)') + assert_equal(repr(b), 'array([+1.23400000e+09])') np.set_printoptions(sign='legacy') assert_equal(repr(a), 'array([ 0., 1., 2., 3.])') assert_equal(repr(np.array(1.)), 'array(1.)') + assert_equal(repr(b), 'array([ 1.23400000e+09])') def test_sign_spacing_structured(self): a = np.ones(2, dtype='f,f') |