Skip to content
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

feat: add string formatter to ak.Array.show #2803

Merged
merged 3 commits into from
Nov 7, 2023

Conversation

agoose77
Copy link
Collaborator

@agoose77 agoose77 commented Nov 7, 2023

This PR closes #1281 by adding support for a subset of NumPy's printoptions simple string formatting configuration. These options are compatible with NumPy's formatting options, apart from strings (only str_kind is compatible with both libraries):

  • NumPy's "numpystr" is ignored
  • Awkward's "str" and "bytes" are used for str/bytes

N.B. we use the g conversion type, which is different to NumPy's default mode.

Precision:

>>> ak.Array([1.0, 2.02313123123123123123123123]).show(precision=4)
[1,
 2.023]

Formatter:

>>> ak.Array(["foo", "bar"]).show(formatter={'str': "<STRING {!r}>".format})
[<STRING 'foo'>,
 <STRING 'bar'>]

I don't think we need to fully replicate NumPy's features here, or support their options context. This PR should add enough control for some additional use cases.

Copy link

codecov bot commented Nov 7, 2023

Codecov Report

Merging #2803 (3c72134) into main (015a362) will increase coverage by 0.12%.
The diff coverage is 93.33%.

Additional details and impacted files
Files Coverage Δ
src/awkward/highlevel.py 77.40% <66.66%> (+0.71%) ⬆️
src/awkward/_prettyprint.py 85.57% <95.49%> (+12.39%) ⬆️

Copy link
Member

@jpivarski jpivarski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Also, I'm glad this is opt-in, so the default precision is still 3 digits. But if someone needs to see more, we can point them to this.

It's also nice that this is following existing conventions, so the documentation only needs to reference https://numpy.org/doc/stable/reference/generated/numpy.set_printoptions.html.

@agoose77
Copy link
Collaborator Author

agoose77 commented Nov 7, 2023

I'm glad this is opt-in, so the default precision is still 3 digits

@jpivarski note that the default precision for complex numbers will increase after this PR: https://github.com/scikit-hep/awkward/pull/2803/files#diff-9d47b8746eed8b0f77589c076ef5be2891fec7d0acd9d41f04dac9cc9bbb1076L227

Are you OK with that change? I'm thinking it's fine; few people are likely using complex numbers, and fewer still should be relying on the string representation. Ideally we'd gracefully handle this but I don't think it's worth the added complexity.

@jpivarski
Copy link
Member

I had checked out the code and had tested it with a floating point value, so I missed the fact that the complex precision used to be 2 digits

>>> ak.Array([np.pi + np.pi*1j]).show()
[3.1+3.1j]

but will now be 3 digits

>>> ak.Array([np.pi + np.pi*1j]).show()
[3.14+3.14j]

The rationale for shorter complex numbers must have been because they use up so much space anyway, with two values, a + and a j. But I don't think this is worse. (Also, no, nobody should be relying on the text representation of numbers. We should be allowed to change that.)

The limit_cols argument gets more important, once one starts expanding the numeric representations.

This PR can be merged.

@agoose77 agoose77 merged commit 9ee586d into main Nov 7, 2023
39 of 41 checks passed
@agoose77 agoose77 deleted the agoose77/feat-string-formatter branch November 7, 2023 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

v2's "show" method should have a "formatter" option for string-formatting
2 participants