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