source: sasview/fittingview/src/sans/perspectives/fitting/fitproblem.py @ f706e09c

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since f706e09c was 7db52f1, checked in by Jae Cho <jhjcho@…>, 13 years ago

now able to reset the model init param values in batch model

  • Property mode set to 100644
File size: 21.2 KB
RevLine 
[3e3ab46]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################################################################################
[e88ebfd]10import copy 
[3e3ab46]11from sans.models.qsmearing import smear_selection
[d89f09b]12
[3e3ab46]13
14class 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        """
[62f851f]45    def set_residuals(self, residuals):
46        """
47        save a copy of residual
48        :param data: data selected
49        """
50    def get_residuals(self):
51        """
52        :return: residuals
53        """
54       
[3e3ab46]55    def set_theory_data(self, data):
56        """
57        save a copy of the data select to fit
58        :param data: data selected
59        """
60    def get_theory_data(self):
61        """
62        :return: list of data dList
63        """
64    def set_fit_data(self, data):
65        """
66         Store of list of data and create  by create new fitproblem of each data
67         id , if there was existing information about model, this information
68         get copy to the new fitproblem
69        :param data: list of data selected
70        """   
71    def get_fit_data(self):
72        """
73        """
74    def set_model_param(self, name, value=None):
75        """
76        Store the name and value of a parameter of this fitproblem's model
77        :param name: name of the given parameter
78        :param value: value of that parameter
79        """
[1b14795]80    def set_param2fit(self, list):
81        """
82        Store param names to fit (checked)
83        :param list: list of the param names
84        """
85    def get_param2fit(self):
86        """
87        return the list param names to fit
88        """
[3e3ab46]89    def get_model_param(self):
90        """
91        return list of couple of parameter name and value
92        """
93    def schedule_tofit(self, schedule=0):
94        """
95        set schedule to true to decide if this fit  must be performed
96        """
97    def get_scheduled(self):
98        """
99        return true or false if a problem as being schedule for fitting
100        """
101    def set_range(self, qmin=None, qmax=None):
102        """
103        set fitting range
104        """
105    def get_range(self):
106        """
107        :return: fitting range
108        """
[f7ef313]109    def set_weight(self, flag=None):
[55bb249c]110        """
111        set fitting range
112        """
113    def get_weight(self):
114        """
115        get fitting weight
116        """
[3e3ab46]117    def clear_model_param(self):
118        """
119        clear constraint info
120        """
121    def set_fit_tab_caption(self, caption):
122        """
123        store the caption of the page associated with object
124        """
125    def get_fit_tab_caption(self):
126        """
127        Return the caption of the page associated with object
128        """
[5e48acb]129    def set_graph_id(self, id):
130        """
131        Set graph id (from data_group_id at the time the graph produced)
132        """
133    def get_graph_id(self): 
134        """
135        Get graph_id
136        """ 
[41661a0]137    def set_result(self, result):
138        """
139        """
140
141    def get_result(self):
142        """
143        get result
144        """   
145
[3e3ab46]146   
147class FitProblemDictionary(FitProblemComponent, dict):
148    """
149    This module implements a dictionary of fitproblem objects
150    """
151    def __init__(self):
152        FitProblemComponent.__init__(self)
153        dict.__init__(self)
154        ## the current model
155        self.model = None
156        ## if 1 this fit problem will be selected to fit , if 0
157        ## it will not be selected for fit
158        self.schedule = 0
159        ##list containing parameter name and value
160        self.list_param = []
161        ## fitting range
162        self.qmin = None
163        self.qmax = None
[5e48acb]164        self.graph_id = None
[3e3ab46]165        self._smear_on = False
166        self.scheduled = 0
167        self.fit_tab_caption = ''
[f64a4b7]168        self.nbr_residuals_computed = 0
169        self.batch_inputs = {}
170        self.batch_outputs = {}
171       
[3e3ab46]172 
173    def enable_smearing(self, flag=False, fid=None):
174        """
175        :param flag: bool.When flag is 1 get the computer smear value. When
176        flag is 0 ingore smear value.
177        """
178        self._smear_on = flag
179        if fid is None:
180            for value in self.itervalues():
181                value.enable_smearing(flag)
182        else:
183            if fid in self.iterkeys():
184                self[fid].enable_smearing(flag)
185       
186    def set_smearer(self, smearer, fid=None):
187        """
188        save reference of  smear object on fitdata
189        :param smear: smear object from DataLoader
190        """
191        if fid is None:
192            for value in self.itervalues():
193                value.set_smearer(smearer)
194        else:
195            if fid in self.iterkeys():
196                self[fid].set_smearer(smearer)
197               
198    def get_smearer(self, fid=None):
199        """
200        return smear object
201        """
[62f851f]202        if fid in self.iterkeys():
203            return self[fid].get_smearer()
204     
[3e3ab46]205   
206    def save_model_name(self, name, fid=None):
207        """
208        """ 
209        if fid is None:
210            for value in self.itervalues():
211                value.save_model_name(name)
212        else:
213            if fid in self.iterkeys():
214                self[fid].save_model_name(name)
215               
216    def get_name(self, fid=None):
217        """
218        """
219        result = []
220        if fid is None:
221            for value in self.itervalues():
222                result.append(value.get_name())
223        else:
224            if fid in self.iterkeys():
225                result.append(self[fid].get_name())
226        return result
227   
228    def set_model(self, model, fid=None):
229        """
230        associates each model with its new created name
231        :param model: model selected
232        :param name: name created for model
233        """
234        self.model = model
235        if fid is None:
236            for value in self.itervalues():
237                value.set_model(self.model)
238        else:
239            if fid in self.iterkeys():
240                self[fid].set_model(self.model)
241     
242    def get_model(self, fid):
243        """
244        :return: saved model
245        """
246        if fid in self.iterkeys():
[62f851f]247            return self[fid].get_model()
[3e3ab46]248       
249    def set_fit_tab_caption(self, caption):
250        """
251        store the caption of the page associated with object
252        """
253        self.fit_tab_caption = caption
254   
255    def get_fit_tab_caption(self):
256        """
257        Return the caption of the page associated with object
258        """
259        return self.fit_tab_caption
260   
[62f851f]261    def set_residuals(self, residuals, fid):
262        """
263        save a copy of residual
264        :param data: data selected
265        """
266        if fid in self.iterkeys():
267            self[fid].set_residuals(residuals)
268           
269    def get_residuals(self, fid):
270        """
271        :return: residuals
272        """
273        if fid in self.iterkeys():
274            return self[fid].get_residuals()
275       
[3e3ab46]276    def set_theory_data(self, fid, data=None):
277        """
278        save a copy of the data select to fit
279        :param data: data selected
280        """
281        if fid in self.iterkeys():
282            self[fid].set_theory_data(data)
283           
284    def get_theory_data(self, fid):
285        """
286        :return: list of data dList
287        """
288        if fid in self.iterkeys():
289            return self[fid].get_theory_data()
290           
291    def add_data(self, data):
292        """
293        Add data to the current dictionary of fitproblem. if data id does not
294        exist create a new fit problem.
295        :note: only data changes in the fit problem
296        """
297        if data.id not in self.iterkeys():
298            self[data.id] = FitProblem()
299        self[data.id].set_fit_data(data)
300       
301    def set_fit_data(self, data):
302        """
303        save a copy of the data select to fit
304        :param data: data selected
305       
306        """
307        self.clear()
308        if data is None:
309            data = []
310        for d in data:
311            if (d is not None):
312                if (d.id not in self.iterkeys()):
313                    self[d.id] = FitProblem()
314                self[d.id].set_fit_data(d)
315                self[d.id].set_model(self.model)
316                self[d.id].set_range(self.qmin, self.qmax)
[55bb249c]317                #self[d.id].set_smearer(self[d.id].get_smearer())
[3e3ab46]318    def get_fit_data(self, fid):
319        """
320       
321        return data for the given fitproblem id
322        :param fid: is key representing a fitproblem. usually extract from data
323                    id
324        """
325        if fid in self.iterkeys():
326            return self[fid].get_fit_data()
327   
328    def set_model_param(self, name, value=None, fid=None):
329        """
330        Store the name and value of a parameter of this fitproblem's model
331        :param name: name of the given parameter
332        :param value: value of that parameter
333        """
334        if fid is None:
335            for value in self.itervalues():
336                value.set_model_param(name, value)
337        else:
338            if fid in self.iterkeys():
339                self[fid].set_model_param(name, value)
340               
341    def get_model_param(self, fid):
342        """
343        return list of couple of parameter name and value
344        """
345        if fid in self.iterkeys():
346            return self[fid].get_model_param()
[1b14795]347   
348    def set_param2fit(self, list):
349        """
350        Store param names to fit (checked)
351        :param list: list of the param names
352        """
353        self.list_param2fit = list
354       
355    def get_param2fit(self):
356        """
357        return the list param names to fit
358        """ 
359        return self.list_param2fit
360         
[3e3ab46]361    def schedule_tofit(self, schedule=0):
362        """
363        set schedule to true to decide if this fit  must be performed
364        """
365        self.scheduled = schedule
366        for value in self.itervalues():
367            value.schedule_tofit(schedule)
368     
369    def get_scheduled(self):
370        """
371        return true or false if a problem as being schedule for fitting
372        """
373        return self.scheduled
374   
375    def set_range(self, qmin=None, qmax=None, fid=None):
376        """
377        set fitting range
378        """
379        self.qmin = qmin
380        self.qmax = qmax
381        if fid is None:
382            for value in self.itervalues():
383                value.set_range(self.qmin, self.qmax)
384        else:
385            if fid in self.iterkeys():
386                self[fid].value.set_range(self.qmin, self.qmax)
387       
388    def get_range(self, fid):
389        """
390        :return: fitting range
391        """
392        if fid in self.iterkeys():
393            return self[fid].get_range()
394       
[f7ef313]395    def set_weight(self,  is2d, flag=None, fid=None):
[55bb249c]396        """
397        fit weight
398        """
399        if fid is None:
400            for value in self.itervalues():
[f7ef313]401                value.set_weight(flag=flag, is2d=is2d)
[55bb249c]402        else:
403            if fid in self.iterkeys():
[f7ef313]404                self[fid].set_weight(flag=flag, is2d=is2d)
[55bb249c]405               
406    def get_weight(self, fid=None):
407        """
408        return fit weight
409        """
410        if fid in self.iterkeys():
411            return self[fid].get_weight()
412                 
[3e3ab46]413    def clear_model_param(self, fid=None):
414        """
415        clear constraint info
416        """
417        if fid is None:
418            for value in self.itervalues():
419                value.clear_model_param()
420        else:
421            if fid in self.iterkeys():
422                self[fid].clear_model_param()
423               
424    def get_fit_problem(self):
425        """
426        return fitproblem contained in this dictionary
427        """
428        return self.itervalues()
429   
[41661a0]430    def  set_result(self, result, fid):
431        """
432        """
433        if fid in self.iterkeys():
434            self[fid].set_result(result)
435           
436    def set_batch_result(self, batch_inputs, batch_outputs):
[3e3ab46]437        """
438        set a list of result
439        """
[f64a4b7]440        self.batch_inputs = batch_inputs
441        self.batch_outputs = batch_outputs
[41661a0]442             
443    def get_result(self, fid):
444        """
445        get result
446        """   
447        if fid in self.iterkeys():
448            return self[fid].get_result()
449           
450    def get_batch_result(self):
[3e3ab46]451        """
452        get result
453        """
[f64a4b7]454        return self.batch_inputs, self.batch_outputs
[3e3ab46]455   
[5e48acb]456    def set_graph_id(self, id):
457        """
458        Set graph id (from data_group_id at the time the graph produced)
459        """
460        self.graph_id = id
461       
462    def get_graph_id(self): 
463        """
464        Get graph_id
465        """ 
466        return self.graph_id
467   
468   
[3e3ab46]469class FitProblem(FitProblemComponent):
[d89f09b]470    """ 
[5062bbf]471    FitProblem class allows to link a model with the new name created in _on_model,
472    a name theory created with that model  and the data fitted with the model.
473    FitProblem is mostly used  as value of the dictionary by fitting module.
[d89f09b]474    """
475    def __init__(self):
[3e3ab46]476        FitProblemComponent.__init__(self)
[d89f09b]477        """
[5062bbf]478        contains information about data and model to fit
[d89f09b]479        """
[925a30e]480        ## data used for fitting
[6bbeacd4]481        self.fit_data = None
482        self.theory_data = None
[62f851f]483        self.residuals = None
[05325ec4]484        # original data: should not be modified
485        self.original_data = None
[2140e68]486        ## the current model
487        self.model = None
[925a30e]488        ## if 1 this fit problem will be selected to fit , if 0
489        ## it will not be selected for fit
[6bbeacd4]490        self.schedule = 0
[925a30e]491        ##list containing parameter name and value
[6bbeacd4]492        self.list_param = []
[925a30e]493        ## smear object to smear or not data1D
[7afcae8]494        self.smearer_computed = False
[3e3ab46]495        self.smearer_enable = False
496        self.smearer_computer_value = None
[2140e68]497        ## fitting range
498        self.qmin = None
499        self.qmax = None
[55bb249c]500        # fit weight
501        self.weight = None
[a3c8e8f]502        self.result = None
[08b9c6c8]503       
[ba1f0b2]504       
[3e3ab46]505    def enable_smearing(self, flag=False):
[9853ad0]506        """
[3e3ab46]507        :param flag: bool.When flag is 1 get the computer smear value. When
508        flag is 0 ingore smear value.
[9853ad0]509        """
[3e3ab46]510        self.smearer_enable = flag
[9853ad0]511       
[08b9c6c8]512    def set_smearer(self, smearer):
[925a30e]513        """
[5062bbf]514        save reference of  smear object on fitdata
515       
516        :param smear: smear object from DataLoader
517       
[925a30e]518        """
[3e3ab46]519        self.smearer_computer_value = smearer
[925a30e]520       
[08b9c6c8]521    def get_smearer(self):
[925a30e]522        """
[5062bbf]523        return smear object
[925a30e]524        """
[3e3ab46]525        if not self.smearer_enable:
526            return None
[7afcae8]527        if not self.smearer_computed:
[3e3ab46]528            #smeari_selection should be call only once per fitproblem
[7afcae8]529            self.smearer_computer_value = smear_selection(self.fit_data,
[3e3ab46]530                                                           self.model)
[7afcae8]531            self.smearer_computed = True
[3e3ab46]532        return self.smearer_computer_value
[08b9c6c8]533   
[5062bbf]534    def save_model_name(self, name):
535        """
536        """ 
[bb18ef1]537        self.name_per_page= name
538       
539    def get_name(self):
[5062bbf]540        """
541        """
[bb18ef1]542        return self.name_per_page
[08b9c6c8]543   
[8aa5788]544    def set_model(self, model):
[d89f09b]545        """
[5062bbf]546        associates each model with its new created name
547        :param model: model selected
548        :param name: name created for model
[d89f09b]549        """
[7afcae8]550        self.model = model
551        self.smearer_computer_value = smear_selection(self.fit_data,
552                                                           self.model)
553        self.smearer_computed = True
[bb18ef1]554       
[2140e68]555    def get_model(self):
[5062bbf]556        """
557        :return: saved model
558        """
[2140e68]559        return self.model
[62f851f]560   
561    def set_residuals(self, residuals):
562        """
563        save a copy of residual
564        :param data: data selected
565        """
566        self.residuals = residuals
567           
568    def get_residuals(self):
569        """
570        :return: residuals
571        """
572        return self.residuals
573       
[6bbeacd4]574    def set_theory_data(self, data):
[d89f09b]575        """
[5062bbf]576        save a copy of the data select to fit
577       
578        :param data: data selected
579       
[d89f09b]580        """
[e88ebfd]581        self.theory_data = copy.deepcopy(data)
[ba1f0b2]582       
[6bbeacd4]583    def get_theory_data(self):
[5062bbf]584        """
[3e3ab46]585        :return: theory generated with the current model and data of this class
[5062bbf]586        """
[6bbeacd4]587        return self.theory_data
[5062bbf]588
[3e3ab46]589    def set_fit_data(self, data):
[2a8fac1]590        """
[3e3ab46]591        Store data associated with this class
592        :param data: list of data selected
[2a8fac1]593        """
[3fb5e68]594        self.original_data = None
595        self.fit_data = None
[05325ec4]596        # original data: should not be modified
[7db52f1]597        self.original_data = data
[05325ec4]598        # fit data: used for fit and can be modified for convenience
[55bb249c]599        self.fit_data = copy.deepcopy(data)
[7afcae8]600        self.smearer_computer_value = smear_selection(self.fit_data,
601                                                           self.model)
602        self.smearer_computed = True
[a3c8e8f]603        self.result = None
[f7ef313]604       
[2a8fac1]605    def get_fit_data(self):
[5062bbf]606        """
[3e3ab46]607        :return: data associate with this class
[5062bbf]608        """
[2a8fac1]609        return self.fit_data
610   
[7db52f1]611    def get_origin_data(self):
612        """
613        """
614        return self.original_data
615   
[f7ef313]616    def set_weight(self, is2d, flag=None):
[55bb249c]617        """
[f7ef313]618        Received flag and compute error on data.
619        :param flag: flag to transform error of data.
620        :param is2d: flag to distinguish 1D to 2D Data
[55bb249c]621        """
[f7ef313]622        from .utils import get_weight
[05325ec4]623        # send original data for weighting
624        self.weight = get_weight(data=self.original_data, is2d=is2d, flag=flag)
[55bb249c]625        if is2d:
626            self.fit_data.err_data = self.weight
627        else:
628            self.fit_data.dy = self.weight
629
630    def get_weight(self):
631        """
632        returns weight array
633        """
634        return self.weight
[1b14795]635   
636    def set_param2fit(self, list):
637        """
638        Store param names to fit (checked)
639        :param list: list of the param names
640        """
641        self.list_param2fit = list
[55bb249c]642       
[1b14795]643    def get_param2fit(self):
644        """
645        return the list param names to fit
646        """ 
647        return self.list_param2fit
648   
[2140e68]649    def set_model_param(self,name,value=None):
[d89f09b]650        """
[5062bbf]651        Store the name and value of a parameter of this fitproblem's model
652        :param name: name of the given parameter
653        :param value: value of that parameter
[d89f09b]654        """
[8e81af0]655        self.list_param.append([name,value])
[925a30e]656       
[00561739]657    def get_model_param(self):
658        """
[5062bbf]659        return list of couple of parameter name and value
[00561739]660        """
[8e81af0]661        return self.list_param
[d89f09b]662       
[948add7]663    def schedule_tofit(self, schedule=0):
[3b19ac9]664        """
[5062bbf]665        set schedule to true to decide if this fit  must be performed
[3b19ac9]666        """
[3e3ab46]667        self.schedule = schedule
[6bcdad1]668       
[3b19ac9]669    def get_scheduled(self):
[5062bbf]670        """
671        return true or false if a problem as being schedule for fitting
672        """
[3b19ac9]673        return self.schedule
[925a30e]674   
[2140e68]675    def set_range(self, qmin=None, qmax=None):
676        """
[3e3ab46]677        set fitting range
678        :param qmin: minimum value to consider for the fit range
679        :param qmax: maximum value to consider for the fit range
[2140e68]680        """
681        self.qmin = qmin
682        self.qmax = qmax
683       
684    def get_range(self):
685        """
[5062bbf]686        :return: fitting range
687       
[2140e68]688        """
689        return self.qmin, self.qmax
[925a30e]690   
[9e27de9]691    def clear_model_param(self):
692        """
693        clear constraint info
694        """
[3e3ab46]695        self.list_param = []
[6bbeacd4]696       
697    def set_fit_tab_caption(self, caption):
698        """
699        """
700        self.fit_tab_caption = str(caption)
701       
702    def get_fit_tab_caption(self):
703        """
704        """
705        return self.fit_tab_caption
[ba1f0b2]706   
[5e48acb]707    def set_graph_id(self, id):
708        """
709        Set graph id (from data_group_id at the time the graph produced)
710        """
711        self.graph_id = id
712       
713    def get_graph_id(self): 
714        """
715        Get graph_id
716        """ 
717        return self.graph_id
[41661a0]718   
719    def set_result(self, result):
720        """
721        """
722        self.result = result
723       
724    def get_result(self):
725        """
726        get result
727        """   
728        return self.result
Note: See TracBrowser for help on using the repository browser.