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