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