Skip to content

repr change for arrays of size 1 even with np.set_printoptions(legacy=True) in numpy master #10026

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

Closed
lesteve opened this issue Nov 15, 2017 · 11 comments

Comments

@lesteve
Copy link
Contributor

lesteve commented Nov 15, 2017

In scikit-learn we have seen failures in doctests for a few days on our build using numpy-dev. This is one of them:

import numpy as np

try:
    np.set_printoptions(legacy=True)
except Exception:
    pass

print(repr(np.array([0.])))
print(repr(np.array([[0.]])))
print(repr(np.array([1., 2., 3.])))

With numpy master:

array([0.])
array([[0.]])
array([ 1.,  2.,  3.])

With numpy 1.13 (note the space before the 0):

array([ 0.])
array([[ 0.]])
array([ 1.,  2.,  3.])
@charris
Copy link
Member

charris commented Nov 15, 2017

It's a feature, not a bug ;) The default print formatting will change in 1.14. We are aware that this will break doctests. From the release notes

Changes to array printing, and the new "legacy" printing mode.
--------------------------------------------------------------
The new ``sign='-'`` option (see new features below) causes the ``repr`` of
float arrays to often omit a whitespace previously printed in the sign
position, and 0d arrays also now print with small whitespace and precision
differences (see changes below). These changes are likely to break downstream
user's doctests.

These new behaviors can be disabled to mostly reproduce numpy 1.13 behavior by
enabling the new "legacy" printing mode. This is enabled by calling
``np.set_printoptions(legacy=True)``, or using the new ``legacy`` argument
to ``np.array2string``.

Note that there are various ways to relax whitespace sensitivity of doctests which are probably worth looking into if you want to continue to rely on doctests. @ahaldane Further suggestions?

@charris
Copy link
Member

charris commented Nov 15, 2017

Note that there are more changes than you show, for instance

In [1]: print(repr(np.array([1., 2., 3.])))
array([1., 2., 3.])

Note missing spaces.

@charris
Copy link
Member

charris commented Nov 15, 2017

Oops, I see that you did use the legacy mode, so we can maybe fix that.

@lesteve
Copy link
Contributor Author

lesteve commented Nov 15, 2017

Oops, I see that you did use the legacy mode, so we can maybe fix that.

That would be great!

For now our strategy is to use the legacy mode to have compatibility of our doctest with numpy 1.14dev and 1.13. When numpy 1.14 is released we will revisit our strategy. One possible solution is to update all of our doctests to work with 1.14 and just skip them for numpy <= 1.13.

@amueller
Copy link

amueller commented Nov 15, 2017

I'm confused. So did numpy decide to definitely break, regardless of the sign/legacy setting?

(this was not the issue I meant to comment on, and this thread actually answered my question, sorry for the noise)

@amueller
Copy link

Something that was printed as 1.0 is now printed as 0.99999999(...?) is that also a feature?

@ahaldane
Copy link
Member

What code exactly printed 0.99999999? There is a new float printing algorithm in 1.14 which, barring a bug, should print just like a python3 float.

@ahaldane
Copy link
Member

I'll take a look at the space in the legacy option, I'm pretty sure that is a fixable bug.

@lesteve
Copy link
Contributor Author

lesteve commented Nov 15, 2017

What code exactly printed 0.99999999? There is a new float printing algorithm in 1.14 which, barring a bug, should print just like a python3 float.

There is a case for np.set_printoptions(legacy=True) to implement the pre-dragon4 printing. I am not sure it is worth the cost, I'll open a separate issue to discuss this. FWIW in scikit-learn we have managed to work around these issues by using doctest ellipsis.

@ahaldane
Copy link
Member

Yes, we can discuss that. But if you see a number that is printed as 0.99999999 when it should be printed as 1.0 let us know.

The goal of the new algorithm is to round "correctly" for all float sizes, so if there is an instance where it isn't rounding correctly, that is important.

@charris
Copy link
Member

charris commented Nov 19, 2017

Just a quick update, it is now legacy='1.13' to get the old printing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants