source: sasview/sansview/perspectives/fitting/model_thread.py @ 3f1af74

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 3f1af74 was e9b4cc4, checked in by Gervaise Alina <gervyh@…>, 16 years ago

add thread for fitting

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