-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tests.py
318 lines (299 loc) · 10.7 KB
/
Tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "Math/ProbFuncMathCore.h"
"""
Created on March 2012
@author: Andrea Dotti (andrea.dotti@cern.ch)
This module contains definition of tests to perform
regression testing. There are tests to
perform a statistical comparison between binned
and unbinned distributions.
Tests input are ROOT's histograms or TTrees.
When possible the ROOT functionalities are used.
See testme function for exanmple
"""
from Utils import DataType,BaseClass,Error
from Utils import getFrom1DHistogram,getFromTree,makeROOTgof
class SkipTest(Error):
pass
class ZeroEntries(SkipTest):
def __init__(self,hname):
SkipTest.__init__(self,"No data: zero statistics, cannot perform test (%s)"%hname)
class ZeroDoF(SkipTest):
def __init__(self,hname):
SkipTest.__init__(self,"No degrees of freedom for Chi2Test in %s"%hname)
class StatTest:
"""
Base Class definint a test object
"""
def __init__(self,data1,data2,dataType):
"""
Create a new test for the specified iunputs of
type data type.
Values are cached.
"""
self.dataType = dataType
self.dataset1 = data1
self.dataset2 = data2
# print("IN INIT DATASET %s"%self.dataset2)
self.cached = False
# def __init__(self,data1):
# """
# Create a new test for the specified iunputs of
# type data type.
# Values are cached.
# """
# self.dataset1 = data1
# self.cached = False
def calc( self ):
"""
If needed perform the
calculations for tests.
"""
if not self.cached:
self.statvalue = self._stat()
self.pvalue = self._pval()
self.ndfvalue = self._ndf()
self.cached = True
def _stat(self ):
"""
To be implemented by derived class:
perform calculation of statistics value
"""
raise BaseClass()
def _pval(self):
"""
To be implemented by derived class:
perform calculation of p-value
"""
raise BaseClass()
def _ndf(self):
"""
To be implemented by derived class:
perform calculation of degrees of freedom
"""
raise BaseClass()
def value(self):
"""
Return value of statistics
"""
raise BaseClass()
def pval(self):
"""
Return p-value
"""
if not self.cached:
self.calc()
return self.pvalue
def stat(self):
"""
Return value of statistics
"""
if not self.cached:
self.calc()
return self.statvalue
def ndf(self):
"""
Return degrees-of-freedom
"""
if not self.cached:
self.calc()
return self.ndfvalue
class Binned1DChi2Test(StatTest):
"""
Chi2 Test for Binned distributions.
Inputs: Two 1D histograms
"""
def __init__(self,h1,h2,option="UU"):
StatTest.__init__(self,h1,h2,DataType.BINNED1D)
self.option = option
self.cached = False
from ROOT import TH1F
theaxis = h1.GetXaxis()
self.residuals = TH1F("%s-Residuals"%h1.GetName(),"%s Residuals"%h1.GetName(),
theaxis.GetNbins(),theaxis.GetXmin(),theaxis.GetXmax())
self.localpval = None
self.localchi2 = None
self.localndf = None
def value(self):
return self.pval()
def _pval(self):
return self.localpval
def _stat(self):
#This is the first method to be called...
#from ctypes import c_double
mysize = self.residuals.GetXaxis().GetNbins()
#myarray = mysize * c_double
#myres = myarray()
#pval = self.dataset1.Chi2Test( self.dataset2 ,"UU",myres)
from array import array
myres = array('d',mysize*[0])
chi2 = array('d',[0])
ndf = array('i',[0])
igood=array('i',[0])
#Chi2 test does not work if there is no statistics
if self.dataset1.GetEntries() == 0:
raise ZeroEntries(self.dataset1.GetName())
if self.dataset2.GetEntries() == 0:
raise ZeroEntries(self.dataset1.GetName())
self.localpval = self.dataset1.Chi2TestX( self.dataset2,chi2,ndf,igood,self.option,myres)
if ndf[0]==0:
raise ZeroDoF(self.dataset1.GetName())
self.localchi2 = chi2[0]
self.localndf = ndf[0]
# print("Binned1DChi2Test %s pval %s"%(self.dataset1.GetName(),self.localpval))
# print("Binned1DChi2Test %s chi2 %s"%(self.dataset1.GetName(),self.localchi2))
# print("Binned1DChi2Test %s ndf %s"%(self.dataset1.GetName(),self.localndf))
for binidx in range(0,mysize):
self.residuals.SetBinContent( binidx+1, myres[binidx] )
return self.localpval
def _ndf(self):
return self.localndf
class BinnedWeighted1DChi2Test(Binned1DChi2Test):
"""
Chi2 Test for Binned distributions, distributions are weighted.
"""
def __init__(self,h1,h2):
Binned1DChi2Test.__init__(self,h1,h2,"WW")
class AndersonDarlingTest(StatTest):
"""
Anderson-Darling Test.
Inputs: Two samples. Both binned or unbinned are accepted
"""
def __init__(self,h1,h2,dataType=DataType.UNBINNED):
if dataType == DataType.BINNED1D:
first = getFrom1DHistogram( h1 )
second= getFrom1DHistogram( h2 )
elif dataType == DataType.UNBINNED:
first = getFromTree( h1 )
second = getFromTree( h2 )
#print first[0:10]
#print second[0:10]
else:
raise WrongDataType()
StatTest.__init__(self,first,second,dataType)
from Utils import makeROOTgof
self.gof=makeROOTgof(self.dataset1,self.dataset2)
def value(self):
return self.pval()
def _pval(self):
return self.gof.AndersonDarling2SamplesTest("p")
def _stat(self):
return self.gof.AndersonDarling2SamplesTest("t")
def _ndf(self):
return 0
def BinnedAndersonDarlingTest(AndersonDarlingTest):
def __init__(self,d1,d2):
AndersonDarlingTest.__init__(self,d1,d2,DataType.BINNED1D)
class KolmogorovSmirnovTest(StatTest):
"""
Kolmogorov-Smirnov Test.
Inputs: Two samples. Both binned or unbinned are accepted (see ROOT's documentation
for TH1::KolmogorovSmirnov test for binned distributions)
"""
def __init__(self,d1,d2,dataType=DataType.UNBINNED):
if dataType == DataType.BINNED1D:
#t print 'WARNING: Using test on binned distributions'
first = d1 #getFrom1DHistogram( d1 )
second= d2 #getFrom1DHistogram( d2 )
elif dataType == DataType.UNBINNED:
first = getFromTree( d1 )
second = getFromTree( d2 )
else:
raise WringDataType()
StatTest.__init__(self,first,second,dataType)
if dataType == DataType.UNBINNED:
self.gof=makeROOTgof(self.dataset1,self.dataset2)
def value(self):
return self.pval()
def _ndf(self):
return 0
def _pval(self):
if self.dataType == DataType.UNBINNED:
return self.gof.KolmogorovSmirnov2SamplesTest("p")
else:
# print("DATASET1 %s"%self.dataset1)
# print("DATASET2 %s"%self.dataset2)
# print("DATASET1 %s DATASET2 %s"%(self.dataset1.GetEntries(),self.dataset2.GetEntries()))
# print("DATASET1 %s DATASET2 %s"%(self.dataset1.Integral(),self.dataset2.Integral()))
if self.dataset1.Integral() == 0 and self.dataset2.Integral() == 0:
return 1.
elif self.dataset1.Integral() != 0 and self.dataset2.Integral() == 0:
return 0.
elif self.dataset1.Integral() == 0 and self.dataset2.Integral() != 0:
return 0.
else:
#G no debugging return self.dataset1.KolmogorovTest( self.dataset2 ,"D")
return self.dataset1.KolmogorovTest( self.dataset2 ,"")
def _stat(self):
return self._pval()
#G if self.dataType == DataType.UNBINNED:
#G return self.gof.KolmogorovSmirnov2SamplesTest("t")
#G else:
#G return self.dataset1.KolmogorovTest( self.dataset2 ,"M")
class BinnedKolmogorovSmirnovTest(KolmogorovSmirnovTest):
def __init__(self,h1,h2):
KolmogorovSmirnovTest.__init__(self,h1,h2,DataType.BINNED1D)
def testme():
"""
Example function
"""
h1 = TH1F("h1","h",100,-10,10)
h2 = TH1F("h2","h",100,-10,10)
h1.FillRandom("gaus")
h2.FillRandom("gaus")
h1.SetLineColor(2)
h1.DrawCopy()
h2.DrawCopy("same")
binned = Binned1DChi2Test( h1 , h2 )
print 'For Chi2Test:',binned.pval()
binned = AndersonDarlingTest( h1 , h2 , DataType.BINNED1D)
print 'For AD: ',binned.pval()
binned = KolmogorovSmirnovTest( h1 , h2, DataType.BINNED1D)
print 'For KS: ',binned.pval()
class NormalCDF(StatTest):
"""
Normal Cumulative Distribution Function Test.
"""
def __init__(self,d1):
StatTest.__init__(self,d1,d1,"")
def value(self):
return self.pval()
def _ndf(self):
return 0
def _pval(self):
# Get number of sigmas
import math
if self.dataset1['ErrorRef'] != 0 :
nsig = (float(self.dataset1['Value']) - float(self.dataset1['ValueRef']))/float(self.dataset1['ErrorRef'])
else:
if float(self.dataset1['Value']) - float(self.dataset1['ValueRef']) != 0 :
print ("Error Ref is 0, ValueRef = %s , Value = %s"%(self.dataset1['Value'],self.dataset1['ValueRef']))
nsig = 0
# 'Cumulative distribution function for the standard normal distribution'
pval = 2.*(1.0 + math.erf(-math.fabs(nsig) / math.sqrt(2.0))) / 2.0
# print "NormalCDF _pval : %s -> %s"%(nsig,pval)
return pval
def _stat(self):
# Get number of sigmas
return self._pval()
#import math
# nsig = (float(self.dataset1['Value']) - float(self.dataset1['ValueRef']))/float(self.dataset1['ErrorRef'])
## 'Cumulative distribution function for the standard normal distribution'
# pval = (1.0 + math.erf(-nsig / math.sqrt(2.0))) / 2.0
# print "NormalCDF _stat : %s -> %s"%(nsig,pval)
# return pval
_testsMap = {
"BinnedAndersonDarlingTest" : BinnedAndersonDarlingTest,
"AndersonDarlingTest" : AndersonDarlingTest ,
"Binned1DChi2Test" : Binned1DChi2Test,
"BinnedWeighted1DChi2Test" : BinnedWeighted1DChi2Test,
"BinnedKolmogorovSmirnovTest" : BinnedKolmogorovSmirnovTest,
"KolmogorovSmirnovTest" : KolmogorovSmirnovTest,
"NormalCDF" : NormalCDF
}
def getTestByName( name ):
"""
Returns the test class by name
"""
if _testsMap.has_key(name):
return _testsMap[name]
return StatTest