[bb18ef1] | 1 | import time |
---|
[e733619] | 2 | from data_util.calcthread import CalcThread |
---|
[bb18ef1] | 3 | import sys |
---|
[d16e396] | 4 | import numpy,math |
---|
[bb18ef1] | 5 | |
---|
| 6 | class Calc2D(CalcThread): |
---|
| 7 | """ |
---|
| 8 | Compute 2D model |
---|
| 9 | This calculation assumes a 2-fold symmetry of the model |
---|
| 10 | where points are computed for one half of the detector |
---|
| 11 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
| 12 | """ |
---|
| 13 | |
---|
[c77d859] | 14 | def __init__(self, x, y, data,model,qmin, qmax,qstep, |
---|
[bb18ef1] | 15 | completefn = None, |
---|
| 16 | updatefn = None, |
---|
| 17 | yieldtime = 0.01, |
---|
| 18 | worktime = 0.01 |
---|
| 19 | ): |
---|
| 20 | CalcThread.__init__(self,completefn, |
---|
| 21 | updatefn, |
---|
| 22 | yieldtime, |
---|
| 23 | worktime) |
---|
| 24 | self.qmin= qmin |
---|
[c77d859] | 25 | self.qmax= qmax |
---|
[bb18ef1] | 26 | self.qstep= qstep |
---|
[e575db9] | 27 | |
---|
| 28 | self.x = x |
---|
| 29 | self.y = y |
---|
[c77d859] | 30 | self.data= data |
---|
[1b001a7] | 31 | # the model on to calculate |
---|
[bb18ef1] | 32 | self.model = model |
---|
[904713c] | 33 | self.starttime = 0 |
---|
[bb18ef1] | 34 | |
---|
| 35 | def compute(self): |
---|
| 36 | """ |
---|
| 37 | Compute the data given a model function |
---|
| 38 | """ |
---|
[1b001a7] | 39 | self.starttime = time.time() |
---|
| 40 | # Determine appropriate q range |
---|
[c77d859] | 41 | if self.qmin==None: |
---|
| 42 | self.qmin = 0 |
---|
| 43 | if self.qmax== None: |
---|
[cd3d15b] | 44 | if self.data !=None: |
---|
| 45 | newx= math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2) |
---|
| 46 | newy= math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2) |
---|
| 47 | self.qmax=math.sqrt( newx + newy ) |
---|
[e575db9] | 48 | |
---|
| 49 | if self.data != None: |
---|
[43e685d] | 50 | self.I_data = self.data.data |
---|
[e575db9] | 51 | self.qx_data = self.data.qx_data |
---|
| 52 | self.qy_data = self.data.qy_data |
---|
| 53 | self.mask = self.data.mask |
---|
| 54 | else: |
---|
| 55 | xbin = numpy.linspace(start= -1*self.qmax, |
---|
| 56 | stop= self.qmax, |
---|
| 57 | num= self.qstep, |
---|
| 58 | endpoint=True ) |
---|
| 59 | ybin = numpy.linspace(start= -1*self.qmax, |
---|
| 60 | stop= self.qmax, |
---|
| 61 | num= self.qstep, |
---|
| 62 | endpoint=True ) |
---|
| 63 | |
---|
| 64 | new_xbin = numpy.tile(xbin, (len(ybin),1)) |
---|
| 65 | new_ybin = numpy.tile(ybin, (len(xbin),1)) |
---|
| 66 | new_ybin = new_ybin.swapaxes(0,1) |
---|
| 67 | new_xbin = new_xbin.flatten() |
---|
| 68 | new_ybin = new_ybin.flatten() |
---|
| 69 | self.qy_data = new_ybin |
---|
| 70 | self.qx_data = new_xbin |
---|
[43e685d] | 71 | # fake data |
---|
| 72 | self.I_data = numpy.ones(len(self.qx_data)) |
---|
[e575db9] | 73 | |
---|
| 74 | self.mask = numpy.ones(len(self.qx_data),dtype=bool) |
---|
| 75 | |
---|
| 76 | # Define matrix where data will be plotted |
---|
| 77 | radius= numpy.sqrt( self.qx_data*self.qx_data + self.qy_data*self.qy_data ) |
---|
| 78 | index_data= (self.qmin<= radius)&(self.mask) |
---|
[43e685d] | 79 | |
---|
[e575db9] | 80 | # For theory, qmax is based on 1d qmax |
---|
| 81 | # so that must be mulitified by sqrt(2) to get actual max for 2d |
---|
| 82 | index_model = ((self.qmin <= radius)&(radius<= self.qmax)) |
---|
| 83 | self.mask = (index_model)&(self.mask) |
---|
[43e685d] | 84 | self.mask = (self.mask)&(numpy.isfinite(self.I_data)) |
---|
[e575db9] | 85 | if self.data ==None: |
---|
[d2caa18] | 86 | # Only qmin value will be consider for the detector |
---|
[e575db9] | 87 | self.mask = index_data |
---|
| 88 | |
---|
| 89 | value = self.model.evalDistribution([self.qx_data[self.mask],self.qy_data[self.mask]] ) |
---|
| 90 | |
---|
| 91 | output = numpy.zeros(len(self.mask)) |
---|
[43e685d] | 92 | |
---|
| 93 | # output default is None |
---|
| 94 | # This method is to distinguish between masked point and data point = 0. |
---|
| 95 | output = output/output |
---|
| 96 | # set value for self.mask==True, else still None to Plottools |
---|
[e575db9] | 97 | output[self.mask] = value |
---|
| 98 | |
---|
[1b001a7] | 99 | elapsed = time.time()-self.starttime |
---|
| 100 | self.complete( image = output, |
---|
| 101 | data = self.data , |
---|
| 102 | model = self.model, |
---|
| 103 | elapsed = elapsed, |
---|
| 104 | qmin = self.qmin, |
---|
[e575db9] | 105 | qmax = self.qmax, |
---|
[1b001a7] | 106 | qstep = self.qstep ) |
---|
| 107 | |
---|
[785c8233] | 108 | |
---|
[bb18ef1] | 109 | |
---|
| 110 | |
---|
| 111 | class Calc1D(CalcThread): |
---|
| 112 | """Compute 1D data""" |
---|
| 113 | |
---|
| 114 | def __init__(self, x, model, |
---|
| 115 | data=None, |
---|
| 116 | qmin=None, |
---|
| 117 | qmax=None, |
---|
| 118 | smearer=None, |
---|
| 119 | completefn = None, |
---|
| 120 | updatefn = None, |
---|
| 121 | yieldtime = 0.01, |
---|
| 122 | worktime = 0.01 |
---|
| 123 | ): |
---|
| 124 | CalcThread.__init__(self,completefn, |
---|
| 125 | updatefn, |
---|
| 126 | yieldtime, |
---|
| 127 | worktime) |
---|
[1b001a7] | 128 | self.x = numpy.array(x) |
---|
[bb18ef1] | 129 | self.data= data |
---|
| 130 | self.qmin= qmin |
---|
| 131 | self.qmax= qmax |
---|
| 132 | self.model = model |
---|
| 133 | self.smearer= smearer |
---|
| 134 | self.starttime = 0 |
---|
| 135 | |
---|
| 136 | def compute(self): |
---|
[c77d859] | 137 | """ |
---|
| 138 | Compute model 1d value given qmin , qmax , x value |
---|
| 139 | """ |
---|
[1b001a7] | 140 | |
---|
[bb18ef1] | 141 | self.starttime = time.time() |
---|
[cad821b] | 142 | output = numpy.zeros((len(self.x))) |
---|
| 143 | index= (self.qmin <= self.x)& (self.x <= self.qmax) |
---|
[bfe4644] | 144 | |
---|
[fb8daaaf] | 145 | ##smearer the ouput of the plot |
---|
[bb18ef1] | 146 | if self.smearer!=None: |
---|
[a0bc608] | 147 | first_bin, last_bin = self.smearer.get_bin_range(self.qmin, self.qmax) |
---|
[e627f19] | 148 | output[first_bin:last_bin] = self.model.evalDistribution(self.x[first_bin:last_bin]) |
---|
[a0bc608] | 149 | output = self.smearer(output, first_bin, last_bin) |
---|
[e627f19] | 150 | else: |
---|
| 151 | output[index] = self.model.evalDistribution(self.x[index]) |
---|
[bfe4644] | 152 | |
---|
[bb18ef1] | 153 | elapsed = time.time()-self.starttime |
---|
[785c8233] | 154 | |
---|
[a0bc608] | 155 | self.complete(x= self.x[index], y= output[index], |
---|
[c77d859] | 156 | elapsed=elapsed, model= self.model, data=self.data) |
---|
[bb18ef1] | 157 | |
---|
[785c8233] | 158 | |
---|
[1b001a7] | 159 | |
---|
[bb18ef1] | 160 | class CalcCommandline: |
---|
| 161 | def __init__(self, n=20000): |
---|
| 162 | #print thread.get_ident() |
---|
| 163 | from sans.models.CylinderModel import CylinderModel |
---|
| 164 | |
---|
[904713c] | 165 | model = CylinderModel() |
---|
[bb18ef1] | 166 | |
---|
| 167 | |
---|
| 168 | print model.runXY([0.01, 0.02]) |
---|
| 169 | |
---|
| 170 | qmax = 0.01 |
---|
| 171 | qstep = 0.0001 |
---|
| 172 | self.done = False |
---|
| 173 | |
---|
[904713c] | 174 | x = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
| 175 | y = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
| 176 | |
---|
[bb18ef1] | 177 | |
---|
[904713c] | 178 | calc_thread_2D = Calc2D(x, y, None, model.clone(),-qmax, qmax,qstep, |
---|
[bb18ef1] | 179 | completefn=self.complete, |
---|
| 180 | updatefn=self.update , |
---|
| 181 | yieldtime=0.0) |
---|
| 182 | |
---|
| 183 | calc_thread_2D.queue() |
---|
| 184 | calc_thread_2D.ready(2.5) |
---|
| 185 | |
---|
| 186 | while not self.done: |
---|
| 187 | time.sleep(1) |
---|
| 188 | |
---|
| 189 | def update(self,output): |
---|
| 190 | print "update" |
---|
| 191 | |
---|
[904713c] | 192 | def complete(self, image, data, model, elapsed, qmin, qmax, qstep ): |
---|
[bb18ef1] | 193 | print "complete" |
---|
| 194 | self.done = True |
---|
| 195 | |
---|
| 196 | if __name__ == "__main__": |
---|
| 197 | CalcCommandline() |
---|
| 198 | |
---|