source: sasview/theoryview/perspectives/theory/model_thread.py @ 1a235fd

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 1a235fd was 74755ff, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on documentation theory view

  • Property mode set to 100644
File size: 6.5 KB
Line 
1import time
2from data_util.calcthread import CalcThread
3import sys
4import numpy,math
5
6class Calc2D(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   
15    def __init__(self, x, y, data,model,qmin, qmax,qstep,
16                 completefn = None,
17                 updatefn   = None,
18                 yieldtime  = 0.01,
19                 worktime   = 0.01
20                 ):
21        CalcThread.__init__(self,completefn,
22                 updatefn,
23                 yieldtime,
24                 worktime)
25        self.qmin= qmin
26        self.qmax= qmax
27        self.qstep= qstep
28
29        self.x = x
30        self.y = y
31        self.data= data
32        self.model = model
33        self.starttime = 0 
34       
35    def compute(self):
36        """
37        Compute the data given a model function
38       
39        """
40        self.starttime = time.time()
41        # Determine appropriate q range
42        if self.qmin==None:
43            self.qmin = 0
44        if self.qmax== None:
45            if self.data !=None:
46                newx= math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2)
47                newy= math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2)
48                self.qmax=math.sqrt( newx + newy )
49       
50        if self.data != None:
51            self.I_data = self.data.data
52            self.qx_data = self.data.qx_data
53            self.qy_data = self.data.qy_data
54            self.mask    = self.data.mask
55        else:         
56            xbin =  numpy.linspace(start= -1*self.qmax,
57                                   stop= self.qmax,
58                                   num= self.qstep,
59                                   endpoint=True ) 
60            ybin = numpy.linspace(start= -1*self.qmax,
61                                   stop= self.qmax,
62                                   num= self.qstep,
63                                   endpoint=True )           
64           
65            new_xbin = numpy.tile(xbin, (len(ybin),1))
66            new_ybin = numpy.tile(ybin, (len(xbin),1))
67            new_ybin = new_ybin.swapaxes(0,1)
68            new_xbin = new_xbin.flatten()
69            new_ybin = new_ybin.flatten()
70            self.qy_data = new_ybin
71            self.qx_data = new_xbin
72            # fake data
73            self.I_data = numpy.ones(len(self.qx_data))
74           
75            self.mask = numpy.ones(len(self.qx_data),dtype=bool)
76           
77        # Define matrix where data will be plotted   
78        radius= numpy.sqrt( self.qx_data*self.qx_data + self.qy_data*self.qy_data )
79        index_data= (self.qmin<= radius)&(self.mask)
80       
81        # For theory, qmax is based on 1d qmax
82        # so that must be mulitified by sqrt(2) to get actual max for 2d
83        index_model = ((self.qmin <= radius)&(radius<= self.qmax))
84        self.mask = (index_model)&(self.mask)
85        self.mask = (self.mask)&(numpy.isfinite(self.I_data))
86        if self.data ==None:
87            # Only qmin value will be consider for the detector
88            self.mask = index_data 
89             
90        value = self.model.evalDistribution([self.qx_data[self.mask],self.qy_data[self.mask]] )
91
92        output = numpy.zeros(len(self.mask))
93               
94        # output default is None
95        # This method is to distinguish between masked point and data point = 0.
96        output = output/output
97        # set value for self.mask==True, else still None to Plottools
98        output[self.mask] = value
99
100        elapsed = time.time()-self.starttime
101        self.complete( image = output,
102                       data = self.data , 
103                       model = self.model,
104                       elapsed = elapsed,
105                       qmin = self.qmin,
106                       qmax = self.qmax,
107                       qstep = self.qstep )
108       
109class Calc1D(CalcThread):
110    """
111    Compute 1D data
112   
113    """
114   
115    def __init__(self, x, model,
116                 data=None,
117                 qmin=None,
118                 qmax=None,
119                 smearer=None,
120                 completefn = None,
121                 updatefn   = None,
122                 yieldtime  = 0.01,
123                 worktime   = 0.01
124                 ):
125        CalcThread.__init__(self,completefn,
126                 updatefn,
127                 yieldtime,
128                 worktime)
129        self.x = numpy.array(x)
130        self.data= data
131        self.qmin= qmin
132        self.qmax= qmax
133        self.model = model
134        self.smearer= smearer
135        self.starttime = 0
136       
137    def compute(self):
138        """
139        Compute model 1d value given qmin , qmax , x value
140       
141        """
142       
143        self.starttime = time.time()
144        output = numpy.zeros((len(self.x)))
145        index= (self.qmin <= self.x)& (self.x <= self.qmax)
146     
147        ##smearer the ouput of the plot   
148        if self.smearer!=None:
149            first_bin, last_bin = self.smearer.get_bin_range(self.qmin, self.qmax)
150            output[first_bin:last_bin] = self.model.evalDistribution(self.x[first_bin:last_bin])
151            output = self.smearer(output, first_bin, last_bin) 
152        else:
153            output[index] = self.model.evalDistribution(self.x[index])
154         
155        elapsed = time.time()-self.starttime
156       
157        self.complete(x= self.x[index], y= output[index], 
158                      elapsed=elapsed, model= self.model, data=self.data)
159"""
160Example of use of Calc2D ::
161
162class CalcCommandline:
163   
164    def __init__(self, n=20000):
165        #print thread.get_ident()
166        from sans.models.CylinderModel import CylinderModel
167       
168        model = CylinderModel()
169        print model.runXY([0.01, 0.02])
170       
171        qmax = 0.01
172        qstep = 0.0001
173        self.done = False
174       
175        x = numpy.arange(-qmax, qmax+qstep*0.01, qstep)
176        y = numpy.arange(-qmax, qmax+qstep*0.01, qstep)
177   
178        calc_thread_2D = Calc2D(x, y, None, model.clone(),-qmax, qmax,qstep,
179                                        completefn=self.complete,
180                                        updatefn=self.update ,
181                                        yieldtime=0.0)
182     
183        calc_thread_2D.queue()
184        calc_thread_2D.ready(2.5)
185       
186        while not self.done:
187            time.sleep(1)
188
189    def update(self,output):
190        print "update"
191
192    def complete(self, image, data, model, elapsed, qmin, qmax, qstep ):
193        print "complete"
194        self.done = True
195
196if __name__ == "__main__":
197    CalcCommandline()
198   
199""" 
200
Note: See TracBrowser for help on using the repository browser.