source: sasview/sansview/perspectives/fitting/model_thread.py @ 92c2345

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 92c2345 was db709e4, checked in by Gervaise Alina <gervyh@…>, 16 years ago

model draw —> parameter changes fixed to redraw model

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