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