source: sasview/sansview/perspectives/fitting/model_thread.py @ 35eeea8

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 35eeea8 was bb18ef1, checked in by Gervaise Alina <gervyh@…>, 16 years ago

some changes of sansview review

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