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