1 | ################################################################################ |
---|
2 | #This software was developed by the University of Tennessee as part of the |
---|
3 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
4 | #project funded by the US National Science Foundation. |
---|
5 | # |
---|
6 | #See the license text in license.txt |
---|
7 | # |
---|
8 | #copyright 2009, University of Tennessee |
---|
9 | ################################################################################ |
---|
10 | import copy |
---|
11 | from sans.models.qsmearing import smear_selection |
---|
12 | |
---|
13 | |
---|
14 | class FitProblemComponent(object): |
---|
15 | """ |
---|
16 | Inferface containing information to store data, model, range of data, etc... |
---|
17 | and retreive this information. This is an inferface |
---|
18 | for a fitProblem i.e relationship between data and model. |
---|
19 | """ |
---|
20 | def enable_smearing(self, flag=False): |
---|
21 | """ |
---|
22 | :param flag: bool.When flag is 1 get the computer smear value. When |
---|
23 | flag is 0 ingore smear value. |
---|
24 | """ |
---|
25 | def get_smearer(self): |
---|
26 | """ |
---|
27 | return smear object |
---|
28 | """ |
---|
29 | def save_model_name(self, name): |
---|
30 | """ |
---|
31 | """ |
---|
32 | def get_name(self): |
---|
33 | """ |
---|
34 | """ |
---|
35 | def set_model(self, model): |
---|
36 | """ |
---|
37 | associates each model with its new created name |
---|
38 | :param model: model selected |
---|
39 | :param name: name created for model |
---|
40 | """ |
---|
41 | def get_model(self): |
---|
42 | """ |
---|
43 | :return: saved model |
---|
44 | """ |
---|
45 | def set_theory_data(self, data): |
---|
46 | """ |
---|
47 | save a copy of the data select to fit |
---|
48 | :param data: data selected |
---|
49 | """ |
---|
50 | def get_theory_data(self): |
---|
51 | """ |
---|
52 | :return: list of data dList |
---|
53 | """ |
---|
54 | def set_fit_data(self, data): |
---|
55 | """ |
---|
56 | Store of list of data and create by create new fitproblem of each data |
---|
57 | id , if there was existing information about model, this information |
---|
58 | get copy to the new fitproblem |
---|
59 | :param data: list of data selected |
---|
60 | """ |
---|
61 | def get_fit_data(self): |
---|
62 | """ |
---|
63 | """ |
---|
64 | def set_model_param(self, name, value=None): |
---|
65 | """ |
---|
66 | Store the name and value of a parameter of this fitproblem's model |
---|
67 | :param name: name of the given parameter |
---|
68 | :param value: value of that parameter |
---|
69 | """ |
---|
70 | def set_param2fit(self, list): |
---|
71 | """ |
---|
72 | Store param names to fit (checked) |
---|
73 | :param list: list of the param names |
---|
74 | """ |
---|
75 | def get_param2fit(self): |
---|
76 | """ |
---|
77 | return the list param names to fit |
---|
78 | """ |
---|
79 | def get_model_param(self): |
---|
80 | """ |
---|
81 | return list of couple of parameter name and value |
---|
82 | """ |
---|
83 | def schedule_tofit(self, schedule=0): |
---|
84 | """ |
---|
85 | set schedule to true to decide if this fit must be performed |
---|
86 | """ |
---|
87 | def get_scheduled(self): |
---|
88 | """ |
---|
89 | return true or false if a problem as being schedule for fitting |
---|
90 | """ |
---|
91 | def set_range(self, qmin=None, qmax=None): |
---|
92 | """ |
---|
93 | set fitting range |
---|
94 | """ |
---|
95 | def get_range(self): |
---|
96 | """ |
---|
97 | :return: fitting range |
---|
98 | """ |
---|
99 | def set_weight(self, flag=None): |
---|
100 | """ |
---|
101 | set fitting range |
---|
102 | """ |
---|
103 | def get_weight(self): |
---|
104 | """ |
---|
105 | get fitting weight |
---|
106 | """ |
---|
107 | def clear_model_param(self): |
---|
108 | """ |
---|
109 | clear constraint info |
---|
110 | """ |
---|
111 | def set_fit_tab_caption(self, caption): |
---|
112 | """ |
---|
113 | store the caption of the page associated with object |
---|
114 | """ |
---|
115 | def get_fit_tab_caption(self): |
---|
116 | """ |
---|
117 | Return the caption of the page associated with object |
---|
118 | """ |
---|
119 | |
---|
120 | |
---|
121 | class FitProblemDictionary(FitProblemComponent, dict): |
---|
122 | """ |
---|
123 | This module implements a dictionary of fitproblem objects |
---|
124 | """ |
---|
125 | def __init__(self): |
---|
126 | FitProblemComponent.__init__(self) |
---|
127 | dict.__init__(self) |
---|
128 | ## the current model |
---|
129 | self.model = None |
---|
130 | ## if 1 this fit problem will be selected to fit , if 0 |
---|
131 | ## it will not be selected for fit |
---|
132 | self.schedule = 0 |
---|
133 | ##list containing parameter name and value |
---|
134 | self.list_param = [] |
---|
135 | ## fitting range |
---|
136 | self.qmin = None |
---|
137 | self.qmax = None |
---|
138 | self._smear_on = False |
---|
139 | self.scheduled = 0 |
---|
140 | self.fit_tab_caption = '' |
---|
141 | |
---|
142 | def enable_smearing(self, flag=False, fid=None): |
---|
143 | """ |
---|
144 | :param flag: bool.When flag is 1 get the computer smear value. When |
---|
145 | flag is 0 ingore smear value. |
---|
146 | """ |
---|
147 | self._smear_on = flag |
---|
148 | if fid is None: |
---|
149 | for value in self.itervalues(): |
---|
150 | value.enable_smearing(flag) |
---|
151 | else: |
---|
152 | if fid in self.iterkeys(): |
---|
153 | self[fid].enable_smearing(flag) |
---|
154 | |
---|
155 | def set_smearer(self, smearer, fid=None): |
---|
156 | """ |
---|
157 | save reference of smear object on fitdata |
---|
158 | :param smear: smear object from DataLoader |
---|
159 | """ |
---|
160 | if fid is None: |
---|
161 | for value in self.itervalues(): |
---|
162 | value.set_smearer(smearer) |
---|
163 | else: |
---|
164 | if fid in self.iterkeys(): |
---|
165 | self[fid].set_smearer(smearer) |
---|
166 | |
---|
167 | def get_smearer(self, fid=None): |
---|
168 | """ |
---|
169 | return smear object |
---|
170 | """ |
---|
171 | result = [] |
---|
172 | if fid is None: |
---|
173 | for value in self.itervalues(): |
---|
174 | result.append(value.get_smearer()) |
---|
175 | else: |
---|
176 | if fid in self.iterkeys(): |
---|
177 | result.append(self[fid].get_smearer()) |
---|
178 | return result |
---|
179 | |
---|
180 | def save_model_name(self, name, fid=None): |
---|
181 | """ |
---|
182 | """ |
---|
183 | if fid is None: |
---|
184 | for value in self.itervalues(): |
---|
185 | value.save_model_name(name) |
---|
186 | else: |
---|
187 | if fid in self.iterkeys(): |
---|
188 | self[fid].save_model_name(name) |
---|
189 | |
---|
190 | def get_name(self, fid=None): |
---|
191 | """ |
---|
192 | """ |
---|
193 | result = [] |
---|
194 | if fid is None: |
---|
195 | for value in self.itervalues(): |
---|
196 | result.append(value.get_name()) |
---|
197 | else: |
---|
198 | if fid in self.iterkeys(): |
---|
199 | result.append(self[fid].get_name()) |
---|
200 | return result |
---|
201 | |
---|
202 | def set_model(self, model, fid=None): |
---|
203 | """ |
---|
204 | associates each model with its new created name |
---|
205 | :param model: model selected |
---|
206 | :param name: name created for model |
---|
207 | """ |
---|
208 | self.model = model |
---|
209 | if fid is None: |
---|
210 | for value in self.itervalues(): |
---|
211 | value.set_model(self.model) |
---|
212 | else: |
---|
213 | if fid in self.iterkeys(): |
---|
214 | self[fid].set_model(self.model) |
---|
215 | |
---|
216 | def get_model(self, fid): |
---|
217 | """ |
---|
218 | :return: saved model |
---|
219 | """ |
---|
220 | if fid in self.iterkeys(): |
---|
221 | self[fid].get_model() |
---|
222 | |
---|
223 | def set_fit_tab_caption(self, caption): |
---|
224 | """ |
---|
225 | store the caption of the page associated with object |
---|
226 | """ |
---|
227 | self.fit_tab_caption = caption |
---|
228 | |
---|
229 | def get_fit_tab_caption(self): |
---|
230 | """ |
---|
231 | Return the caption of the page associated with object |
---|
232 | """ |
---|
233 | return self.fit_tab_caption |
---|
234 | |
---|
235 | def set_theory_data(self, fid, data=None): |
---|
236 | """ |
---|
237 | save a copy of the data select to fit |
---|
238 | :param data: data selected |
---|
239 | """ |
---|
240 | if fid in self.iterkeys(): |
---|
241 | self[fid].set_theory_data(data) |
---|
242 | |
---|
243 | def get_theory_data(self, fid): |
---|
244 | """ |
---|
245 | :return: list of data dList |
---|
246 | """ |
---|
247 | if fid in self.iterkeys(): |
---|
248 | return self[fid].get_theory_data() |
---|
249 | |
---|
250 | def add_data(self, data): |
---|
251 | """ |
---|
252 | Add data to the current dictionary of fitproblem. if data id does not |
---|
253 | exist create a new fit problem. |
---|
254 | :note: only data changes in the fit problem |
---|
255 | """ |
---|
256 | if data.id not in self.iterkeys(): |
---|
257 | self[data.id] = FitProblem() |
---|
258 | self[data.id].set_fit_data(data) |
---|
259 | |
---|
260 | def set_fit_data(self, data): |
---|
261 | """ |
---|
262 | save a copy of the data select to fit |
---|
263 | :param data: data selected |
---|
264 | |
---|
265 | """ |
---|
266 | self.clear() |
---|
267 | if data is None: |
---|
268 | data = [] |
---|
269 | for d in data: |
---|
270 | if (d is not None): |
---|
271 | if (d.id not in self.iterkeys()): |
---|
272 | self[d.id] = FitProblem() |
---|
273 | self[d.id].set_fit_data(d) |
---|
274 | self[d.id].set_model(self.model) |
---|
275 | self[d.id].set_range(self.qmin, self.qmax) |
---|
276 | #self[d.id].set_smearer(self[d.id].get_smearer()) |
---|
277 | def get_fit_data(self, fid): |
---|
278 | """ |
---|
279 | |
---|
280 | return data for the given fitproblem id |
---|
281 | :param fid: is key representing a fitproblem. usually extract from data |
---|
282 | id |
---|
283 | """ |
---|
284 | if fid in self.iterkeys(): |
---|
285 | return self[fid].get_fit_data() |
---|
286 | |
---|
287 | def set_model_param(self, name, value=None, fid=None): |
---|
288 | """ |
---|
289 | Store the name and value of a parameter of this fitproblem's model |
---|
290 | :param name: name of the given parameter |
---|
291 | :param value: value of that parameter |
---|
292 | """ |
---|
293 | if fid is None: |
---|
294 | for value in self.itervalues(): |
---|
295 | value.set_model_param(name, value) |
---|
296 | else: |
---|
297 | if fid in self.iterkeys(): |
---|
298 | self[fid].set_model_param(name, value) |
---|
299 | |
---|
300 | def get_model_param(self, fid): |
---|
301 | """ |
---|
302 | return list of couple of parameter name and value |
---|
303 | """ |
---|
304 | if fid in self.iterkeys(): |
---|
305 | return self[fid].get_model_param() |
---|
306 | |
---|
307 | def set_param2fit(self, list): |
---|
308 | """ |
---|
309 | Store param names to fit (checked) |
---|
310 | :param list: list of the param names |
---|
311 | """ |
---|
312 | self.list_param2fit = list |
---|
313 | |
---|
314 | def get_param2fit(self): |
---|
315 | """ |
---|
316 | return the list param names to fit |
---|
317 | """ |
---|
318 | return self.list_param2fit |
---|
319 | |
---|
320 | def schedule_tofit(self, schedule=0): |
---|
321 | """ |
---|
322 | set schedule to true to decide if this fit must be performed |
---|
323 | """ |
---|
324 | self.scheduled = schedule |
---|
325 | for value in self.itervalues(): |
---|
326 | value.schedule_tofit(schedule) |
---|
327 | |
---|
328 | def get_scheduled(self): |
---|
329 | """ |
---|
330 | return true or false if a problem as being schedule for fitting |
---|
331 | """ |
---|
332 | return self.scheduled |
---|
333 | |
---|
334 | def set_range(self, qmin=None, qmax=None, fid=None): |
---|
335 | """ |
---|
336 | set fitting range |
---|
337 | """ |
---|
338 | self.qmin = qmin |
---|
339 | self.qmax = qmax |
---|
340 | if fid is None: |
---|
341 | for value in self.itervalues(): |
---|
342 | value.set_range(self.qmin, self.qmax) |
---|
343 | else: |
---|
344 | if fid in self.iterkeys(): |
---|
345 | self[fid].value.set_range(self.qmin, self.qmax) |
---|
346 | |
---|
347 | def get_range(self, fid): |
---|
348 | """ |
---|
349 | :return: fitting range |
---|
350 | """ |
---|
351 | if fid in self.iterkeys(): |
---|
352 | return self[fid].get_range() |
---|
353 | |
---|
354 | def set_weight(self, is2d, flag=None, fid=None): |
---|
355 | """ |
---|
356 | fit weight |
---|
357 | """ |
---|
358 | if fid is None: |
---|
359 | for value in self.itervalues(): |
---|
360 | value.set_weight(flag=flag, is2d=is2d) |
---|
361 | else: |
---|
362 | if fid in self.iterkeys(): |
---|
363 | self[fid].set_weight(flag=flag, is2d=is2d) |
---|
364 | |
---|
365 | def get_weight(self, fid=None): |
---|
366 | """ |
---|
367 | return fit weight |
---|
368 | """ |
---|
369 | if fid in self.iterkeys(): |
---|
370 | return self[fid].get_weight() |
---|
371 | |
---|
372 | def clear_model_param(self, fid=None): |
---|
373 | """ |
---|
374 | clear constraint info |
---|
375 | """ |
---|
376 | if fid is None: |
---|
377 | for value in self.itervalues(): |
---|
378 | value.clear_model_param() |
---|
379 | else: |
---|
380 | if fid in self.iterkeys(): |
---|
381 | self[fid].clear_model_param() |
---|
382 | |
---|
383 | def get_fit_problem(self): |
---|
384 | """ |
---|
385 | return fitproblem contained in this dictionary |
---|
386 | """ |
---|
387 | return self.itervalues() |
---|
388 | |
---|
389 | def set_result(self, result): |
---|
390 | """ |
---|
391 | set a list of result |
---|
392 | """ |
---|
393 | self.result = result |
---|
394 | |
---|
395 | def get_result(self): |
---|
396 | """ |
---|
397 | get result |
---|
398 | """ |
---|
399 | return self.result |
---|
400 | |
---|
401 | |
---|
402 | class FitProblem(FitProblemComponent): |
---|
403 | """ |
---|
404 | FitProblem class allows to link a model with the new name created in _on_model, |
---|
405 | a name theory created with that model and the data fitted with the model. |
---|
406 | FitProblem is mostly used as value of the dictionary by fitting module. |
---|
407 | """ |
---|
408 | def __init__(self): |
---|
409 | FitProblemComponent.__init__(self) |
---|
410 | """ |
---|
411 | contains information about data and model to fit |
---|
412 | """ |
---|
413 | ## data used for fitting |
---|
414 | self.fit_data = None |
---|
415 | self.theory_data = None |
---|
416 | # original data: should not be modified |
---|
417 | self.original_data = None |
---|
418 | ## the current model |
---|
419 | self.model = None |
---|
420 | ## if 1 this fit problem will be selected to fit , if 0 |
---|
421 | ## it will not be selected for fit |
---|
422 | self.schedule = 0 |
---|
423 | ##list containing parameter name and value |
---|
424 | self.list_param = [] |
---|
425 | ## smear object to smear or not data1D |
---|
426 | self.smearer_computed = False |
---|
427 | self.smearer_enable = False |
---|
428 | self.smearer_computer_value = None |
---|
429 | ## fitting range |
---|
430 | self.qmin = None |
---|
431 | self.qmax = None |
---|
432 | # fit weight |
---|
433 | self.weight = None |
---|
434 | |
---|
435 | |
---|
436 | def enable_smearing(self, flag=False): |
---|
437 | """ |
---|
438 | :param flag: bool.When flag is 1 get the computer smear value. When |
---|
439 | flag is 0 ingore smear value. |
---|
440 | """ |
---|
441 | self.smearer_enable = flag |
---|
442 | |
---|
443 | def set_smearer(self, smearer): |
---|
444 | """ |
---|
445 | save reference of smear object on fitdata |
---|
446 | |
---|
447 | :param smear: smear object from DataLoader |
---|
448 | |
---|
449 | """ |
---|
450 | self.smearer_computer_value = smearer |
---|
451 | |
---|
452 | def get_smearer(self): |
---|
453 | """ |
---|
454 | return smear object |
---|
455 | """ |
---|
456 | if not self.smearer_enable: |
---|
457 | return None |
---|
458 | if not self.smearer_computed: |
---|
459 | #smeari_selection should be call only once per fitproblem |
---|
460 | self.smearer_computer_value = smear_selection(self.fit_data, |
---|
461 | self.model) |
---|
462 | self.smearer_computed = True |
---|
463 | return self.smearer_computer_value |
---|
464 | |
---|
465 | def save_model_name(self, name): |
---|
466 | """ |
---|
467 | """ |
---|
468 | self.name_per_page= name |
---|
469 | |
---|
470 | def get_name(self): |
---|
471 | """ |
---|
472 | """ |
---|
473 | return self.name_per_page |
---|
474 | |
---|
475 | def set_model(self, model): |
---|
476 | """ |
---|
477 | associates each model with its new created name |
---|
478 | :param model: model selected |
---|
479 | :param name: name created for model |
---|
480 | """ |
---|
481 | self.model = model |
---|
482 | self.smearer_computer_value = smear_selection(self.fit_data, |
---|
483 | self.model) |
---|
484 | self.smearer_computed = True |
---|
485 | |
---|
486 | def get_model(self): |
---|
487 | """ |
---|
488 | :return: saved model |
---|
489 | """ |
---|
490 | return self.model |
---|
491 | |
---|
492 | def set_theory_data(self, data): |
---|
493 | """ |
---|
494 | save a copy of the data select to fit |
---|
495 | |
---|
496 | :param data: data selected |
---|
497 | |
---|
498 | """ |
---|
499 | self.theory_data = copy.deepcopy(data) |
---|
500 | |
---|
501 | |
---|
502 | |
---|
503 | def get_theory_data(self): |
---|
504 | """ |
---|
505 | :return: theory generated with the current model and data of this class |
---|
506 | """ |
---|
507 | return self.theory_data |
---|
508 | |
---|
509 | def set_fit_data(self, data): |
---|
510 | """ |
---|
511 | Store data associated with this class |
---|
512 | :param data: list of data selected |
---|
513 | """ |
---|
514 | # original data: should not be modified |
---|
515 | self.original_data = copy.deepcopy(data) |
---|
516 | # fit data: used for fit and can be modified for convenience |
---|
517 | self.fit_data = copy.deepcopy(data) |
---|
518 | self.smearer_computer_value = smear_selection(self.fit_data, |
---|
519 | self.model) |
---|
520 | self.smearer_computed = True |
---|
521 | |
---|
522 | def get_fit_data(self): |
---|
523 | """ |
---|
524 | :return: data associate with this class |
---|
525 | """ |
---|
526 | return self.fit_data |
---|
527 | |
---|
528 | def set_weight(self, is2d, flag=None): |
---|
529 | """ |
---|
530 | Received flag and compute error on data. |
---|
531 | :param flag: flag to transform error of data. |
---|
532 | :param is2d: flag to distinguish 1D to 2D Data |
---|
533 | """ |
---|
534 | from .utils import get_weight |
---|
535 | # send original data for weighting |
---|
536 | self.weight = get_weight(data=self.original_data, is2d=is2d, flag=flag) |
---|
537 | if is2d: |
---|
538 | self.fit_data.err_data = self.weight |
---|
539 | else: |
---|
540 | self.fit_data.dy = self.weight |
---|
541 | |
---|
542 | def get_weight(self): |
---|
543 | """ |
---|
544 | returns weight array |
---|
545 | """ |
---|
546 | return self.weight |
---|
547 | |
---|
548 | def set_param2fit(self, list): |
---|
549 | """ |
---|
550 | Store param names to fit (checked) |
---|
551 | :param list: list of the param names |
---|
552 | """ |
---|
553 | self.list_param2fit = list |
---|
554 | |
---|
555 | def get_param2fit(self): |
---|
556 | """ |
---|
557 | return the list param names to fit |
---|
558 | """ |
---|
559 | return self.list_param2fit |
---|
560 | |
---|
561 | def set_model_param(self,name,value=None): |
---|
562 | """ |
---|
563 | Store the name and value of a parameter of this fitproblem's model |
---|
564 | :param name: name of the given parameter |
---|
565 | :param value: value of that parameter |
---|
566 | """ |
---|
567 | self.list_param.append([name,value]) |
---|
568 | |
---|
569 | def get_model_param(self): |
---|
570 | """ |
---|
571 | return list of couple of parameter name and value |
---|
572 | """ |
---|
573 | return self.list_param |
---|
574 | |
---|
575 | def schedule_tofit(self, schedule=0): |
---|
576 | """ |
---|
577 | set schedule to true to decide if this fit must be performed |
---|
578 | """ |
---|
579 | self.schedule = schedule |
---|
580 | |
---|
581 | def get_scheduled(self): |
---|
582 | """ |
---|
583 | return true or false if a problem as being schedule for fitting |
---|
584 | """ |
---|
585 | return self.schedule |
---|
586 | |
---|
587 | def set_range(self, qmin=None, qmax=None): |
---|
588 | """ |
---|
589 | set fitting range |
---|
590 | :param qmin: minimum value to consider for the fit range |
---|
591 | :param qmax: maximum value to consider for the fit range |
---|
592 | """ |
---|
593 | self.qmin = qmin |
---|
594 | self.qmax = qmax |
---|
595 | |
---|
596 | def get_range(self): |
---|
597 | """ |
---|
598 | :return: fitting range |
---|
599 | |
---|
600 | """ |
---|
601 | return self.qmin, self.qmax |
---|
602 | |
---|
603 | def clear_model_param(self): |
---|
604 | """ |
---|
605 | clear constraint info |
---|
606 | """ |
---|
607 | self.list_param = [] |
---|
608 | |
---|
609 | def set_fit_tab_caption(self, caption): |
---|
610 | """ |
---|
611 | """ |
---|
612 | self.fit_tab_caption = str(caption) |
---|
613 | |
---|
614 | def get_fit_tab_caption(self): |
---|
615 | """ |
---|
616 | """ |
---|
617 | return self.fit_tab_caption |
---|
618 | |
---|
619 | |
---|
620 | |
---|