source: sasview/park_integration/AbstractFitEngine.py @ 94b44293

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

some little change on Abstract fit engine

  • Property mode set to 100644
File size: 5.8 KB
Line 
1
2class FitEngine:
3   
4    def _concatenateData(self, listdata=[]):
5        """ 
6            _concatenateData method concatenates each fields of all data contains ins listdata.
7            @param listdata: list of data
8           
9            @return xtemp, ytemp,dytemp:  x,y,dy respectively of data all combined
10                if xi,yi,dyi of two or more data are the same the second appearance of xi,yi,
11                dyi is ignored in the concatenation.
12               
13            @raise: if listdata is empty  will return None
14            @raise: if data in listdata don't contain dy field ,will create an error
15            during fitting
16        """
17        if listdata==[]:
18            raise ValueError, " data list missing"
19        else:
20            xtemp=[]
21            ytemp=[]
22            dytemp=[]
23               
24            for data in listdata:
25                for i in range(len(data.x)):
26                    xtemp.append(data.x[i])
27                    ytemp.append(data.y[i])
28                    if data.dy is not None and len(data.dy)==len(data.y):   
29                        dytemp.append(data.dy[i])
30                    else:
31                        raise RuntimeError, "Fit._concatenateData: y-errors missinge"
32            return xtemp, ytemp,dytemp
33   
34    def set_model(self,model,name,Uid,pars={}):
35        """
36     
37            Receive a dictionary of parameter and save it Parameter list
38            For scipy.fit use.
39            Set model in a FitArrange object and add that object in a dictionary
40            with key Uid.
41            @param model: model on with parameter values are set
42            @param name: model name
43            @param Uid: unique key corresponding to a fitArrange object with model
44            @param pars: dictionary of paramaters name and value
45            pars={parameter's name: parameter's value}
46           
47        """
48        self.parameters=[]
49        if model==None:
50            raise ValueError, "Cannot set parameters for empty model"
51        else:
52            model.name=name
53            for key, value in pars.iteritems():
54                param = Parameter(model, key, value)
55                self.parameters.append(param)
56       
57        #A fitArrange is already created but contains dList only at Uid
58        if self.fitArrangeList.has_key(Uid):
59            self.fitArrangeList[Uid].set_model(model)
60        else:
61        #no fitArrange object has been create with this Uid
62            fitproblem= FitArrange()
63            fitproblem.set_model(model)
64            self.fitArrangeList[Uid]=fitproblem
65       
66       
67    def set_data(self,data,Uid):
68        """ Receives plottable, creates a list of data to fit,set data
69            in a FitArrange object and adds that object in a dictionary
70            with key Uid.
71            @param data: data added
72            @param Uid: unique key corresponding to a fitArrange object with data
73            """
74        #A fitArrange is already created but contains model only at Uid
75        if self.fitArrangeList.has_key(Uid):
76            self.fitArrangeList[Uid].add_data(data)
77        else:
78        #no fitArrange object has been create with this Uid
79            fitproblem= FitArrange()
80            fitproblem.add_data(data)
81            self.fitArrangeList[Uid]=fitproblem
82           
83    def get_model(self,Uid):
84        """
85            @param Uid: Uid is key in the dictionary containing the model to return
86            @return  a model at this uid or None if no FitArrange element was created
87            with this Uid
88        """
89        if self.fitArrangeList.has_key(Uid):
90            return self.fitArrangeList[Uid].get_model()
91        else:
92            return None
93   
94    def remove_Fit_Problem(self,Uid):
95        """remove   fitarrange in Uid"""
96        if self.fitArrangeList.has_key(Uid):
97            del self.fitArrangeList[Uid]
98           
99     
100   
101   
102class Parameter:
103    """
104        Class to handle model parameters
105    """
106    def __init__(self, model, name, value=None):
107            self.model = model
108            self.name = name
109            if not value==None:
110                self.model.setParam(self.name, value)
111           
112    def set(self, value):
113        """
114            Set the value of the parameter
115        """
116        self.model.setParam(self.name, value)
117
118    def __call__(self):
119        """
120            Return the current value of the parameter
121        """
122        return self.model.getParam(self.name)
123   
124class FitArrange:
125    def __init__(self):
126        """
127            Class FitArrange contains a set of data for a given model
128            to perform the Fit.FitArrange must contain exactly one model
129            and at least one data for the fit to be performed.
130            model: the model selected by the user
131            Ldata: a list of data what the user wants to fit
132           
133        """
134        self.model = None
135        self.dList =[]
136       
137    def set_model(self,model):
138        """
139            set_model save a copy of the model
140            @param model: the model being set
141        """
142        self.model = model
143       
144    def add_data(self,data):
145        """
146            add_data fill a self.dList with data to fit
147            @param data: Data to add in the list 
148        """
149        if not data in self.dList:
150            self.dList.append(data)
151           
152    def get_model(self):
153        """ @return: saved model """
154        return self.model   
155     
156    def get_data(self):
157        """ @return:  list of data dList"""
158        return self.dList
159     
160    def remove_data(self,data):
161        """
162            Remove one element from the list
163            @param data: Data to remove from dList
164        """
165        if data in self.dList:
166            self.dList.remove(data)
167   
168
169
170   
Note: See TracBrowser for help on using the repository browser.