|
3 | 3 | import ROOT |
4 | 4 |
|
5 | 5 |
|
6 | | -class FillWithNumpyArray(unittest.TestCase): |
| 6 | +class FillWithArrayLike(unittest.TestCase): |
7 | 7 | """ |
8 | 8 | Test for the FillN method of TH1 and subclasses, which fills |
9 | | - the histogram with a numpy array. |
| 9 | + the histogram with array-like input. |
10 | 10 | """ |
11 | 11 |
|
12 | | - # Tests |
13 | | - def test_fill(self): |
| 12 | + def _run_fill_test(self, data): |
14 | 13 | import numpy as np |
15 | | - # Create sample data |
16 | | - data = np.array([1., 2, 2, 3, 3, 3, 4, 4, 5]) |
| 14 | + |
| 15 | + # Convert once for the reference FillN call |
| 16 | + data_np = np.asanyarray(data, dtype=np.float64) |
| 17 | + n = len(data_np) |
| 18 | + |
17 | 19 | # Create histograms |
18 | 20 | nbins = 5 |
19 | | - min_val = 0 |
20 | | - max_val = 10 |
21 | | - verbose_hist = ROOT.TH1F("verbose_hist", "verbose_hist", nbins, min_val, max_val) |
22 | | - simple_hist = ROOT.TH1F("simple_hist", "simple_hist", nbins, min_val, max_val) |
23 | | - # Fill histograms |
24 | | - verbose_hist.FillN(len(data), data, np.ones(len(data))) |
| 21 | + verbose_hist = ROOT.TH1F("verbose_hist", "verbose_hist", nbins, 0, 10) |
| 22 | + simple_hist = ROOT.TH1F("simple_hist", "simple_hist", nbins, 0, 10) |
| 23 | + |
| 24 | + # Test filling without weights |
| 25 | + verbose_hist.FillN(n, data_np, np.ones(n)) |
25 | 26 | simple_hist.Fill(data) |
26 | | - # Test if the histograms have the same content |
| 27 | + |
27 | 28 | for i in range(nbins): |
28 | 29 | self.assertAlmostEqual(verbose_hist.GetBinContent(i), simple_hist.GetBinContent(i)) |
| 30 | + |
29 | 31 | # Test filling with weights |
30 | | - weights = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]) |
31 | | - verbose_hist.FillN(len(data), data, weights) |
| 32 | + weights_np = np.linspace(0.1, 0.9, n) |
| 33 | + weights = list(weights_np) # also array-like |
| 34 | + |
| 35 | + verbose_hist.FillN(n, data_np, weights_np) |
32 | 36 | simple_hist.Fill(data, weights) |
| 37 | + |
33 | 38 | for i in range(nbins): |
34 | 39 | self.assertAlmostEqual(verbose_hist.GetBinContent(i), simple_hist.GetBinContent(i)) |
35 | | - # Test filling with weights with a different length |
36 | | - weights = np.array([0.1, 0.2, 0.3, 0.4]) |
| 40 | + |
| 41 | + # Test mismatched weight size |
37 | 42 | with self.assertRaises(ValueError): |
38 | | - simple_hist.Fill(data, weights) |
| 43 | + simple_hist.Fill(data, [0.1, 0.2, 0.3]) # too short |
| 44 | + |
| 45 | + # Run with différent inputs |
| 46 | + def test_fill_arraylike(self): |
| 47 | + import numpy as np |
| 48 | + |
| 49 | + inputs = [ |
| 50 | + np.array([1.0, 2, 2, 3, 3, 3, 4, 4, 5]), # numpy |
| 51 | + [1.0, 2, 2, 3, 3, 3, 4, 4, 5], # list |
| 52 | + range(9), # range |
| 53 | + ] |
| 54 | + |
| 55 | + for input_data in inputs: |
| 56 | + with self.subTest(input_type=type(input_data).__name__): |
| 57 | + self._run_fill_test(input_data) |
| 58 | + |
39 | 59 |
|
40 | | -if __name__ == '__main__': |
| 60 | +if __name__ == "__main__": |
41 | 61 | unittest.main() |
0 commit comments