Belle II Software development
TestComparison Class Reference
Inheritance diagram for TestComparison:

Public Member Functions

 root_name (self, name)
 
 setUp (self)
 
 test_compare_profiles (self)
 
 test_compare_profiles_identical (self)
 
 test_compare_identical_profiles_kolmogorov (self)
 
 test_compare_profiles_almost_equal (self)
 
 test_compare_zero_error_profiles (self)
 
 test_compare_histograms (self)
 
 test_compare_unsupported_object (self)
 
 test_compare_differing_objects (self)
 
 test_compare_tefficiencies (self)
 
 test_compare_tefficiencies_same (self)
 
 test_compare_differing_bins (self)
 

Static Public Member Functions

 create_profile (name, entries=5000, mu=10, sigma=0.3, max_fill=50, fixed_number=None)
 
 create_teff (name, bins=20, eff=0.9)
 
 create_histogram (name, entries=5000, mu=10, sigma=3)
 

Public Attributes

int call_iteration = 0
 if we would at some point later want to implement several runs, we use this as a counter variable to set up different names.
 
 profileA = self.create_profile(self.root_name("profileA"))
 Profile A.
 
 profileB = self.create_profile(self.root_name("profileB"))
 Profile B.
 
 profileC
 Profile C.
 
 profileZeroErrorBins
 Profile with bins with 0 error.
 
 profileZeroErrorBinsTwo
 Profile with bins with 0 error.
 
 histogramA
 Histogram A.
 
 histogramB
 Histogram B (should be almost equal to profile A)
 
 profileAequal
 Profile should be almost equal to A.
 
 profileBequal
 Profile should be almost equal to B.
 
 profileDifferentBins
 Profile with different bins.
 
 teffA = self.create_teff(self.root_name("teffA"))
 TEfficiemcy A.
 
 teffB = self.create_teff(self.root_name("teffB"))
 TEfficiency B.
 

Detailed Description

Various test cases for the Chi2Test wrapper class

Definition at line 72 of file test_comparison.py.

Member Function Documentation

◆ create_histogram()

create_histogram ( name,
entries = 5000,
mu = 10,
sigma = 3 )
static
Create a TH1D and fill with random content

Definition at line 110 of file test_comparison.py.

110 def create_histogram(name, entries=5000, mu=10, sigma=3):
111 """
112 Create a TH1D and fill with random content
113 """
114 p = ROOT.TH1D(name, name, 50, 0, 20.0)
115 for i in range(0, entries):
116 p.Fill(random.gauss(mu, sigma))
117 return p
118

◆ create_profile()

create_profile ( name,
entries = 5000,
mu = 10,
sigma = 0.3,
max_fill = 50,
fixed_number = None )
static
Create a TProfile object with various content

Definition at line 78 of file test_comparison.py.

80 ):
81 """
82 Create a TProfile object with various content
83 """
84 p = ROOT.TProfile(name, name, 50, 0, 50.0)
85 entries_per_bin = int(entries / max_fill)
86 for i in range(0, max_fill + 1):
87 for e in range(0, entries_per_bin):
88 if fixed_number is None:
89 p.Fill(i + 0.5, random.gauss(mu, sigma))
90 else:
91 p.Fill(i + 0.5, fixed_number)
92 return p
93

◆ create_teff()

create_teff ( name,
bins = 20,
eff = 0.9 )
static
Creates and fills a root TEfficiency plot

Definition at line 95 of file test_comparison.py.

95 def create_teff(name, bins=20, eff=0.9):
96 """
97 Creates and fills a root TEfficiency plot
98 """
99 p = ROOT.TEfficiency(name, name, bins, 0, 50)
100
101 for i in range(0, 5000):
102 passed = random.uniform(0, 1.0) < eff
103 bin_content = random.uniform(0.0, 50.0)
104 p.Fill(passed, bin_content)
105
106 return p
107

◆ root_name()

root_name ( self,
name )
Generates unique names for ROOT objects

Definition at line 119 of file test_comparison.py.

