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