[f32d144] | 1 | """ |
---|
| 2 | Calculation thread for modeling |
---|
| 3 | """ |
---|
[5062bbf] | 4 | |
---|
[bb18ef1] | 5 | import time |
---|
[7e7e806] | 6 | import numpy |
---|
| 7 | import math |
---|
[b699768] | 8 | from sas.sascalc.data_util.calcthread import CalcThread |
---|
[5062bbf] | 9 | |
---|
[bb18ef1] | 10 | class Calc2D(CalcThread): |
---|
| 11 | """ |
---|
[5062bbf] | 12 | Compute 2D model |
---|
| 13 | This calculation assumes a 2-fold symmetry of the model |
---|
| 14 | where points are computed for one half of the detector |
---|
| 15 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
[bb18ef1] | 16 | """ |
---|
[f32d144] | 17 | def __init__(self, data, model, smearer, qmin, qmax, page_id, |
---|
[5ef55d2] | 18 | state=None, |
---|
[62f851f] | 19 | weight=None, |
---|
[f64a4b7] | 20 | fid=None, |
---|
[fa65e99] | 21 | toggle_mode_on=False, |
---|
[7e7e806] | 22 | completefn=None, |
---|
| 23 | updatefn=None, |
---|
[2296316] | 24 | update_chisqr=True, |
---|
[e3f6ef5] | 25 | source='model', |
---|
[7e7e806] | 26 | yieldtime=0.04, |
---|
[934ce649] | 27 | worktime=0.04, |
---|
| 28 | exception_handler=None, |
---|
[bb18ef1] | 29 | ): |
---|
[934ce649] | 30 | CalcThread.__init__(self, completefn, updatefn, yieldtime, worktime, |
---|
| 31 | exception_handler=exception_handler) |
---|
[7e7e806] | 32 | self.qmin = qmin |
---|
| 33 | self.qmax = qmax |
---|
[62f851f] | 34 | self.weight = weight |
---|
[f64a4b7] | 35 | self.fid = fid |
---|
[7e7e806] | 36 | #self.qstep = qstep |
---|
[fa65e99] | 37 | self.toggle_mode_on = toggle_mode_on |
---|
[7e7e806] | 38 | self.data = data |
---|
[66ff250] | 39 | self.page_id = page_id |
---|
[5ef55d2] | 40 | self.state = None |
---|
[1b001a7] | 41 | # the model on to calculate |
---|
[bb18ef1] | 42 | self.model = model |
---|
[7e7e806] | 43 | self.smearer = smearer |
---|
[f32d144] | 44 | self.starttime = 0 |
---|
[2296316] | 45 | self.update_chisqr = update_chisqr |
---|
[e3f6ef5] | 46 | self.source = source |
---|
[2f4b430] | 47 | |
---|
[bb18ef1] | 48 | def compute(self): |
---|
| 49 | """ |
---|
[5062bbf] | 50 | Compute the data given a model function |
---|
[bb18ef1] | 51 | """ |
---|
[1b001a7] | 52 | self.starttime = time.time() |
---|
| 53 | # Determine appropriate q range |
---|
[7e7e806] | 54 | if self.qmin == None: |
---|
[c77d859] | 55 | self.qmin = 0 |
---|
[7e7e806] | 56 | if self.qmax == None: |
---|
| 57 | if self.data != None: |
---|
| 58 | newx = math.pow(max(math.fabs(self.data.xmax), |
---|
| 59 | math.fabs(self.data.xmin)), 2) |
---|
| 60 | newy = math.pow(max(math.fabs(self.data.ymax), |
---|
| 61 | math.fabs(self.data.ymin)), 2) |
---|
| 62 | self.qmax = math.sqrt(newx + newy) |
---|
[2f4b430] | 63 | |
---|
[7e7e806] | 64 | if self.data is None: |
---|
[f32d144] | 65 | msg = "Compute Calc2D receive data = %s.\n" % str(self.data) |
---|
[7e7e806] | 66 | raise ValueError, msg |
---|
[2f4b430] | 67 | |
---|
[f32d144] | 68 | # Define matrix where data will be plotted |
---|
| 69 | radius = numpy.sqrt((self.data.qx_data * self.data.qx_data) + \ |
---|
[7e7e806] | 70 | (self.data.qy_data * self.data.qy_data)) |
---|
[43e685d] | 71 | |
---|
[e575db9] | 72 | # For theory, qmax is based on 1d qmax |
---|
| 73 | # so that must be mulitified by sqrt(2) to get actual max for 2d |
---|
[7e7e806] | 74 | index_model = (self.qmin <= radius) & (radius <= self.qmax) |
---|
| 75 | index_model = index_model & self.data.mask |
---|
| 76 | index_model = index_model & numpy.isfinite(self.data.data) |
---|
[2f4b430] | 77 | |
---|
[7e7e806] | 78 | if self.smearer is not None: |
---|
[f72333f] | 79 | # Set smearer w/ data, model and index. |
---|
| 80 | fn = self.smearer |
---|
| 81 | fn.set_model(self.model) |
---|
| 82 | fn.set_index(index_model) |
---|
| 83 | # Get necessary data from self.data and set the data for smearing |
---|
| 84 | fn.get_data() |
---|
[f32d144] | 85 | # Calculate smeared Intensity |
---|
[7e7e806] | 86 | #(by Gaussian averaging): DataLoader/smearing2d/Smearer2D() |
---|
[f72333f] | 87 | value = fn.get_value() |
---|
[f32d144] | 88 | else: |
---|
[f72333f] | 89 | # calculation w/o smearing |
---|
[f32d144] | 90 | value = self.model.evalDistribution(\ |
---|
| 91 | [self.data.qx_data[index_model], |
---|
| 92 | self.data.qy_data[index_model]]) |
---|
[7e7e806] | 93 | output = numpy.zeros(len(self.data.qx_data)) |
---|
[43e685d] | 94 | # output default is None |
---|
[f32d144] | 95 | # This method is to distinguish between masked |
---|
[7e7e806] | 96 | #point(nan) and data point = 0. |
---|
[f32d144] | 97 | output = output / output |
---|
[43e685d] | 98 | # set value for self.mask==True, else still None to Plottools |
---|
[f32d144] | 99 | output[index_model] = value |
---|
| 100 | elapsed = time.time() - self.starttime |
---|
[cbcdd2c] | 101 | #self.complete(image=output, |
---|
| 102 | # data=self.data, |
---|
| 103 | # page_id=self.page_id, |
---|
| 104 | # model=self.model, |
---|
| 105 | # state=self.state, |
---|
| 106 | # toggle_mode_on=self.toggle_mode_on, |
---|
| 107 | # elapsed=elapsed, |
---|
| 108 | # index=index_model, |
---|
| 109 | # fid=self.fid, |
---|
| 110 | # qmin=self.qmin, |
---|
| 111 | # qmax=self.qmax, |
---|
| 112 | # weight=self.weight, |
---|
| 113 | # #qstep=self.qstep, |
---|
| 114 | # update_chisqr=self.update_chisqr, |
---|
| 115 | # source=self.source) |
---|
| 116 | return (output, |
---|
| 117 | self.data, |
---|
| 118 | self.page_id, |
---|
| 119 | self.model, |
---|
| 120 | self.state, |
---|
| 121 | self.toggle_mode_on, |
---|
| 122 | elapsed, |
---|
| 123 | index_model, |
---|
| 124 | self.fid, |
---|
| 125 | self.qmin, |
---|
| 126 | self.qmax, |
---|
| 127 | self.weight, |
---|
| 128 | self.update_chisqr, |
---|
| 129 | self.source) |
---|
[2f4b430] | 130 | |
---|
[bb18ef1] | 131 | |
---|
| 132 | class Calc1D(CalcThread): |
---|
[5062bbf] | 133 | """ |
---|
| 134 | Compute 1D data |
---|
| 135 | """ |
---|
[7e7e806] | 136 | def __init__(self, model, |
---|
[66ff250] | 137 | page_id, |
---|
[7e7e806] | 138 | data, |
---|
[f64a4b7] | 139 | fid=None, |
---|
[bb18ef1] | 140 | qmin=None, |
---|
| 141 | qmax=None, |
---|
[62f851f] | 142 | weight=None, |
---|
[bb18ef1] | 143 | smearer=None, |
---|
[fa65e99] | 144 | toggle_mode_on=False, |
---|
[5ef55d2] | 145 | state=None, |
---|
[f32d144] | 146 | completefn=None, |
---|
[2296316] | 147 | update_chisqr=True, |
---|
[e3f6ef5] | 148 | source='model', |
---|
[7e7e806] | 149 | updatefn=None, |
---|
| 150 | yieldtime=0.01, |
---|
[934ce649] | 151 | worktime=0.01, |
---|
| 152 | exception_handler=None, |
---|
[bb18ef1] | 153 | ): |
---|
[5062bbf] | 154 | """ |
---|
| 155 | """ |
---|
[934ce649] | 156 | CalcThread.__init__(self, completefn, updatefn, yieldtime, worktime, |
---|
| 157 | exception_handler=exception_handler) |
---|
[f64a4b7] | 158 | self.fid = fid |
---|
[fa65e99] | 159 | self.data = data |
---|
| 160 | self.qmin = qmin |
---|
| 161 | self.qmax = qmax |
---|
[bb18ef1] | 162 | self.model = model |
---|
[62f851f] | 163 | self.weight = weight |
---|
[fa65e99] | 164 | self.toggle_mode_on = toggle_mode_on |
---|
[5ef55d2] | 165 | self.state = state |
---|
[66ff250] | 166 | self.page_id = page_id |
---|
[fa65e99] | 167 | self.smearer = smearer |
---|
[bb18ef1] | 168 | self.starttime = 0 |
---|
[2296316] | 169 | self.update_chisqr = update_chisqr |
---|
[e3f6ef5] | 170 | self.source = source |
---|
[da7cacb] | 171 | self.out = None |
---|
| 172 | self.index = None |
---|
[2f4b430] | 173 | |
---|
[bb18ef1] | 174 | def compute(self): |
---|
[c77d859] | 175 | """ |
---|
[f32d144] | 176 | Compute model 1d value given qmin , qmax , x value |
---|
[c77d859] | 177 | """ |
---|
[bb18ef1] | 178 | self.starttime = time.time() |
---|
[7e7e806] | 179 | output = numpy.zeros((len(self.data.x))) |
---|
[f32d144] | 180 | index = (self.qmin <= self.data.x) & (self.data.x <= self.qmax) |
---|
[2f4b430] | 181 | |
---|
[f32d144] | 182 | ##smearer the ouput of the plot |
---|
[7e7e806] | 183 | if self.smearer is not None: |
---|
[f32d144] | 184 | first_bin, last_bin = self.smearer.get_bin_range(self.qmin, |
---|
[7e7e806] | 185 | self.qmax) |
---|
[a3f125f0] | 186 | mask = self.data.x[first_bin:last_bin+1] |
---|
| 187 | output[first_bin:last_bin+1] = self.model.evalDistribution(mask) |
---|
[f32d144] | 188 | output = self.smearer(output, first_bin, last_bin) |
---|
[e627f19] | 189 | else: |
---|
[7e7e806] | 190 | output[index] = self.model.evalDistribution(self.data.x[index]) |
---|
[2f4b430] | 191 | |
---|
[5062bbf] | 192 | elapsed = time.time() - self.starttime |
---|
[2f4b430] | 193 | |
---|
[cbcdd2c] | 194 | #self.complete(x=self.data.x[index], y=output[index], |
---|
| 195 | # page_id=self.page_id, |
---|
| 196 | # state=self.state, |
---|
| 197 | # weight=self.weight, |
---|
| 198 | # fid=self.fid, |
---|
| 199 | # toggle_mode_on=self.toggle_mode_on, |
---|
| 200 | # elapsed=elapsed, index=index, model=self.model, |
---|
| 201 | # data=self.data, |
---|
| 202 | # update_chisqr=self.update_chisqr, |
---|
| 203 | # source=self.source) |
---|
| 204 | return (self.data.x[index], output[index], |
---|
| 205 | self.page_id, |
---|
| 206 | self.state, |
---|
| 207 | self.weight, |
---|
| 208 | self.fid, |
---|
| 209 | self.toggle_mode_on, |
---|
| 210 | elapsed, index, self.model, |
---|
| 211 | self.data, |
---|
| 212 | self.update_chisqr, |
---|
| 213 | self.source) |
---|
[2f4b430] | 214 | |
---|
[f72333f] | 215 | def results(self): |
---|
| 216 | """ |
---|
[5062bbf] | 217 | Send resuts of the computation |
---|
[f72333f] | 218 | """ |
---|
| 219 | return [self.out, self.index] |
---|
[5062bbf] | 220 | |
---|
| 221 | """ |
---|
| 222 | Example: :: |
---|
[2f4b430] | 223 | |
---|
[5062bbf] | 224 | class CalcCommandline: |
---|
| 225 | def __init__(self, n=20000): |
---|
| 226 | #print thread.get_ident() |
---|
[79492222] | 227 | from sas.models.CylinderModel import CylinderModel |
---|
[2f4b430] | 228 | |
---|
[5062bbf] | 229 | model = CylinderModel() |
---|
[2f4b430] | 230 | |
---|
| 231 | |
---|
[5062bbf] | 232 | print model.runXY([0.01, 0.02]) |
---|
[2f4b430] | 233 | |
---|
[5062bbf] | 234 | qmax = 0.01 |
---|
| 235 | qstep = 0.0001 |
---|
| 236 | self.done = False |
---|
[2f4b430] | 237 | |
---|
[5062bbf] | 238 | x = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
| 239 | y = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
[2f4b430] | 240 | |
---|
| 241 | |
---|
[5062bbf] | 242 | calc_thread_2D = Calc2D(x, y, None, model.clone(),None, |
---|
| 243 | -qmax, qmax,qstep, |
---|
| 244 | completefn=self.complete, |
---|
| 245 | updatefn=self.update , |
---|
| 246 | yieldtime=0.0) |
---|
[2f4b430] | 247 | |
---|
[5062bbf] | 248 | calc_thread_2D.queue() |
---|
| 249 | calc_thread_2D.ready(2.5) |
---|
[2f4b430] | 250 | |
---|
[5062bbf] | 251 | while not self.done: |
---|
| 252 | time.sleep(1) |
---|
[2f4b430] | 253 | |
---|
[5062bbf] | 254 | def update(self,output): |
---|
| 255 | print "update" |
---|
[2f4b430] | 256 | |
---|
[5062bbf] | 257 | def complete(self, image, data, model, elapsed, qmin, qmax,index, qstep ): |
---|
| 258 | print "complete" |
---|
| 259 | self.done = True |
---|
[2f4b430] | 260 | |
---|
[5062bbf] | 261 | if __name__ == "__main__": |
---|
| 262 | CalcCommandline() |
---|
[f32d144] | 263 | """ |
---|