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