[f32d144] | 1 | """ |
---|
| 2 | Calculation thread for modeling |
---|
| 3 | """ |
---|
[5062bbf] | 4 | |
---|
[bb18ef1] | 5 | import time |
---|
[7e7e806] | 6 | import numpy |
---|
| 7 | import math |
---|
[a855fec] | 8 | from sans.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, |
---|
| 27 | worktime=0.04 |
---|
[bb18ef1] | 28 | ): |
---|
[f32d144] | 29 | CalcThread.__init__(self, completefn, updatefn, yieldtime, worktime) |
---|
[7e7e806] | 30 | self.qmin = qmin |
---|
| 31 | self.qmax = qmax |
---|
[62f851f] | 32 | self.weight = weight |
---|
[f64a4b7] | 33 | self.fid = fid |
---|
[7e7e806] | 34 | #self.qstep = qstep |
---|
[fa65e99] | 35 | self.toggle_mode_on = toggle_mode_on |
---|
[7e7e806] | 36 | self.data = data |
---|
[66ff250] | 37 | self.page_id = page_id |
---|
[5ef55d2] | 38 | self.state = None |
---|
[1b001a7] | 39 | # the model on to calculate |
---|
[bb18ef1] | 40 | self.model = model |
---|
[7e7e806] | 41 | self.smearer = smearer |
---|
[f32d144] | 42 | self.starttime = 0 |
---|
[2296316] | 43 | self.update_chisqr = update_chisqr |
---|
[e3f6ef5] | 44 | self.source = source |
---|
[bb18ef1] | 45 | |
---|
| 46 | def compute(self): |
---|
| 47 | """ |
---|
[5062bbf] | 48 | Compute the data given a model function |
---|
[bb18ef1] | 49 | """ |
---|
[1b001a7] | 50 | self.starttime = time.time() |
---|
| 51 | # Determine appropriate q range |
---|
[7e7e806] | 52 | if self.qmin == None: |
---|
[c77d859] | 53 | self.qmin = 0 |
---|
[7e7e806] | 54 | if self.qmax == None: |
---|
| 55 | if self.data != None: |
---|
| 56 | newx = math.pow(max(math.fabs(self.data.xmax), |
---|
| 57 | math.fabs(self.data.xmin)), 2) |
---|
| 58 | newy = math.pow(max(math.fabs(self.data.ymax), |
---|
| 59 | math.fabs(self.data.ymin)), 2) |
---|
| 60 | self.qmax = math.sqrt(newx + newy) |
---|
[e575db9] | 61 | |
---|
[7e7e806] | 62 | if self.data is None: |
---|
[f32d144] | 63 | msg = "Compute Calc2D receive data = %s.\n" % str(self.data) |
---|
[7e7e806] | 64 | raise ValueError, msg |
---|
[e575db9] | 65 | |
---|
[f32d144] | 66 | # Define matrix where data will be plotted |
---|
| 67 | radius = numpy.sqrt((self.data.qx_data * self.data.qx_data) + \ |
---|
[7e7e806] | 68 | (self.data.qy_data * self.data.qy_data)) |
---|
[43e685d] | 69 | |
---|
[e575db9] | 70 | # For theory, qmax is based on 1d qmax |
---|
| 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) |
---|
| 73 | index_model = index_model & self.data.mask |
---|
| 74 | index_model = index_model & numpy.isfinite(self.data.data) |
---|
| 75 | |
---|
| 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) |
---|
| 81 | # Get necessary data from self.data and set the data for smearing |
---|
| 82 | fn.get_data() |
---|
[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 |
---|
[f32d144] | 88 | value = self.model.evalDistribution(\ |
---|
| 89 | [self.data.qx_data[index_model], |
---|
| 90 | self.data.qy_data[index_model]]) |
---|
[7e7e806] | 91 | output = numpy.zeros(len(self.data.qx_data)) |
---|
[43e685d] | 92 | # output default is None |
---|
[f32d144] | 93 | # This method is to distinguish between masked |
---|
[7e7e806] | 94 | #point(nan) and data point = 0. |
---|
[f32d144] | 95 | output = output / output |
---|
[43e685d] | 96 | # set value for self.mask==True, else still None to Plottools |
---|
[f32d144] | 97 | output[index_model] = value |
---|
| 98 | elapsed = time.time() - self.starttime |
---|
[6bbeacd4] | 99 | self.complete(image=output, |
---|
[f32d144] | 100 | data=self.data, |
---|
[66ff250] | 101 | page_id=self.page_id, |
---|
[6bbeacd4] | 102 | model=self.model, |
---|
[5ef55d2] | 103 | state=self.state, |
---|
[fa65e99] | 104 | toggle_mode_on=self.toggle_mode_on, |
---|
[6bbeacd4] | 105 | elapsed=elapsed, |
---|
| 106 | index=index_model, |
---|
[f64a4b7] | 107 | fid=self.fid, |
---|
[6bbeacd4] | 108 | qmin=self.qmin, |
---|
| 109 | qmax=self.qmax, |
---|
[62f851f] | 110 | weight=self.weight, |
---|
[7e7e806] | 111 | #qstep=self.qstep, |
---|
[f32d144] | 112 | update_chisqr=self.update_chisqr, |
---|
[e3f6ef5] | 113 | source=self.source) |
---|
[1b001a7] | 114 | |
---|
[bb18ef1] | 115 | |
---|
| 116 | class Calc1D(CalcThread): |
---|
[5062bbf] | 117 | """ |
---|
| 118 | Compute 1D data |
---|
| 119 | """ |
---|
[7e7e806] | 120 | def __init__(self, model, |
---|
[66ff250] | 121 | page_id, |
---|
[7e7e806] | 122 | data, |
---|
[f64a4b7] | 123 | fid=None, |
---|
[bb18ef1] | 124 | qmin=None, |
---|
| 125 | qmax=None, |
---|
[62f851f] | 126 | weight=None, |
---|
[bb18ef1] | 127 | smearer=None, |
---|
[fa65e99] | 128 | toggle_mode_on=False, |
---|
[5ef55d2] | 129 | state=None, |
---|
[f32d144] | 130 | completefn=None, |
---|
[2296316] | 131 | update_chisqr=True, |
---|
[e3f6ef5] | 132 | source='model', |
---|
[7e7e806] | 133 | updatefn=None, |
---|
| 134 | yieldtime=0.01, |
---|
| 135 | worktime=0.01 |
---|
[bb18ef1] | 136 | ): |
---|
[5062bbf] | 137 | """ |
---|
| 138 | """ |
---|
[f32d144] | 139 | CalcThread.__init__(self, completefn, |
---|
[bb18ef1] | 140 | updatefn, |
---|
| 141 | yieldtime, |
---|
| 142 | worktime) |
---|
[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 |
---|
[bb18ef1] | 158 | |
---|
| 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() |
---|
[7e7e806] | 164 | output = numpy.zeros((len(self.data.x))) |
---|
[f32d144] | 165 | index = (self.qmin <= self.data.x) & (self.data.x <= self.qmax) |
---|
[bfe4644] | 166 | |
---|
[f32d144] | 167 | ##smearer the ouput of the plot |
---|
[7e7e806] | 168 | if self.smearer is not None: |
---|
[f32d144] | 169 | first_bin, last_bin = self.smearer.get_bin_range(self.qmin, |
---|
[7e7e806] | 170 | self.qmax) |
---|
| 171 | mask = self.data.x[first_bin:last_bin] |
---|
| 172 | output[first_bin:last_bin] = self.model.evalDistribution(mask) |
---|
[f32d144] | 173 | output = self.smearer(output, first_bin, last_bin) |
---|
[e627f19] | 174 | else: |
---|
[7e7e806] | 175 | output[index] = self.model.evalDistribution(self.data.x[index]) |
---|
[bfe4644] | 176 | |
---|
[5062bbf] | 177 | elapsed = time.time() - self.starttime |
---|
[785c8233] | 178 | |
---|
[f32d144] | 179 | self.complete(x=self.data.x[index], y=output[index], |
---|
[66ff250] | 180 | page_id=self.page_id, |
---|
[5ef55d2] | 181 | state=self.state, |
---|
[62f851f] | 182 | weight=self.weight, |
---|
[f64a4b7] | 183 | fid=self.fid, |
---|
[fa65e99] | 184 | toggle_mode_on=self.toggle_mode_on, |
---|
[f32d144] | 185 | elapsed=elapsed, index=index, model=self.model, |
---|
| 186 | data=self.data, |
---|
| 187 | update_chisqr=self.update_chisqr, |
---|
[e3f6ef5] | 188 | source=self.source) |
---|
[bb18ef1] | 189 | |
---|
[f72333f] | 190 | def results(self): |
---|
| 191 | """ |
---|
[5062bbf] | 192 | Send resuts of the computation |
---|
[f72333f] | 193 | """ |
---|
| 194 | return [self.out, self.index] |
---|
[5062bbf] | 195 | |
---|
| 196 | """ |
---|
| 197 | Example: :: |
---|
| 198 | |
---|
| 199 | class CalcCommandline: |
---|
| 200 | def __init__(self, n=20000): |
---|
| 201 | #print thread.get_ident() |
---|
| 202 | from sans.models.CylinderModel import CylinderModel |
---|
| 203 | |
---|
| 204 | model = CylinderModel() |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | print model.runXY([0.01, 0.02]) |
---|
| 208 | |
---|
| 209 | qmax = 0.01 |
---|
| 210 | qstep = 0.0001 |
---|
| 211 | self.done = False |
---|
| 212 | |
---|
| 213 | x = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
| 214 | y = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
[bb18ef1] | 215 | |
---|
| 216 | |
---|
[5062bbf] | 217 | calc_thread_2D = Calc2D(x, y, None, model.clone(),None, |
---|
| 218 | -qmax, qmax,qstep, |
---|
| 219 | completefn=self.complete, |
---|
| 220 | updatefn=self.update , |
---|
| 221 | yieldtime=0.0) |
---|
[bb18ef1] | 222 | |
---|
[5062bbf] | 223 | calc_thread_2D.queue() |
---|
| 224 | calc_thread_2D.ready(2.5) |
---|
| 225 | |
---|
| 226 | while not self.done: |
---|
| 227 | time.sleep(1) |
---|
[904713c] | 228 | |
---|
[5062bbf] | 229 | def update(self,output): |
---|
| 230 | print "update" |
---|
[bb18ef1] | 231 | |
---|
[5062bbf] | 232 | def complete(self, image, data, model, elapsed, qmin, qmax,index, qstep ): |
---|
| 233 | print "complete" |
---|
| 234 | self.done = True |
---|
| 235 | |
---|
| 236 | if __name__ == "__main__": |
---|
| 237 | CalcCommandline() |
---|
[f32d144] | 238 | """ |
---|