[bb18ef1] | 1 | import time |
---|
| 2 | from calcthread import CalcThread |
---|
| 3 | import sys |
---|
[d16e396] | 4 | import numpy,math |
---|
[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))) |
---|
[c77d859] | 111 | if self.qmin==None: |
---|
| 112 | self.qmin = 0 |
---|
| 113 | if self.qmax== None: |
---|
[cd3d15b] | 114 | if self.data !=None: |
---|
| 115 | newx= math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2) |
---|
| 116 | newy= math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2) |
---|
| 117 | self.qmax=math.sqrt( newx + newy ) |
---|
| 118 | else: |
---|
| 119 | self.qmax= max(self.x) |
---|
[bb18ef1] | 120 | |
---|
[c77d859] | 121 | self.starttime = time.time() |
---|
[bb18ef1] | 122 | lx = len(self.x) |
---|
| 123 | for i_x in range(len(self.x)): |
---|
| 124 | # Check whether we need to bail out |
---|
| 125 | self.update(output=output ) |
---|
| 126 | self.isquit() |
---|
| 127 | |
---|
| 128 | for i_y in range(int(len(self.y))): |
---|
[d16e396] | 129 | radius = math.sqrt(self.x[i_x]*self.x[i_x]+self.y[i_y]*self.y[i_y]) |
---|
[5b0c2cb] | 130 | ## for data ignore the qmax |
---|
| 131 | if self.data == None: |
---|
| 132 | if self.qmin <= radius : |
---|
| 133 | value = self.model.runXY( [self.x[i_x], self.y[i_y]] ) |
---|
| 134 | output[i_y][i_x] =value |
---|
| 135 | else: |
---|
| 136 | output[i_y][i_x] =0 |
---|
| 137 | else: |
---|
| 138 | if self.qmin <= radius and radius<= self.qmax: |
---|
| 139 | value = self.model.runXY( [self.x[i_x], self.y[i_y]] ) |
---|
| 140 | output[i_y][i_x] =value |
---|
| 141 | else: |
---|
| 142 | output[i_y][i_x] =0 |
---|
[bb18ef1] | 143 | |
---|
| 144 | elapsed = time.time()-self.starttime |
---|
[c77d859] | 145 | self.complete( image = output, |
---|
| 146 | data = self.data , |
---|
| 147 | model = self.model, |
---|
| 148 | elapsed = elapsed, |
---|
| 149 | qmin = self.qmin, |
---|
| 150 | qmax =self.qmax, |
---|
| 151 | qstep = self.qstep ) |
---|
[bb18ef1] | 152 | |
---|
| 153 | |
---|
| 154 | class Calc2D_4fold(CalcThread): |
---|
| 155 | """ |
---|
| 156 | Compute 2D model |
---|
| 157 | This calculation assumes a 4-fold symmetry of the model. |
---|
| 158 | Really is the same calculation time since we have to |
---|
| 159 | calculate points for 0<phi<pi anyway. |
---|
| 160 | """ |
---|
| 161 | |
---|
| 162 | def __init__(self, x, y, model, |
---|
| 163 | completefn = None, |
---|
| 164 | updatefn = None, |
---|
| 165 | yieldtime = 0.01, |
---|
| 166 | worktime = 0.01 |
---|
| 167 | ): |
---|
| 168 | CalcThread.__init__(self,completefn, |
---|
| 169 | updatefn, |
---|
| 170 | yieldtime, |
---|
| 171 | worktime) |
---|
| 172 | self.x = x |
---|
| 173 | self.y = y |
---|
| 174 | self.model = model |
---|
| 175 | self.starttime = 0 |
---|
| 176 | |
---|
| 177 | def compute(self): |
---|
| 178 | x = self.x |
---|
| 179 | y = self.y |
---|
| 180 | output = numpy.zeros((len(x),len(y))) |
---|
| 181 | |
---|
| 182 | self.starttime = time.time() |
---|
| 183 | lx = len(self.x) |
---|
| 184 | |
---|
| 185 | for i_x in range(int(len(self.x)/2)): |
---|
| 186 | if i_x%2==1: |
---|
| 187 | continue |
---|
| 188 | |
---|
| 189 | # Check whether we need to bail out |
---|
| 190 | self.update(output=output) |
---|
| 191 | self.isquit() |
---|
| 192 | |
---|
| 193 | for i_y in range(int(len(self.y)/2)): |
---|
| 194 | value1 = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
| 195 | value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]]) |
---|
| 196 | output[i_y][i_x] = value1 + value2 |
---|
| 197 | output[lx-i_y-1][lx-i_x-1] = value1 + value2 |
---|
| 198 | output[lx-i_y-1][i_x] = value1 + value2 |
---|
| 199 | output[i_y][lx-i_x-1] = value1 + value2 |
---|
| 200 | |
---|
| 201 | if lx%2==1: |
---|
| 202 | i_x = int(len(self.x)/2) |
---|
| 203 | for i_y in range(int(len(self.y)/2)): |
---|
| 204 | value1 = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
| 205 | value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]]) |
---|
| 206 | output[i_y][i_x] = value1 + value2 |
---|
| 207 | output[lx-i_y-1][lx-i_x-1] = value1 + value2 |
---|
| 208 | output[lx-i_y-1][i_x] = value1 + value2 |
---|
| 209 | output[i_y][lx-i_x-1] = value1 + value2 |
---|
| 210 | |
---|
| 211 | for i_x in range(int(len(self.x)/2)): |
---|
| 212 | if not i_x%2==1: |
---|
| 213 | continue |
---|
| 214 | |
---|
| 215 | # Check whether we need to bail out |
---|
| 216 | self.update(output=output) |
---|
| 217 | self.isquit() |
---|
| 218 | |
---|
| 219 | for i_y in range(int(len(self.y)/2)): |
---|
| 220 | value1 = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
| 221 | value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]]) |
---|
| 222 | output[i_y][i_x] = value1 + value2 |
---|
| 223 | output[lx-i_y-1][lx-i_x-1] = value1 + value2 |
---|
| 224 | output[lx-i_y-1][i_x] = value1 + value2 |
---|
| 225 | output[i_y][lx-i_x-1] = value1 + value2 |
---|
| 226 | |
---|
| 227 | elapsed = time.time()-self.starttime |
---|
| 228 | self.complete(output=output, elapsed=elapsed) |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | class Calc1D(CalcThread): |
---|
| 233 | """Compute 1D data""" |
---|
| 234 | |
---|
| 235 | def __init__(self, x, model, |
---|
| 236 | data=None, |
---|
| 237 | qmin=None, |
---|
| 238 | qmax=None, |
---|
| 239 | smearer=None, |
---|
| 240 | completefn = None, |
---|
| 241 | updatefn = None, |
---|
| 242 | yieldtime = 0.01, |
---|
| 243 | worktime = 0.01 |
---|
| 244 | ): |
---|
| 245 | CalcThread.__init__(self,completefn, |
---|
| 246 | updatefn, |
---|
| 247 | yieldtime, |
---|
| 248 | worktime) |
---|
| 249 | self.x = x |
---|
| 250 | self.data= data |
---|
| 251 | self.qmin= qmin |
---|
| 252 | self.qmax= qmax |
---|
| 253 | self.model = model |
---|
| 254 | self.smearer= smearer |
---|
| 255 | self.starttime = 0 |
---|
| 256 | |
---|
| 257 | def compute(self): |
---|
[c77d859] | 258 | """ |
---|
| 259 | Compute model 1d value given qmin , qmax , x value |
---|
| 260 | """ |
---|
[bb18ef1] | 261 | output = numpy.zeros(len(self.x)) |
---|
[c77d859] | 262 | |
---|
[bb18ef1] | 263 | self.starttime = time.time() |
---|
| 264 | |
---|
| 265 | for i_x in range(len(self.x)): |
---|
| 266 | self.update(x= self.x, output=output ) |
---|
| 267 | # Check whether we need to bail out |
---|
| 268 | self.isquit() |
---|
| 269 | if self.qmin <= self.x[i_x] and self.x[i_x] <= self.qmax: |
---|
| 270 | value = self.model.run(self.x[i_x]) |
---|
| 271 | output[i_x] = value |
---|
[c77d859] | 272 | |
---|
[bb18ef1] | 273 | if self.smearer!=None: |
---|
| 274 | output = self.smearer(output) |
---|
[c77d859] | 275 | |
---|
| 276 | |
---|
[bb18ef1] | 277 | elapsed = time.time()-self.starttime |
---|
[c77d859] | 278 | self.complete(x= self.x, y= output, |
---|
| 279 | elapsed=elapsed, model= self.model, data=self.data) |
---|
[bb18ef1] | 280 | |
---|
| 281 | |
---|
| 282 | |
---|
| 283 | class CalcCommandline: |
---|
| 284 | def __init__(self, n=20000): |
---|
| 285 | #print thread.get_ident() |
---|
| 286 | from sans.models.CylinderModel import CylinderModel |
---|
| 287 | |
---|
[904713c] | 288 | model = CylinderModel() |
---|
[bb18ef1] | 289 | |
---|
| 290 | |
---|
| 291 | print model.runXY([0.01, 0.02]) |
---|
| 292 | |
---|
| 293 | qmax = 0.01 |
---|
| 294 | qstep = 0.0001 |
---|
| 295 | self.done = False |
---|
| 296 | |
---|
[904713c] | 297 | x = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
| 298 | y = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
| 299 | |
---|
[bb18ef1] | 300 | |
---|
[904713c] | 301 | calc_thread_2D = Calc2D(x, y, None, model.clone(),-qmax, qmax,qstep, |
---|
[bb18ef1] | 302 | completefn=self.complete, |
---|
| 303 | updatefn=self.update , |
---|
| 304 | yieldtime=0.0) |
---|
| 305 | |
---|
| 306 | calc_thread_2D.queue() |
---|
| 307 | calc_thread_2D.ready(2.5) |
---|
| 308 | |
---|
| 309 | while not self.done: |
---|
| 310 | time.sleep(1) |
---|
| 311 | |
---|
| 312 | def update(self,output): |
---|
| 313 | print "update" |
---|
| 314 | |
---|
[904713c] | 315 | def complete(self, image, data, model, elapsed, qmin, qmax, qstep ): |
---|
[bb18ef1] | 316 | print "complete" |
---|
| 317 | self.done = True |
---|
| 318 | |
---|
| 319 | if __name__ == "__main__": |
---|
| 320 | CalcCommandline() |
---|
| 321 | |
---|