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