[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 |
---|
[1b001a7] | 27 | # Reshape dimensions of x and y to call evalDistribution |
---|
| 28 | self.x_array = numpy.reshape(x,[1,len(x)]) |
---|
| 29 | self.y_array = numpy.reshape(y,[len(y),1]) |
---|
| 30 | # Numpy array of dimensions 1 used for model.run method |
---|
| 31 | self.x= numpy.array(x) |
---|
| 32 | self.y= numpy.array(y) |
---|
[c77d859] | 33 | self.data= data |
---|
[1b001a7] | 34 | # the model on to calculate |
---|
[bb18ef1] | 35 | self.model = model |
---|
[904713c] | 36 | self.starttime = 0 |
---|
[bb18ef1] | 37 | |
---|
| 38 | def compute(self): |
---|
| 39 | """ |
---|
| 40 | Compute the data given a model function |
---|
| 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 ) |
---|
[1b001a7] | 51 | # Define matrix where data will be plotted |
---|
| 52 | radius= numpy.sqrt(self.x_array**2 + self.y_array**2) |
---|
| 53 | index_data= (self.qmin<= radius) |
---|
| 54 | index_model = (self.qmin <= radius)&(radius<= self.qmax) |
---|
| 55 | |
---|
[785c8233] | 56 | try: |
---|
| 57 | ## receive only list of 2 numpy array |
---|
| 58 | ## One must reshape to vertical and the other to horizontal |
---|
| 59 | value = self.model.evalDistribution([self.y_array,self.x_array] ) |
---|
| 60 | ## for data ignore the qmax |
---|
| 61 | if self.data == None: |
---|
| 62 | # Only qmin value will be consider for the detector |
---|
| 63 | output = value *index_data |
---|
| 64 | else: |
---|
| 65 | # The user can define qmin and qmax for the detector |
---|
| 66 | output = value*index_model |
---|
| 67 | except: |
---|
| 68 | raise |
---|
| 69 | |
---|
| 70 | |
---|
[1b001a7] | 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 | |
---|
[785c8233] | 80 | |
---|
[bb18ef1] | 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) |
---|
[1b001a7] | 100 | self.x = numpy.array(x) |
---|
[bb18ef1] | 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): |
---|
[c77d859] | 109 | """ |
---|
| 110 | Compute model 1d value given qmin , qmax , x value |
---|
| 111 | """ |
---|
[1b001a7] | 112 | |
---|
[bb18ef1] | 113 | self.starttime = time.time() |
---|
| 114 | |
---|
[785c8233] | 115 | try: |
---|
| 116 | index= (self.qmin <= self.x)& (self.x <= self.qmax) |
---|
| 117 | output = self.model.evalDistribution(self.x[index]) |
---|
| 118 | except: |
---|
| 119 | raise |
---|
| 120 | |
---|
| 121 | |
---|
[fb8daaaf] | 122 | ##smearer the ouput of the plot |
---|
[bb18ef1] | 123 | if self.smearer!=None: |
---|
[3087295] | 124 | output = self.smearer(output) #Todo: Why always output[0]=0??? |
---|
| 125 | |
---|
[f50330b7] | 126 | ######Temp. FIX for Qrange w/ smear. #ToDo: Should not pass all the data to 'run' or 'smear'... |
---|
[1b001a7] | 127 | new_index = (self.qmin > self.x) |(self.x > self.qmax) |
---|
| 128 | output[new_index] = None |
---|
[3087295] | 129 | |
---|
[bb18ef1] | 130 | elapsed = time.time()-self.starttime |
---|
[785c8233] | 131 | |
---|
[c77d859] | 132 | self.complete(x= self.x, y= output, |
---|
| 133 | elapsed=elapsed, model= self.model, data=self.data) |
---|
[bb18ef1] | 134 | |
---|
[785c8233] | 135 | |
---|
[1b001a7] | 136 | |
---|
[bb18ef1] | 137 | class CalcCommandline: |
---|
| 138 | def __init__(self, n=20000): |
---|
| 139 | #print thread.get_ident() |
---|
| 140 | from sans.models.CylinderModel import CylinderModel |
---|
| 141 | |
---|
[904713c] | 142 | model = CylinderModel() |
---|
[bb18ef1] | 143 | |
---|
| 144 | |
---|
| 145 | print model.runXY([0.01, 0.02]) |
---|
| 146 | |
---|
| 147 | qmax = 0.01 |
---|
| 148 | qstep = 0.0001 |
---|
| 149 | self.done = False |
---|
| 150 | |
---|
[904713c] | 151 | x = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
| 152 | y = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
| 153 | |
---|
[bb18ef1] | 154 | |
---|
[904713c] | 155 | calc_thread_2D = Calc2D(x, y, None, model.clone(),-qmax, qmax,qstep, |
---|
[bb18ef1] | 156 | completefn=self.complete, |
---|
| 157 | updatefn=self.update , |
---|
| 158 | yieldtime=0.0) |
---|
| 159 | |
---|
| 160 | calc_thread_2D.queue() |
---|
| 161 | calc_thread_2D.ready(2.5) |
---|
| 162 | |
---|
| 163 | while not self.done: |
---|
| 164 | time.sleep(1) |
---|
| 165 | |
---|
| 166 | def update(self,output): |
---|
| 167 | print "update" |
---|
| 168 | |
---|
[904713c] | 169 | def complete(self, image, data, model, elapsed, qmin, qmax, qstep ): |
---|
[bb18ef1] | 170 | print "complete" |
---|
| 171 | self.done = True |
---|
| 172 | |
---|
| 173 | if __name__ == "__main__": |
---|
| 174 | CalcCommandline() |
---|
| 175 | |
---|