[dc5ef15] | 1 | """ |
---|
| 2 | Calculation thread for modeling |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | import time |
---|
| 6 | import numpy |
---|
| 7 | import math |
---|
| 8 | from sas.sascalc.data_util.calcthread import CalcThread |
---|
| 9 | from sas.sascalc.fit.MultiplicationModel import MultiplicationModel |
---|
[d4dac80] | 10 | import sas.qtgui.Utilities.LocalConfig as LocalConfig |
---|
[dc5ef15] | 11 | |
---|
| 12 | class Calc2D(CalcThread): |
---|
| 13 | """ |
---|
| 14 | Compute 2D model |
---|
| 15 | This calculation assumes a 2-fold symmetry of the model |
---|
| 16 | where points are computed for one half of the detector |
---|
| 17 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
| 18 | """ |
---|
| 19 | def __init__(self, data, model, smearer, qmin, qmax, page_id, |
---|
| 20 | state=None, |
---|
| 21 | weight=None, |
---|
| 22 | fid=None, |
---|
| 23 | toggle_mode_on=False, |
---|
| 24 | completefn=None, |
---|
| 25 | updatefn=None, |
---|
| 26 | update_chisqr=True, |
---|
| 27 | source='model', |
---|
| 28 | yieldtime=0.04, |
---|
| 29 | worktime=0.04, |
---|
| 30 | exception_handler=None, |
---|
| 31 | ): |
---|
| 32 | CalcThread.__init__(self, completefn, updatefn, yieldtime, worktime, |
---|
| 33 | exception_handler=exception_handler) |
---|
| 34 | self.qmin = qmin |
---|
| 35 | self.qmax = qmax |
---|
| 36 | self.weight = weight |
---|
| 37 | self.fid = fid |
---|
| 38 | #self.qstep = qstep |
---|
| 39 | self.toggle_mode_on = toggle_mode_on |
---|
| 40 | self.data = data |
---|
| 41 | self.page_id = page_id |
---|
| 42 | self.state = None |
---|
| 43 | # the model on to calculate |
---|
| 44 | self.model = model |
---|
| 45 | self.smearer = smearer |
---|
| 46 | self.starttime = 0 |
---|
| 47 | self.update_chisqr = update_chisqr |
---|
| 48 | self.source = source |
---|
| 49 | |
---|
| 50 | def compute(self): |
---|
| 51 | """ |
---|
| 52 | Compute the data given a model function |
---|
| 53 | """ |
---|
| 54 | self.starttime = time.time() |
---|
| 55 | # Determine appropriate q range |
---|
[cee5c78] | 56 | if self.qmin is None: |
---|
[dc5ef15] | 57 | self.qmin = 0 |
---|
[cee5c78] | 58 | if self.qmax is None: |
---|
| 59 | if self.data is not None: |
---|
[dc5ef15] | 60 | newx = math.pow(max(math.fabs(self.data.xmax), |
---|
| 61 | math.fabs(self.data.xmin)), 2) |
---|
| 62 | newy = math.pow(max(math.fabs(self.data.ymax), |
---|
| 63 | math.fabs(self.data.ymin)), 2) |
---|
| 64 | self.qmax = math.sqrt(newx + newy) |
---|
| 65 | |
---|
| 66 | if self.data is None: |
---|
| 67 | msg = "Compute Calc2D receive data = %s.\n" % str(self.data) |
---|
[b3e8629] | 68 | raise ValueError(msg) |
---|
[dc5ef15] | 69 | |
---|
| 70 | # Define matrix where data will be plotted |
---|
| 71 | radius = numpy.sqrt((self.data.qx_data * self.data.qx_data) + \ |
---|
| 72 | (self.data.qy_data * self.data.qy_data)) |
---|
| 73 | |
---|
| 74 | # For theory, qmax is based on 1d qmax |
---|
| 75 | # so that must be mulitified by sqrt(2) to get actual max for 2d |
---|
| 76 | index_model = (self.qmin <= radius) & (radius <= self.qmax) |
---|
| 77 | index_model = index_model & self.data.mask |
---|
| 78 | index_model = index_model & numpy.isfinite(self.data.data) |
---|
| 79 | |
---|
| 80 | if self.smearer is not None: |
---|
| 81 | # Set smearer w/ data, model and index. |
---|
| 82 | fn = self.smearer |
---|
| 83 | fn.set_model(self.model) |
---|
| 84 | fn.set_index(index_model) |
---|
| 85 | # Calculate smeared Intensity |
---|
| 86 | #(by Gaussian averaging): DataLoader/smearing2d/Smearer2D() |
---|
| 87 | value = fn.get_value() |
---|
| 88 | else: |
---|
| 89 | # calculation w/o smearing |
---|
| 90 | value = self.model.evalDistribution([ |
---|
| 91 | self.data.qx_data[index_model], |
---|
| 92 | self.data.qy_data[index_model] |
---|
| 93 | ]) |
---|
| 94 | output = numpy.zeros(len(self.data.qx_data)) |
---|
| 95 | # output default is None |
---|
| 96 | # This method is to distinguish between masked |
---|
| 97 | #point(nan) and data point = 0. |
---|
| 98 | output = output / output |
---|
| 99 | # set value for self.mask==True, else still None to Plottools |
---|
| 100 | output[index_model] = value |
---|
| 101 | elapsed = time.time() - self.starttime |
---|
[d4dac80] | 102 | |
---|
[dcabba7] | 103 | res = dict(image = output, data = self.data, page_id = self.page_id, |
---|
| 104 | model = self.model, state = self.state, |
---|
| 105 | toggle_mode_on = self.toggle_mode_on, elapsed = elapsed, |
---|
| 106 | index = index_model, fid = self.fid, |
---|
| 107 | qmin = self.qmin, qmax = self.qmax, |
---|
| 108 | weight = self.weight, update_chisqr = self.update_chisqr, |
---|
| 109 | source = self.source) |
---|
| 110 | |
---|
[d4dac80] | 111 | if LocalConfig.USING_TWISTED: |
---|
[dcabba7] | 112 | return res |
---|
[d4dac80] | 113 | else: |
---|
[dcabba7] | 114 | self.completefn(res) |
---|
[dc5ef15] | 115 | |
---|
| 116 | class Calc1D(CalcThread): |
---|
| 117 | """ |
---|
| 118 | Compute 1D data |
---|
| 119 | """ |
---|
| 120 | def __init__(self, model, |
---|
| 121 | page_id, |
---|
| 122 | data, |
---|
| 123 | fid=None, |
---|
| 124 | qmin=None, |
---|
| 125 | qmax=None, |
---|
| 126 | weight=None, |
---|
| 127 | smearer=None, |
---|
| 128 | toggle_mode_on=False, |
---|
| 129 | state=None, |
---|
| 130 | completefn=None, |
---|
| 131 | update_chisqr=True, |
---|
| 132 | source='model', |
---|
| 133 | updatefn=None, |
---|
| 134 | yieldtime=0.01, |
---|
| 135 | worktime=0.01, |
---|
| 136 | exception_handler=None, |
---|
| 137 | ): |
---|
| 138 | """ |
---|
| 139 | """ |
---|
| 140 | CalcThread.__init__(self, completefn, updatefn, yieldtime, worktime, |
---|
| 141 | exception_handler=exception_handler) |
---|
| 142 | self.fid = fid |
---|
| 143 | self.data = data |
---|
| 144 | self.qmin = qmin |
---|
| 145 | self.qmax = qmax |
---|
| 146 | self.model = model |
---|
| 147 | self.weight = weight |
---|
| 148 | self.toggle_mode_on = toggle_mode_on |
---|
| 149 | self.state = state |
---|
| 150 | self.page_id = page_id |
---|
| 151 | self.smearer = smearer |
---|
| 152 | self.starttime = 0 |
---|
| 153 | self.update_chisqr = update_chisqr |
---|
| 154 | self.source = source |
---|
| 155 | self.out = None |
---|
| 156 | self.index = None |
---|
| 157 | |
---|
| 158 | def compute(self): |
---|
| 159 | """ |
---|
| 160 | Compute model 1d value given qmin , qmax , x value |
---|
| 161 | """ |
---|
| 162 | self.starttime = time.time() |
---|
| 163 | output = numpy.zeros((len(self.data.x))) |
---|
| 164 | index = (self.qmin <= self.data.x) & (self.data.x <= self.qmax) |
---|
| 165 | |
---|
| 166 | # If we use a smearer, also return the unsmeared model |
---|
| 167 | unsmeared_output = None |
---|
| 168 | unsmeared_data = None |
---|
| 169 | unsmeared_error = None |
---|
| 170 | ##smearer the ouput of the plot |
---|
| 171 | if self.smearer is not None: |
---|
| 172 | first_bin, last_bin = self.smearer.get_bin_range(self.qmin, |
---|
| 173 | self.qmax) |
---|
| 174 | mask = self.data.x[first_bin:last_bin+1] |
---|
| 175 | unsmeared_output = numpy.zeros((len(self.data.x))) |
---|
| 176 | unsmeared_output[first_bin:last_bin+1] = self.model.evalDistribution(mask) |
---|
| 177 | output = self.smearer(unsmeared_output, first_bin, last_bin) |
---|
| 178 | |
---|
| 179 | # Rescale data to unsmeared model |
---|
| 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. |
---|
| 182 | if isinstance(self.data.y, numpy.ndarray) and output.shape == self.data.y.shape: |
---|
| 183 | unsmeared_data = numpy.zeros((len(self.data.x))) |
---|
| 184 | unsmeared_error = numpy.zeros((len(self.data.x))) |
---|
| 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] |
---|
| 191 | unsmeared_output=unsmeared_output[index] |
---|
| 192 | unsmeared_data=unsmeared_data[index] |
---|
| 193 | unsmeared_error=unsmeared_error |
---|
| 194 | else: |
---|
| 195 | output[index] = self.model.evalDistribution(self.data.x[index]) |
---|
| 196 | |
---|
| 197 | sq_values = None |
---|
| 198 | pq_values = None |
---|
| 199 | s_model = None |
---|
| 200 | p_model = None |
---|
| 201 | if isinstance(self.model, MultiplicationModel): |
---|
| 202 | s_model = self.model.s_model |
---|
| 203 | p_model = self.model.p_model |
---|
[3ae9179] | 204 | elif hasattr(self.model, "calc_composition_models"): |
---|
| 205 | results = self.model.calc_composition_models(self.data.x[index]) |
---|
| 206 | if results is not None: |
---|
| 207 | pq_values, sq_values = results |
---|
[dc5ef15] | 208 | |
---|
[3ae9179] | 209 | if pq_values is None or sq_values is None: |
---|
| 210 | if p_model is not None and s_model is not None: |
---|
| 211 | sq_values = numpy.zeros((len(self.data.x))) |
---|
| 212 | pq_values = numpy.zeros((len(self.data.x))) |
---|
| 213 | sq_values[index] = s_model.evalDistribution(self.data.x[index]) |
---|
| 214 | pq_values[index] = p_model.evalDistribution(self.data.x[index]) |
---|
[dc5ef15] | 215 | |
---|
| 216 | elapsed = time.time() - self.starttime |
---|
| 217 | |
---|
[dcabba7] | 218 | res = dict(x = self.data.x[index], y = output[index], |
---|
| 219 | page_id = self.page_id, state = self.state, weight = self.weight, |
---|
| 220 | fid = self.fid, toggle_mode_on = self.toggle_mode_on, |
---|
| 221 | elapsed = elapsed, index = index, model = self.model, |
---|
| 222 | data = self.data, update_chisqr = self.update_chisqr, |
---|
| 223 | source = self.source, unsmeared_output = unsmeared_output, |
---|
| 224 | unsmeared_data = unsmeared_data, unsmeared_error = unsmeared_error, |
---|
| 225 | pq_values = pq_values, sq_values = sq_values) |
---|
| 226 | |
---|
[d4dac80] | 227 | if LocalConfig.USING_TWISTED: |
---|
[dcabba7] | 228 | return res |
---|
[d4dac80] | 229 | else: |
---|
[dcabba7] | 230 | self.completefn(res) |
---|
[dc5ef15] | 231 | |
---|
| 232 | def results(self): |
---|
| 233 | """ |
---|
| 234 | Send resuts of the computation |
---|
| 235 | """ |
---|
| 236 | return [self.out, self.index] |
---|