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 | ## the current model |
---|
395 | self.model = None |
---|
396 | ## if 1 this fit problem will be selected to fit , if 0 |
---|
397 | ## it will not be selected for fit |
---|
398 | self.schedule = 0 |
---|
399 | ##list containing parameter name and value |
---|
400 | self.list_param = [] |
---|
401 | ## smear object to smear or not data1D |
---|
402 | self.smearer_compute_count = 0 |
---|
403 | self.smearer_enable = False |
---|
404 | self.smearer_computer_value = None |
---|
405 | ## fitting range |
---|
406 | self.qmin = None |
---|
407 | self.qmax = None |
---|
408 | # fit weight |
---|
409 | self.weight = None |
---|
410 | |
---|
411 | |
---|
412 | def enable_smearing(self, flag=False): |
---|
413 | """ |
---|
414 | :param flag: bool.When flag is 1 get the computer smear value. When |
---|
415 | flag is 0 ingore smear value. |
---|
416 | """ |
---|
417 | self.smearer_enable = flag |
---|
418 | |
---|
419 | def set_smearer(self, smearer): |
---|
420 | """ |
---|
421 | save reference of smear object on fitdata |
---|
422 | |
---|
423 | :param smear: smear object from DataLoader |
---|
424 | |
---|
425 | """ |
---|
426 | self.smearer_computer_value = smearer |
---|
427 | |
---|
428 | def get_smearer(self): |
---|
429 | """ |
---|
430 | return smear object |
---|
431 | """ |
---|
432 | if not self.smearer_enable: |
---|
433 | return None |
---|
434 | if self.smearer_computer_value is None and \ |
---|
435 | self.smearer_compute_count > 1: |
---|
436 | #smeari_selection should be call only once per fitproblem |
---|
437 | self.smearer_computer_value = smear_selection(self.fit_data, |
---|
438 | self.model) |
---|
439 | self.smearer_compute_count += 1 |
---|
440 | return self.smearer_computer_value |
---|
441 | |
---|
442 | def save_model_name(self, name): |
---|
443 | """ |
---|
444 | """ |
---|
445 | self.name_per_page= name |
---|
446 | |
---|
447 | def get_name(self): |
---|
448 | """ |
---|
449 | """ |
---|
450 | return self.name_per_page |
---|
451 | |
---|
452 | def set_model(self,model): |
---|
453 | """ |
---|
454 | associates each model with its new created name |
---|
455 | :param model: model selected |
---|
456 | :param name: name created for model |
---|
457 | """ |
---|
458 | self.model= model |
---|
459 | |
---|
460 | def get_model(self): |
---|
461 | """ |
---|
462 | :return: saved model |
---|
463 | """ |
---|
464 | return self.model |
---|
465 | |
---|
466 | def set_theory_data(self, data): |
---|
467 | """ |
---|
468 | save a copy of the data select to fit |
---|
469 | |
---|
470 | :param data: data selected |
---|
471 | |
---|
472 | """ |
---|
473 | self.theory_data = copy.deepcopy(data) |
---|
474 | |
---|
475 | |
---|
476 | |
---|
477 | def get_theory_data(self): |
---|
478 | """ |
---|
479 | :return: theory generated with the current model and data of this class |
---|
480 | """ |
---|
481 | return self.theory_data |
---|
482 | |
---|
483 | def set_fit_data(self, data): |
---|
484 | """ |
---|
485 | Store data associated with this class |
---|
486 | :param data: list of data selected |
---|
487 | """ |
---|
488 | self.fit_data = copy.deepcopy(data) |
---|
489 | |
---|
490 | def get_fit_data(self): |
---|
491 | """ |
---|
492 | :return: data associate with this class |
---|
493 | """ |
---|
494 | return self.fit_data |
---|
495 | |
---|
496 | def set_weight(self, is2d, flag=None): |
---|
497 | """ |
---|
498 | Received flag and compute error on data. |
---|
499 | :param flag: flag to transform error of data. |
---|
500 | :param is2d: flag to distinguish 1D to 2D Data |
---|
501 | """ |
---|
502 | from .utils import get_weight |
---|
503 | self.weight = get_weight(data=self.fit_data, is2d=is2d, flag=flag) |
---|
504 | if is2d: |
---|
505 | self.fit_data.err_data = self.weight |
---|
506 | else: |
---|
507 | self.fit_data.dy = self.weight |
---|
508 | |
---|
509 | def get_weight(self): |
---|
510 | """ |
---|
511 | returns weight array |
---|
512 | """ |
---|
513 | return self.weight |
---|
514 | |
---|
515 | def set_model_param(self,name,value=None): |
---|
516 | """ |
---|
517 | Store the name and value of a parameter of this fitproblem's model |
---|
518 | :param name: name of the given parameter |
---|
519 | :param value: value of that parameter |
---|
520 | """ |
---|
521 | self.list_param.append([name,value]) |
---|
522 | |
---|
523 | def get_model_param(self): |
---|
524 | """ |
---|
525 | return list of couple of parameter name and value |
---|
526 | """ |
---|
527 | return self.list_param |
---|
528 | |
---|
529 | def schedule_tofit(self, schedule=0): |
---|
530 | """ |
---|
531 | set schedule to true to decide if this fit must be performed |
---|
532 | """ |
---|
533 | self.schedule = schedule |
---|
534 | |
---|
535 | def get_scheduled(self): |
---|
536 | """ |
---|
537 | return true or false if a problem as being schedule for fitting |
---|
538 | """ |
---|
539 | return self.schedule |
---|
540 | |
---|
541 | def set_range(self, qmin=None, qmax=None): |
---|
542 | """ |
---|
543 | set fitting range |
---|
544 | :param qmin: minimum value to consider for the fit range |
---|
545 | :param qmax: maximum value to consider for the fit range |
---|
546 | """ |
---|
547 | self.qmin = qmin |
---|
548 | self.qmax = qmax |
---|
549 | |
---|
550 | def get_range(self): |
---|
551 | """ |
---|
552 | :return: fitting range |
---|
553 | |
---|
554 | """ |
---|
555 | return self.qmin, self.qmax |
---|
556 | |
---|
557 | def clear_model_param(self): |
---|
558 | """ |
---|
559 | clear constraint info |
---|
560 | """ |
---|
561 | self.list_param = [] |
---|
562 | |
---|
563 | def set_fit_tab_caption(self, caption): |
---|
564 | """ |
---|
565 | """ |
---|
566 | self.fit_tab_caption = str(caption) |
---|
567 | |
---|
568 | def get_fit_tab_caption(self): |
---|
569 | """ |
---|
570 | """ |
---|
571 | return self.fit_tab_caption |
---|
572 | |
---|
573 | |
---|
574 | |
---|