source: sasview/guiframe/model_thread.py @ dd66fbd

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

status bar changed—-removed error from data remove

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