Discussion:
[Numpy-discussion] Numpy set_printoptions, silent failure, bug?
John Ladasky
2016-07-19 21:41:04 UTC
Permalink
Hi there,

I've been using Numpy for several years and appreciate it very much.

The following minimal code has been tried on Python 3.4 and 3.5, with Numpy
1.8 and Numpy 1.11, respectively. I want to temporarily change the way
that a Numpy array is printed, then change it back.

import numpy as np

a = np.random.random((4,3))
print(a, "\n")
opt = np.get_printoptions()
np.set_printoptions(precision = 3, suppress = True)
print(a, "\n")
np.set_printoptions(opt)
print(a, "\n\nDone.\n")


Here is the traceback:


Traceback (most recent call last):
File "set_printoptions test.py", line 11, in <module>
print(a, "\n\nDone.\n")
File "/usr/lib/python3/dist-packages/numpy/core/numeric.py", line 1615,
in array_str
return array2string(a, max_line_width, precision, suppress_small, ' ',
"", str)
File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 454,
in array2string
separator, prefix, formatter=formatter)
File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 328,
in _array2string
_summaryEdgeItems, summary_insert)[:-1]
File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 523,
in _formatArray
summary_insert)
File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 497,
in _formatArray
word = format_function(a[-i]) + separator
File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 616,
in __call__
s = self.format % x
AttributeError: 'FloatFormat' object has no attribute 'format'


It took me a while to discover the bug, on the second to last line. That
line should read:

np.set_printoptions(**opt)


This unpacks the dictionary, opt, which was retrieved earlier in the
program with the call to get_printoptions.

I am wondering why set_printoptions would accept the dictionary as a single
argument. Shouldn't that raise a TypeError or something?

The AttributeError that is raised the next time that one attempts to print
a numpy array is remote from the problem, and thus rather uninformative,
which is why I did not immediately diagnose my error.

Should this be considered a Numpy bug, or is there some reason that
set_printoptions would legitimately need to accept a dictionary as a single
argument?
--
*John J. Ladasky Jr., Ph.D.*
*Research Scientist*
*International Technological University*
*2711 N. First St, San Jose, CA 95134 USA*
Robert Kern
2016-07-19 21:43:56 UTC
Permalink
Post by John Ladasky
Should this be considered a Numpy bug, or is there some reason that
set_printoptions would legitimately need to accept a dictionary as a single
argument?

There is no such reason. One could certainly add more validation to the
arguments to np.set_printoptions().

--
Robert Kern
John Ladasky
2016-07-19 21:49:10 UTC
Permalink
Hi Robert,

Thanks for your reply. If no one disagrees with you or with me that this
is a Numpy bug, I would appreciate being directed to the appropriate page
to submit a bug-fix request.
Post by John Ladasky
Post by John Ladasky
Should this be considered a Numpy bug, or is there some reason that
set_printoptions would legitimately need to accept a dictionary as a single
argument?
There is no such reason. One could certainly add more validation to the
arguments to np.set_printoptions().
--
Robert Kern
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
--
*John J. Ladasky Jr., Ph.D.*
*Research Scientist*
*International Technological University*
*2711 N. First St, San Jose, CA 95134 USA*
Juan Nunez-Iglesias
2016-07-19 21:55:11 UTC
Permalink
https://github.com/numpy/numpy/issues


From: John Ladasky <***@itu.edu> <***@itu.edu>
Reply: Discussion of Numerical Python <numpy-***@scipy.org>
<numpy-***@scipy.org>
Date: 20 July 2016 at 7:49:10 AM
To: Discussion of Numerical Python <numpy-***@scipy.org>
<numpy-***@scipy.org>
Subject: Re: [Numpy-discussion] Numpy set_printoptions, silent failure,
bug?

Hi Robert,
Post by John Ladasky
Thanks for your reply. If no one disagrees with you or with me that this
is a Numpy bug, I would appreciate being directed to the appropriate page
to submit a bug-fix request.
Post by John Ladasky
Post by John Ladasky
Should this be considered a Numpy bug, or is there some reason that
set_printoptions would legitimately need to accept a dictionary as a single
argument?
There is no such reason. One could certainly add more validation to the
arguments to np.set_printoptions().
--
Robert Kern
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
--
*John J. Ladasky Jr., Ph.D.*
*Research Scientist*
*International Technological University*
*2711 N. First St, San Jose, CA 95134 USA*
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
John Ladasky
2016-07-19 22:37:34 UTC
Permalink
Thank you Juan, I've just joined GitHub and I've submitted the description
of the bug.
Post by Juan Nunez-Iglesias
https://github.com/numpy/numpy/issues
Date: 20 July 2016 at 7:49:10 AM
Subject: Re: [Numpy-discussion] Numpy set_printoptions, silent failure,
bug?
Hi Robert,
Post by John Ladasky
Thanks for your reply. If no one disagrees with you or with me that this
is a Numpy bug, I would appreciate being directed to the appropriate page
to submit a bug-fix request.
Post by John Ladasky
Post by John Ladasky
Should this be considered a Numpy bug, or is there some reason that
set_printoptions would legitimately need to accept a dictionary as a single
argument?
There is no such reason. One could certainly add more validation to the
arguments to np.set_printoptions().
--
Robert Kern
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
--
*John J. Ladasky Jr., Ph.D.*
*Research Scientist*
*International Technological University*
*2711 N. First St, San Jose, CA 95134 USA*
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
--
*John J. Ladasky Jr., Ph.D.*
*Research Scientist*
*International Technological University*
*2711 N. First St, San Jose, CA 95134 USA*
Loading...