root icon indicating copy to clipboard operation
root copied to clipboard

[PyROOT] Add NumPy style Histogram creator

Open aaronj0 opened this issue 1 year ago • 2 comments

This PR enables a pythonic histogram creator that automatically returns a TH1/2/3 object filled with the numpy data, and configures the binning mode based on keyword args:

n = 10000
x = np.linspace(1, 10, n)
y = 2*np.log(x) + np.random.rand(n)
a = np.stack((x, y), axis = 1)
hist2 = ROOT.CreateHisto(name = "2D uniform binning",
                         bins = (100, 100), data = a)
var_bins = np.array(np.linspace(0, 3.16, 100))**2
hist2 = ROOT.CreateHisto(name = "2D variable binning",
                         bins = (var_bins, 10), data = a)

aaronj0 avatar Nov 27 '24 16:11 aaronj0

Test Results

    18 files      18 suites   5d 0h 54m 42s ⏱️  2 723 tests  2 723 ✅ 0 💤 0 ❌ 47 323 runs  47 323 ✅ 0 💤 0 ❌

Results for commit a18c9705.

:recycle: This comment has been updated with latest results.

github-actions[bot] avatar Nov 27 '24 21:11 github-actions[bot]

Hi @aaronj0 , thank you for the PR, cool feature! One point I would like to raise is that now with the Unified Histogram Interface (UHI), we can already directly fill a histogram with numpy data, adapting your example:

n = 10000
x = np.linspace(1, 10, n)
y = 2*np.log(x) + np.random.rand(n)
hist2 = ROOT.TH2D("hist2", "2D uniform binning", 100, 1, 10, 100, np.min(y), np.max(y))
hist2[...] = np.histogram2d(x, y, bins=(100, 100))[0]

Given this functionality maybe we can discuss further:

  • Is this a simpler interface than UHI?
  • Are there features this PR adds that UHI doesn't support?
  • Maybe the implementation can be adapted to avoid the for loop with the call to Fill for every entry by computing the bin contents with numpy directly and leveraging the UHI implementation which directly manipulates thefArray array of data?
  • ...

siliataider avatar May 30 '25 08:05 siliataider