source: sasview/park_integration/AbstractFitEngine.py @ ee5b04c

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

comment added

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