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