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