source: sasview/sansview/perspectives/fitting/model_thread.py @ 4faf4ba

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

sld calculator implemented using elements package

  • Property mode set to 100644
File size: 5.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    def __init__(self, x, y, data,model,qmin, qmax,qstep,
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        self.qmin= qmin
25        self.qmax= qmax
26        self.qstep= qstep
27        # Reshape dimensions of x and y to call evalDistribution
28        self.x_array = numpy.reshape(x,[1,len(x)])
29        self.y_array = numpy.reshape(y,[len(y),1])
30        # Numpy array of dimensions 1 used for model.run method
31        self.x= numpy.array(x)
32        self.y= numpy.array(y)
33        self.data= data
34        # the model on to calculate
35        self.model = model
36        self.starttime = 0 
37       
38    def compute(self):
39        """
40            Compute the data given a model function
41        """
42        self.starttime = time.time()
43        # Determine appropriate q range
44        if self.qmin==None:
45            self.qmin = 0
46        if self.qmax== None:
47            if self.data !=None:
48                newx= math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2)
49                newy= math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2)
50                self.qmax=math.sqrt( newx + newy )
51        # Define matrix where data will be plotted       
52        radius= numpy.sqrt(self.x_array**2 + self.y_array**2)
53        index_data= (self.qmin<= radius)
54        index_model = (self.qmin <= radius)&(radius<= self.qmax)
55       
56        output = numpy.zeros((len(self.x),len(self.y)))
57        try:
58            ## receive only list of 2 numpy array
59            ## One must reshape to vertical and the other to horizontal
60            value = self.model.evalDistribution([self.y_array,self.x_array] )
61            ## for data ignore the qmax
62            if self.data == None:
63                # Only qmin value will be consider for the detector
64                output = value *index_data 
65            else:
66                # The user can define qmin and qmax for the detector
67                output = value*index_model
68        except:
69            raise
70           
71       
72        elapsed = time.time()-self.starttime
73        self.complete( image = output,
74                       data = self.data , 
75                       model = self.model,
76                       elapsed = elapsed,
77                       qmin = self.qmin,
78                       qmax =self.qmax,
79                       qstep = self.qstep )
80       
81   
82   
83
84class Calc1D(CalcThread):
85    """Compute 1D data"""
86   
87    def __init__(self, x, model,
88                 data=None,
89                 qmin=None,
90                 qmax=None,
91                 smearer=None,
92                 completefn = None,
93                 updatefn   = None,
94                 yieldtime  = 0.01,
95                 worktime   = 0.01
96                 ):
97        CalcThread.__init__(self,completefn,
98                 updatefn,
99                 yieldtime,
100                 worktime)
101        self.x = numpy.array(x)
102        self.data= data
103        self.qmin= qmin
104        self.qmax= qmax
105        self.model = model
106        self.smearer= smearer
107        self.starttime = 0
108       
109    def compute(self):
110        """
111            Compute model 1d value given qmin , qmax , x value
112        """
113       
114        self.starttime = time.time()
115        output = numpy.zeros((len(self.x)))
116        index= (self.qmin <= self.x)& (self.x <= self.qmax)
117        output[index] = self.model.evalDistribution(self.x[index])
118     
119        ##smearer the ouput of the plot   
120        if self.smearer!=None:
121            output = self.smearer(output) #Todo: Why always output[0]=0???
122       
123        ######Temp. FIX for Qrange w/ smear. #ToDo: Should not pass all the data to 'run' or 'smear'...
124        #new_index = (self.qmin > self.x) |(self.x > self.qmax)
125        #output[new_index] = None
126               
127        elapsed = time.time()-self.starttime
128       
129        self.complete(x= self.x, y= output, 
130                      elapsed=elapsed, model= self.model, data=self.data)
131       
132 
133               
134class CalcCommandline:
135    def __init__(self, n=20000):
136        #print thread.get_ident()
137        from sans.models.CylinderModel import CylinderModel
138       
139        model = CylinderModel()
140       
141         
142        print model.runXY([0.01, 0.02])
143       
144        qmax = 0.01
145        qstep = 0.0001
146        self.done = False
147       
148        x = numpy.arange(-qmax, qmax+qstep*0.01, qstep)
149        y = numpy.arange(-qmax, qmax+qstep*0.01, qstep)
150   
151   
152        calc_thread_2D = Calc2D(x, y, None, model.clone(),-qmax, qmax,qstep,
153                                        completefn=self.complete,
154                                        updatefn=self.update ,
155                                        yieldtime=0.0)
156     
157        calc_thread_2D.queue()
158        calc_thread_2D.ready(2.5)
159       
160        while not self.done:
161            time.sleep(1)
162
163    def update(self,output):
164        print "update"
165
166    def complete(self, image, data, model, elapsed, qmin, qmax, qstep ):
167        print "complete"
168        self.done = True
169
170if __name__ == "__main__":
171    CalcCommandline()
172   
Note: See TracBrowser for help on using the repository browser.