1 | |
---|
2 | class 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 len(pars) >0: |
---|
52 | self.parameters=[] |
---|
53 | if model==None: |
---|
54 | raise ValueError, "AbstractFitEngine: Specify parameters to fit" |
---|
55 | else: |
---|
56 | model.name=name |
---|
57 | for param_name in pars: |
---|
58 | value=model.getParam(param_name) |
---|
59 | if value==None: |
---|
60 | raise ValueError ,"%s has not value set"%param_name |
---|
61 | param = Parameter(model,param_name,value) |
---|
62 | self.parameters.append(param) |
---|
63 | |
---|
64 | self.paramList.append(param_name) |
---|
65 | print "AbstractFitEngine: self.paramList2", self.paramList |
---|
66 | #A fitArrange is already created but contains dList only at Uid |
---|
67 | if self.fitArrangeList.has_key(Uid): |
---|
68 | self.fitArrangeList[Uid].set_model(model) |
---|
69 | else: |
---|
70 | #no fitArrange object has been create with this Uid |
---|
71 | fitproblem= FitArrange() |
---|
72 | fitproblem.set_model(model) |
---|
73 | self.fitArrangeList[Uid]=fitproblem |
---|
74 | else: |
---|
75 | raise ValueError, "park_integration:missing parameters" |
---|
76 | |
---|
77 | |
---|
78 | def set_data(self,data,Uid): |
---|
79 | """ Receives plottable, creates a list of data to fit,set data |
---|
80 | in a FitArrange object and adds that object in a dictionary |
---|
81 | with key Uid. |
---|
82 | @param data: data added |
---|
83 | @param Uid: unique key corresponding to a fitArrange object with data |
---|
84 | """ |
---|
85 | #A fitArrange is already created but contains model only at Uid |
---|
86 | if self.fitArrangeList.has_key(Uid): |
---|
87 | self.fitArrangeList[Uid].add_data(data) |
---|
88 | else: |
---|
89 | #no fitArrange object has been create with this Uid |
---|
90 | fitproblem= FitArrange() |
---|
91 | fitproblem.add_data(data) |
---|
92 | self.fitArrangeList[Uid]=fitproblem |
---|
93 | |
---|
94 | def get_model(self,Uid): |
---|
95 | """ |
---|
96 | @param Uid: Uid is key in the dictionary containing the model to return |
---|
97 | @return a model at this uid or None if no FitArrange element was created |
---|
98 | with this Uid |
---|
99 | """ |
---|
100 | if self.fitArrangeList.has_key(Uid): |
---|
101 | return self.fitArrangeList[Uid].get_model() |
---|
102 | else: |
---|
103 | return None |
---|
104 | |
---|
105 | def remove_Fit_Problem(self,Uid): |
---|
106 | """remove fitarrange in Uid""" |
---|
107 | if self.fitArrangeList.has_key(Uid): |
---|
108 | del self.fitArrangeList[Uid] |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | class Parameter: |
---|
114 | """ |
---|
115 | Class to handle model parameters |
---|
116 | """ |
---|
117 | def __init__(self, model, name, value=None): |
---|
118 | self.model = model |
---|
119 | self.name = name |
---|
120 | if not value==None: |
---|
121 | self.model.setParam(self.name, value) |
---|
122 | |
---|
123 | def set(self, value): |
---|
124 | """ |
---|
125 | Set the value of the parameter |
---|
126 | """ |
---|
127 | self.model.setParam(self.name, value) |
---|
128 | |
---|
129 | def __call__(self): |
---|
130 | """ |
---|
131 | Return the current value of the parameter |
---|
132 | """ |
---|
133 | return self.model.getParam(self.name) |
---|
134 | |
---|
135 | class FitArrange: |
---|
136 | def __init__(self): |
---|
137 | """ |
---|
138 | Class FitArrange contains a set of data for a given model |
---|
139 | to perform the Fit.FitArrange must contain exactly one model |
---|
140 | and at least one data for the fit to be performed. |
---|
141 | model: the model selected by the user |
---|
142 | Ldata: a list of data what the user wants to fit |
---|
143 | |
---|
144 | """ |
---|
145 | self.model = None |
---|
146 | self.dList =[] |
---|
147 | |
---|
148 | def set_model(self,model): |
---|
149 | """ |
---|
150 | set_model save a copy of the model |
---|
151 | @param model: the model being set |
---|
152 | """ |
---|
153 | self.model = model |
---|
154 | |
---|
155 | def add_data(self,data): |
---|
156 | """ |
---|
157 | add_data fill a self.dList with data to fit |
---|
158 | @param data: Data to add in the list |
---|
159 | """ |
---|
160 | if not data in self.dList: |
---|
161 | self.dList.append(data) |
---|
162 | |
---|
163 | def get_model(self): |
---|
164 | """ @return: saved model """ |
---|
165 | return self.model |
---|
166 | |
---|
167 | def get_data(self): |
---|
168 | """ @return: list of data dList""" |
---|
169 | return self.dList |
---|
170 | |
---|
171 | def remove_data(self,data): |
---|
172 | """ |
---|
173 | Remove one element from the list |
---|
174 | @param data: Data to remove from dList |
---|
175 | """ |
---|
176 | if data in self.dList: |
---|
177 | self.dList.remove(data) |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | |
---|