source: sasview/guiframe/model_thread.py @ 8f274b11

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

working on history panel

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