[5062bbf] | 1 | |
---|
| 2 | |
---|
[bb18ef1] | 3 | import time |
---|
[e733619] | 4 | from data_util.calcthread import CalcThread |
---|
[bb18ef1] | 5 | import sys |
---|
[d16e396] | 6 | import numpy,math |
---|
[e4957fb] | 7 | from sans.models.smearing_2d import Smearer2D |
---|
[5062bbf] | 8 | |
---|
[bb18ef1] | 9 | class Calc2D(CalcThread): |
---|
| 10 | """ |
---|
[5062bbf] | 11 | Compute 2D model |
---|
| 12 | This calculation assumes a 2-fold symmetry of the model |
---|
| 13 | where points are computed for one half of the detector |
---|
| 14 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
[bb18ef1] | 15 | """ |
---|
[f72333f] | 16 | def __init__(self, x, y, data,model,smearer,qmin, qmax,qstep, |
---|
[66ff250] | 17 | page_id , |
---|
[5ef55d2] | 18 | state=None, |
---|
[fa65e99] | 19 | toggle_mode_on=False, |
---|
[bb18ef1] | 20 | completefn = None, |
---|
| 21 | updatefn = None, |
---|
[2296316] | 22 | update_chisqr=True, |
---|
[bb18ef1] | 23 | yieldtime = 0.01, |
---|
| 24 | worktime = 0.01 |
---|
| 25 | ): |
---|
| 26 | CalcThread.__init__(self,completefn, |
---|
| 27 | updatefn, |
---|
| 28 | yieldtime, |
---|
| 29 | worktime) |
---|
| 30 | self.qmin= qmin |
---|
[c77d859] | 31 | self.qmax= qmax |
---|
[bb18ef1] | 32 | self.qstep= qstep |
---|
[fa65e99] | 33 | self.toggle_mode_on = toggle_mode_on |
---|
[e575db9] | 34 | self.x = x |
---|
| 35 | self.y = y |
---|
[c77d859] | 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 |
---|
[f72333f] | 41 | self.smearer = smearer#(data=self.data,model=self.model) |
---|
[904713c] | 42 | self.starttime = 0 |
---|
[2296316] | 43 | self.update_chisqr = update_chisqr |
---|
[bb18ef1] | 44 | |
---|
| 45 | def compute(self): |
---|
| 46 | """ |
---|
[5062bbf] | 47 | Compute the data given a model function |
---|
[bb18ef1] | 48 | """ |
---|
[1b001a7] | 49 | self.starttime = time.time() |
---|
| 50 | # Determine appropriate q range |
---|
[c77d859] | 51 | if self.qmin==None: |
---|
| 52 | self.qmin = 0 |
---|
| 53 | if self.qmax== None: |
---|
[cd3d15b] | 54 | if self.data !=None: |
---|
| 55 | newx= math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2) |
---|
| 56 | newy= math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2) |
---|
| 57 | self.qmax=math.sqrt( newx + newy ) |
---|
[e575db9] | 58 | |
---|
| 59 | if self.data != None: |
---|
[43e685d] | 60 | self.I_data = self.data.data |
---|
[e575db9] | 61 | self.qx_data = self.data.qx_data |
---|
| 62 | self.qy_data = self.data.qy_data |
---|
[f72333f] | 63 | self.dqx_data = self.data.dqx_data |
---|
| 64 | self.dqy_data = self.data.dqy_data |
---|
[e575db9] | 65 | self.mask = self.data.mask |
---|
| 66 | else: |
---|
| 67 | xbin = numpy.linspace(start= -1*self.qmax, |
---|
| 68 | stop= self.qmax, |
---|
| 69 | num= self.qstep, |
---|
| 70 | endpoint=True ) |
---|
| 71 | ybin = numpy.linspace(start= -1*self.qmax, |
---|
| 72 | stop= self.qmax, |
---|
| 73 | num= self.qstep, |
---|
| 74 | endpoint=True ) |
---|
| 75 | |
---|
| 76 | new_xbin = numpy.tile(xbin, (len(ybin),1)) |
---|
| 77 | new_ybin = numpy.tile(ybin, (len(xbin),1)) |
---|
| 78 | new_ybin = new_ybin.swapaxes(0,1) |
---|
| 79 | new_xbin = new_xbin.flatten() |
---|
| 80 | new_ybin = new_ybin.flatten() |
---|
| 81 | self.qy_data = new_ybin |
---|
| 82 | self.qx_data = new_xbin |
---|
[43e685d] | 83 | # fake data |
---|
| 84 | self.I_data = numpy.ones(len(self.qx_data)) |
---|
[e575db9] | 85 | |
---|
| 86 | self.mask = numpy.ones(len(self.qx_data),dtype=bool) |
---|
| 87 | |
---|
| 88 | # Define matrix where data will be plotted |
---|
| 89 | radius= numpy.sqrt( self.qx_data*self.qx_data + self.qy_data*self.qy_data ) |
---|
| 90 | index_data= (self.qmin<= radius)&(self.mask) |
---|
[43e685d] | 91 | |
---|
[e575db9] | 92 | # For theory, qmax is based on 1d qmax |
---|
| 93 | # so that must be mulitified by sqrt(2) to get actual max for 2d |
---|
| 94 | index_model = ((self.qmin <= radius)&(radius<= self.qmax)) |
---|
[51a71a3] | 95 | index_model = (index_model)&(self.mask) |
---|
| 96 | index_model = (index_model)&(numpy.isfinite(self.I_data)) |
---|
[e575db9] | 97 | if self.data ==None: |
---|
[d2caa18] | 98 | # Only qmin value will be consider for the detector |
---|
[51a71a3] | 99 | index_model = index_data |
---|
[f72333f] | 100 | |
---|
| 101 | if self.smearer != None: |
---|
| 102 | # Set smearer w/ data, model and index. |
---|
| 103 | fn = self.smearer |
---|
| 104 | fn.set_model(self.model) |
---|
| 105 | fn.set_index(index_model) |
---|
| 106 | # Get necessary data from self.data and set the data for smearing |
---|
| 107 | fn.get_data() |
---|
| 108 | # Calculate smeared Intensity (by Gaussian averaging): DataLoader/smearing2d/Smearer2D() |
---|
| 109 | value = fn.get_value() |
---|
| 110 | |
---|
| 111 | else: |
---|
| 112 | # calculation w/o smearing |
---|
| 113 | value = self.model.evalDistribution([self.qx_data[index_model],self.qy_data[index_model]]) |
---|
[e575db9] | 114 | |
---|
[51a71a3] | 115 | output = numpy.zeros(len(self.qx_data)) |
---|
[43e685d] | 116 | |
---|
| 117 | # output default is None |
---|
[f72333f] | 118 | # This method is to distinguish between masked point(nan) and data point = 0. |
---|
[43e685d] | 119 | output = output/output |
---|
| 120 | # set value for self.mask==True, else still None to Plottools |
---|
[51a71a3] | 121 | output[index_model] = value |
---|
[f72333f] | 122 | |
---|
[1b001a7] | 123 | elapsed = time.time()-self.starttime |
---|
[6bbeacd4] | 124 | self.complete(image=output, |
---|
| 125 | data=self.data, |
---|
[66ff250] | 126 | page_id=self.page_id, |
---|
[6bbeacd4] | 127 | model=self.model, |
---|
[5ef55d2] | 128 | state=self.state, |
---|
[fa65e99] | 129 | toggle_mode_on=self.toggle_mode_on, |
---|
[6bbeacd4] | 130 | elapsed=elapsed, |
---|
| 131 | index=index_model, |
---|
| 132 | qmin=self.qmin, |
---|
| 133 | qmax=self.qmax, |
---|
[2296316] | 134 | qstep=self.qstep, |
---|
| 135 | update_chisqr = self.update_chisqr ) |
---|
[1b001a7] | 136 | |
---|
[bb18ef1] | 137 | |
---|
| 138 | class Calc1D(CalcThread): |
---|
[5062bbf] | 139 | """ |
---|
| 140 | Compute 1D data |
---|
| 141 | """ |
---|
[bb18ef1] | 142 | def __init__(self, x, model, |
---|
[66ff250] | 143 | page_id, |
---|
[bb18ef1] | 144 | data=None, |
---|
| 145 | qmin=None, |
---|
| 146 | qmax=None, |
---|
| 147 | smearer=None, |
---|
[fa65e99] | 148 | toggle_mode_on=False, |
---|
[5ef55d2] | 149 | state=None, |
---|
[bb18ef1] | 150 | completefn = None, |
---|
[2296316] | 151 | update_chisqr=True, |
---|
[bb18ef1] | 152 | updatefn = None, |
---|
| 153 | yieldtime = 0.01, |
---|
| 154 | worktime = 0.01 |
---|
| 155 | ): |
---|
[5062bbf] | 156 | """ |
---|
| 157 | """ |
---|
[bb18ef1] | 158 | CalcThread.__init__(self,completefn, |
---|
| 159 | updatefn, |
---|
| 160 | yieldtime, |
---|
| 161 | worktime) |
---|
[1b001a7] | 162 | self.x = numpy.array(x) |
---|
[fa65e99] | 163 | self.data = data |
---|
| 164 | self.qmin = qmin |
---|
| 165 | self.qmax = qmax |
---|
[bb18ef1] | 166 | self.model = model |
---|
[fa65e99] | 167 | self.toggle_mode_on = toggle_mode_on |
---|
[5ef55d2] | 168 | self.state = state |
---|
[66ff250] | 169 | self.page_id = page_id |
---|
[fa65e99] | 170 | self.smearer = smearer |
---|
[bb18ef1] | 171 | self.starttime = 0 |
---|
[2296316] | 172 | self.update_chisqr = update_chisqr |
---|
[bb18ef1] | 173 | |
---|
| 174 | def compute(self): |
---|
[c77d859] | 175 | """ |
---|
[5062bbf] | 176 | Compute model 1d value given qmin , qmax , x value |
---|
[c77d859] | 177 | """ |
---|
[bb18ef1] | 178 | self.starttime = time.time() |
---|
[cad821b] | 179 | output = numpy.zeros((len(self.x))) |
---|
| 180 | index= (self.qmin <= self.x)& (self.x <= self.qmax) |
---|
[bfe4644] | 181 | |
---|
[fb8daaaf] | 182 | ##smearer the ouput of the plot |
---|
[bb18ef1] | 183 | if self.smearer!=None: |
---|
[a0bc608] | 184 | first_bin, last_bin = self.smearer.get_bin_range(self.qmin, self.qmax) |
---|
[e627f19] | 185 | output[first_bin:last_bin] = self.model.evalDistribution(self.x[first_bin:last_bin]) |
---|
[a0bc608] | 186 | output = self.smearer(output, first_bin, last_bin) |
---|
[e627f19] | 187 | else: |
---|
| 188 | output[index] = self.model.evalDistribution(self.x[index]) |
---|
[bfe4644] | 189 | |
---|
[5062bbf] | 190 | elapsed = time.time() - self.starttime |
---|
[785c8233] | 191 | |
---|
[5062bbf] | 192 | self.complete(x=self.x[index], y=output[index], |
---|
[66ff250] | 193 | page_id=self.page_id, |
---|
[5ef55d2] | 194 | state=self.state, |
---|
[fa65e99] | 195 | toggle_mode_on=self.toggle_mode_on, |
---|
[5062bbf] | 196 | elapsed=elapsed,index=index, model=self.model, |
---|
[2296316] | 197 | data=self.data, |
---|
| 198 | update_chisqr = self.update_chisqr ) |
---|
[bb18ef1] | 199 | |
---|
[f72333f] | 200 | def results(self): |
---|
| 201 | """ |
---|
[5062bbf] | 202 | Send resuts of the computation |
---|
[f72333f] | 203 | """ |
---|
| 204 | return [self.out, self.index] |
---|
[5062bbf] | 205 | |
---|
| 206 | """ |
---|
| 207 | Example: :: |
---|
| 208 | |
---|
| 209 | class CalcCommandline: |
---|
| 210 | def __init__(self, n=20000): |
---|
| 211 | #print thread.get_ident() |
---|
| 212 | from sans.models.CylinderModel import CylinderModel |
---|
| 213 | |
---|
| 214 | model = CylinderModel() |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | print model.runXY([0.01, 0.02]) |
---|
| 218 | |
---|
| 219 | qmax = 0.01 |
---|
| 220 | qstep = 0.0001 |
---|
| 221 | self.done = False |
---|
| 222 | |
---|
| 223 | x = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
| 224 | y = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
[bb18ef1] | 225 | |
---|
| 226 | |
---|
[5062bbf] | 227 | calc_thread_2D = Calc2D(x, y, None, model.clone(),None, |
---|
| 228 | -qmax, qmax,qstep, |
---|
| 229 | completefn=self.complete, |
---|
| 230 | updatefn=self.update , |
---|
| 231 | yieldtime=0.0) |
---|
[bb18ef1] | 232 | |
---|
[5062bbf] | 233 | calc_thread_2D.queue() |
---|
| 234 | calc_thread_2D.ready(2.5) |
---|
| 235 | |
---|
| 236 | while not self.done: |
---|
| 237 | time.sleep(1) |
---|
[904713c] | 238 | |
---|
[5062bbf] | 239 | def update(self,output): |
---|
| 240 | print "update" |
---|
[bb18ef1] | 241 | |
---|
[5062bbf] | 242 | def complete(self, image, data, model, elapsed, qmin, qmax,index, qstep ): |
---|
| 243 | print "complete" |
---|
| 244 | self.done = True |
---|
| 245 | |
---|
| 246 | if __name__ == "__main__": |
---|
| 247 | CalcCommandline() |
---|
| 248 | """ |
---|