1 | """ |
---|
2 | ScipyFitting module contains FitArrange , ScipyFit, |
---|
3 | Parameter classes.All listed classes work together to perform a |
---|
4 | simple fit with scipy optimizer. |
---|
5 | """ |
---|
6 | import sys |
---|
7 | import copy |
---|
8 | |
---|
9 | import numpy |
---|
10 | |
---|
11 | from sans.fit.AbstractFitEngine import FitEngine |
---|
12 | from sans.fit.AbstractFitEngine import FResult |
---|
13 | |
---|
14 | class SansAssembly: |
---|
15 | """ |
---|
16 | Sans Assembly class a class wrapper to be call in optimizer.leastsq method |
---|
17 | """ |
---|
18 | def __init__(self, paramlist, model=None, data=None, fitresult=None, |
---|
19 | handler=None, curr_thread=None, msg_q=None): |
---|
20 | """ |
---|
21 | :param Model: the model wrapper fro sans -model |
---|
22 | :param Data: the data wrapper for sans data |
---|
23 | |
---|
24 | """ |
---|
25 | self.model = model |
---|
26 | self.data = data |
---|
27 | self.paramlist = paramlist |
---|
28 | self.msg_q = msg_q |
---|
29 | self.curr_thread = curr_thread |
---|
30 | self.handler = handler |
---|
31 | self.fitresult = fitresult |
---|
32 | self.res = [] |
---|
33 | self.true_res = [] |
---|
34 | self.func_name = "Functor" |
---|
35 | self.theory = None |
---|
36 | |
---|
37 | def chisq(self): |
---|
38 | """ |
---|
39 | Calculates chi^2 |
---|
40 | |
---|
41 | :param params: list of parameter values |
---|
42 | |
---|
43 | :return: chi^2 |
---|
44 | |
---|
45 | """ |
---|
46 | total = 0 |
---|
47 | for item in self.true_res: |
---|
48 | total += item * item |
---|
49 | if len(self.true_res) == 0: |
---|
50 | return None |
---|
51 | return total / (len(self.true_res) - len(self.paramlist)) |
---|
52 | |
---|
53 | def __call__(self, params): |
---|
54 | """ |
---|
55 | Compute residuals |
---|
56 | :param params: value of parameters to fit |
---|
57 | """ |
---|
58 | #import thread |
---|
59 | self.model.set_params(self.paramlist, params) |
---|
60 | #print "params", params |
---|
61 | self.true_res, theory = self.data.residuals(self.model.eval) |
---|
62 | self.theory = copy.deepcopy(theory) |
---|
63 | # check parameters range |
---|
64 | if self.check_param_range(): |
---|
65 | # if the param value is outside of the bound |
---|
66 | # just silent return res = inf |
---|
67 | return self.res |
---|
68 | self.res = self.true_res |
---|
69 | |
---|
70 | if self.fitresult is not None: |
---|
71 | self.fitresult.set_model(model=self.model) |
---|
72 | self.fitresult.residuals = self.true_res |
---|
73 | self.fitresult.iterations += 1 |
---|
74 | self.fitresult.theory = theory |
---|
75 | |
---|
76 | #fitness = self.chisq(params=params) |
---|
77 | fitness = self.chisq() |
---|
78 | self.fitresult.pvec = params |
---|
79 | self.fitresult.set_fitness(fitness=fitness) |
---|
80 | if self.msg_q is not None: |
---|
81 | self.msg_q.put(self.fitresult) |
---|
82 | |
---|
83 | if self.handler is not None: |
---|
84 | self.handler.set_result(result=self.fitresult) |
---|
85 | self.handler.update_fit() |
---|
86 | |
---|
87 | if self.curr_thread != None: |
---|
88 | try: |
---|
89 | self.curr_thread.isquit() |
---|
90 | except: |
---|
91 | #msg = "Fitting: Terminated... Note: Forcing to stop " |
---|
92 | #msg += "fitting may cause a 'Functor error message' " |
---|
93 | #msg += "being recorded in the log file....." |
---|
94 | #self.handler.stop(msg) |
---|
95 | raise |
---|
96 | |
---|
97 | return self.res |
---|
98 | |
---|
99 | def check_param_range(self): |
---|
100 | """ |
---|
101 | Check the lower and upper bound of the parameter value |
---|
102 | and set res to the inf if the value is outside of the |
---|
103 | range |
---|
104 | :limitation: the initial values must be within range. |
---|
105 | """ |
---|
106 | |
---|
107 | #time.sleep(0.01) |
---|
108 | is_outofbound = False |
---|
109 | # loop through the fit parameters |
---|
110 | model = self.model.model |
---|
111 | for p in self.paramlist: |
---|
112 | value = model.getParam(p) |
---|
113 | low,high = model.details[p][1:3] |
---|
114 | if low is not None and numpy.isfinite(low): |
---|
115 | if p.value == 0: |
---|
116 | # This value works on Scipy |
---|
117 | # Do not change numbers below |
---|
118 | value = _SMALLVALUE |
---|
119 | # For leastsq, it needs a bit step back from the boundary |
---|
120 | val = low - value * _SMALLVALUE |
---|
121 | if value < val: |
---|
122 | self.res *= 1e+6 |
---|
123 | is_outofbound = True |
---|
124 | break |
---|
125 | if high is not None and numpy.isfinite(high): |
---|
126 | # This value works on Scipy |
---|
127 | # Do not change numbers below |
---|
128 | if value == 0: |
---|
129 | value = _SMALLVALUE |
---|
130 | # For leastsq, it needs a bit step back from the boundary |
---|
131 | val = high + value * _SMALLVALUE |
---|
132 | if value > val: |
---|
133 | self.res *= 1e+6 |
---|
134 | is_outofbound = True |
---|
135 | break |
---|
136 | |
---|
137 | return is_outofbound |
---|
138 | |
---|
139 | class ScipyFit(FitEngine): |
---|
140 | """ |
---|
141 | ScipyFit performs the Fit.This class can be used as follow: |
---|
142 | #Do the fit SCIPY |
---|
143 | create an engine: engine = ScipyFit() |
---|
144 | Use data must be of type plottable |
---|
145 | Use a sans model |
---|
146 | |
---|
147 | Add data with a dictionnary of FitArrangeDict where Uid is a key and data |
---|
148 | is saved in FitArrange object. |
---|
149 | engine.set_data(data,Uid) |
---|
150 | |
---|
151 | Set model parameter "M1"= model.name add {model.parameter.name:value}. |
---|
152 | |
---|
153 | :note: Set_param() if used must always preceded set_model() |
---|
154 | for the fit to be performed.In case of Scipyfit set_param is called in |
---|
155 | fit () automatically. |
---|
156 | |
---|
157 | engine.set_param( model,"M1", {'A':2,'B':4}) |
---|
158 | |
---|
159 | Add model with a dictionnary of FitArrangeDict{} where Uid is a key and model |
---|
160 | is save in FitArrange object. |
---|
161 | engine.set_model(model,Uid) |
---|
162 | |
---|
163 | engine.fit return chisqr,[model.parameter 1,2,..],[[err1....][..err2...]] |
---|
164 | chisqr1, out1, cov1=engine.fit({model.parameter.name:value},qmin,qmax) |
---|
165 | """ |
---|
166 | def __init__(self): |
---|
167 | """ |
---|
168 | Creates a dictionary (self.fit_arrange_dict={})of FitArrange elements |
---|
169 | with Uid as keys |
---|
170 | """ |
---|
171 | FitEngine.__init__(self) |
---|
172 | self.curr_thread = None |
---|
173 | #def fit(self, *args, **kw): |
---|
174 | # return profile(self._fit, *args, **kw) |
---|
175 | |
---|
176 | def fit(self, msg_q=None, |
---|
177 | q=None, handler=None, curr_thread=None, |
---|
178 | ftol=1.49012e-8, reset_flag=False): |
---|
179 | """ |
---|
180 | """ |
---|
181 | fitproblem = [] |
---|
182 | for fproblem in self.fit_arrange_dict.itervalues(): |
---|
183 | if fproblem.get_to_fit() == 1: |
---|
184 | fitproblem.append(fproblem) |
---|
185 | if len(fitproblem) > 1 : |
---|
186 | msg = "Scipy can't fit more than a single fit problem at a time." |
---|
187 | raise RuntimeError, msg |
---|
188 | elif len(fitproblem) == 0 : |
---|
189 | raise RuntimeError, "No Assembly scheduled for Scipy fitting." |
---|
190 | model = fitproblem[0].get_model() |
---|
191 | if reset_flag: |
---|
192 | # reset the initial value; useful for batch |
---|
193 | for name in fitproblem[0].pars: |
---|
194 | ind = fitproblem[0].pars.index(name) |
---|
195 | model.model.setParam(name, fitproblem[0].vals[ind]) |
---|
196 | listdata = [] |
---|
197 | listdata = fitproblem[0].get_data() |
---|
198 | # Concatenate dList set (contains one or more data)before fitting |
---|
199 | data = listdata |
---|
200 | |
---|
201 | self.curr_thread = curr_thread |
---|
202 | ftol = ftol |
---|
203 | |
---|
204 | # Check the initial value if it is within range |
---|
205 | _check_param_range(model.model, self.param_list) |
---|
206 | |
---|
207 | result = FResult(model=model.model, data=data, param_list=self.param_list) |
---|
208 | result.pars = fitproblem[0].pars |
---|
209 | result.fitter_id = self.fitter_id |
---|
210 | if handler is not None: |
---|
211 | handler.set_result(result=result) |
---|
212 | functor = SansAssembly(paramlist=self.param_list, |
---|
213 | model=model, |
---|
214 | data=data, |
---|
215 | handler=handler, |
---|
216 | fitresult=result, |
---|
217 | curr_thread=curr_thread, |
---|
218 | msg_q=msg_q) |
---|
219 | try: |
---|
220 | # This import must be here; otherwise it will be confused when more |
---|
221 | # than one thread exist. |
---|
222 | from scipy import optimize |
---|
223 | |
---|
224 | out, cov_x, _, mesg, success = optimize.leastsq(functor, |
---|
225 | model.get_params(self.param_list), |
---|
226 | ftol=ftol, |
---|
227 | full_output=1) |
---|
228 | except: |
---|
229 | if hasattr(sys, 'last_type') and sys.last_type == KeyboardInterrupt: |
---|
230 | if handler is not None: |
---|
231 | msg = "Fitting: Terminated!!!" |
---|
232 | handler.stop(msg) |
---|
233 | raise KeyboardInterrupt, msg |
---|
234 | else: |
---|
235 | raise |
---|
236 | chisqr = functor.chisq() |
---|
237 | |
---|
238 | if cov_x is not None and numpy.isfinite(cov_x).all(): |
---|
239 | stderr = numpy.sqrt(numpy.diag(cov_x)) |
---|
240 | else: |
---|
241 | stderr = [] |
---|
242 | |
---|
243 | result.index = data.idx |
---|
244 | result.fitness = chisqr |
---|
245 | result.stderr = stderr |
---|
246 | result.pvec = out |
---|
247 | result.success = success |
---|
248 | result.theory = functor.theory |
---|
249 | if handler is not None: |
---|
250 | handler.set_result(result=result) |
---|
251 | handler.update_fit(last=True) |
---|
252 | if q is not None: |
---|
253 | q.put(result) |
---|
254 | return q |
---|
255 | if success < 1 or success > 5: |
---|
256 | result.fitness = None |
---|
257 | return [result] |
---|
258 | |
---|
259 | |
---|
260 | def _check_param_range(model, param_list): |
---|
261 | """ |
---|
262 | Check parameter range and set the initial value inside |
---|
263 | if it is out of range. |
---|
264 | |
---|
265 | : model: park model object |
---|
266 | """ |
---|
267 | # loop through parameterset |
---|
268 | for p in param_list: |
---|
269 | value = model.getParam(p) |
---|
270 | low,high = model.details[p][1:3] |
---|
271 | # if the range was defined, check the range |
---|
272 | if low is not None and value <= low: |
---|
273 | value = low + _get_zero_shift(low) |
---|
274 | if high is not None and value > high: |
---|
275 | value = high - _get_zero_shift(high) |
---|
276 | # Check one more time if the new value goes below |
---|
277 | # the low bound, If so, re-evaluate the value |
---|
278 | # with the mean of the range. |
---|
279 | if low is not None and value < low: |
---|
280 | value = 0.5 * (low+high) |
---|
281 | model.setParam(p, value) |
---|
282 | |
---|
283 | def _get_zero_shift(limit): |
---|
284 | """ |
---|
285 | Get 10% shift of the param value = 0 based on the range value |
---|
286 | |
---|
287 | : param range: min or max value of the bounds |
---|
288 | """ |
---|
289 | return 0.1 * (limit if limit != 0.0 else 1.0) |
---|
290 | |
---|
291 | |
---|
292 | #def profile(fn, *args, **kw): |
---|
293 | # import cProfile, pstats, os |
---|
294 | # global call_result |
---|
295 | # def call(): |
---|
296 | # global call_result |
---|
297 | # call_result = fn(*args, **kw) |
---|
298 | # cProfile.runctx('call()', dict(call=call), {}, 'profile.out') |
---|
299 | # stats = pstats.Stats('profile.out') |
---|
300 | # stats.sort_stats('time') |
---|
301 | # stats.sort_stats('calls') |
---|
302 | # stats.print_stats() |
---|
303 | # os.unlink('profile.out') |
---|
304 | # return call_result |
---|
305 | |
---|
306 | |
---|