119 def root_name(self, name):
120 """
121 Generates unique names for ROOT objects
122 """
123 return f"{name}_{self.call_iteration}"
124

◆ setUp()

setUp ( self)
Setup method to generate profiles and histograms for tests

Definition at line 125 of file test_comparison.py.

125 def setUp(self):
126 """
127 Setup method to generate profiles and histograms for tests
128 """
129 random.seed(23)
130
131
134 self.call_iteration = 0
135
136
137 self.profileA = self.create_profile(self.root_name("profileA"))
138
139
140 self.profileB = self.create_profile(self.root_name("profileB"))
141
142
143 self.profileC = self.create_profile(
144 self.root_name("profileC"), 5000, 5, 3
145 )
146
147
148 self.profileZeroErrorBins = self.create_profile(
149 self.root_name("profileZeroErrorBins"), max_fill=49
150 )
151 self.profileZeroErrorBins.SetBinError(35, 0.0)
152
153
154 self.profileZeroErrorBinsTwo = self.create_profile(
155 self.root_name("profileZeroErrorBinsTwo"), max_fill=49
156 )
157 self.profileZeroErrorBinsTwo.SetBinError(35, 0.0)
158
159
160 self.histogramA = self.create_histogram(
161 self.root_name("histogramA"), 5000, 5, 3
162 )
163
164
165 self.histogramB = self.create_histogram(
166 self.root_name("histogramB"), 5000, 5, 3
167 )
168
169
170 self.profileAequal = self.create_profile(
171 self.root_name("profileA_almostequal"), sigma=0.4
172 )
173
174
175 self.profileBequal = self.create_profile(
176 self.root_name("profileB_almostequal"), sigma=0.4
177 )
178
179
180 self.profileDifferentBins = ROOT.TProfile(
181 self.root_name("profileDifferentBins"),
182 self.root_name("profileDifferentBins"),
183 40,
184 0,
185 50.0,
186 )
187
188
189 self.teffA = self.create_teff(self.root_name("teffA"))
190
191
192 self.teffB = self.create_teff(self.root_name("teffB"))
193

◆ test_compare_differing_bins()

test_compare_differing_bins ( self)
Test if the comparison attempt of profiles with differing bin count
fails properly

Definition at line 321 of file test_comparison.py.

321 def test_compare_differing_bins(self):
322 """
323 Test if the comparison attempt of profiles with differing bin count
324 fails properly
325 """
326 c = validationcomparison.Chi2Test(
327 self.profileA, self.profileDifferentBins
328 )
329 self.assertFalse(c.can_compare())
330
331 with self.assertRaises(validationcomparison.DifferingBinCount):
332 c._compute()
333
334

◆ test_compare_differing_objects()

test_compare_differing_objects ( self)
Test if the comparison of differing objects is rejected

Definition at line 281 of file test_comparison.py.

281 def test_compare_differing_objects(self):
282 """
283 Test if the comparison of differing objects is rejected
284 """
285 c = validationcomparison.Chi2Test(self.profileA, self.histogramA)
286 self.assertFalse(c.can_compare())
287
288 with self.assertRaises(validationcomparison.ObjectsNotSupported):
289 c._compute()
290

◆ test_compare_histograms()

test_compare_histograms ( self)
Test comparison of regular histograms

Definition at line 259 of file test_comparison.py.

259 def test_compare_histograms(self):
260 """
261 Test comparison of regular histograms
262 """
263
264 c = validationcomparison.Chi2Test(self.histogramA, self.histogramB)
265
266 self.assertTrue(c.can_compare())
267 c.ensure_compute()
268 self.assertAlmostEqual(c._pvalue, 0.371600562118221)
269 self.assertAlmostEqual(c._chi2, 42.308970111484086)
270 self.assertAlmostEqual(c._chi2ndf, 1.0577242527871022)
271 self.assertEqual(c._ndf, 40)
272

◆ test_compare_identical_profiles_kolmogorov()

test_compare_identical_profiles_kolmogorov ( self)
 Test if comparing to identical TProfiles with Kolmo Test works

Definition at line 214 of file test_comparison.py.

