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

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 0d7426b was f64a4b7, checked in by Gervaise Alina <gervyh@…>, 13 years ago

working display residuals

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