[41340860] | 1 | |
---|
| 2 | |
---|
| 3 | import time |
---|
[2b63df0] | 4 | import numpy |
---|
| 5 | import copy |
---|
| 6 | import math |
---|
[41340860] | 7 | import sys |
---|
| 8 | import wx |
---|
| 9 | |
---|
| 10 | from calcthread import CalcThread |
---|
| 11 | |
---|
[2b63df0] | 12 | class CalcChisqr1D(CalcThread): |
---|
[41340860] | 13 | """ |
---|
[2b63df0] | 14 | Compute chisqr |
---|
[41340860] | 15 | """ |
---|
[2b63df0] | 16 | def __init__(self, x, y,dy, model, |
---|
| 17 | smearer=None, |
---|
| 18 | qmin=None, |
---|
| 19 | qmax=None, |
---|
[41340860] | 20 | completefn = None, |
---|
| 21 | updatefn = None, |
---|
| 22 | yieldtime = 0.01, |
---|
| 23 | worktime = 0.01 |
---|
| 24 | ): |
---|
| 25 | CalcThread.__init__(self,completefn, |
---|
| 26 | updatefn, |
---|
| 27 | yieldtime, |
---|
| 28 | worktime) |
---|
[2b63df0] | 29 | self.smearer =smearer |
---|
| 30 | self.y = numpy.array(y) |
---|
| 31 | self.x = numpy.array(x) |
---|
| 32 | self.dy= copy.deepcopy(dy) |
---|
| 33 | self.model = model |
---|
| 34 | self.qmin = qmin |
---|
| 35 | self.qmax = qmax |
---|
[41340860] | 36 | self.smearer = smearer |
---|
| 37 | self.starttime = 0 |
---|
| 38 | |
---|
[2b63df0] | 39 | def isquit(self): |
---|
| 40 | """ |
---|
| 41 | @raise KeyboardInterrupt: when the thread is interrupted |
---|
| 42 | """ |
---|
| 43 | try: |
---|
| 44 | CalcThread.isquit(self) |
---|
| 45 | except KeyboardInterrupt: |
---|
| 46 | raise KeyboardInterrupt |
---|
| 47 | |
---|
| 48 | |
---|
[41340860] | 49 | def compute(self): |
---|
| 50 | """ |
---|
| 51 | Compute the data given a model function |
---|
| 52 | """ |
---|
[2b63df0] | 53 | self.starttime = time.time() |
---|
[52611f5] | 54 | |
---|
[2b63df0] | 55 | x,y = [numpy.asarray(v) for v in (self.x,self.y)] |
---|
| 56 | if self.dy==None or self.dy==[]: |
---|
| 57 | self.dy= numpy.zeros(len(self.y)) |
---|
| 58 | self.dy[self.dy==0]=1 |
---|
[52611f5] | 59 | |
---|
[2b63df0] | 60 | if self.qmin==None: |
---|
[52611f5] | 61 | if min (self.x) ==0.0 and self.x[0]==0 and not numpy.isfinite(self.y[0]): |
---|
| 62 | self.qmin = min(self.x[sel.x!=0]) |
---|
| 63 | else: |
---|
| 64 | self.qmin= min(self.x) |
---|
[2b63df0] | 65 | |
---|
| 66 | if self.qmax==None: |
---|
| 67 | self.qmax= max(self.x) |
---|
| 68 | |
---|
| 69 | fx = numpy.zeros(len(self.x)) |
---|
| 70 | |
---|
| 71 | output= None |
---|
| 72 | res=[] |
---|
| 73 | try: |
---|
| 74 | |
---|
| 75 | for i_x in range(len(self.x)): |
---|
| 76 | |
---|
| 77 | # Check whether we need to bail out |
---|
[52611f5] | 78 | self.isquit() |
---|
| 79 | |
---|
[15ff344] | 80 | fx[i_x]=self.model.run(self.x[i_x]) |
---|
[2b63df0] | 81 | |
---|
| 82 | if self.smearer!=None: |
---|
| 83 | fx= self.smearer(fx) |
---|
[52611f5] | 84 | |
---|
[2b63df0] | 85 | for i_y in range(len(fx)): |
---|
| 86 | # Check whether we need to bail out |
---|
| 87 | self.isquit() |
---|
[52611f5] | 88 | |
---|
[2b63df0] | 89 | temp=(self.y[i_y] - fx[i_y])/self.dy[i_y] |
---|
| 90 | res.append(temp*temp) |
---|
| 91 | #sum of residuals |
---|
| 92 | sum=0 |
---|
| 93 | for item in res: |
---|
| 94 | # Check whether we need to bail out |
---|
| 95 | self.isquit() |
---|
| 96 | if numpy.isfinite(item): |
---|
| 97 | sum +=item |
---|
| 98 | output = sum/ len(res) |
---|
| 99 | elapsed = time.time()-self.starttime |
---|
| 100 | self.complete(output= output, elapsed=elapsed) |
---|
| 101 | |
---|
| 102 | except KeyboardInterrupt: |
---|
| 103 | # Thread was interrupted, just proceed and re-raise. |
---|
| 104 | # Real code should not print, but this is an example... |
---|
| 105 | raise |
---|
| 106 | except: |
---|
| 107 | raise |
---|
| 108 | |
---|
| 109 | class CalcChisqr2D(CalcThread): |
---|
| 110 | """ |
---|
| 111 | Compute chisqr |
---|
| 112 | """ |
---|
| 113 | |
---|
| 114 | def __init__(self, x_bins, y_bins,data,err_data, model, |
---|
| 115 | qmin, |
---|
| 116 | qmax, |
---|
| 117 | completefn = None, |
---|
| 118 | updatefn = None, |
---|
| 119 | yieldtime = 0.01, |
---|
| 120 | worktime = 0.01 |
---|
| 121 | ): |
---|
| 122 | CalcThread.__init__(self,completefn, |
---|
| 123 | updatefn, |
---|
| 124 | yieldtime, |
---|
| 125 | worktime) |
---|
| 126 | |
---|
| 127 | self.y_bins = y_bins |
---|
| 128 | self.x_bins = x_bins |
---|
| 129 | self.data= data |
---|
| 130 | self.err_data= copy.deepcopy(err_data) |
---|
| 131 | self.model = model |
---|
| 132 | self.qmin = qmin |
---|
| 133 | self.qmax = qmax |
---|
| 134 | |
---|
| 135 | self.starttime = 0 |
---|
| 136 | |
---|
| 137 | def isquit(self): |
---|
| 138 | """ |
---|
| 139 | @raise KeyboardInterrupt: when the thread is interrupted |
---|
| 140 | """ |
---|
| 141 | try: |
---|
| 142 | CalcThread.isquit(self) |
---|
| 143 | except KeyboardInterrupt: |
---|
| 144 | raise KeyboardInterrupt |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | def compute(self): |
---|
| 148 | """ |
---|
| 149 | Compute the data given a model function |
---|
| 150 | """ |
---|
| 151 | self.starttime = time.time() |
---|
| 152 | if self.err_data==None or self.err_data==[]: |
---|
| 153 | self.err_data= numpy.zeros(len(self.x_bins),len(self.y_bins)) |
---|
| 154 | |
---|
| 155 | self.err_data[self.err_data==0]=1 |
---|
| 156 | |
---|
| 157 | output= None |
---|
| 158 | res=[] |
---|
| 159 | try: |
---|
| 160 | |
---|
| 161 | for i in range(len(self.x_bins)): |
---|
| 162 | # Check whether we need to bail out |
---|
| 163 | self.isquit() |
---|
| 164 | for j in range(len(self.y_bins)): |
---|
| 165 | #Check the range containing data between self.qmin_x and self.qmax_x |
---|
| 166 | value = math.pow(self.x_bins[i],2)+ math.pow(self.y_bins[j],2) |
---|
| 167 | if value >= math.pow(self.qmin,2) and value <= math.pow(self.qmax,2): |
---|
| 168 | |
---|
| 169 | temp = [self.x_bins[i],self.y_bins[j]] |
---|
| 170 | error= self.err_data[j][i] |
---|
| 171 | chisqrji = (self.data[j][i]- self.model.runXY(temp ))/error |
---|
| 172 | #Vector containing residuals |
---|
| 173 | res.append( math.pow(chisqrji,2) ) |
---|
| 174 | |
---|
| 175 | sum=0 |
---|
| 176 | for item in res: |
---|
| 177 | # Check whether we need to bail out |
---|
| 178 | self.isquit() |
---|
| 179 | if numpy.isfinite(item): |
---|
| 180 | sum +=item |
---|
| 181 | output = sum/ len(res) |
---|
| 182 | elapsed = time.time()-self.starttime |
---|
| 183 | self.complete(output= output, elapsed=elapsed) |
---|
| 184 | |
---|
| 185 | except KeyboardInterrupt: |
---|
| 186 | # Thread was interrupted, just proceed and re-raise. |
---|
| 187 | # Real code should not print, but this is an example... |
---|
| 188 | raise |
---|