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

fix: array copy and logic for obtaining variances and values #488

Merged
merged 7 commits into from
Apr 2, 2024

Conversation

jonas-eschle
Copy link
Contributor

I've noticed the new .copy() to avoid that the underlying array changes (such as here https://github.com/scikit-hep/mplhep/blob/master/src/mplhep/plot.py#L191).

I would suggest to follow closer the array protocol, which uses the np.copy -> other, array-like types (like JAX, TensorFlow, PyTorch etc) may don't have the copy attribute but support the np.copy (as we've noticed in the CI).

Also, the logic was possibly flawed, as a histogram also has values and therefore, it would take the second if, wouldn't it? Removed also the re-assignement of 0 to the flows, they were already initialized with 0.

Does the new flow makes sense?

@andrzejnovak
Copy link
Member

andrzejnovak commented Apr 1, 2024

Thanks! I got a bit lazy about the copies, should have fixed that :)

Also, the logic was possibly flawed, as a histogram also has values and therefore, it would take the second if, wouldn't it?

I think this is what's tripping up the CI. hasattr(h, "axes") and hasattr(h.axes[0], "traits") uproot TH1 probably also has traits, just not the under/overflow features. It's a bit annoying to be duck-typing between hist/uproot but I don't think there's a better solution.

Removed also the re-assignement of 0 to the flows, they were already initialized with 0.

Sounds good

PS. Makes me kind of happy to see this kind of stuff caught on CI downstream

@jonas-eschle
Copy link
Contributor Author

I think this is what's tripping up the CI. hasattr(h, "axes") and hasattr(h.axes[0], "traits") uproot TH1 probably also has traits, just not the under/overflow features. It's a bit annoying to be duck-typing between hist/uproot but I don't think there's a better solution.

true, I've refactored and re-arranged a bit. Also added a test without the variances, as this would fail.

PS. Makes me kind of happy to see this kind of stuff caught on CI downstream

yes, the advantage of a nice library that is used by many others ;)

@jonas-eschle
Copy link
Contributor Author

Looks fine from my side, mypy fails because of the over- and underflow being arrays, not floats. Feel free to adjust, I think it was like this before as well, or let me know what's the best way to go.

Otherwise can be merged from my side

@andrzejnovak
Copy link
Member

andrzejnovak commented Apr 2, 2024

Huh, so the return type for np.copy(float) is apparently not float which is what was tripping up mypy. Calling copy on the array instead is probably a touch less efficient but works and we're not too concerned about performance for plotting.

Can you check if this still fine in zfit? I'll merge it then.

We could also do copy.copy for these

@jonas-eschle
Copy link
Contributor Author

Huh, so the return type for np.copy(float) is apparently not float which is what was tripping up mypy. Calling copy on the array instead is probably a touch less efficient but works and we're not too concerned about performance for plotting.

the type returned was actually a numpy.float64, but now it's an array with shape (), afaiu. The performance should be about the same, the expensive part is anyways the copy of large array (if at all), and that happens in both cases.

Can you check if this still fine in zfit? I'll merge it then.

yes, works, can be merged

We could also do copy.copy for these

that should also work I think, alternatively, we could also convert to floats... all works, as long as we don't use the copy attribute

@andrzejnovak
Copy link
Member

Alright, let's run with this for now, see if anything comes up laters. Thanks for the PR!

@andrzejnovak andrzejnovak merged commit d557913 into master Apr 2, 2024
9 checks passed
@andrzejnovak andrzejnovak deleted the je_fix_array_copy branch April 2, 2024 19:32
jonas-eschle added a commit that referenced this pull request Apr 22, 2024
* fix: array copy and logic

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: new logic with values and flow

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* tests: expand test to incorporate hists _without_ variance

* tests: fix parametrized histplot flow variances test

* ci: fix mypy

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: andrzejnovak <novak5andrzej@gmail.com>
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.

2 participants