Skip to content

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

Closed

Description

@lesteve

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.])

Activity

  1. charris commented on Nov 15, 2017

    @charris
    Member

    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?

  2. charris commented on Nov 15, 2017

    @charris
    Member

    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.

  3. charris commented on Nov 15, 2017

    @charris
    Member

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

  4. lesteve commented on Nov 15, 2017

    @lesteve
    ContributorAuthor

    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.

  5. amueller commented on Nov 15, 2017

    @amueller

    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)

  6. amueller commented on Nov 15, 2017

    @amueller

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

  7. ahaldane commented on Nov 15, 2017

    @ahaldane
    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.

  8. ahaldane commented on Nov 15, 2017

    @ahaldane
    Member

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

  9. lesteve commented on Nov 15, 2017

    @lesteve
    ContributorAuthor

    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.

  10. ahaldane commented on Nov 15, 2017

    @ahaldane
    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.

  11. charris commented on Nov 19, 2017

    @charris
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions