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: conversion of ROOT histograms to PlottableHistogram #27

Merged
merged 5 commits into from Jun 13, 2021

Conversation

pieterdavid
Copy link
Contributor

Tested for TH1F by comparing as follows:

import ROOT
h = ROOT.TH1F("h1", "h1", 50, -2.5, 2.5)
h.FillRandom("gaus", 10000)
h.Draw()
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
import mplhep
mplhep.histplot(h)
plt.show()

The conversion to numpy exploits the fact that TH1 inherits from TArray (which can directly be converted to a numpy array) for storing the counts, and the sums of squared weights is stored as another TArray (so most could be replaced by equivalent PyROOT pythonizations in the future).
For more-dimensional histograms I put order="F" in the reshape such that .values()[i,j] equals .GetBinContent(i+1, j+1), but didn't test beyond that. Overflow bins are dropped.

CC @andrzejnovak

@henryiii
Copy link
Member

henryiii commented Jun 8, 2021

This is awesome, thanks! Bug me if I don't review it by the end of the week.

@henryiii
Copy link
Member

henryiii commented Jun 8, 2021

PS: Sourcery (c34d809) recommends using "guard" style, which has the short-circuit branch come first. It's easier to read with less action at a distance, and avoids excessive indentation.

@andrzejnovak
Copy link
Member

andrzejnovak commented Jun 8, 2021

I was just wondering if this was already in, would be great to have by PyHEP :)

Copy link
Member

@henryiii henryiii left a comment

Choose a reason for hiding this comment

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

Looks good! A couple of optional ideas:

  • We could either make type aliases to improve readability for the ROOT types (TAxis = Any), or maybe even make them Protocols (TAxis would have GetNbins, etc). But it's really not any different from "Any".
  • It's optional and beyond the strict protocol, but maybe we could promote fName to .name?

@pieterdavid
Copy link
Contributor Author

pieterdavid commented Jun 9, 2021

Thanks for the review!

  • We could either make type aliases to improve readability for the ROOT types (TAxis = Any), or maybe even make them Protocols (TAxis would have GetNbins, etc). But it's really not any different from "Any".

I'm not familiar enough with the type annotations, protocols etc. yet to tackle that, sorry, but feel free to add it.

  • It's optional and beyond the strict protocol, but maybe we could promote fName to .name?

Good idea, done

@henryiii henryiii force-pushed the addTH1Fhelpers branch 2 times, most recently from bb9249b to f9ede8b Compare June 11, 2021 04:10
@henryiii
Copy link
Member

henryiii commented Jun 11, 2021

I've added a ROOT test system (you can even run it locally with nox, though it will be a bit slow until wntrblm/nox#444, and it will be a little harder to maintain unless wntrblm/nox#447 gets addressed). Could you add a test converting a ROOT histogram to the mostly-empty test file I've added?

@sourcery-ai
Copy link

sourcery-ai bot commented Jun 12, 2021

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 2.63%.

Quality metrics Before After Change
Complexity 2.84 ⭐ 2.39 ⭐ -0.45 👍
Method Length 30.91 ⭐ 28.12 ⭐ -2.79 👍
Working memory 6.00 ⭐ 5.46 ⭐ -0.54 👍
Quality 82.81% 85.44% 2.63% 👍
Other metrics Before After Change
Lines 237 378 141
Changed files Quality Before Quality After Quality Change
noxfile.py 89.48% ⭐ 89.97% ⭐ 0.49% 👍
src/uhi/numpy_plottable.py 80.09% ⭐ 84.49% ⭐ 4.40% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
src/uhi/numpy_plottable.py ensure_plottable_histogram 15 🙂 192 😞 12 😞 41.34% 😞 Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

@henryiii
Copy link
Member

Beautiful, thank you!

@henryiii henryiii changed the title Add conversion of ROOT histograms to PlottableHistogram feat: conversion of ROOT histograms to PlottableHistogram Jun 13, 2021
@henryiii henryiii merged commit c2e481e into scikit-hep:main Jun 13, 2021
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.

None yet

3 participants