Numpy set_printoptions, silent failure, bug?
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*
On Tue, Jul 19, 2016 at 10:41 PM, 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
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.
On Tue, Jul 19, 2016 at 2:43 PM, Robert Kern
On Tue, Jul 19, 2016 at 10:41 PM, John Ladasky
wrote: 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 NumPy-Discussion@scipy.org 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*
https://github.com/numpy/numpy/issues
From: 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.
On Tue, Jul 19, 2016 at 2:43 PM, Robert Kern
wrote: On Tue, Jul 19, 2016 at 10:41 PM, John Ladasky
wrote: 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 NumPy-Discussion@scipy.org 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 NumPy-Discussion@scipy.org https://mail.scipy.org/mailman/listinfo/numpy-discussion
Thank you Juan, I've just joined GitHub and I've submitted the description
of the bug.
On Tue, Jul 19, 2016 at 2:55 PM, Juan Nunez-Iglesias
https://github.com/numpy/numpy/issues
From: John Ladasky
Reply: Discussion of Numerical Python Date: 20 July 2016 at 7:49:10 AM To: Discussion of Numerical Python Subject: Re: [Numpy-discussion] Numpy set_printoptions, silent failure, bug? 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.
On Tue, Jul 19, 2016 at 2:43 PM, Robert Kern
wrote: On Tue, Jul 19, 2016 at 10:41 PM, John Ladasky
wrote: 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 NumPy-Discussion@scipy.org 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 NumPy-Discussion@scipy.org https://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org 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*
participants (3)
-
John Ladasky
-
Juan Nunez-Iglesias
-
Robert Kern