[bb18ef1] | 1 | import time |
---|
| 2 | from calcthread import CalcThread |
---|
| 3 | import sys |
---|
| 4 | |
---|
| 5 | class Calc2D_all(CalcThread): |
---|
| 6 | """ |
---|
| 7 | Compute 2D model |
---|
| 8 | This calculation assumes a 2-fold symmetry of the model |
---|
| 9 | where points are computed for one half of the detector |
---|
| 10 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
| 11 | """ |
---|
| 12 | |
---|
| 13 | def __init__(self, x, y, model, |
---|
| 14 | completefn = None, |
---|
| 15 | updatefn = None, |
---|
| 16 | yieldtime = 0.01, |
---|
| 17 | worktime = 0.01 |
---|
| 18 | ): |
---|
| 19 | CalcThread.__init__(self,completefn, |
---|
| 20 | updatefn, |
---|
| 21 | yieldtime, |
---|
| 22 | worktime) |
---|
| 23 | |
---|
| 24 | self.x = x |
---|
| 25 | self.y = y |
---|
| 26 | self.model = model |
---|
| 27 | self.starttime = 0 |
---|
| 28 | |
---|
| 29 | def isquit(self): |
---|
| 30 | try: |
---|
| 31 | CalcThread.isquit(self) |
---|
| 32 | except KeyboardInterrupt: |
---|
| 33 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 34 | "Calc %s interrupted" % self.model.name)) |
---|
| 35 | raise KeyboardInterrupt |
---|
| 36 | |
---|
| 37 | def compute(self): |
---|
| 38 | import numpy |
---|
| 39 | x = self.x |
---|
| 40 | y = self.y |
---|
| 41 | output = numpy.zeros((len(x),len(y))) |
---|
| 42 | |
---|
| 43 | self.starttime = time.time() |
---|
| 44 | lx = len(self.x) |
---|
| 45 | |
---|
| 46 | #for i_x in range(int(len(self.x)/2)): |
---|
| 47 | for i_x in range(len(self.x)): |
---|
| 48 | if i_x%2==1: |
---|
| 49 | continue |
---|
| 50 | |
---|
| 51 | # Check whether we need to bail out |
---|
| 52 | self.update(output=output) |
---|
| 53 | self.isquit() |
---|
| 54 | |
---|
| 55 | for i_y in range(len(self.y)): |
---|
| 56 | value = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
| 57 | output[i_y][i_x] = value |
---|
| 58 | #output[lx-i_y-1][lx-i_x-1] = value |
---|
| 59 | |
---|
| 60 | if lx%2==1: |
---|
| 61 | i_x = int(len(self.x)/2) |
---|
| 62 | for i_y in range(len(self.y)): |
---|
| 63 | value = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
| 64 | output[i_y][i_x] = value |
---|
| 65 | |
---|
| 66 | #for i_x in range(int(len(self.x)/2)): |
---|
| 67 | for i_x in range(len(self.x)): |
---|
| 68 | if not i_x%2==1: |
---|
| 69 | continue |
---|
| 70 | |
---|
| 71 | # Check whether we need to bail out |
---|
| 72 | self.update(output=output) |
---|
| 73 | self.isquit() |
---|
| 74 | |
---|
| 75 | for i_y in range(len(self.y)): |
---|
| 76 | value = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
| 77 | output[i_y][i_x] = value |
---|
| 78 | #output[lx-i_y-1][lx-i_x-1] = value |
---|
| 79 | |
---|
| 80 | elapsed = time.time()-self.starttime |
---|
| 81 | self.complete(output=output, elapsed=elapsed) |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | class Calc2D(CalcThread): |
---|
| 85 | """ |
---|
| 86 | Compute 2D model |
---|
| 87 | This calculation assumes a 2-fold symmetry of the model |
---|
| 88 | where points are computed for one half of the detector |
---|
| 89 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
| 90 | """ |
---|
| 91 | |
---|
[c77d859] | 92 | def __init__(self, x, y, data,model,qmin, qmax,qstep, |
---|
[bb18ef1] | 93 | completefn = None, |
---|
| 94 | updatefn = None, |
---|
| 95 | yieldtime = 0.01, |
---|
| 96 | worktime = 0.01 |
---|
| 97 | ): |
---|
| 98 | CalcThread.__init__(self,completefn, |
---|
| 99 | updatefn, |
---|
| 100 | yieldtime, |
---|
| 101 | worktime) |
---|
| 102 | self.qmin= qmin |
---|
[c77d859] | 103 | self.qmax= qmax |
---|
[bb18ef1] | 104 | self.qstep= qstep |
---|
| 105 | self.x = x |
---|
| 106 | self.y = y |
---|
[c77d859] | 107 | self.data= data |
---|
[bb18ef1] | 108 | ## the model on to calculate |
---|
| 109 | self.model = model |
---|
| 110 | self.starttime = 0 |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | def isquit(self): |
---|
| 114 | try: |
---|
| 115 | CalcThread.isquit(self) |
---|
| 116 | except KeyboardInterrupt: |
---|
| 117 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 118 | "Calc %s interrupted" % self.model.name)) |
---|
| 119 | |
---|
| 120 | raise KeyboardInterrupt |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | def compute(self): |
---|
| 124 | """ |
---|
| 125 | Compute the data given a model function |
---|
| 126 | """ |
---|
| 127 | import numpy |
---|
| 128 | x = self.x |
---|
| 129 | y = self.y |
---|
| 130 | output = numpy.zeros((len(x),len(y))) |
---|
| 131 | |
---|
[c77d859] | 132 | if self.qmin==None: |
---|
| 133 | self.qmin = 0 |
---|
| 134 | if self.qmax== None: |
---|
| 135 | if data ==None: |
---|
| 136 | return |
---|
| 137 | newx= math.pow(max(math.fabs(data.xmax),math.fabs(data.xmin)),2) |
---|
| 138 | newy= math.pow(max(math.fabs(data.ymax),math.fabs(data.ymin)),2) |
---|
| 139 | self.qmax=math.sqrt( newx + newy ) |
---|
[bb18ef1] | 140 | |
---|
[c77d859] | 141 | self.starttime = time.time() |
---|
[bb18ef1] | 142 | |
---|
| 143 | lx = len(self.x) |
---|
| 144 | for i_x in range(len(self.x)): |
---|
| 145 | # Check whether we need to bail out |
---|
| 146 | self.update(output=output ) |
---|
| 147 | self.isquit() |
---|
| 148 | |
---|
| 149 | for i_y in range(int(len(self.y))): |
---|
[c77d859] | 150 | radius = self.x[i_x]*self.x[i_x]+self.y[i_y]*self.y[i_y] |
---|
| 151 | |
---|
| 152 | if self.qmin <= radius or radius<= self.qmax: |
---|
| 153 | value = self.model.runXY( [self.x[i_x], self.y[i_y]] ) |
---|
[bb18ef1] | 154 | output[i_x] [i_y]=value |
---|
[c77d859] | 155 | else: |
---|
| 156 | output[i_x] [i_y]=0 |
---|
[bb18ef1] | 157 | |
---|
| 158 | elapsed = time.time()-self.starttime |
---|
[c77d859] | 159 | self.complete( image = output, |
---|
| 160 | data = self.data , |
---|
| 161 | model = self.model, |
---|
| 162 | elapsed = elapsed, |
---|
| 163 | qmin = self.qmin, |
---|
| 164 | qmax =self.qmax, |
---|
| 165 | qstep = self.qstep ) |
---|
[bb18ef1] | 166 | |
---|
| 167 | |
---|
| 168 | class Calc2D_4fold(CalcThread): |
---|
| 169 | """ |
---|
| 170 | Compute 2D model |
---|
| 171 | This calculation assumes a 4-fold symmetry of the model. |
---|
| 172 | Really is the same calculation time since we have to |
---|
| 173 | calculate points for 0<phi<pi anyway. |
---|
| 174 | """ |
---|
| 175 | |
---|
| 176 | def __init__(self, x, y, model, |
---|
| 177 | completefn = None, |
---|
| 178 | updatefn = None, |
---|
| 179 | yieldtime = 0.01, |
---|
| 180 | worktime = 0.01 |
---|
| 181 | ): |
---|
| 182 | CalcThread.__init__(self,completefn, |
---|
| 183 | updatefn, |
---|
| 184 | yieldtime, |
---|
| 185 | worktime) |
---|
| 186 | self.x = x |
---|
| 187 | self.y = y |
---|
| 188 | self.model = model |
---|
| 189 | self.starttime = 0 |
---|
| 190 | |
---|
| 191 | def isquit(self): |
---|
| 192 | try: |
---|
| 193 | CalcThread.isquit(self) |
---|
| 194 | except KeyboardInterrupt: |
---|
| 195 | #printEVT("Calc %s interrupted" % self.model.name) |
---|
| 196 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 197 | "Calc %s interrupted" % self.model.name)) |
---|
| 198 | |
---|
| 199 | raise KeyboardInterrupt |
---|
| 200 | |
---|
| 201 | def compute(self): |
---|
| 202 | import numpy |
---|
| 203 | x = self.x |
---|
| 204 | y = self.y |
---|
| 205 | output = numpy.zeros((len(x),len(y))) |
---|
| 206 | |
---|
| 207 | self.starttime = time.time() |
---|
| 208 | lx = len(self.x) |
---|
| 209 | |
---|
| 210 | for i_x in range(int(len(self.x)/2)): |
---|
| 211 | if i_x%2==1: |
---|
| 212 | continue |
---|
| 213 | |
---|
| 214 | # Check whether we need to bail out |
---|
| 215 | self.update(output=output) |
---|
| 216 | self.isquit() |
---|
| 217 | |
---|
| 218 | for i_y in range(int(len(self.y)/2)): |
---|
| 219 | value1 = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
| 220 | value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]]) |
---|
| 221 | output[i_y][i_x] = value1 + value2 |
---|
| 222 | output[lx-i_y-1][lx-i_x-1] = value1 + value2 |
---|
| 223 | output[lx-i_y-1][i_x] = value1 + value2 |
---|
| 224 | output[i_y][lx-i_x-1] = value1 + value2 |
---|
| 225 | |
---|
| 226 | if lx%2==1: |
---|
| 227 | i_x = int(len(self.x)/2) |
---|
| 228 | for i_y in range(int(len(self.y)/2)): |
---|
| 229 | value1 = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
| 230 | value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]]) |
---|
| 231 | output[i_y][i_x] = value1 + value2 |
---|
| 232 | output[lx-i_y-1][lx-i_x-1] = value1 + value2 |
---|
| 233 | output[lx-i_y-1][i_x] = value1 + value2 |
---|
| 234 | output[i_y][lx-i_x-1] = value1 + value2 |
---|
| 235 | |
---|
| 236 | for i_x in range(int(len(self.x)/2)): |
---|
| 237 | if not i_x%2==1: |
---|
| 238 | continue |
---|
| 239 | |
---|
| 240 | # Check whether we need to bail out |
---|
| 241 | self.update(output=output) |
---|
| 242 | self.isquit() |
---|
| 243 | |
---|
| 244 | for i_y in range(int(len(self.y)/2)): |
---|
| 245 | value1 = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
| 246 | value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]]) |
---|
| 247 | output[i_y][i_x] = value1 + value2 |
---|
| 248 | output[lx-i_y-1][lx-i_x-1] = value1 + value2 |
---|
| 249 | output[lx-i_y-1][i_x] = value1 + value2 |
---|
| 250 | output[i_y][lx-i_x-1] = value1 + value2 |
---|
| 251 | |
---|
| 252 | elapsed = time.time()-self.starttime |
---|
| 253 | self.complete(output=output, elapsed=elapsed) |
---|
| 254 | |
---|
| 255 | |
---|
| 256 | |
---|
| 257 | class Calc1D(CalcThread): |
---|
| 258 | """Compute 1D data""" |
---|
| 259 | |
---|
| 260 | def __init__(self, x, model, |
---|
| 261 | data=None, |
---|
| 262 | qmin=None, |
---|
| 263 | qmax=None, |
---|
| 264 | smearer=None, |
---|
| 265 | completefn = None, |
---|
| 266 | updatefn = None, |
---|
| 267 | yieldtime = 0.01, |
---|
| 268 | worktime = 0.01 |
---|
| 269 | ): |
---|
| 270 | CalcThread.__init__(self,completefn, |
---|
| 271 | updatefn, |
---|
| 272 | yieldtime, |
---|
| 273 | worktime) |
---|
| 274 | self.x = x |
---|
| 275 | self.data= data |
---|
| 276 | self.qmin= qmin |
---|
| 277 | self.qmax= qmax |
---|
| 278 | self.model = model |
---|
| 279 | self.smearer= smearer |
---|
| 280 | self.starttime = 0 |
---|
| 281 | |
---|
| 282 | def compute(self): |
---|
[c77d859] | 283 | """ |
---|
| 284 | Compute model 1d value given qmin , qmax , x value |
---|
| 285 | """ |
---|
[bb18ef1] | 286 | import numpy |
---|
| 287 | |
---|
| 288 | output = numpy.zeros(len(self.x)) |
---|
[c77d859] | 289 | |
---|
[bb18ef1] | 290 | self.starttime = time.time() |
---|
| 291 | |
---|
| 292 | for i_x in range(len(self.x)): |
---|
| 293 | self.update(x= self.x, output=output ) |
---|
| 294 | # Check whether we need to bail out |
---|
| 295 | self.isquit() |
---|
| 296 | if self.qmin <= self.x[i_x] and self.x[i_x] <= self.qmax: |
---|
| 297 | value = self.model.run(self.x[i_x]) |
---|
| 298 | output[i_x] = value |
---|
[c77d859] | 299 | |
---|
[bb18ef1] | 300 | if self.smearer!=None: |
---|
| 301 | output = self.smearer(output) |
---|
[c77d859] | 302 | |
---|
| 303 | |
---|
[bb18ef1] | 304 | elapsed = time.time()-self.starttime |
---|
[c77d859] | 305 | self.complete(x= self.x, y= output, |
---|
| 306 | elapsed=elapsed, model= self.model, data=self.data) |
---|
[bb18ef1] | 307 | |
---|
| 308 | |
---|
| 309 | |
---|
| 310 | class CalcCommandline: |
---|
| 311 | def __init__(self, n=20000): |
---|
| 312 | #print thread.get_ident() |
---|
| 313 | from sans.models.CylinderModel import CylinderModel |
---|
| 314 | from sans.models.DisperseModel import DisperseModel |
---|
| 315 | import pylab |
---|
| 316 | |
---|
| 317 | submodel = CylinderModel() |
---|
| 318 | |
---|
| 319 | model = DisperseModel(submodel, ['cyl_phi', 'cyl_theta', 'length'], |
---|
| 320 | [0.2, 0.2, 10.0]) |
---|
| 321 | model.setParam('n_pts', 10) |
---|
| 322 | |
---|
| 323 | print model.runXY([0.01, 0.02]) |
---|
| 324 | |
---|
| 325 | qmax = 0.01 |
---|
| 326 | qstep = 0.0001 |
---|
| 327 | self.done = False |
---|
| 328 | |
---|
| 329 | x = pylab.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
| 330 | y = pylab.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
| 331 | |
---|
| 332 | calc_thread_2D = Calc2D(x, y, model.clone(),-qmax, qmax,qstep, |
---|
| 333 | completefn=self.complete, |
---|
| 334 | updatefn=self.update , |
---|
| 335 | yieldtime=0.0) |
---|
| 336 | |
---|
| 337 | calc_thread_2D.queue() |
---|
| 338 | calc_thread_2D.ready(2.5) |
---|
| 339 | |
---|
| 340 | while not self.done: |
---|
| 341 | time.sleep(1) |
---|
| 342 | |
---|
| 343 | def update(self,output): |
---|
| 344 | print "update" |
---|
| 345 | |
---|
| 346 | def complete(self, output, model, elapsed, qmin, qmax, qstep ): |
---|
| 347 | print "complete" |
---|
| 348 | self.done = True |
---|
| 349 | |
---|
| 350 | if __name__ == "__main__": |
---|
| 351 | CalcCommandline() |
---|
| 352 | |
---|