214 def test_compare_identical_profiles_kolmogorov(self):
215 """ Test if comparing to identical TProfiles with Kolmo Test works"""
216 c = validationcomparison.KolmogorovTest(self.profileA, self.profileA)
217 self.assertTrue(c.can_compare())
218 c.ensure_compute()
219 self.assertAlmostEqual(c._pvalue, 1)
220

◆ test_compare_profiles()

test_compare_profiles ( self)
Test if comparing two similar TProfiles works

Definition at line 194 of file test_comparison.py.

194 def test_compare_profiles(self):
195 """
196 Test if comparing two similar TProfiles works
197 """
198 c = validationcomparison.Chi2Test(self.profileA, self.profileB)
199
200 self.assertTrue(c.can_compare())
201 c.ensure_compute()
202 self.assertAlmostEqual(c._pvalue, 0.22495088947037362)
203

◆ test_compare_profiles_almost_equal()

test_compare_profiles_almost_equal ( self)
Test if the comparison of two TProfiles with very similar content works

Definition at line 221 of file test_comparison.py.

221 def test_compare_profiles_almost_equal(self):
222 """
223 Test if the comparison of two TProfiles with very similar content works
224 """
225 c = validationcomparison.Chi2Test(
226 self.profileAequal, self.profileBequal
227 )
228
229 self.assertTrue(c.can_compare())
230 c.ensure_compute()
231 self.assertAlmostEqual(c._pvalue, 0.43093514577898634)
232 self.assertAlmostEqual(c._ndf, 49)
233

◆ test_compare_profiles_identical()

test_compare_profiles_identical ( self)
Test if comparing two identical TProfiles works

Definition at line 204 of file test_comparison.py.

204 def test_compare_profiles_identical(self):
205 """
206 Test if comparing two identical TProfiles works
207 """
208 c = validationcomparison.Chi2Test(self.profileA, self.profileA)
209
210 self.assertTrue(c.can_compare())
211 c.ensure_compute()
212 self.assertAlmostEqual(c._pvalue, 1)
213

◆ test_compare_tefficiencies()

test_compare_tefficiencies ( self)
Test if two TEfficiency objects can be compared. Is a bit tricky
as TEfficiency does not support

Definition at line 291 of file test_comparison.py.

291 def test_compare_tefficiencies(self):
292 """
293 Test if two TEfficiency objects can be compared. Is a bit tricky
294 as TEfficiency does not support
295 """
296
297 c = validationcomparison.Chi2Test(self.teffA, self.teffB)
298
299 self.assertTrue(c.can_compare())
300 c.ensure_compute()
301 self.assertAlmostEqual(c._pvalue, 0.9760318312199932)
302 self.assertAlmostEqual(c._chi2, 8.16784873)
303 self.assertAlmostEqual(c._chi2ndf, 0.45376937)
304 self.assertEqual(c._ndf, 18)
305

◆ test_compare_tefficiencies_same()

test_compare_tefficiencies_same ( self)
Test if two TEfficiency objects can be compared. Is a bit tricky as
TEfficiency does not support Comparing the exact same TEfficiency
object should give back 100% agreement

Definition at line 306 of file test_comparison.py.

306 def test_compare_tefficiencies_same(self):
307 """
308 Test if two TEfficiency objects can be compared. Is a bit tricky as
309 TEfficiency does not support Comparing the exact same TEfficiency
310 object should give back 100% agreement
311 """
312
313 c = validationcomparison.Chi2Test(self.teffA, self.teffA)
314 self.assertTrue(c.can_compare())
315 c.ensure_compute()
316 self.assertAlmostEqual(c._pvalue, 1.0)
317 self.assertAlmostEqual(c._chi2, 0.0)
318 self.assertAlmostEqual(c._chi2ndf, 0.0)
319 self.assertEqual(c._ndf, 18)
320

◆ test_compare_unsupported_object()

test_compare_unsupported_object ( self)
Test if unsupported ROOT objects are treated properly

Definition at line 273 of file test_comparison.py.

