-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
ENH: Update scalar representations as per NEP 51 #22449
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
Merged
Merged
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
20ac540
ENH: Changed repr of np.bool_
ganesh-k13 9af45ba
ENH: Used raw boolean value
ganesh-k13 587e6fd
ENH: Refactored to use PyUnicode_FromString
ganesh-k13 b573496
ENH: Fixed doc-string
ganesh-k13 d0ecf91
ENH: Added release notes (#17592)
ganesh-k13 4243ee7
ENH: Fixed release notes `np.bool_` repr (#17592)
ganesh-k13 7431a2d
Update doc/release/upcoming_changes/17592.improvement.rst
eric-wieser 341c245
ENH: Changed repr of ints
ganesh-k13 f2119c6
ENH: Changed repr of ints to use tp_name
ganesh-k13 834f3f9
WIP: Get to the NEP 51 state (and try to figure that out...)
seberg 4550643
TST: Fixup tests for repr changes
seberg eef1cbd
TST: Some more test fixups for repr changes
seberg b9d0cd3
TST: STrengthen test on newer Python versions
seberg 46785ee
Fixup tests, repr, and guard against using full repr in array printing
seberg a7b045b
WIP: Continue with refactoring more...
seberg 145998a
WIP: Refactor array formatter with a new `get_formatter`
seberg 4e16d86
ENH: Fixup records and void scalar printing
seberg e7ce9eb
ENH: Only print type information when helpful for MA fill value
seberg b74dbfd
API: Switch `ndarray.tofile` back to defaulting to `str()` usage
seberg ae28524
TST: Fixup tests to make windows/32bit systems and linter happy
seberg 6316dd4
Adapt format to not block s and r format codes
seberg e8f0492
WIP: Fixup arrayprint for scalar values
seberg 413f1a7
MAINT: Switch to also have str explicitly, fixup MA and forward str/r…
seberg 4c6ff64
DOC: Update reference to pass refguide
seberg 3c0c3d2
STY: Fix linter
seberg 1a53d88
BUG: Fixups for last changes to make CI pass
seberg a01c893
DOC: First small refdoc update (very verbose update required, too lar…
seberg 6cceecc
DOC: Remove outdated release note (eventually need new referencing NEP)
seberg 342b786
DOC: Fixup docs; only "big" changes not all scalars
seberg d681bac
fix rebase error
seberg 90a8b61
MAINT: Allow all options again (because subarrays need them) and fix …
seberg 815c8a1
DOC: More doc fixes to pass tests
seberg b6f83b6
MAINT: Special case string fill-value in MA repr
seberg afd5e87
DOC: Add a release note for NEP 51 related changes
seberg ea9063f
ENH; Add legacy fallback mode for non NEP 51 printing because...
seberg f0302f8
Allow legacy fallback (fix float16, heh)
seberg 768fcf0
TST,MAINT: Add tests for both new and old scalar repr and fix complex
seberg 66341e5
DOC: Mention `np.set_printoptions` in the release note
seberg 62f9d5a
TST: Ensure little-endian int est
seberg 01a1d29
Simplify, removing new fmt option
mhvk d40922d
Minimize changes relative to main
mhvk 7a61d6d
Remove unnecessary legacy check and longdouble quoting support
seberg c8df697
MAINT: Undo bad rebase (or maybe small accidental commit)
seberg a6645e0
Address some other review comments
seberg 6097389
Hack strings to be correct for fill-value
seberg 7fcf5ca
Update release note and add note to NEP that it was not fully impleme…
seberg 19ed59e
MAINT: Tweak string check (forgot the kind, but maybe tuple is nice)
seberg 0329f18
Also force repr for object
seberg 51eb71e
MAINT: A few small fixups from review
seberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Representation of NumPy scalars changed | ||
--------------------------------------- | ||
As per :ref:`NEP 51 <NEP51>`, the scalar representation has been | ||
updated to include the type information to avoid confusion with | ||
Python scalars. | ||
The are now printed as ``np.float64(3.0)`` rather than just ``3.0``. | ||
This may disrupt workflows that store representations of numbers | ||
(e.g. to files) making it harder to read them. They should be stored as | ||
explicit strings, for example by using ``str()`` or ``f"{scalar!s}"``. | ||
For the time being, affected users can use ``np.set_printoptions(legacy="1.25")`` | ||
to get the old behavior (with possibly a few exceptions). | ||
Documentation of downstream projects may require larger updates, | ||
if code snippets are tested. We are working on tooling for: | ||
`doctest-plus <https://github.com/scientific-python/pytest-doctestplus/issues/107>`__ | ||
to facilitate updates. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,12 +88,14 @@ def _make_options_dict(precision=None, threshold=None, edgeitems=None, | |
options['legacy'] = 113 | ||
elif legacy == '1.21': | ||
options['legacy'] = 121 | ||
elif legacy == '1.25': | ||
options['legacy'] = 125 | ||
elif legacy is None: | ||
pass # OK, do nothing. | ||
else: | ||
warnings.warn( | ||
"legacy printing option can currently only be '1.13', '1.21', or " | ||
"`False`", stacklevel=3) | ||
"legacy printing option can currently only be '1.13', '1.21', " | ||
"'1.25', or `False`", stacklevel=3) | ||
|
||
if threshold is not None: | ||
# forbid the bad threshold arg suggested by stack overflow, gh-12351 | ||
|
@@ -288,6 +290,8 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, | |
_format_options['sign'] = '-' | ||
elif _format_options['legacy'] == 121: | ||
set_legacy_print_mode(121) | ||
elif _format_options['legacy'] == 125: | ||
set_legacy_print_mode(125) | ||
elif _format_options['legacy'] == sys.maxsize: | ||
set_legacy_print_mode(0) | ||
|
||
|
@@ -321,7 +325,7 @@ def get_printoptions(): | |
""" | ||
opts = _format_options.copy() | ||
opts['legacy'] = { | ||
113: '1.13', 121: '1.21', sys.maxsize: False, | ||
113: '1.13', 121: '1.21', 125: '1.25', sys.maxsize: False, | ||
}[opts['legacy']] | ||
return opts | ||
|
||
|
@@ -395,9 +399,13 @@ def _object_format(o): | |
return fmt.format(o) | ||
|
||
def repr_format(x): | ||
if isinstance(x, (np.str_, np.bytes_)): | ||
mhvk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return repr(x.item()) | ||
return repr(x) | ||
|
||
def str_format(x): | ||
if isinstance(x, (np.str_, np.bytes_)): | ||
return str(x.item()) | ||
return str(x) | ||
|
||
def _get_formatdict(data, *, precision, floatmode, suppress, sign, legacy, | ||
|
@@ -1400,13 +1408,23 @@ def __call__(self, x): | |
return "({})".format(", ".join(str_fields)) | ||
|
||
|
||
def _void_scalar_repr(x): | ||
def _void_scalar_to_string(x, is_repr=True): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the name change is fine! |
||
""" | ||
Implements the repr for structured-void scalars. It is called from the | ||
scalartypes.c.src code, and is placed here because it uses the elementwise | ||
formatters defined above. | ||
""" | ||
return StructuredVoidFormat.from_data(array(x), **_format_options)(x) | ||
options = _format_options.copy() | ||
if options.get('formatter') is None: | ||
options['formatter'] = {} | ||
options['formatter'].setdefault('float_kind', str) | ||
val_repr = StructuredVoidFormat.from_data(array(x), **options)(x) | ||
if not is_repr: | ||
return val_repr | ||
cls = type(x) | ||
cls_fqn = cls.__module__.replace("numpy", "np") + "." + cls.__name__ | ||
void_dtype = np.dtype((np.void, x.dtype)) | ||
return f"{cls_fqn}({val_repr}, dtype={void_dtype!s})" | ||
|
||
|
||
_typelessdata = [int_, float_, complex_, bool_] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.