source: sasview/sansview/perspectives/fitting/model_thread.py @ 5b0c2cb

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 5b0c2cb was 5b0c2cb, checked in by Gervaise Alina <gervyh@…>, 15 years ago

plotting range for data2D

  • Property mode set to 100644
File size: 10.1 KB
Line 
1import time
2from calcthread import CalcThread
3import sys
4import numpy,math
5
6class Calc2D_all(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, model,
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       
25        self.x = x
26        self.y = y
27        self.model = model
28        self.starttime = 0
29       
30    def compute(self):
31        x = self.x
32        y = self.y
33        output = numpy.zeros((len(x),len(y)))
34           
35        self.starttime = time.time()
36        lx = len(self.x)
37       
38        #for i_x in range(int(len(self.x)/2)):
39        for i_x in range(len(self.x)):
40            if i_x%2==1:
41                continue
42           
43            # Check whether we need to bail out
44            self.update(output=output)
45            self.isquit()
46               
47            for i_y in range(len(self.y)):
48                value = self.model.runXY([self.x[i_x], self.y[i_y]])
49                output[i_y][i_x] = value
50                #output[lx-i_y-1][lx-i_x-1] = value
51               
52        if lx%2==1:
53            i_x = int(len(self.x)/2)
54            for i_y in range(len(self.y)):
55                value = self.model.runXY([self.x[i_x], self.y[i_y]])
56                output[i_y][i_x] = value
57               
58        #for i_x in range(int(len(self.x)/2)):
59        for i_x in range(len(self.x)):
60            if not i_x%2==1:
61                continue
62
63            # Check whether we need to bail out
64            self.update(output=output)
65            self.isquit()
66           
67            for i_y in range(len(self.y)):
68                value = self.model.runXY([self.x[i_x], self.y[i_y]])
69                output[i_y][i_x] = value
70                #output[lx-i_y-1][lx-i_x-1] = value
71           
72        elapsed = time.time()-self.starttime
73        self.complete(output=output, elapsed=elapsed)
74
75
76class Calc2D(CalcThread):
77    """
78        Compute 2D model
79        This calculation assumes a 2-fold symmetry of the model
80        where points are computed for one half of the detector
81        and I(qx, qy) = I(-qx, -qy) is assumed.
82    """
83   
84    def __init__(self, x, y, data,model,qmin, qmax,qstep,
85                 completefn = None,
86                 updatefn   = None,
87                 yieldtime  = 0.01,
88                 worktime   = 0.01
89                 ):
90        CalcThread.__init__(self,completefn,
91                 updatefn,
92                 yieldtime,
93                 worktime)
94        self.qmin= qmin
95        self.qmax= qmax
96        self.qstep= qstep
97        self.x = x
98        self.y = y
99        self.data= data
100        ## the model on to calculate
101        self.model = model
102        self.starttime = 0 
103       
104    def compute(self):
105        """
106            Compute the data given a model function
107        """
108        x = self.x
109        y = self.y
110        output = numpy.zeros((len(x),len(y)))
111        if self.qmin==None:
112            self.qmin = 0
113        if self.qmax== None:
114            if self.data ==None:
115                return
116            newx= math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2)
117            newy= math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2)
118            self.qmax=math.sqrt( newx + newy )
119       
120        self.starttime = time.time()
121        lx = len(self.x)
122        for i_x in range(len(self.x)):
123            # Check whether we need to bail out
124            self.update(output=output )
125            self.isquit()
126       
127            for i_y in range(int(len(self.y))):
128                radius = math.sqrt(self.x[i_x]*self.x[i_x]+self.y[i_y]*self.y[i_y])
129                ## for data ignore the qmax
130                if self.data == None:
131                    if  self.qmin <= radius :
132                        value = self.model.runXY( [self.x[i_x], self.y[i_y]] )
133                        output[i_y][i_x] =value   
134                    else:
135                        output[i_y][i_x] =0   
136                else: 
137                    if  self.qmin <= radius and radius<= self.qmax:
138                        value = self.model.runXY( [self.x[i_x], self.y[i_y]] )
139                        output[i_y][i_x] =value   
140                    else:
141                        output[i_y][i_x] =0   
142           
143        elapsed = time.time()-self.starttime
144        self.complete( image = output,
145                       data = self.data , 
146                       model = self.model,
147                       elapsed = elapsed,
148                       qmin = self.qmin,
149                       qmax =self.qmax,
150                       qstep = self.qstep )
151
152
153class Calc2D_4fold(CalcThread):
154    """
155        Compute 2D model
156        This calculation assumes a 4-fold symmetry of the model.
157        Really is the same calculation time since we have to
158        calculate points for 0<phi<pi anyway.
159    """
160   
161    def __init__(self, x, y, model,
162                 completefn = None,
163                 updatefn   = None,
164                 yieldtime  = 0.01,
165                 worktime   = 0.01
166                 ):
167        CalcThread.__init__(self,completefn,
168                 updatefn,
169                 yieldtime,
170                 worktime)
171        self.x = x
172        self.y = y
173        self.model = model
174        self.starttime = 0
175       
176    def compute(self):
177        x = self.x
178        y = self.y
179        output = numpy.zeros((len(x),len(y)))
180           
181        self.starttime = time.time()
182        lx = len(self.x)
183       
184        for i_x in range(int(len(self.x)/2)):
185            if i_x%2==1:
186                continue
187           
188            # Check whether we need to bail out
189            self.update(output=output)
190            self.isquit()
191               
192            for i_y in range(int(len(self.y)/2)):
193                value1 = self.model.runXY([self.x[i_x], self.y[i_y]])
194                value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]])
195                output[i_y][i_x] = value1 + value2
196                output[lx-i_y-1][lx-i_x-1] = value1 + value2
197                output[lx-i_y-1][i_x] = value1 + value2
198                output[i_y][lx-i_x-1] = value1 + value2
199               
200        if lx%2==1:
201            i_x = int(len(self.x)/2)
202            for i_y in range(int(len(self.y)/2)):
203                value1 = self.model.runXY([self.x[i_x], self.y[i_y]])
204                value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]])
205                output[i_y][i_x] = value1 + value2
206                output[lx-i_y-1][lx-i_x-1] = value1 + value2
207                output[lx-i_y-1][i_x] = value1 + value2
208                output[i_y][lx-i_x-1] = value1 + value2
209               
210        for i_x in range(int(len(self.x)/2)):
211            if not i_x%2==1:
212                continue
213
214            # Check whether we need to bail out
215            self.update(output=output)
216            self.isquit()
217           
218            for i_y in range(int(len(self.y)/2)):
219                value1 = self.model.runXY([self.x[i_x], self.y[i_y]])
220                value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]])
221                output[i_y][i_x] = value1 + value2
222                output[lx-i_y-1][lx-i_x-1] = value1 + value2
223                output[lx-i_y-1][i_x] = value1 + value2
224                output[i_y][lx-i_x-1] = value1 + value2
225           
226        elapsed = time.time()-self.starttime
227        self.complete(output=output, elapsed=elapsed)
228
229
230
231class Calc1D(CalcThread):
232    """Compute 1D data"""
233   
234    def __init__(self, x, model,
235                 data=None,
236                 qmin=None,
237                 qmax=None,
238                 smearer=None,
239                 completefn = None,
240                 updatefn   = None,
241                 yieldtime  = 0.01,
242                 worktime   = 0.01
243                 ):
244        CalcThread.__init__(self,completefn,
245                 updatefn,
246                 yieldtime,
247                 worktime)
248        self.x = x
249        self.data= data
250        self.qmin= qmin
251        self.qmax= qmax
252        self.model = model
253        self.smearer= smearer
254        self.starttime = 0
255       
256    def compute(self):
257        """
258            Compute model 1d value given qmin , qmax , x value
259        """
260        output = numpy.zeros(len(self.x))
261       
262        self.starttime = time.time()
263       
264        for i_x in range(len(self.x)):
265            self.update(x= self.x, output=output )
266            # Check whether we need to bail out
267            self.isquit()
268            if self.qmin <= self.x[i_x] and self.x[i_x] <= self.qmax:
269                value = self.model.run(self.x[i_x])
270                output[i_x] = value
271               
272        if self.smearer!=None:
273            output = self.smearer(output)
274           
275                     
276        elapsed = time.time()-self.starttime
277        self.complete(x= self.x, y= output, 
278                      elapsed=elapsed, model= self.model, data=self.data)
279       
280       
281
282class CalcCommandline:
283    def __init__(self, n=20000):
284        #print thread.get_ident()
285        from sans.models.CylinderModel import CylinderModel
286       
287        model = CylinderModel()
288       
289         
290        print model.runXY([0.01, 0.02])
291       
292        qmax = 0.01
293        qstep = 0.0001
294        self.done = False
295       
296        x = numpy.arange(-qmax, qmax+qstep*0.01, qstep)
297        y = numpy.arange(-qmax, qmax+qstep*0.01, qstep)
298   
299   
300        calc_thread_2D = Calc2D(x, y, None, model.clone(),-qmax, qmax,qstep,
301                                        completefn=self.complete,
302                                        updatefn=self.update ,
303                                        yieldtime=0.0)
304     
305        calc_thread_2D.queue()
306        calc_thread_2D.ready(2.5)
307       
308        while not self.done:
309            time.sleep(1)
310
311    def update(self,output):
312        print "update"
313
314    def complete(self, image, data, model, elapsed, qmin, qmax, qstep ):
315        print "complete"
316        self.done = True
317
318if __name__ == "__main__":
319    CalcCommandline()
320   
Note: See TracBrowser for help on using the repository browser.