273 def test_compare_unsupported_object(self):
274 """
275 Test if unsupported ROOT objects are treated properly
276 """
277 obj_not_supported = ROOT.TH2D("h2d", "h2d", 50, 0, 50, 50, 0, 50)
278 c = validationcomparison.Chi2Test(self.profileA, obj_not_supported)
279 self.assertFalse(c.can_compare())
280

◆ test_compare_zero_error_profiles()

test_compare_zero_error_profiles ( self)
Test if bins with zero error are treated properly

Definition at line 234 of file test_comparison.py.

234 def test_compare_zero_error_profiles(self):
235 """
236 Test if bins with zero error are treated properly
237 """
238
239 # add entry in the last bin, which will increase the bin weight,
240 # but leave the error at zero
241 self.profileZeroErrorBins.Fill(49.8, 1.0)
242 self.profileZeroErrorBins.Fill(49.8, 1.0)
243
244 # the comparison should set the bin content of this bin to zero
245 # to disable comparison of this bin instead of
246 # not doing the comparison at all
247 c = validationcomparison.Chi2Test(
248 self.profileZeroErrorBins, self.profileZeroErrorBinsTwo
249 )
250
251 self.assertTrue(c.can_compare())
252
253 c.ensure_compute()
254
255 self.assertAlmostEqual(c._pvalue, 0.4835651485797353)
256 # should still be only 49 ndf
257 self.assertEqual(c._ndf, 49)
258

Member Data Documentation

◆ call_iteration

int call_iteration = 0

if we would at some point later want to implement several runs, we use this as a counter variable to set up different names.

However not implemented yet.

Definition at line 134 of file test_comparison.py.

◆ histogramA

histogramA
Initial value:
= self.create_histogram(
self.root_name("histogramA"), 5000, 5, 3
)

Histogram A.

Definition at line 160 of file test_comparison.py.

◆ histogramB

histogramB
Initial value:
= self.create_histogram(
self.root_name("histogramB"), 5000, 5, 3
)

Histogram B (should be almost equal to profile A)

Definition at line 165 of file test_comparison.py.

◆ profileA

profileA = self.create_profile(self.root_name("profileA"))

Profile A.

Definition at line 137 of file test_comparison.py.

◆ profileAequal

profileAequal
Initial value:
= self.create_profile(
self.root_name("profileA_almostequal"), sigma=0.4
)

Profile should be almost equal to A.

Definition at line 170 of file test_comparison.py.

◆ profileB

profileB = self.create_profile(self.root_name("profileB"))

Profile B.

Definition at line 140 of file test_comparison.py.

◆ profileBequal

profileBequal
Initial value:
= self.create_profile(
self.root_name("profileB_almostequal"), sigma=0.4
)

Profile should be almost equal to B.

Definition at line 175 of file test_comparison.py.

◆ profileC

profileC
Initial value:
= self.create_profile(
self.root_name("profileC"), 5000, 5, 3
)

Profile C.

Definition at line 143 of file test_comparison.py.

◆ profileDifferentBins

profileDifferentBins
Initial value:
= ROOT.TProfile(
self.root_name("profileDifferentBins"),
self.root_name("profileDifferentBins"),
40,
0,
50.0,
)

Profile with different bins.

Definition at line 180 of file test_comparison.py.

◆ profileZeroErrorBins

profileZeroErrorBins
Initial value:
= self.create_profile(
self.root_name("profileZeroErrorBins"), max_fill=49
)

Profile with bins with 0 error.

Definition at line 148 of file test_comparison.py.

◆ profileZeroErrorBinsTwo

profileZeroErrorBinsTwo
Initial value:
= self.create_profile(
self.root_name("profileZeroErrorBinsTwo"), max_fill=49
)

Profile with bins with 0 error.

Definition at line 154 of file test_comparison.py.

◆ teffA

teffA = self.create_teff(self.root_name("teffA"))

TEfficiemcy A.

Definition at line 189 of file test_comparison.py.

◆ teffB

teffB = self.create_teff(self.root_name("teffB"))

TEfficiency B.

Definition at line 192 of file test_comparison.py.


The documentation for this class was generated from the following file: