1 | |
---|
2 | import copy |
---|
3 | #import logging |
---|
4 | import sys |
---|
5 | import numpy |
---|
6 | import math |
---|
7 | import park |
---|
8 | from sans.dataloader.data_info import Data1D |
---|
9 | from sans.dataloader.data_info import Data2D |
---|
10 | import time |
---|
11 | _SMALLVALUE = 1.0e-10 |
---|
12 | |
---|
13 | class SansParameter(park.Parameter): |
---|
14 | """ |
---|
15 | SANS model parameters for use in the PARK fitting service. |
---|
16 | The parameter attribute value is redirected to the underlying |
---|
17 | parameter value in the SANS model. |
---|
18 | """ |
---|
19 | def __init__(self, name, model, data): |
---|
20 | """ |
---|
21 | :param name: the name of the model parameter |
---|
22 | :param model: the sans model to wrap as a park model |
---|
23 | |
---|
24 | """ |
---|
25 | park.Parameter.__init__(self, name) |
---|
26 | self._model, self._name = model, name |
---|
27 | self.data = data |
---|
28 | self.model = model |
---|
29 | #set the value for the parameter of the given name |
---|
30 | self.set(model.getParam(name)) |
---|
31 | |
---|
32 | def _getvalue(self): |
---|
33 | """ |
---|
34 | override the _getvalue of park parameter |
---|
35 | |
---|
36 | :return value the parameter associates with self.name |
---|
37 | |
---|
38 | """ |
---|
39 | return self._model.getParam(self.name) |
---|
40 | |
---|
41 | def _setvalue(self, value): |
---|
42 | """ |
---|
43 | override the _setvalue pf park parameter |
---|
44 | |
---|
45 | :param value: the value to set on a given parameter |
---|
46 | |
---|
47 | """ |
---|
48 | self._model.setParam(self.name, value) |
---|
49 | |
---|
50 | value = property(_getvalue, _setvalue) |
---|
51 | |
---|
52 | def _getrange(self): |
---|
53 | """ |
---|
54 | Override _getrange of park parameter |
---|
55 | return the range of parameter |
---|
56 | """ |
---|
57 | #if not self.name in self._model.getDispParamList(): |
---|
58 | lo, hi = self._model.details[self.name][1:3] |
---|
59 | if lo is None: lo = -numpy.inf |
---|
60 | if hi is None: hi = numpy.inf |
---|
61 | #else: |
---|
62 | #lo,hi = self._model.details[self.name][1:] |
---|
63 | #if lo is None: lo = -numpy.inf |
---|
64 | #if hi is None: hi = numpy.inf |
---|
65 | if lo > hi: |
---|
66 | raise ValueError,"wrong fit range for parameters" |
---|
67 | |
---|
68 | return lo, hi |
---|
69 | |
---|
70 | def get_name(self): |
---|
71 | """ |
---|
72 | """ |
---|
73 | return self._getname() |
---|
74 | |
---|
75 | def _setrange(self, r): |
---|
76 | """ |
---|
77 | override _setrange of park parameter |
---|
78 | |
---|
79 | :param r: the value of the range to set |
---|
80 | |
---|
81 | """ |
---|
82 | self._model.details[self.name][1:3] = r |
---|
83 | range = property(_getrange, _setrange) |
---|
84 | |
---|
85 | class Model(park.Model): |
---|
86 | """ |
---|
87 | PARK wrapper for SANS models. |
---|
88 | """ |
---|
89 | def __init__(self, sans_model, sans_data=None, **kw): |
---|
90 | """ |
---|
91 | :param sans_model: the sans model to wrap using park interface |
---|
92 | |
---|
93 | """ |
---|
94 | park.Model.__init__(self, **kw) |
---|
95 | self.model = sans_model |
---|
96 | self.name = sans_model.name |
---|
97 | self.data = sans_data |
---|
98 | #list of parameters names |
---|
99 | self.sansp = sans_model.getParamList() |
---|
100 | #list of park parameter |
---|
101 | self.parkp = [SansParameter(p, sans_model, sans_data) for p in self.sansp] |
---|
102 | #list of parameterset |
---|
103 | self.parameterset = park.ParameterSet(sans_model.name, pars=self.parkp) |
---|
104 | self.pars = [] |
---|
105 | |
---|
106 | def get_params(self, fitparams): |
---|
107 | """ |
---|
108 | return a list of value of paramter to fit |
---|
109 | |
---|
110 | :param fitparams: list of paramaters name to fit |
---|
111 | |
---|
112 | """ |
---|
113 | list_params = [] |
---|
114 | self.pars = [] |
---|
115 | self.pars = fitparams |
---|
116 | for item in fitparams: |
---|
117 | for element in self.parkp: |
---|
118 | if element.name == str(item): |
---|
119 | list_params.append(element.value) |
---|
120 | return list_params |
---|
121 | |
---|
122 | def set_params(self, paramlist, params): |
---|
123 | """ |
---|
124 | Set value for parameters to fit |
---|
125 | |
---|
126 | :param params: list of value for parameters to fit |
---|
127 | |
---|
128 | """ |
---|
129 | try: |
---|
130 | for i in range(len(self.parkp)): |
---|
131 | for j in range(len(paramlist)): |
---|
132 | if self.parkp[i].name == paramlist[j]: |
---|
133 | self.parkp[i].value = params[j] |
---|
134 | self.model.setParam(self.parkp[i].name, params[j]) |
---|
135 | except: |
---|
136 | raise |
---|
137 | |
---|
138 | def eval(self, x): |
---|
139 | """ |
---|
140 | override eval method of park model. |
---|
141 | |
---|
142 | :param x: the x value used to compute a function |
---|
143 | |
---|
144 | """ |
---|
145 | try: |
---|
146 | return self.model.evalDistribution(x) |
---|
147 | except: |
---|
148 | raise |
---|
149 | |
---|
150 | def eval_derivs(self, x, pars=[]): |
---|
151 | """ |
---|
152 | Evaluate the model and derivatives wrt pars at x. |
---|
153 | |
---|
154 | pars is a list of the names of the parameters for which derivatives |
---|
155 | are desired. |
---|
156 | |
---|
157 | This method needs to be specialized in the model to evaluate the |
---|
158 | model function. Alternatively, the model can implement is own |
---|
159 | version of residuals which calculates the residuals directly |
---|
160 | instead of calling eval. |
---|
161 | """ |
---|
162 | return [] |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | class FitData1D(Data1D): |
---|
167 | """ |
---|
168 | Wrapper class for SANS data |
---|
169 | FitData1D inherits from DataLoader.data_info.Data1D. Implements |
---|
170 | a way to get residuals from data. |
---|
171 | """ |
---|
172 | def __init__(self, x, y, dx=None, dy=None, smearer=None, data=None): |
---|
173 | """ |
---|
174 | :param smearer: is an object of class QSmearer or SlitSmearer |
---|
175 | that will smear the theory data (slit smearing or resolution |
---|
176 | smearing) when set. |
---|
177 | |
---|
178 | The proper way to set the smearing object would be to |
---|
179 | do the following: :: |
---|
180 | |
---|
181 | from DataLoader.qsmearing import smear_selection |
---|
182 | smearer = smear_selection(some_data) |
---|
183 | fitdata1d = FitData1D( x= [1,3,..,], |
---|
184 | y= [3,4,..,8], |
---|
185 | dx=None, |
---|
186 | dy=[1,2...], smearer= smearer) |
---|
187 | |
---|
188 | :Note: that some_data _HAS_ to be of class DataLoader.data_info.Data1D |
---|
189 | Setting it back to None will turn smearing off. |
---|
190 | |
---|
191 | """ |
---|
192 | Data1D.__init__(self, x=x, y=y, dx=dx, dy=dy) |
---|
193 | self.sans_data = data |
---|
194 | self.smearer = smearer |
---|
195 | self._first_unsmeared_bin = None |
---|
196 | self._last_unsmeared_bin = None |
---|
197 | # Check error bar; if no error bar found, set it constant(=1) |
---|
198 | # TODO: Should provide an option for users to set it like percent, |
---|
199 | # constant, or dy data |
---|
200 | if dy == None or dy == [] or dy.all() == 0: |
---|
201 | self.dy = numpy.ones(len(y)) |
---|
202 | else: |
---|
203 | self.dy = numpy.asarray(dy).copy() |
---|
204 | |
---|
205 | ## Min Q-value |
---|
206 | #Skip the Q=0 point, especially when y(q=0)=None at x[0]. |
---|
207 | if min (self.x) == 0.0 and self.x[0] == 0 and\ |
---|
208 | not numpy.isfinite(self.y[0]): |
---|
209 | self.qmin = min(self.x[self.x!=0]) |
---|
210 | else: |
---|
211 | self.qmin = min(self.x) |
---|
212 | ## Max Q-value |
---|
213 | self.qmax = max(self.x) |
---|
214 | |
---|
215 | # Range used for input to smearing |
---|
216 | self._qmin_unsmeared = self.qmin |
---|
217 | self._qmax_unsmeared = self.qmax |
---|
218 | # Identify the bin range for the unsmeared and smeared spaces |
---|
219 | self.idx = (self.x >= self.qmin) & (self.x <= self.qmax) |
---|
220 | self.idx_unsmeared = (self.x >= self._qmin_unsmeared) \ |
---|
221 | & (self.x <= self._qmax_unsmeared) |
---|
222 | |
---|
223 | def set_fit_range(self, qmin=None, qmax=None): |
---|
224 | """ to set the fit range""" |
---|
225 | # Skip Q=0 point, (especially for y(q=0)=None at x[0]). |
---|
226 | # ToDo: Find better way to do it. |
---|
227 | if qmin == 0.0 and not numpy.isfinite(self.y[qmin]): |
---|
228 | self.qmin = min(self.x[self.x != 0]) |
---|
229 | elif qmin != None: |
---|
230 | self.qmin = qmin |
---|
231 | if qmax != None: |
---|
232 | self.qmax = qmax |
---|
233 | # Determine the range needed in unsmeared-Q to cover |
---|
234 | # the smeared Q range |
---|
235 | self._qmin_unsmeared = self.qmin |
---|
236 | self._qmax_unsmeared = self.qmax |
---|
237 | |
---|
238 | self._first_unsmeared_bin = 0 |
---|
239 | self._last_unsmeared_bin = len(self.x) - 1 |
---|
240 | |
---|
241 | if self.smearer != None: |
---|
242 | self._first_unsmeared_bin, self._last_unsmeared_bin = \ |
---|
243 | self.smearer.get_bin_range(self.qmin, self.qmax) |
---|
244 | self._qmin_unsmeared = self.x[self._first_unsmeared_bin] |
---|
245 | self._qmax_unsmeared = self.x[self._last_unsmeared_bin] |
---|
246 | |
---|
247 | # Identify the bin range for the unsmeared and smeared spaces |
---|
248 | self.idx = (self.x >= self.qmin) & (self.x <= self.qmax) |
---|
249 | ## zero error can not participate for fitting |
---|
250 | self.idx = self.idx & (self.dy != 0) |
---|
251 | self.idx_unsmeared = (self.x >= self._qmin_unsmeared) \ |
---|
252 | & (self.x <= self._qmax_unsmeared) |
---|
253 | |
---|
254 | def get_fit_range(self): |
---|
255 | """ |
---|
256 | return the range of data.x to fit |
---|
257 | """ |
---|
258 | return self.qmin, self.qmax |
---|
259 | |
---|
260 | def residuals(self, fn): |
---|
261 | """ |
---|
262 | Compute residuals. |
---|
263 | |
---|
264 | If self.smearer has been set, use if to smear |
---|
265 | the data before computing chi squared. |
---|
266 | |
---|
267 | :param fn: function that return model value |
---|
268 | |
---|
269 | :return: residuals |
---|
270 | |
---|
271 | """ |
---|
272 | # Compute theory data f(x) |
---|
273 | fx = numpy.zeros(len(self.x)) |
---|
274 | fx[self.idx_unsmeared] = fn(self.x[self.idx_unsmeared]) |
---|
275 | |
---|
276 | ## Smear theory data |
---|
277 | if self.smearer is not None: |
---|
278 | fx = self.smearer(fx, self._first_unsmeared_bin, |
---|
279 | self._last_unsmeared_bin) |
---|
280 | ## Sanity check |
---|
281 | if numpy.size(self.dy) != numpy.size(fx): |
---|
282 | msg = "FitData1D: invalid error array " |
---|
283 | msg += "%d <> %d" % (numpy.shape(self.dy), numpy.size(fx)) |
---|
284 | raise RuntimeError, msg |
---|
285 | return (self.y[self.idx] - fx[self.idx]) / self.dy[self.idx], fx[self.idx] |
---|
286 | |
---|
287 | |
---|
288 | def residuals_deriv(self, model, pars=[]): |
---|
289 | """ |
---|
290 | :return: residuals derivatives . |
---|
291 | |
---|
292 | :note: in this case just return empty array |
---|
293 | |
---|
294 | """ |
---|
295 | return [] |
---|
296 | |
---|
297 | class FitData2D(Data2D): |
---|
298 | """ Wrapper class for SANS data """ |
---|
299 | def __init__(self, sans_data2d, data=None, err_data=None): |
---|
300 | Data2D.__init__(self, data=data, err_data=err_data) |
---|
301 | """ |
---|
302 | Data can be initital with a data (sans plottable) |
---|
303 | or with vectors. |
---|
304 | """ |
---|
305 | self.res_err_image = [] |
---|
306 | self.idx = [] |
---|
307 | self.qmin = None |
---|
308 | self.qmax = None |
---|
309 | self.smearer = None |
---|
310 | self.radius = 0 |
---|
311 | self.res_err_data = [] |
---|
312 | self.sans_data = sans_data2d |
---|
313 | self.set_data(sans_data2d) |
---|
314 | |
---|
315 | def set_data(self, sans_data2d, qmin=None, qmax=None): |
---|
316 | """ |
---|
317 | Determine the correct qx_data and qy_data within range to fit |
---|
318 | """ |
---|
319 | self.data = sans_data2d.data |
---|
320 | self.err_data = sans_data2d.err_data |
---|
321 | self.qx_data = sans_data2d.qx_data |
---|
322 | self.qy_data = sans_data2d.qy_data |
---|
323 | self.mask = sans_data2d.mask |
---|
324 | |
---|
325 | x_max = max(math.fabs(sans_data2d.xmin), math.fabs(sans_data2d.xmax)) |
---|
326 | y_max = max(math.fabs(sans_data2d.ymin), math.fabs(sans_data2d.ymax)) |
---|
327 | |
---|
328 | ## fitting range |
---|
329 | if qmin == None: |
---|
330 | self.qmin = 1e-16 |
---|
331 | if qmax == None: |
---|
332 | self.qmax = math.sqrt(x_max * x_max + y_max * y_max) |
---|
333 | ## new error image for fitting purpose |
---|
334 | if self.err_data == None or self.err_data == []: |
---|
335 | self.res_err_data = numpy.ones(len(self.data)) |
---|
336 | else: |
---|
337 | self.res_err_data = copy.deepcopy(self.err_data) |
---|
338 | #self.res_err_data[self.res_err_data==0]=1 |
---|
339 | |
---|
340 | self.radius = numpy.sqrt(self.qx_data**2 + self.qy_data**2) |
---|
341 | |
---|
342 | # Note: mask = True: for MASK while mask = False for NOT to mask |
---|
343 | self.idx = ((self.qmin <= self.radius)&\ |
---|
344 | (self.radius <= self.qmax)) |
---|
345 | self.idx = (self.idx) & (self.mask) |
---|
346 | self.idx = (self.idx) & (numpy.isfinite(self.data)) |
---|
347 | |
---|
348 | def set_smearer(self, smearer): |
---|
349 | """ |
---|
350 | Set smearer |
---|
351 | """ |
---|
352 | if smearer == None: |
---|
353 | return |
---|
354 | self.smearer = smearer |
---|
355 | self.smearer.set_index(self.idx) |
---|
356 | self.smearer.get_data() |
---|
357 | |
---|
358 | def set_fit_range(self, qmin=None, qmax=None): |
---|
359 | """ to set the fit range""" |
---|
360 | if qmin == 0.0: |
---|
361 | self.qmin = 1e-16 |
---|
362 | elif qmin != None: |
---|
363 | self.qmin = qmin |
---|
364 | if qmax != None: |
---|
365 | self.qmax = qmax |
---|
366 | self.radius = numpy.sqrt(self.qx_data**2 + self.qy_data**2) |
---|
367 | self.index_model = ((self.qmin <= self.radius)&\ |
---|
368 | (self.radius <= self.qmax)) |
---|
369 | self.idx = (self.idx) &(self.mask) |
---|
370 | self.idx = (self.idx) & (numpy.isfinite(self.data)) |
---|
371 | self.idx = (self.idx) & (self.res_err_data != 0) |
---|
372 | |
---|
373 | def get_fit_range(self): |
---|
374 | """ |
---|
375 | return the range of data.x to fit |
---|
376 | """ |
---|
377 | return self.qmin, self.qmax |
---|
378 | |
---|
379 | def residuals(self, fn): |
---|
380 | """ |
---|
381 | return the residuals |
---|
382 | """ |
---|
383 | if self.smearer != None: |
---|
384 | fn.set_index(self.idx) |
---|
385 | # Get necessary data from self.data and set the data for smearing |
---|
386 | fn.get_data() |
---|
387 | |
---|
388 | gn = fn.get_value() |
---|
389 | else: |
---|
390 | gn = fn([self.qx_data[self.idx], |
---|
391 | self.qy_data[self.idx]]) |
---|
392 | # use only the data point within ROI range |
---|
393 | res = (self.data[self.idx] - gn)/self.res_err_data[self.idx] |
---|
394 | return res, gn |
---|
395 | |
---|
396 | def residuals_deriv(self, model, pars=[]): |
---|
397 | """ |
---|
398 | :return: residuals derivatives . |
---|
399 | |
---|
400 | :note: in this case just return empty array |
---|
401 | |
---|
402 | """ |
---|
403 | return [] |
---|
404 | |
---|
405 | class FitAbort(Exception): |
---|
406 | """ |
---|
407 | Exception raise to stop the fit |
---|
408 | """ |
---|
409 | #pass |
---|
410 | #print"Creating fit abort Exception" |
---|
411 | |
---|
412 | |
---|
413 | class SansAssembly: |
---|
414 | """ |
---|
415 | Sans Assembly class a class wrapper to be call in optimizer.leastsq method |
---|
416 | """ |
---|
417 | def __init__(self, paramlist, model=None , data=None, fitresult=None, |
---|
418 | handler=None, curr_thread=None, msg_q=None): |
---|
419 | """ |
---|
420 | :param Model: the model wrapper fro sans -model |
---|
421 | :param Data: the data wrapper for sans data |
---|
422 | |
---|
423 | """ |
---|
424 | self.model = model |
---|
425 | self.data = data |
---|
426 | self.paramlist = paramlist |
---|
427 | self.msg_q = msg_q |
---|
428 | self.curr_thread = curr_thread |
---|
429 | self.handler = handler |
---|
430 | self.fitresult = fitresult |
---|
431 | self.res = [] |
---|
432 | self.true_res = [] |
---|
433 | self.func_name = "Functor" |
---|
434 | self.theory = None |
---|
435 | |
---|
436 | #def chisq(self, params): |
---|
437 | def chisq(self): |
---|
438 | """ |
---|
439 | Calculates chi^2 |
---|
440 | |
---|
441 | :param params: list of parameter values |
---|
442 | |
---|
443 | :return: chi^2 |
---|
444 | |
---|
445 | """ |
---|
446 | sum = 0 |
---|
447 | for item in self.true_res: |
---|
448 | sum += item * item |
---|
449 | if len(self.true_res) == 0: |
---|
450 | return None |
---|
451 | return sum / len(self.true_res) |
---|
452 | |
---|
453 | def __call__(self, params): |
---|
454 | """ |
---|
455 | Compute residuals |
---|
456 | |
---|
457 | :param params: value of parameters to fit |
---|
458 | |
---|
459 | """ |
---|
460 | #import thread |
---|
461 | self.model.set_params(self.paramlist, params) |
---|
462 | self.true_res, theory = self.data.residuals(self.model.eval) |
---|
463 | self.theory = copy.deepcopy(theory) |
---|
464 | # check parameters range |
---|
465 | if self.check_param_range(): |
---|
466 | # if the param value is outside of the bound |
---|
467 | # just silent return res = inf |
---|
468 | return self.res |
---|
469 | self.res = self.true_res |
---|
470 | |
---|
471 | if self.fitresult is not None: |
---|
472 | self.fitresult.set_model(model=self.model) |
---|
473 | self.fitresult.residuals = self.true_res |
---|
474 | self.fitresult.theory = theory |
---|
475 | |
---|
476 | #fitness = self.chisq(params=params) |
---|
477 | fitness = self.chisq() |
---|
478 | self.fitresult.pvec = params |
---|
479 | self.fitresult.set_fitness(fitness=fitness) |
---|
480 | if self.msg_q is not None: |
---|
481 | self.msg_q.put(self.fitresult) |
---|
482 | |
---|
483 | if self.handler is not None: |
---|
484 | self.handler.set_result(result=self.fitresult) |
---|
485 | self.handler.update_fit() |
---|
486 | |
---|
487 | if self.curr_thread != None : |
---|
488 | try: |
---|
489 | self.curr_thread.isquit() |
---|
490 | except: |
---|
491 | msg = "Fitting: Terminated... Note: Forcing to stop " |
---|
492 | msg += "fitting may cause a 'Functor error message' " |
---|
493 | msg += "being recorded in the log file....." |
---|
494 | self.handler.error(msg) |
---|
495 | raise |
---|
496 | #return |
---|
497 | |
---|
498 | return self.res |
---|
499 | |
---|
500 | def check_param_range(self): |
---|
501 | """ |
---|
502 | Check the lower and upper bound of the parameter value |
---|
503 | and set res to the inf if the value is outside of the |
---|
504 | range |
---|
505 | :limitation: the initial values must be within range. |
---|
506 | """ |
---|
507 | |
---|
508 | #time.sleep(0.01) |
---|
509 | is_outofbound = False |
---|
510 | # loop through the fit parameters |
---|
511 | for p in self.model.parameterset: |
---|
512 | param_name = p.get_name() |
---|
513 | if param_name in self.paramlist: |
---|
514 | |
---|
515 | # if the range was defined, check the range |
---|
516 | if numpy.isfinite(p.range[0]): |
---|
517 | if p.value == 0: |
---|
518 | # This value works on Scipy |
---|
519 | # Do not change numbers below |
---|
520 | value = _SMALLVALUE |
---|
521 | else: |
---|
522 | value = p.value |
---|
523 | # For leastsq, it needs a bit step back from the boundary |
---|
524 | val = p.range[0] - value * _SMALLVALUE |
---|
525 | if p.value < val: |
---|
526 | self.res *= 1e+6 |
---|
527 | |
---|
528 | is_outofbound = True |
---|
529 | break |
---|
530 | if numpy.isfinite(p.range[1]): |
---|
531 | # This value works on Scipy |
---|
532 | # Do not change numbers below |
---|
533 | if p.value == 0: |
---|
534 | value = _SMALLVALUE |
---|
535 | else: |
---|
536 | value = p.value |
---|
537 | # For leastsq, it needs a bit step back from the boundary |
---|
538 | val = p.range[1] + value * _SMALLVALUE |
---|
539 | if p.value > val: |
---|
540 | self.res *= 1e+6 |
---|
541 | is_outofbound = True |
---|
542 | break |
---|
543 | |
---|
544 | return is_outofbound |
---|
545 | |
---|
546 | |
---|
547 | class FitEngine: |
---|
548 | def __init__(self): |
---|
549 | """ |
---|
550 | Base class for scipy and park fit engine |
---|
551 | """ |
---|
552 | #List of parameter names to fit |
---|
553 | self.param_list = [] |
---|
554 | #Dictionnary of fitArrange element (fit problems) |
---|
555 | self.fit_arrange_dict = {} |
---|
556 | |
---|
557 | def set_model(self, model, id, pars=[], constraints=[], data=None): |
---|
558 | """ |
---|
559 | set a model on a given in the fit engine. |
---|
560 | |
---|
561 | :param model: sans.models type |
---|
562 | :param : is the key of the fitArrange dictionary where model is |
---|
563 | saved as a value |
---|
564 | :param pars: the list of parameters to fit |
---|
565 | :param constraints: list of |
---|
566 | tuple (name of parameter, value of parameters) |
---|
567 | the value of parameter must be a string to constraint 2 different |
---|
568 | parameters. |
---|
569 | Example: |
---|
570 | we want to fit 2 model M1 and M2 both have parameters A and B. |
---|
571 | constraints can be: |
---|
572 | constraints = [(M1.A, M2.B+2), (M1.B= M2.A *5),...,] |
---|
573 | |
---|
574 | |
---|
575 | :note: pars must contains only name of existing model's parameters |
---|
576 | |
---|
577 | """ |
---|
578 | if model == None: |
---|
579 | raise ValueError, "AbstractFitEngine: Need to set model to fit" |
---|
580 | |
---|
581 | new_model = model |
---|
582 | if not issubclass(model.__class__, Model): |
---|
583 | new_model = Model(model, data) |
---|
584 | |
---|
585 | if len(constraints) > 0: |
---|
586 | for constraint in constraints: |
---|
587 | name, value = constraint |
---|
588 | try: |
---|
589 | new_model.parameterset[str(name)].set(str(value)) |
---|
590 | except: |
---|
591 | msg = "Fit Engine: Error occurs when setting the constraint" |
---|
592 | msg += " %s for parameter %s " % (value, name) |
---|
593 | raise ValueError, msg |
---|
594 | |
---|
595 | if len(pars) > 0: |
---|
596 | temp = [] |
---|
597 | for item in pars: |
---|
598 | if item in new_model.model.getParamList(): |
---|
599 | temp.append(item) |
---|
600 | self.param_list.append(item) |
---|
601 | else: |
---|
602 | |
---|
603 | msg = "wrong parameter %s used" % str(item) |
---|
604 | msg += "to set model %s. Choose" % str(new_model.model.name) |
---|
605 | msg += "parameter name within %s" % \ |
---|
606 | str(new_model.model.getParamList()) |
---|
607 | raise ValueError, msg |
---|
608 | |
---|
609 | #A fitArrange is already created but contains data_list only at id |
---|
610 | if self.fit_arrange_dict.has_key(id): |
---|
611 | self.fit_arrange_dict[id].set_model(new_model) |
---|
612 | self.fit_arrange_dict[id].pars = pars |
---|
613 | else: |
---|
614 | #no fitArrange object has been create with this id |
---|
615 | fitproblem = FitArrange() |
---|
616 | fitproblem.set_model(new_model) |
---|
617 | fitproblem.pars = pars |
---|
618 | self.fit_arrange_dict[id] = fitproblem |
---|
619 | vals = [] |
---|
620 | for name in pars: |
---|
621 | vals.append(new_model.model.getParam(name)) |
---|
622 | self.fit_arrange_dict[id].vals = vals |
---|
623 | else: |
---|
624 | raise ValueError, "park_integration:missing parameters" |
---|
625 | |
---|
626 | def set_data(self, data, id, smearer=None, qmin=None, qmax=None): |
---|
627 | """ |
---|
628 | Receives plottable, creates a list of data to fit,set data |
---|
629 | in a FitArrange object and adds that object in a dictionary |
---|
630 | with key id. |
---|
631 | |
---|
632 | :param data: data added |
---|
633 | :param id: unique key corresponding to a fitArrange object with data |
---|
634 | |
---|
635 | """ |
---|
636 | if data.__class__.__name__ == 'Data2D': |
---|
637 | fitdata = FitData2D(sans_data2d=data, data=data.data, |
---|
638 | err_data=data.err_data) |
---|
639 | else: |
---|
640 | fitdata = FitData1D(x=data.x, y=data.y , |
---|
641 | dx=data.dx, dy=data.dy, smearer=smearer) |
---|
642 | fitdata.sans_data = data |
---|
643 | |
---|
644 | fitdata.set_fit_range(qmin=qmin, qmax=qmax) |
---|
645 | #A fitArrange is already created but contains model only at id |
---|
646 | if self.fit_arrange_dict.has_key(id): |
---|
647 | self.fit_arrange_dict[id].add_data(fitdata) |
---|
648 | else: |
---|
649 | #no fitArrange object has been create with this id |
---|
650 | fitproblem = FitArrange() |
---|
651 | fitproblem.add_data(fitdata) |
---|
652 | self.fit_arrange_dict[id] = fitproblem |
---|
653 | |
---|
654 | def get_model(self, id): |
---|
655 | """ |
---|
656 | |
---|
657 | :param id: id is key in the dictionary containing the model to return |
---|
658 | |
---|
659 | :return: a model at this id or None if no FitArrange element was |
---|
660 | created with this id |
---|
661 | |
---|
662 | """ |
---|
663 | if self.fit_arrange_dict.has_key(id): |
---|
664 | return self.fit_arrange_dict[id].get_model() |
---|
665 | else: |
---|
666 | return None |
---|
667 | |
---|
668 | def remove_fit_problem(self, id): |
---|
669 | """remove fitarrange in id""" |
---|
670 | if self.fit_arrange_dict.has_key(id): |
---|
671 | del self.fit_arrange_dict[id] |
---|
672 | |
---|
673 | def select_problem_for_fit(self, id, value): |
---|
674 | """ |
---|
675 | select a couple of model and data at the id position in dictionary |
---|
676 | and set in self.selected value to value |
---|
677 | |
---|
678 | :param value: the value to allow fitting. |
---|
679 | can only have the value one or zero |
---|
680 | |
---|
681 | """ |
---|
682 | if self.fit_arrange_dict.has_key(id): |
---|
683 | self.fit_arrange_dict[id].set_to_fit(value) |
---|
684 | |
---|
685 | def get_problem_to_fit(self, id): |
---|
686 | """ |
---|
687 | return the self.selected value of the fit problem of id |
---|
688 | |
---|
689 | :param id: the id of the problem |
---|
690 | |
---|
691 | """ |
---|
692 | if self.fit_arrange_dict.has_key(id): |
---|
693 | self.fit_arrange_dict[id].get_to_fit() |
---|
694 | |
---|
695 | class FitArrange: |
---|
696 | def __init__(self): |
---|
697 | """ |
---|
698 | Class FitArrange contains a set of data for a given model |
---|
699 | to perform the Fit.FitArrange must contain exactly one model |
---|
700 | and at least one data for the fit to be performed. |
---|
701 | |
---|
702 | model: the model selected by the user |
---|
703 | Ldata: a list of data what the user wants to fit |
---|
704 | |
---|
705 | """ |
---|
706 | self.model = None |
---|
707 | self.data_list = [] |
---|
708 | self.pars = [] |
---|
709 | self.vals = [] |
---|
710 | #self.selected is zero when this fit problem is not schedule to fit |
---|
711 | #self.selected is 1 when schedule to fit |
---|
712 | self.selected = 0 |
---|
713 | |
---|
714 | def set_model(self, model): |
---|
715 | """ |
---|
716 | set_model save a copy of the model |
---|
717 | |
---|
718 | :param model: the model being set |
---|
719 | |
---|
720 | """ |
---|
721 | self.model = model |
---|
722 | |
---|
723 | def add_data(self, data): |
---|
724 | """ |
---|
725 | add_data fill a self.data_list with data to fit |
---|
726 | |
---|
727 | :param data: Data to add in the list |
---|
728 | |
---|
729 | """ |
---|
730 | if not data in self.data_list: |
---|
731 | self.data_list.append(data) |
---|
732 | |
---|
733 | def get_model(self): |
---|
734 | """ |
---|
735 | |
---|
736 | :return: saved model |
---|
737 | |
---|
738 | """ |
---|
739 | return self.model |
---|
740 | |
---|
741 | def get_data(self): |
---|
742 | """ |
---|
743 | |
---|
744 | :return: list of data data_list |
---|
745 | |
---|
746 | """ |
---|
747 | #return self.data_list |
---|
748 | return self.data_list[0] |
---|
749 | |
---|
750 | def remove_data(self, data): |
---|
751 | """ |
---|
752 | Remove one element from the list |
---|
753 | |
---|
754 | :param data: Data to remove from data_list |
---|
755 | |
---|
756 | """ |
---|
757 | if data in self.data_list: |
---|
758 | self.data_list.remove(data) |
---|
759 | |
---|
760 | def set_to_fit (self, value=0): |
---|
761 | """ |
---|
762 | set self.selected to 0 or 1 for other values raise an exception |
---|
763 | |
---|
764 | :param value: integer between 0 or 1 |
---|
765 | |
---|
766 | """ |
---|
767 | self.selected = value |
---|
768 | |
---|
769 | def get_to_fit(self): |
---|
770 | """ |
---|
771 | return self.selected value |
---|
772 | """ |
---|
773 | return self.selected |
---|
774 | |
---|
775 | |
---|
776 | IS_MAC = True |
---|
777 | if sys.platform.count("win32") > 0: |
---|
778 | IS_MAC = False |
---|
779 | |
---|
780 | class FResult(object): |
---|
781 | """ |
---|
782 | Storing fit result |
---|
783 | """ |
---|
784 | def __init__(self, model=None, param_list=None, data=None): |
---|
785 | self.calls = None |
---|
786 | self.fitness = None |
---|
787 | self.chisqr = None |
---|
788 | self.pvec = [] |
---|
789 | self.cov = [] |
---|
790 | self.info = None |
---|
791 | self.mesg = None |
---|
792 | self.success = None |
---|
793 | self.stderr = None |
---|
794 | self.residuals = [] |
---|
795 | self.index = [] |
---|
796 | self.parameters = None |
---|
797 | self.is_mac = IS_MAC |
---|
798 | self.model = model |
---|
799 | self.data = data |
---|
800 | self.theory = [] |
---|
801 | self.param_list = param_list |
---|
802 | self.iterations = 0 |
---|
803 | self.inputs = [] |
---|
804 | if self.model is not None and self.data is not None: |
---|
805 | self.inputs = [(self.model, self.data)] |
---|
806 | |
---|
807 | def set_model(self, model): |
---|
808 | """ |
---|
809 | """ |
---|
810 | self.model = model |
---|
811 | |
---|
812 | def set_fitness(self, fitness): |
---|
813 | """ |
---|
814 | """ |
---|
815 | self.fitness = fitness |
---|
816 | |
---|
817 | def __str__(self): |
---|
818 | """ |
---|
819 | """ |
---|
820 | if self.pvec == None and self.model is None and self.param_list is None: |
---|
821 | return "No results" |
---|
822 | n = len(self.model.parameterset) |
---|
823 | self.iterations += 1 |
---|
824 | result_param = zip(xrange(n), self.model.parameterset) |
---|
825 | msg1 = ["[Iteration #: %s ]" % self.iterations] |
---|
826 | msg3 = ["=== goodness of fit: %s ===" % (str(self.fitness))] |
---|
827 | if not self.is_mac: |
---|
828 | msg2 = ["P%-3d %s......|.....%s" % \ |
---|
829 | (p[0], p[1], p[1].value)\ |
---|
830 | for p in result_param if p[1].name in self.param_list] |
---|
831 | msg = msg1 + msg3 + msg2 |
---|
832 | else: |
---|
833 | msg = msg1 + msg3 |
---|
834 | msg = "\n".join(msg) |
---|
835 | return msg |
---|
836 | |
---|
837 | def print_summary(self): |
---|
838 | """ |
---|
839 | """ |
---|
840 | print self |
---|