Skip to content

ndarray printing is inconsistent across the platforms #13468

Closed

Description

@soupault

Hi,

I've discovered that numpy array printing is slightly different for different platforms (might be related to platform-dependent types somehow - https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_9.0.0/com.ibm.mq.ref.dev.doc/q104610_.htm).
This inconsistency makes the testing of docstring examples challenging (scikit-image/scikit-image#3873) across the operating systems.

Reproducing code example:

Notice the dtype part:

import numpy as np

### Windows 32bit

np.set_printoptions(legacy='1.13')
np.argmin([[0,2,-1,3], [1,2,3,4]], axis=1)
# array([2, 0], dtype=int32)

np.set_printoptions(legacy=False)
np.argmin([[0,2,-1,3], [1,2,3,4]], axis=1)
# array([2, 0], dtype=int32)

### Windows 64bit

np.set_printoptions(legacy='1.13')
np.argmin([[0,2,-1,3], [1,2,3,4]], axis=1)
# array([2, 0], dtype=int64)

np.set_printoptions(legacy=False)
np.argmin([[0,2,-1,3], [1,2,3,4]], axis=1)
# array([2, 0], dtype=int32)

### Linux / MacOS 64bit

np.set_printoptions(legacy='1.13')
np.argmin([[0,2,-1,3], [1,2,3,4]], axis=1)
# array([2, 0])

np.set_printoptions(legacy=False)
np.argmin([[0,2,-1,3], [1,2,3,4]], axis=1)
# array([2, 0])

Numpy/Python version information:

(for Windows VM)
1.16.3, wide range of Python 3 versions

(for MacOS PC)
1.16.2 3.6.8 |Anaconda, Inc.| (default, Dec 29 2018, 19:04:46)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]

Activity

  1. seberg commented on May 8, 2019

    @seberg
    Member

    I think the different printing is to be probably a bit inconsistent at points, but has to be expected unfortunately due to differences between the long and ssize_t. @tylerjereddy can you point quickly on how we solve this in numpy, since I think we run such tests as well? If not, I may try to dig into it.

  2. tylerjereddy commented on May 8, 2019

    @tylerjereddy
    Contributor

    I made a detailed comment in the cross-linked scikit-image PR, which I've reproduced below. A consistent ecosystem solution would be useful, as discussed in many places, but there are some options at least for now.

    Making the executable docstrings fully portable is indeed tricky--for NumPy and SciPy I think we do two things for this:

    1. Typically just run the docstring checks on a single platform in CI
    2. Improve portability by using refguide_check.py, which falls back to return np.allclose(want, got, atol=self.atol, rtol=self.rtol) when vanilla doctest doesn't think the results match; this almost certainly solves the problem you describe in the cognate NumPy issue

    When I check the different results you mention in the cross-linked NumPy issue, I can see the refguide will likely indicate that these are the same because np.allclose(np.array([2, 0], dtype=np.int32), np.array([2, 0], dtype=np.int64)) is True, even though their reprs differ.

    Vendoring refguide_check.py would make a bit more work though. There's also pytest-doctestplus, proposals to start fusing it with refguide features, and another solution in sympy, I think.

    I've also been dreaming about doctest line coverage reports, described elegantly by Raymond Hettinger.

  3. added
    57 - Close?
    on May 8, 2019
  4. seberg commented on May 8, 2019

    @seberg
    Member

    Thanks tyler! Interesting stuff, I hope it helps with this thing. It is interesting info here maybe, but probably can close (will go through the laballed issues anyway soon though).

  5. soupault commented on May 19, 2019

    @soupault
    ContributorAuthor

    What do you think about adding a parameter to printoptions to suppress the dtype field (or even everything besides the numerical data) in the repr?

    Either way, please, feel free to close. I'm more or less fine with the current situation and glad that we're all aligned on this issue.

  6. seberg commented on May 19, 2019

    @seberg
    Member

    OK, thanks @soupault. Closing it then, just pinging @ahaldane in case he has an opinion about changing print options. It seems to me that printing already does not print out the dtype, and only repr does (which arguably should normally).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    57 - Close?component: documentation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions