source: sasview/guiframe/model_thread.py @ 858f2ae5

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 858f2ae5 was 858f2ae5, checked in by Gervaise Alina <gervyh@…>, 15 years ago

improve status bar

  • Property mode set to 100644
File size: 11.2 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,qstep,
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.qstep= qstep
108        self.x = x
109        self.y = y
110        self.model = model
111        self.starttime = 0
112        wx.PostEvent(self.parent, StatusEvent(status=\
113                    "Start Drawing model  ",curr_thread=self,type="start"))
114       
115    def isquit(self):
116        try:
117            CalcThread.isquit(self)
118        except KeyboardInterrupt:
119            #printEVT("Calc %s interrupted" % self.model.name)
120            wx.PostEvent(self.parent, StatusEvent(status=\
121                       "Calc %s interrupted" % self.model.name))
122           
123            raise KeyboardInterrupt
124       
125    def update(self, output=None):
126       
127        wx.PostEvent(self.parent, StatusEvent(status="Plot \
128        #updating ... ",curr_thread=self,type="update"))
129       
130       
131    def compute(self):
132        import numpy
133        x = self.x
134        y = self.y
135        output = numpy.zeros((len(x),len(y)))
136     
137        center_x=0
138        center_y=0
139       
140        self.starttime = time.time()
141       
142       
143        lx = len(self.x)
144        wx.PostEvent(self.parent, StatusEvent(status=\
145                       "Computing ",curr_thread=self,type="progress"))
146        for i_x in range(len(self.x)):
147            # Check whether we need to bail out
148            self.update(output=output )
149            self.isquit()
150           
151            for i_y in range(int(len(self.y))):
152                try:
153                    if (self.x[i_x]*self.x[i_x]+self.y[i_y]*self.y[i_y]) \
154                        < self.qmin * self.qmin:
155                       
156                        output[i_x] [i_y]=0   
157                    else:
158                        value = self.model.runXY([self.x[i_x]-center_x, self.y[i_y]-center_y])
159                        output[i_x] [i_y]=value   
160                except:
161                     wx.PostEvent(self.parent, StatusEvent(status=\
162                       "Error computing %s at [%g,%g]" %(self.model.name, self.x[i_x],self.y[i_y])))
163           
164        elapsed = time.time()-self.starttime
165        self.complete(
166                      output=output, elapsed=elapsed,model= self.model,
167                      qmin= self.qmin,
168                      qmax=self.qmax,
169                      qstep=self.qstep)
170
171class Calc2D_4fold(CalcThread):
172    """
173        Compute 2D model
174        This calculation assumes a 4-fold symmetry of the model.
175        Really is the same calculation time since we have to
176        calculate points for 0<phi<pi anyway.
177    """
178   
179    def __init__(self, x, y, model,
180                 completefn = None,
181                 updatefn   = None,
182                 yieldtime  = 0.01,
183                 worktime   = 0.01
184                 ):
185        CalcThread.__init__(self,completefn,
186                 updatefn,
187                 yieldtime,
188                 worktime)
189        self.x = x
190        self.y = y
191        self.model = model
192        self.starttime = 0
193       
194    def isquit(self):
195        try:
196            CalcThread.isquit(self)
197        except KeyboardInterrupt:
198            #printEVT("Calc %s interrupted" % self.model.name)
199            wx.PostEvent(self.parent, StatusEvent(status=\
200                       "Calc %s interrupted" % self.model.name))
201           
202            raise KeyboardInterrupt
203       
204    def compute(self):
205        import numpy
206        x = self.x
207        y = self.y
208        output = numpy.zeros((len(x),len(y)))
209           
210        self.starttime = time.time()
211        lx = len(self.x)
212       
213        for i_x in range(int(len(self.x)/2)):
214            if i_x%2==1:
215                continue
216           
217            # Check whether we need to bail out
218            self.update(output=output)
219            self.isquit()
220               
221            for i_y in range(int(len(self.y)/2)):
222                value1 = self.model.runXY([self.x[i_x], self.y[i_y]])
223                value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]])
224                output[i_y][i_x] = value1 + value2
225                output[lx-i_y-1][lx-i_x-1] = value1 + value2
226                output[lx-i_y-1][i_x] = value1 + value2
227                output[i_y][lx-i_x-1] = value1 + value2
228               
229        if lx%2==1:
230            i_x = int(len(self.x)/2)
231            for i_y in range(int(len(self.y)/2)):
232                value1 = self.model.runXY([self.x[i_x], self.y[i_y]])
233                value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]])
234                output[i_y][i_x] = value1 + value2
235                output[lx-i_y-1][lx-i_x-1] = value1 + value2
236                output[lx-i_y-1][i_x] = value1 + value2
237                output[i_y][lx-i_x-1] = value1 + value2
238               
239        for i_x in range(int(len(self.x)/2)):
240            if not i_x%2==1:
241                continue
242
243            # Check whether we need to bail out
244            self.update(output=output)
245            self.isquit()
246           
247            for i_y in range(int(len(self.y)/2)):
248                value1 = self.model.runXY([self.x[i_x], self.y[i_y]])
249                value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]])
250                output[i_y][i_x] = value1 + value2
251                output[lx-i_y-1][lx-i_x-1] = value1 + value2
252                output[lx-i_y-1][i_x] = value1 + value2
253                output[i_y][lx-i_x-1] = value1 + value2
254           
255        elapsed = time.time()-self.starttime
256        self.complete(output=output, elapsed=elapsed)
257
258
259
260class Calc1D(CalcThread):
261    """Compute 1D data"""
262   
263    def __init__(self, x, model,
264                 completefn = None,
265                 updatefn   = None,
266                 yieldtime  = 0.01,
267                 worktime   = 0.01
268                 ):
269        CalcThread.__init__(self,completefn,
270                 updatefn,
271                 yieldtime,
272                 worktime)
273        self.x = x
274        self.model = model
275        self.starttime = 0
276       
277    def compute(self):
278        import numpy
279        x = self.x
280        output = numpy.zeros(len(x))
281           
282        self.starttime = time.time()
283       
284        for i_x in range(len(self.x)):
285             
286            # Check whether we need to bail out
287            self.isquit()
288               
289            try:
290                value = self.model.run(self.x[i_x])
291                output[i_x] = value
292            except:
293           
294                wx.PostEvent(self.parent, StatusEvent(status=\
295                       "Error computing %s at %g" %(self.model.name, self.x[i_x])))
296           
297        elapsed = time.time()-self.starttime
298        self.complete(output=output, elapsed=elapsed)
299
300class CalcCommandline:
301    def __init__(self, n=20000):
302        #print thread.get_ident()
303        from sans.models.CylinderModel import CylinderModel
304        from sans.models.DisperseModel import DisperseModel
305        import Averager2D
306        import pylab
307       
308        submodel = CylinderModel()
309        #model = Averager2D.Averager2D()
310        #model.set_model(submodel)
311        #model.set_dispersity([['cyl_phi',0.2,10],
312        #                      ['cyl_theta',0.2,10],
313        #                      ['length',10,10],])
314       
315        model = DisperseModel(submodel, ['cyl_phi', 'cyl_theta', 'length'],
316                                        [0.2, 0.2, 10.0])
317        model.setParam('n_pts', 10)
318         
319        print model.runXY([0.01, 0.02])
320       
321        qmax = 0.01
322        qstep = 0.0001
323        self.done = False
324       
325        x = pylab.arange(-qmax, qmax+qstep*0.01, qstep)
326        y = pylab.arange(-qmax, qmax+qstep*0.01, qstep)
327   
328        calc_thread_2D = Calc2D(x, y, model.clone(), 
329                                        completefn=self.complete,
330                                        updatefn=self.update,
331                                        yieldtime=0.0)
332     
333        calc_thread_2D.queue()
334        calc_thread_2D.ready(2.5)
335       
336        while not self.done:
337            time.sleep(1)
338
339    def update(self,output):
340        print "update"
341
342    def complete(self,output, elapsed=0.0):
343        print "complete"
344        self.done = True
345
346if __name__ == "__main__":
347    CalcCommandline()
348   
Note: See TracBrowser for help on using the repository browser.