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