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