[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 |
---|
| 27 | # Reshape dimensions of x and y to call evalDistribution |
---|
| 28 | #self.x_array = numpy.reshape(x,[len(x),1]) |
---|
| 29 | #self.y_array = numpy.reshape(y,[1,len(y)]) |
---|
| 30 | self.x_array = numpy.reshape(x,[1,len(x)]) |
---|
| 31 | self.y_array = numpy.reshape(y,[len(y),1]) |
---|
| 32 | # Numpy array of dimensions 1 used for model.run method |
---|
| 33 | self.x= numpy.array(x) |
---|
| 34 | self.y= numpy.array(y) |
---|
| 35 | self.data= data |
---|
| 36 | # the model on to calculate |
---|
| 37 | self.model = model |
---|
| 38 | self.starttime = 0 |
---|
| 39 | |
---|
| 40 | def compute(self): |
---|
| 41 | """ |
---|
| 42 | Compute the data given a model function |
---|
| 43 | """ |
---|
| 44 | self.starttime = time.time() |
---|
| 45 | # Determine appropriate q range |
---|
| 46 | if self.qmin==None: |
---|
| 47 | self.qmin = 0 |
---|
| 48 | if self.qmax== None: |
---|
| 49 | if self.data !=None: |
---|
| 50 | newx= math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2) |
---|
| 51 | newy= math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2) |
---|
| 52 | self.qmax=math.sqrt( newx + newy ) |
---|
| 53 | # Define matrix where data will be plotted |
---|
| 54 | radius= numpy.sqrt( self.x_array**2 + self.y_array**2 ) |
---|
| 55 | index_data= (self.qmin<= radius) |
---|
| 56 | index_model = (self.qmin <= radius)&(radius<= self.qmax) |
---|
| 57 | |
---|
| 58 | output = numpy.zeros((len(self.x),len(self.y))) |
---|
| 59 | |
---|
| 60 | ## receive only list of 2 numpy array |
---|
| 61 | ## One must reshape to vertical and the other to horizontal |
---|
| 62 | value = self.model.evalDistribution([self.x_array,self.y_array] ) |
---|
| 63 | ## for data ignore the qmax |
---|
| 64 | if self.data == None: |
---|
| 65 | # Only qmin value will be consider for the detector |
---|
| 66 | output = value *index_data |
---|
| 67 | else: |
---|
| 68 | # The user can define qmin and qmax for the detector |
---|
| 69 | output = index_model*value |
---|
| 70 | |
---|
| 71 | elapsed = time.time()-self.starttime |
---|
| 72 | self.complete( image = output, |
---|
| 73 | data = self.data , |
---|
| 74 | model = self.model, |
---|
| 75 | elapsed = elapsed, |
---|
| 76 | qmin = self.qmin, |
---|
| 77 | qmax =self.qmax, |
---|
| 78 | qstep = self.qstep ) |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | class Calc1D(CalcThread): |
---|
| 84 | """Compute 1D data""" |
---|
| 85 | |
---|
| 86 | def __init__(self, x, model, |
---|
| 87 | data=None, |
---|
| 88 | qmin=None, |
---|
| 89 | qmax=None, |
---|
| 90 | smearer=None, |
---|
| 91 | completefn = None, |
---|
| 92 | updatefn = None, |
---|
| 93 | yieldtime = 0.01, |
---|
| 94 | worktime = 0.01 |
---|
| 95 | ): |
---|
| 96 | CalcThread.__init__(self,completefn, |
---|
| 97 | updatefn, |
---|
| 98 | yieldtime, |
---|
| 99 | worktime) |
---|
| 100 | self.x = numpy.array(x) |
---|
| 101 | self.data= data |
---|
| 102 | self.qmin= qmin |
---|
| 103 | self.qmax= qmax |
---|
| 104 | self.model = model |
---|
| 105 | self.smearer= smearer |
---|
| 106 | self.starttime = 0 |
---|
| 107 | |
---|
| 108 | def compute(self): |
---|
| 109 | """ |
---|
| 110 | Compute model 1d value given qmin , qmax , x value |
---|
| 111 | """ |
---|
| 112 | |
---|
| 113 | self.starttime = time.time() |
---|
| 114 | output = numpy.zeros((len(self.x))) |
---|
| 115 | index= (self.qmin <= self.x)& (self.x <= self.qmax) |
---|
| 116 | |
---|
| 117 | ##smearer the ouput of the plot |
---|
| 118 | if self.smearer!=None: |
---|
| 119 | first_bin, last_bin = self.smearer.get_bin_range(self.qmin, self.qmax) |
---|
| 120 | output[first_bin:last_bin] = self.model.evalDistribution(self.x[first_bin:last_bin]) |
---|
| 121 | output = self.smearer(output, first_bin, last_bin) |
---|
| 122 | else: |
---|
| 123 | output[index] = self.model.evalDistribution(self.x[index]) |
---|
| 124 | |
---|
| 125 | elapsed = time.time()-self.starttime |
---|
| 126 | |
---|
| 127 | self.complete(x= self.x[index], y= output[index], |
---|
| 128 | elapsed=elapsed, model= self.model, data=self.data) |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | class CalcCommandline: |
---|
| 133 | def __init__(self, n=20000): |
---|
| 134 | #print thread.get_ident() |
---|
| 135 | from sans.models.CylinderModel import CylinderModel |
---|
| 136 | |
---|
| 137 | model = CylinderModel() |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | print model.runXY([0.01, 0.02]) |
---|
| 141 | |
---|
| 142 | qmax = 0.01 |
---|
| 143 | qstep = 0.0001 |
---|
| 144 | self.done = False |
---|
| 145 | |
---|
| 146 | x = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
| 147 | y = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | calc_thread_2D = Calc2D(x, y, None, model.clone(),-qmax, qmax,qstep, |
---|
| 151 | completefn=self.complete, |
---|
| 152 | updatefn=self.update , |
---|
| 153 | yieldtime=0.0) |
---|
| 154 | |
---|
| 155 | calc_thread_2D.queue() |
---|
| 156 | calc_thread_2D.ready(2.5) |
---|
| 157 | |
---|
| 158 | while not self.done: |
---|
| 159 | time.sleep(1) |
---|
| 160 | |
---|
| 161 | def update(self,output): |
---|
| 162 | print "update" |
---|
| 163 | |
---|
| 164 | def complete(self, image, data, model, elapsed, qmin, qmax, qstep ): |
---|
| 165 | print "complete" |
---|
| 166 | self.done = True |
---|
| 167 | |
---|
| 168 | if __name__ == "__main__": |
---|
| 169 | CalcCommandline() |
---|
| 170 | |
---|