[f32d144] | 1 | """ |
---|
| 2 | Calculation thread for modeling |
---|
| 3 | """ |
---|
[5062bbf] | 4 | |
---|
[bb18ef1] | 5 | import time |
---|
[7e7e806] | 6 | import numpy |
---|
| 7 | import math |
---|
| 8 | from 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 |
---|
[bb18ef1] | 156 | |
---|
| 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() |
---|
[7e7e806] | 162 | output = numpy.zeros((len(self.data.x))) |
---|
[f32d144] | 163 | index = (self.qmin <= self.data.x) & (self.data.x <= self.qmax) |
---|
[bfe4644] | 164 | |
---|
[f32d144] | 165 | ##smearer the ouput of the plot |
---|
[7e7e806] | 166 | if self.smearer is not None: |
---|
[f32d144] | 167 | first_bin, last_bin = self.smearer.get_bin_range(self.qmin, |
---|
[7e7e806] | 168 | self.qmax) |
---|
| 169 | mask = self.data.x[first_bin:last_bin] |
---|
| 170 | output[first_bin:last_bin] = self.model.evalDistribution(mask) |
---|
[f32d144] | 171 | output = self.smearer(output, first_bin, last_bin) |
---|
[e627f19] | 172 | else: |
---|
[7e7e806] | 173 | output[index] = self.model.evalDistribution(self.data.x[index]) |
---|
[bfe4644] | 174 | |
---|
[5062bbf] | 175 | elapsed = time.time() - self.starttime |
---|
[785c8233] | 176 | |
---|
[f32d144] | 177 | self.complete(x=self.data.x[index], y=output[index], |
---|
[66ff250] | 178 | page_id=self.page_id, |
---|
[5ef55d2] | 179 | state=self.state, |
---|
[62f851f] | 180 | weight=self.weight, |
---|
[f64a4b7] | 181 | fid=self.fid, |
---|
[fa65e99] | 182 | toggle_mode_on=self.toggle_mode_on, |
---|
[f32d144] | 183 | elapsed=elapsed, index=index, model=self.model, |
---|
| 184 | data=self.data, |
---|
| 185 | update_chisqr=self.update_chisqr, |
---|
[e3f6ef5] | 186 | source=self.source) |
---|
[bb18ef1] | 187 | |
---|
[f72333f] | 188 | def results(self): |
---|
| 189 | """ |
---|
[5062bbf] | 190 | Send resuts of the computation |
---|
[f72333f] | 191 | """ |
---|
| 192 | return [self.out, self.index] |
---|
[5062bbf] | 193 | |
---|
| 194 | """ |
---|
| 195 | Example: :: |
---|
| 196 | |
---|
| 197 | class CalcCommandline: |
---|
| 198 | def __init__(self, n=20000): |
---|
| 199 | #print thread.get_ident() |
---|
| 200 | from sans.models.CylinderModel import CylinderModel |
---|
| 201 | |
---|
| 202 | model = CylinderModel() |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | print model.runXY([0.01, 0.02]) |
---|
| 206 | |
---|
| 207 | qmax = 0.01 |
---|
| 208 | qstep = 0.0001 |
---|
| 209 | self.done = False |
---|
| 210 | |
---|
| 211 | x = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
| 212 | y = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
[bb18ef1] | 213 | |
---|
| 214 | |
---|
[5062bbf] | 215 | calc_thread_2D = Calc2D(x, y, None, model.clone(),None, |
---|
| 216 | -qmax, qmax,qstep, |
---|
| 217 | completefn=self.complete, |
---|
| 218 | updatefn=self.update , |
---|
| 219 | yieldtime=0.0) |
---|
[bb18ef1] | 220 | |
---|
[5062bbf] | 221 | calc_thread_2D.queue() |
---|
| 222 | calc_thread_2D.ready(2.5) |
---|
| 223 | |
---|
| 224 | while not self.done: |
---|
| 225 | time.sleep(1) |
---|
[904713c] | 226 | |
---|
[5062bbf] | 227 | def update(self,output): |
---|
| 228 | print "update" |
---|
[bb18ef1] | 229 | |
---|
[5062bbf] | 230 | def complete(self, image, data, model, elapsed, qmin, qmax,index, qstep ): |
---|
| 231 | print "complete" |
---|
| 232 | self.done = True |
---|
| 233 | |
---|
| 234 | if __name__ == "__main__": |
---|
| 235 | CalcCommandline() |
---|
[f32d144] | 236 | """ |
---|