-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
MAINT: Legacy mode specified as string, fix all-zeros legacy bug #10030
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
Conversation
0e2e17b
to
e2be765
Compare
numpy/core/arrayprint.py
Outdated
@@ -87,6 +87,9 @@ def _make_options_dict(precision=None, threshold=None, edgeitems=None, | |||
if sign not in [None, '-', '+', ' ']: | |||
raise ValueError("sign option must be one of ' ', '+', or '-'") | |||
|
|||
if legacy not in [None, '', '1.13']: | |||
raise ValueError("legacy option can currently only be '1.13' or ''") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should warn, not error - otherwise, code which does not want 1.15 behavior (passing legacy='1.14'
), will crash when run on 1.13.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree.
Unsure if we should be using strings or tuples here. Tuples would work better for version comparison, but perhaps that's not something we want to encourage. |
My 2c: use strings and |
@lesteve We don't need to worry about actual versions, just need a way to specify which printing style we want. I'd go for a string. |
e2be765
to
1c77282
Compare
You're right to warn, instead of raise. Fixed. |
numpy/core/arrayprint.py
Outdated
@@ -87,6 +87,10 @@ def _make_options_dict(precision=None, threshold=None, edgeitems=None, | |||
if sign not in [None, '-', '+', ' ']: | |||
raise ValueError("sign option must be one of ' ', '+', or '-'") | |||
|
|||
if legacy not in [None, '', '1.13']: | |||
warnings.warn("legacy printing option can currently only be '1.13' or " | |||
"''", stacklevel=3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty string seems weird to me. Can we use False
instead? I'm guessing None
is a problem because of needing a default value in set_printoptions
?
numpy/core/arrayprint.py
Outdated
valid = not_equal(data, 0) & ~special | ||
non_zero = data[valid] | ||
non_special = data[~special] | ||
non_zero = non_special[non_special != 0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments might be nice here explaining why each of these are needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A 'not' prefix has more force than 'non'. Not worth worrying about here, just a comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would reserve a not_
prefix for a boolean type, so non_zero = data[not_zero]
1c77282
to
8eebda7
Compare
Thanks for the suggestions. I made |
@@ -841,7 +834,7 @@ def __init__(self, *args, **kwargs): | |||
def format_float_scientific(x, precision=None, unique=True, trim='k', | |||
sign=False, pad_left=None, exp_digits=None): | |||
""" | |||
Format a floating-point scalar as a string in scientific notation. | |||
Format a floating-point scalar as a decimal string in scientific notation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is an unrelated small doc tweak I am piggybacking on this PR
8eebda7
to
3416cc8
Compare
numpy/core/arrayprint.py
Outdated
If set to the string `'1.13'` enables 1.13 legacy printing mode. This | ||
approximates numpy 1.13 print output by including a space in the sign | ||
position of floats and different behavior for 0d arrays. If set to | ||
`False`, disables legacy mode. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs .. versionadded::
. If folks want to use this they will need to check the NumPy version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want a comment here explaining that invalid versions will be ignored for forward-compatibility too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget to add this to both docstrings
finite_vals = data[isfinite(data)] | ||
|
||
# choose exponential mode based on the non-zero finite values: | ||
abs_non_zero = absolute(finite_vals[finite_vals != 0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Way clearer now :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eric-wieser If you are good with this, I'll make the fix and merge.
@@ -701,40 +707,32 @@ def __init__(self, data, precision, floatmode, suppress_small, sign=False, **kwa | |||
self.fillFormat(data) | |||
|
|||
def fillFormat(self, data): | |||
with errstate(all='ignore'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this line go away?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I think I need to keep it. It must be because the division operation can overflow, eg 1.0/np.float64(2.2250738585072e-310)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I just made the other fixes online.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may need to update from origin.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe push it to a deeper scope this time
A little puzzled by the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Might be nice to squash @charris' commits together
numpy/core/tests/test_arrayprint.py
Outdated
def test_float_overflow_nowarn(self): | ||
# make sure internal computations in FloatingFormat don't | ||
# warn about overflow | ||
repr(array([1.0, 1e-310], dtype='f8')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm but actually tests just finished running on my system and I don't think this triggers the problem because the min_val < 0.0001
short-circtuits out the division clause anytime overflow could occur.
I think overflow may only be posible for float16 types under certain circumstances. np.array([1e4, 0.1], dtype='f2')
works.
c371c08
to
86bd257
Compare
Note that the `legacy` keyword was added in NumPy 1.14.0. [ci skip]
3b3cde8
to
85dabca
Compare
The |
85dabca
to
47a12f1
Compare
Thanks Allan. |
Fixes #10020 and #10026
The first commit makes the
legacy
option specify version number, asnp.set_printoptions(legacy='1.13')
.The second commit fixes a bug where the legacy mode spacing for float arrays didn't get activated for arrays which didn't have any non-zero finite values.