1import unittest 2import math 3import pygsl.histogram 4import warnings 5import exceptions 6import pygsl.errors 7import tempfile 8import os 9import os.path 10 11 12pygsl.set_debug_level(0) 13class histogram_tests(unittest.TestCase): 14 """ 15 gsl_histogram tests 16 """ 17 18 def test_alloc(self): 19 """ 20 allocate a histogram 21 """ 22 hist=pygsl.histogram.histogram(100) 23 self.failIf(hist.bins()!=100) 24 hist.alloc(500) 25 self.failIf(hist.bins()!=500) 26 exception_seen=0 27 try: 28 hist.alloc(-1) 29 except pygsl.errors.gsl_Error: 30 exception_seen=1 31 self.failIf(exception_seen==0) 32 self.failIf(hist.bins()!=500) 33 34 def test_initialize(self): 35 """ 36 allocate a histogram 37 """ 38 hist=pygsl.histogram.histogram(100) 39 self.failIf(hist.bins()!=100) 40 hist.set_ranges_uniform(0.0,2.0) 41 self.failIf(hist.min()!=0.0) 42 self.failIf(hist.max()!=2.0) 43 44 def test_DomainWarning(self): 45 """ 46 """ 47 hist=pygsl.histogram.histogram(100) 48 hist.set_ranges_uniform(1,20) 49 # raise domain warning as exception here... 50 exception_seen=0 51 warnings.filterwarnings("error", 52 category=pygsl.errors.gsl_DomainWarning) 53 try: 54 hist.increment(22) 55 except pygsl.errors.gsl_DomainWarning: 56 exception_seen=1 57 self.failIf(exception_seen==0) 58 # no warning/exception should occur 59 hist.increment(10) 60 61 def test_ranges(self): 62 hist=pygsl.histogram.histogram(100) 63 hist.set_ranges_uniform(0,20) 64 self.failIf(0!=hist.get_range(0)[0]) 65 self.failIf(20!=hist.get_range(99)[1]) 66 self.failIf(0!=hist.find(0)) 67 self.failIf(99!=hist.find(19.9999)) 68 hist.set_ranges(range(len(hist)+1)) 69 self.failIf(100!=hist.max()) 70 self.failIf(0!=hist.min()) 71 hist.alloc(2) 72 exception_seen=0 73 try: 74 hist.set_ranges((1,2)) 75 except pygsl.errors.gsl_InvalidArgumentError: 76 exception_seen=1 77 self.failIf(exception_seen==0) 78 exception_seen=0 79 try: 80 hist.set_ranges((1,2,"bla")) 81 except exceptions.Exception: 82 exception_seen=1 83 self.failIf(exception_seen==0) 84 85 def test_minimax(self): 86 hist=pygsl.histogram.histogram(100) 87 hist.set_ranges_uniform(0,20) 88 hist.increment(10) 89 hist.accumulate(0,-1) 90 self.failIf(1!=hist.max_val()) 91 self.failIf(50!=hist.max_bin()) 92 self.failIf(-1!=hist.min_val()) 93 self.failIf(0!=hist.min_bin()) 94 95 def test_meansigmasum(self): 96 hist=pygsl.histogram.histogram(100) 97 hist.set_ranges_uniform(0,20) 98 hist.increment(10) 99 self.failIf(10.1!=hist.mean()) 100 self.failIf(hist.sum()!=1) 101 102 def test_as_map(self): 103 hist=pygsl.histogram.histogram(100) 104 hist.set_ranges_uniform(0,100) 105 self.failIf(len(hist)!=100) 106 hist[2]=1 107 self.failIf(hist.get(2)!=1) 108 self.failIf(hist[2]!=1) 109 del hist[2] 110 self.failIf(hist[2]!=0) 111 112 def test_shiftscale(self): 113 hist=pygsl.histogram.histogram(100) 114 hist.shift(1) 115 self.failIf(hist[1]!=1) 116 hist.scale(10) 117 self.failIf(hist[1]!=10) 118 119 def test_compare_ranges(self): 120 hist1=pygsl.histogram.histogram(100) 121 hist2=pygsl.histogram.histogram(100) 122 hist1.set_ranges_uniform(1,2) 123 hist2.set_ranges_uniform(1,2) 124 self.failIf(not hist1.equal_bins_p(hist2)) 125 126 def test_add_sub(self): 127 hist1=pygsl.histogram.histogram(100) 128 hist2=pygsl.histogram.histogram(100) 129 hist1.set_ranges_uniform(1,2) 130 hist2.set_ranges_uniform(1,2) 131 hist1.shift(1) 132 hist2.shift(2) 133 hist1.add(hist2) 134 self.failIf(hist1[1]!=3) 135 hist2.sub(hist1) 136 self.failIf(hist2[1]!=-1) 137 self.failIf(hist1[1]!=3) 138 139 def test_readwrite(self): 140 self.my_filename=tempfile.mktemp() 141 hist=pygsl.histogram.histogram(100) 142 hist.set_ranges_uniform(1,2) 143 hist[10]=1 144 # write it 145 hist_file=file(self.my_filename,'w') 146 hist.write(hist_file) 147 hist_file.close() 148 # clear and change histogram 149 hist.set_ranges_uniform(0,1) 150 # reread it from file 151 hist_file=file(self.my_filename,'r') 152 hist.read(hist_file) 153 hist_file.close() 154 self.failIf(hist.max()!=2 or hist.min()!=1 or hist[10]!=1 or hist[99]!=0, 155 "rereading histogram failed") 156 157 def test_scanfprintf(self): 158 self.my_filename=tempfile.mktemp() 159 hist=pygsl.histogram.histogram(100) 160 hist.set_ranges_uniform(1,2) 161 hist[10]=1 162 # write it 163 hist_file=file(self.my_filename,'w') 164 hist.printf(hist_file,"%g","%f") 165 hist_file.close() 166 # clear and change histogram 167 hist.set_ranges_uniform(0,1) 168 # reread it from file 169 hist_file=file(self.my_filename,'r') 170 hist.scanf(hist_file) 171 hist_file.close() 172 self.failIf(hist.max()!=2 or hist.min()!=1 or hist[10]!=1 or hist[99]!=0, 173 "rescaning histogram failed") 174 175 def tearDown(self): 176 # clean up files 177 if 'my_filename' in vars(self) and os.path.isfile(self.my_filename): 178 os.remove(self.my_filename) 179 del self.my_filename 180 181 182class histogram2d_tests(unittest.TestCase): 183 """ 184 gsl_histogram2d tests 185 """ 186 def test_alloc(self): 187 """ 188 allocate a histogram 189 """ 190 hist=pygsl.histogram.histogram2d(100,100) 191 # clone will fail, if not initialised 192 hist.set_ranges_uniform(-1,1,-1,1) 193 hist[3,3]=5 194 self.failIf(hist[3,3]!=5,"mapping protocol failed") 195 hist2=pygsl.histogram.histogram2d(hist) 196 self.failIf(hist2[3,3]!=5,"copy constructor failed") 197 hist2=hist.clone() 198 self.failIf(hist2[3,3]!=5,"clone failed") 199 200 def test_initialize(self): 201 """ 202 allocate a histogram 203 """ 204 hist=pygsl.histogram.histogram2d(100,200) 205 self.failIf(hist.nx()!=100) 206 self.failIf(hist.ny()!=200) 207 hist.set_ranges_uniform(0.0,2.0,0.0,2.0) 208 self.failIf(hist.xmin()!=0.0) 209 self.failIf(hist.xmax()!=2.0) 210 self.failIf(hist.ymin()!=0.0) 211 self.failIf(hist.ymax()!=2.0) 212 213 def test_DomainWarning(self): 214 """ 215 """ 216 hist=pygsl.histogram.histogram2d(100,100) 217 hist.set_ranges_uniform(1,20,1,20) 218 # raise domain warning as exception here... 219 exception_seen=0 220 warnings.filterwarnings("error", 221 category=pygsl.errors.gsl_DomainWarning) 222 try: 223 hist.increment(22,0) 224 except pygsl.errors.gsl_Warning: 225 exception_seen=1 226 self.failIf(exception_seen==0) 227 # no warning/exception should occur 228 hist.increment(10,10) 229 230 def test_mapping_protocol(self): 231 """ 232 test things like h[3,4]=0 233 """ 234 hist=pygsl.histogram.histogram2d(100,100) 235 # raise domain warning as exception here... 236 self.failIf(len(hist)!=100*100,"length function does not work") 237 hist[0,0]=1 238 hist[99,99]=1 239 self.failIf(hist[0,0]!=1 or hist[99,99]!=1,"subscription does not work") 240 exception_seen=0 241 try: 242 hist[-1,-1]=0 243 except pygsl.errors.gsl_Error: 244 exception_seen=1 245 self.failIf(exception_seen==0,"no error produced by hist[-1,-1]") 246 exception_seen=0 247 try: 248 hist[100,0]=0 249 except pygsl.errors.gsl_Error: 250 exception_seen=1 251 self.failIf(exception_seen==0,"no error produced by hist[100,0]") 252 253 def test_statistics(self): 254 """ 255 sum, mean, sigma and cov 256 """ 257 hist=pygsl.histogram.histogram2d(100,100) 258 hist.set_ranges_uniform(-1,1,-1,1) 259 xmean=0.0 260 ymean=0.0 261 xsigma=0.3 262 ysigma=0.3 263 cov=0.5 264 # bivariate normal distribution 265 my_dist=lambda x,y:math.exp((((x-xmean)/xsigma)**2\ 266 -2*cov*((x-xmean)/xsigma)*((y-ymean)/ysigma)\ 267 +((y-ymean)/ysigma)**2)\ 268 /(-2.0*(1-cov**2)))\ 269 /(2.0*math.pi*xsigma*ysigma*math.sqrt(1-cov**2)) 270 for i in xrange(0,hist.nx()): 271 (ux,lx)=hist.get_xrange(i) 272 mx=(ux+lx)/2.0 273 for j in xrange(0,hist.ny()): 274 (uy,ly)=hist.get_yrange(j) 275 hist[i,j]=my_dist(mx,(uy+ly)/2.0) 276 self.failIf(abs(hist.sum()/(hist.nx()*hist.ny()/(hist.xmax()-hist.xmin())/(hist.ymax()-hist.ymin()))\ 277 -1)>2.0/hist.nx()) 278 self.failIf(abs(hist.xmean()-xmean)>2.0/hist.nx()) 279 self.failIf(abs(hist.ymean()-ymean)>2.0/hist.ny()) 280 self.failIf(abs(hist.xsigma()-xsigma)>2.0/hist.nx()) 281 self.failIf(abs(hist.ysigma()-ysigma)>2.0/hist.ny()) 282 self.failIf(abs(hist.cov()/(hist.xsigma()*hist.ysigma())-cov)>2.0/hist.nx()) 283 284 def test_set_ranges(self): 285 hist=pygsl.histogram.histogram2d(50,100) 286 hist.set_ranges(xrange(0,51),xrange(2,103)) 287 for i in xrange(0,hist.nx()): 288 (lx,ux)=hist.get_xrange(i) 289 self.failIf(lx!=i or ux!=i+1) 290 for j in xrange(0,hist.ny()): 291 (ly,uy)=hist.get_yrange(j) 292 self.failIf(ly!=j+2 or uy!=j+3) 293 294 def test_addsubdivmul(self): 295 """ 296 ToDo 297 """ 298 hist1=pygsl.histogram.histogram2d(100,10) 299 hist2=pygsl.histogram.histogram2d(100,10) 300 hist1.set_ranges_uniform(1,2,1,2) 301 hist2.set_ranges_uniform(1,2,1,2) 302 hist1.shift(1) 303 hist2.shift(2) 304 hist1.add(hist2) 305 self.failIf(hist1[1,3]!=3) 306 hist2.sub(hist1) 307 self.failIf(hist2[1,3]!=-1) 308 self.failIf(hist1[1,3]!=3) 309 310 def test_copy(self): 311 """ 312 tests the copy command 313 """ 314 hist=pygsl.histogram.histogram2d(100,10) 315 hist.set_ranges_uniform(-1,1,-2,2) 316 hist[1,3]=5 317 hist2=pygsl.histogram.histogram2d(100,10) 318 hist2.copy(hist) 319 self.failIf(hist2.xmin()!=-1 or hist2.xmax()!=1,"copy failed") 320 self.failIf(hist2.ymin()!=-2 or hist2.ymax()!=2,"copy failed") 321 self.failIf(hist2[1,3]!=5,"copy failed") 322 hist2=pygsl.histogram.histogram2d(10,10) 323 exception_seen=0 324 try: 325 hist2.copy(hist) 326 except pygsl.errors.gsl_Error: 327 exception_seen=1 328 self.failIf(exception_seen==0,"no error produced by hist.copy") 329 330 def test_readwrite(self): 331 self.my_filename=tempfile.mktemp() 332 hist=pygsl.histogram.histogram2d(100,100) 333 hist.set_ranges_uniform(1,2,3,4) 334 hist[10,50]=1 335 # write it 336 hist_file=file(self.my_filename,'w') 337 hist.write(hist_file) 338 hist_file.close() 339 # clear and change histogram 340 hist.set_ranges_uniform(0,1,0,1) 341 # reread it from file 342 hist_file=file(self.my_filename,'r') 343 hist.read(hist_file) 344 hist_file.close() 345 self.failIf(hist.xmax()!=2 or hist.ymin()!=3 or hist[10,50]!=1 or hist[99,99]!=0, 346 "rereading histogram failed") 347 348 def test_scanfprintf(self): 349 self.my_filename=tempfile.mktemp() 350 hist=pygsl.histogram.histogram2d(100,100) 351 hist.set_ranges_uniform(1,2,3,4) 352 hist[10,50]=1 353 # write it 354 hist_file=file(self.my_filename,'w') 355 hist.printf(hist_file,"%g","%f") 356 hist_file.close() 357 # clear and change histogram 358 hist.set_ranges_uniform(0,1,0,1) 359 # reread it from file 360 hist_file=file(self.my_filename,'r') 361 hist.scanf(hist_file) 362 hist_file.close() 363 self.failIf(hist.xmax()!=2 or hist.ymin()!=3 or hist[10,50]!=1 or hist[99,99]!=0, 364 "rereading histogram failed") 365 366 def tearDown(self): 367 # clean up files 368 if 'my_filename' in vars(self) and os.path.isfile(self.my_filename): 369 os.remove(self.my_filename) 370 del self.my_filename 371 372 373 374#test=unittest.TestSuite((histogram_tests, histogram2d_tests)) 375 376if __name__ == "__main__": 377 unittest.main() 378 379