Ticket #7383: FitLowStatsChallenge.py

File FitLowStatsChallenge.py, 1.7 KB (added by Anders Markvardsen, 7 years ago)
Line 
1import math
2# arrays to fill
3resx=numpy.zeros(200)
4resy=numpy.zeros(200)
5rese=numpy.zeros(200)
6resChi=numpy.zeros(200)
7res0=numpy.zeros(200)
8resNZ=numpy.zeros(200)
9NB=10000 # number of bins
10x12arr=numpy.zeros(NB)
11y12arr=numpy.zeros(NB)
12e12arr=numpy.zeros(NB)
13ws=CreateWorkspace(x12arr,y12arr,e12arr,1,OutputWorkspace="Hello")
14for x in range(200):
15        lam=math.exp((x-75.0)/10.0) # expected rate (log scale to show detail)
16        Xarr=range(NB) # "time" bins
17        Yarr=numpy.random.poisson(lam,NB) # actual counts
18        Earr=numpy.sqrt(Yarr) # and the errors
19        ws.dataX(0)[:]=Xarr
20        ws.dataY(0)[:]=Yarr
21        ws.dataE(0)[:]=Earr
22        (stat,chisq,Covar,params,curves)=Fit(Function="name=FlatBackground,A0=1.0",InputWorkspace="Hello",Output="Hello")
23        print lam," -> ",params.column(1)[0]," +- ",params.column(2)[0]," chisq=",chisq," st=",stat
24        resx[x]=lam
25        resy[x]=params.column(1)[0]
26        rese[x]=params.column(2)[0]
27        resNZ[x]=(len(Yarr)-numpy.count_nonzero(Yarr))/(len(Yarr)+0.0)
28        resChi[x]=chisq
29
30DeleteWorkspace("Hello")
31CreateWorkspace(resx,resy,rese,1,OutputWorkspace="Summary") # what the fit thought the count rate was
32CreateWorkspace(resx,resx,res0,1,OutputWorkspace="Ideal") # the intended count rate to plot alongside
33CreateWorkspace(resx,resNZ,res0,1,OutputWorkspace="FractionOfZeros")
34CreateWorkspace(resx,resChi,res0,1,OutputWorkspace="ChiSquared")
35
36Minus(LHSWorkspace='Summary',RHSWorkspace='Ideal',OutputWorkspace='FitDifference')
37Divide(LHSWorkspace='Summary',RHSWorkspace='Ideal',OutputWorkspace='FitRatio')
38
39p=plotSpectrum("FitDifference",0,True)
40l=p.activeLayer()
41l.setAxisScale(Layer.Bottom,0.001,1000000.0,Layer.Log10)
42
43p2=plotSpectrum("FitRatio",0,True)
44l2=p2.activeLayer()
45l2.setAxisScale(Layer.Bottom,0.001,1000000.0,Layer.Log10)