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 | def set_graph_id(self, id): |
---|
120 | """ |
---|
121 | Set graph id (from data_group_id at the time the graph produced) |
---|
122 | """ |
---|
123 | def get_graph_id(self): |
---|
124 | """ |
---|
125 | Get graph_id |
---|
126 | """ |
---|
127 | |
---|
128 | class FitProblemDictionary(FitProblemComponent, dict): |
---|
129 | """ |
---|
130 | This module implements a dictionary of fitproblem objects |
---|
131 | """ |
---|
132 | def __init__(self): |
---|
133 | FitProblemComponent.__init__(self) |
---|
134 | dict.__init__(self) |
---|
135 | ## the current model |
---|
136 | self.model = None |
---|
137 | ## if 1 this fit problem will be selected to fit , if 0 |
---|
138 | ## it will not be selected for fit |
---|
139 | self.schedule = 0 |
---|
140 | ##list containing parameter name and value |
---|
141 | self.list_param = [] |
---|
142 | ## fitting range |
---|
143 | self.qmin = None |
---|
144 | self.qmax = None |
---|
145 | self.graph_id = None |
---|
146 | self._smear_on = False |
---|
147 | self.scheduled = 0 |
---|
148 | self.fit_tab_caption = '' |
---|
149 | |
---|
150 | def enable_smearing(self, flag=False, fid=None): |
---|
151 | """ |
---|
152 | :param flag: bool.When flag is 1 get the computer smear value. When |
---|
153 | flag is 0 ingore smear value. |
---|
154 | """ |
---|
155 | self._smear_on = flag |
---|
156 | if fid is None: |
---|
157 | for value in self.itervalues(): |
---|
158 | value.enable_smearing(flag) |
---|
159 | else: |
---|
160 | if fid in self.iterkeys(): |
---|
161 | self[fid].enable_smearing(flag) |
---|
162 | |
---|
163 | def set_smearer(self, smearer, fid=None): |
---|
164 | """ |
---|
165 | save reference of smear object on fitdata |
---|
166 | :param smear: smear object from DataLoader |
---|
167 | """ |
---|
168 | if fid is None: |
---|
169 | for value in self.itervalues(): |
---|
170 | value.set_smearer(smearer) |
---|
171 | else: |
---|
172 | if fid in self.iterkeys(): |
---|
173 | self[fid].set_smearer(smearer) |
---|
174 | |
---|
175 | def get_smearer(self, fid=None): |
---|
176 | """ |
---|
177 | return smear object |
---|
178 | """ |
---|
179 | result = [] |
---|
180 | if fid is None: |
---|
181 | for value in self.itervalues(): |
---|
182 | result.append(value.get_smearer()) |
---|
183 | else: |
---|
184 | if fid in self.iterkeys(): |
---|
185 | result.append(self[fid].get_smearer()) |
---|
186 | return result |
---|
187 | |
---|
188 | def save_model_name(self, name, fid=None): |
---|
189 | """ |
---|
190 | """ |
---|
191 | if fid is None: |
---|
192 | for value in self.itervalues(): |
---|
193 | value.save_model_name(name) |
---|
194 | else: |
---|
195 | if fid in self.iterkeys(): |
---|
196 | self[fid].save_model_name(name) |
---|
197 | |
---|
198 | def get_name(self, fid=None): |
---|
199 | """ |
---|
200 | """ |
---|
201 | result = [] |
---|
202 | if fid is None: |
---|
203 | for value in self.itervalues(): |
---|
204 | result.append(value.get_name()) |
---|
205 | else: |
---|
206 | if fid in self.iterkeys(): |
---|
207 | result.append(self[fid].get_name()) |
---|
208 | return result |
---|
209 | |
---|
210 | def set_model(self, model, fid=None): |
---|
211 | """ |
---|
212 | associates each model with its new created name |
---|
213 | :param model: model selected |
---|
214 | :param name: name created for model |
---|
215 | """ |
---|
216 | self.model = model |
---|
217 | if fid is None: |
---|
218 | for value in self.itervalues(): |
---|
219 | value.set_model(self.model) |
---|
220 | else: |
---|
221 | if fid in self.iterkeys(): |
---|
222 | self[fid].set_model(self.model) |
---|
223 | |
---|
224 | def get_model(self, fid): |
---|
225 | """ |
---|
226 | :return: saved model |
---|
227 | """ |
---|
228 | if fid in self.iterkeys(): |
---|
229 | self[fid].get_model() |
---|
230 | |
---|
231 | def set_fit_tab_caption(self, caption): |
---|
232 | """ |
---|
233 | store the caption of the page associated with object |
---|
234 | """ |
---|
235 | self.fit_tab_caption = caption |
---|
236 | |
---|
237 | def get_fit_tab_caption(self): |
---|
238 | """ |
---|
239 | Return the caption of the page associated with object |
---|
240 | """ |
---|
241 | return self.fit_tab_caption |
---|
242 | |
---|
243 | def set_theory_data(self, fid, data=None): |
---|
244 | """ |
---|
245 | save a copy of the data select to fit |
---|
246 | :param data: data selected |
---|
247 | """ |
---|
248 | if fid in self.iterkeys(): |
---|
249 | self[fid].set_theory_data(data) |
---|
250 | |
---|
251 | def get_theory_data(self, fid): |
---|
252 | """ |
---|
253 | :return: list of data dList |
---|
254 | """ |
---|
255 | if fid in self.iterkeys(): |
---|
256 | return self[fid].get_theory_data() |
---|
257 | |
---|
258 | def add_data(self, data): |
---|
259 | """ |
---|
260 | Add data to the current dictionary of fitproblem. if data id does not |
---|
261 | exist create a new fit problem. |
---|
262 | :note: only data changes in the fit problem |
---|
263 | """ |
---|
264 | if data.id not in self.iterkeys(): |
---|
265 | self[data.id] = FitProblem() |
---|
266 | self[data.id].set_fit_data(data) |
---|
267 | |
---|
268 | def set_fit_data(self, data): |
---|
269 | """ |
---|
270 | save a copy of the data select to fit |
---|
271 | :param data: data selected |
---|
272 | |
---|
273 | """ |
---|
274 | self.clear() |
---|
275 | if data is None: |
---|
276 | data = [] |
---|
277 | for d in data: |
---|
278 | if (d is not None): |
---|
279 | if (d.id not in self.iterkeys()): |
---|
280 | self[d.id] = FitProblem() |
---|
281 | self[d.id].set_fit_data(d) |
---|
282 | self[d.id].set_model(self.model) |
---|
283 | self[d.id].set_range(self.qmin, self.qmax) |
---|
284 | #self[d.id].set_smearer(self[d.id].get_smearer()) |
---|
285 | def get_fit_data(self, fid): |
---|
286 | """ |
---|
287 | |
---|
288 | return data for the given fitproblem id |
---|
289 | :param fid: is key representing a fitproblem. usually extract from data |
---|
290 | id |
---|
291 | """ |
---|
292 | if fid in self.iterkeys(): |
---|
293 | return self[fid].get_fit_data() |
---|
294 | |
---|
295 | def set_model_param(self, name, value=None, fid=None): |
---|
296 | """ |
---|
297 | Store the name and value of a parameter of this fitproblem's model |
---|
298 | :param name: name of the given parameter |
---|
299 | :param value: value of that parameter |
---|
300 | """ |
---|
301 | if fid is None: |
---|
302 | for value in self.itervalues(): |
---|
303 | value.set_model_param(name, value) |
---|
304 | else: |
---|
305 | if fid in self.iterkeys(): |
---|
306 | self[fid].set_model_param(name, value) |
---|
307 | |
---|
308 | def get_model_param(self, fid): |
---|
309 | """ |
---|
310 | return list of couple of parameter name and value |
---|
311 | """ |
---|
312 | if fid in self.iterkeys(): |
---|
313 | return self[fid].get_model_param() |
---|
314 | |
---|
315 | def set_param2fit(self, list): |
---|
316 | """ |
---|
317 | Store param names to fit (checked) |
---|
318 | :param list: list of the param names |
---|
319 | """ |
---|
320 | self.list_param2fit = list |
---|
321 | |
---|
322 | def get_param2fit(self): |
---|
323 | """ |
---|
324 | return the list param names to fit |
---|
325 | """ |
---|
326 | return self.list_param2fit |
---|
327 | |
---|
328 | def schedule_tofit(self, schedule=0): |
---|
329 | """ |
---|
330 | set schedule to true to decide if this fit must be performed |
---|
331 | """ |
---|
332 | self.scheduled = schedule |
---|
333 | for value in self.itervalues(): |
---|
334 | value.schedule_tofit(schedule) |
---|
335 | |
---|
336 | def get_scheduled(self): |
---|
337 | """ |
---|
338 | return true or false if a problem as being schedule for fitting |
---|
339 | """ |
---|
340 | return self.scheduled |
---|
341 | |
---|
342 | def set_range(self, qmin=None, qmax=None, fid=None): |
---|
343 | """ |
---|
344 | set fitting range |
---|
345 | """ |
---|
346 | self.qmin = qmin |
---|
347 | self.qmax = qmax |
---|
348 | if fid is None: |
---|
349 | for value in self.itervalues(): |
---|
350 | value.set_range(self.qmin, self.qmax) |
---|
351 | else: |
---|
352 | if fid in self.iterkeys(): |
---|
353 | self[fid].value.set_range(self.qmin, self.qmax) |
---|
354 | |
---|
355 | def get_range(self, fid): |
---|
356 | """ |
---|
357 | :return: fitting range |
---|
358 | """ |
---|
359 | if fid in self.iterkeys(): |
---|
360 | return self[fid].get_range() |
---|
361 | |
---|
362 | def set_weight(self, is2d, flag=None, fid=None): |
---|
363 | """ |
---|
364 | fit weight |
---|
365 | """ |
---|
366 | if fid is None: |
---|
367 | for value in self.itervalues(): |
---|
368 | value.set_weight(flag=flag, is2d=is2d) |
---|
369 | else: |
---|
370 | if fid in self.iterkeys(): |
---|
371 | self[fid].set_weight(flag=flag, is2d=is2d) |
---|
372 | |
---|
373 | def get_weight(self, fid=None): |
---|
374 | """ |
---|
375 | return fit weight |
---|
376 | """ |
---|
377 | if fid in self.iterkeys(): |
---|
378 | return self[fid].get_weight() |
---|
379 | |
---|
380 | def clear_model_param(self, fid=None): |
---|
381 | """ |
---|
382 | clear constraint info |
---|
383 | """ |
---|
384 | if fid is None: |
---|
385 | for value in self.itervalues(): |
---|
386 | value.clear_model_param() |
---|
387 | else: |
---|
388 | if fid in self.iterkeys(): |
---|
389 | self[fid].clear_model_param() |
---|
390 | |
---|
391 | def get_fit_problem(self): |
---|
392 | """ |
---|
393 | return fitproblem contained in this dictionary |
---|
394 | """ |
---|
395 | return self.itervalues() |
---|
396 | |
---|
397 | def set_result(self, result): |
---|
398 | """ |
---|
399 | set a list of result |
---|
400 | """ |
---|
401 | self.result = result |
---|
402 | |
---|
403 | def get_result(self): |
---|
404 | """ |
---|
405 | get result |
---|
406 | """ |
---|
407 | return self.result |
---|
408 | |
---|
409 | def set_graph_id(self, id): |
---|
410 | """ |
---|
411 | Set graph id (from data_group_id at the time the graph produced) |
---|
412 | """ |
---|
413 | self.graph_id = id |
---|
414 | |
---|
415 | def get_graph_id(self): |
---|
416 | """ |
---|
417 | Get graph_id |
---|
418 | """ |
---|
419 | return self.graph_id |
---|
420 | |
---|
421 | |
---|
422 | class FitProblem(FitProblemComponent): |
---|
423 | """ |
---|
424 | FitProblem class allows to link a model with the new name created in _on_model, |
---|
425 | a name theory created with that model and the data fitted with the model. |
---|
426 | FitProblem is mostly used as value of the dictionary by fitting module. |
---|
427 | """ |
---|
428 | def __init__(self): |
---|
429 | FitProblemComponent.__init__(self) |
---|
430 | """ |
---|
431 | contains information about data and model to fit |
---|
432 | """ |
---|
433 | ## data used for fitting |
---|
434 | self.fit_data = None |
---|
435 | self.theory_data = None |
---|
436 | # original data: should not be modified |
---|
437 | self.original_data = None |
---|
438 | ## the current model |
---|
439 | self.model = None |
---|
440 | ## if 1 this fit problem will be selected to fit , if 0 |
---|
441 | ## it will not be selected for fit |
---|
442 | self.schedule = 0 |
---|
443 | ##list containing parameter name and value |
---|
444 | self.list_param = [] |
---|
445 | ## smear object to smear or not data1D |
---|
446 | self.smearer_computed = False |
---|
447 | self.smearer_enable = False |
---|
448 | self.smearer_computer_value = None |
---|
449 | ## fitting range |
---|
450 | self.qmin = None |
---|
451 | self.qmax = None |
---|
452 | # fit weight |
---|
453 | self.weight = None |
---|
454 | |
---|
455 | |
---|
456 | def enable_smearing(self, flag=False): |
---|
457 | """ |
---|
458 | :param flag: bool.When flag is 1 get the computer smear value. When |
---|
459 | flag is 0 ingore smear value. |
---|
460 | """ |
---|
461 | self.smearer_enable = flag |
---|
462 | |
---|
463 | def set_smearer(self, smearer): |
---|
464 | """ |
---|
465 | save reference of smear object on fitdata |
---|
466 | |
---|
467 | :param smear: smear object from DataLoader |
---|
468 | |
---|
469 | """ |
---|
470 | self.smearer_computer_value = smearer |
---|
471 | |
---|
472 | def get_smearer(self): |
---|
473 | """ |
---|
474 | return smear object |
---|
475 | """ |
---|
476 | if not self.smearer_enable: |
---|
477 | return None |
---|
478 | if not self.smearer_computed: |
---|
479 | #smeari_selection should be call only once per fitproblem |
---|
480 | self.smearer_computer_value = smear_selection(self.fit_data, |
---|
481 | self.model) |
---|
482 | self.smearer_computed = True |
---|
483 | return self.smearer_computer_value |
---|
484 | |
---|
485 | def save_model_name(self, name): |
---|
486 | """ |
---|
487 | """ |
---|
488 | self.name_per_page= name |
---|
489 | |
---|
490 | def get_name(self): |
---|
491 | """ |
---|
492 | """ |
---|
493 | return self.name_per_page |
---|
494 | |
---|
495 | def set_model(self, model): |
---|
496 | """ |
---|
497 | associates each model with its new created name |
---|
498 | :param model: model selected |
---|
499 | :param name: name created for model |
---|
500 | """ |
---|
501 | self.model = model |
---|
502 | self.smearer_computer_value = smear_selection(self.fit_data, |
---|
503 | self.model) |
---|
504 | self.smearer_computed = True |
---|
505 | |
---|
506 | def get_model(self): |
---|
507 | """ |
---|
508 | :return: saved model |
---|
509 | """ |
---|
510 | return self.model |
---|
511 | |
---|
512 | def set_theory_data(self, data): |
---|
513 | """ |
---|
514 | save a copy of the data select to fit |
---|
515 | |
---|
516 | :param data: data selected |
---|
517 | |
---|
518 | """ |
---|
519 | self.theory_data = copy.deepcopy(data) |
---|
520 | |
---|
521 | |
---|
522 | |
---|
523 | def get_theory_data(self): |
---|
524 | """ |
---|
525 | :return: theory generated with the current model and data of this class |
---|
526 | """ |
---|
527 | return self.theory_data |
---|
528 | |
---|
529 | def set_fit_data(self, data): |
---|
530 | """ |
---|
531 | Store data associated with this class |
---|
532 | :param data: list of data selected |
---|
533 | """ |
---|
534 | # original data: should not be modified |
---|
535 | self.original_data = copy.deepcopy(data) |
---|
536 | # fit data: used for fit and can be modified for convenience |
---|
537 | self.fit_data = copy.deepcopy(data) |
---|
538 | self.smearer_computer_value = smear_selection(self.fit_data, |
---|
539 | self.model) |
---|
540 | self.smearer_computed = True |
---|
541 | |
---|
542 | def get_fit_data(self): |
---|
543 | """ |
---|
544 | :return: data associate with this class |
---|
545 | """ |
---|
546 | return self.fit_data |
---|
547 | |
---|
548 | def set_weight(self, is2d, flag=None): |
---|
549 | """ |
---|
550 | Received flag and compute error on data. |
---|
551 | :param flag: flag to transform error of data. |
---|
552 | :param is2d: flag to distinguish 1D to 2D Data |
---|
553 | """ |
---|
554 | from .utils import get_weight |
---|
555 | # send original data for weighting |
---|
556 | self.weight = get_weight(data=self.original_data, is2d=is2d, flag=flag) |
---|
557 | if is2d: |
---|
558 | self.fit_data.err_data = self.weight |
---|
559 | else: |
---|
560 | self.fit_data.dy = self.weight |
---|
561 | |
---|
562 | def get_weight(self): |
---|
563 | """ |
---|
564 | returns weight array |
---|
565 | """ |
---|
566 | return self.weight |
---|
567 | |
---|
568 | def set_param2fit(self, list): |
---|
569 | """ |
---|
570 | Store param names to fit (checked) |
---|
571 | :param list: list of the param names |
---|
572 | """ |
---|
573 | self.list_param2fit = list |
---|
574 | |
---|
575 | def get_param2fit(self): |
---|
576 | """ |
---|
577 | return the list param names to fit |
---|
578 | """ |
---|
579 | return self.list_param2fit |
---|
580 | |
---|
581 | def set_model_param(self,name,value=None): |
---|
582 | """ |
---|
583 | Store the name and value of a parameter of this fitproblem's model |
---|
584 | :param name: name of the given parameter |
---|
585 | :param value: value of that parameter |
---|
586 | """ |
---|
587 | self.list_param.append([name,value]) |
---|
588 | |
---|
589 | def get_model_param(self): |
---|
590 | """ |
---|
591 | return list of couple of parameter name and value |
---|
592 | """ |
---|
593 | return self.list_param |
---|
594 | |
---|
595 | def schedule_tofit(self, schedule=0): |
---|
596 | """ |
---|
597 | set schedule to true to decide if this fit must be performed |
---|
598 | """ |
---|
599 | self.schedule = schedule |
---|
600 | |
---|
601 | def get_scheduled(self): |
---|
602 | """ |
---|
603 | return true or false if a problem as being schedule for fitting |
---|
604 | """ |
---|
605 | return self.schedule |
---|
606 | |
---|
607 | def set_range(self, qmin=None, qmax=None): |
---|
608 | """ |
---|
609 | set fitting range |
---|
610 | :param qmin: minimum value to consider for the fit range |
---|
611 | :param qmax: maximum value to consider for the fit range |
---|
612 | """ |
---|
613 | self.qmin = qmin |
---|
614 | self.qmax = qmax |
---|
615 | |
---|
616 | def get_range(self): |
---|
617 | """ |
---|
618 | :return: fitting range |
---|
619 | |
---|
620 | """ |
---|
621 | return self.qmin, self.qmax |
---|
622 | |
---|
623 | def clear_model_param(self): |
---|
624 | """ |
---|
625 | clear constraint info |
---|
626 | """ |
---|
627 | self.list_param = [] |
---|
628 | |
---|
629 | def set_fit_tab_caption(self, caption): |
---|
630 | """ |
---|
631 | """ |
---|
632 | self.fit_tab_caption = str(caption) |
---|
633 | |
---|
634 | def get_fit_tab_caption(self): |
---|
635 | """ |
---|
636 | """ |
---|
637 | return self.fit_tab_caption |
---|
638 | |
---|
639 | def set_graph_id(self, id): |
---|
640 | """ |
---|
641 | Set graph id (from data_group_id at the time the graph produced) |
---|
642 | """ |
---|
643 | self.graph_id = id |
---|
644 | |
---|
645 | def get_graph_id(self): |
---|
646 | """ |
---|
647 | Get graph_id |
---|
648 | """ |
---|
649 | return self.graph_id |
---|
650 | |
---|