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

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 dadf255 was 05325ec4, checked in by Jae Cho <jhjcho@…>, 13 years ago

fixed weighting problem for use dydata

  • Property mode set to 100644
File size: 17.6 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        """
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        """
[f7ef313]90    def set_weight(self, flag=None):
[55bb249c]91        """
92        set fitting range
93        """
94    def get_weight(self):
95        """
96        get fitting weight
97        """
[3e3ab46]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   
112class 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)
[55bb249c]267                #self[d.id].set_smearer(self[d.id].get_smearer())
[3e3ab46]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       
[f7ef313]332    def set_weight(self,  is2d, flag=None, fid=None):
[55bb249c]333        """
334        fit weight
335        """
336        if fid is None:
337            for value in self.itervalues():
[f7ef313]338                value.set_weight(flag=flag, is2d=is2d)
[55bb249c]339        else:
340            if fid in self.iterkeys():
[f7ef313]341                self[fid].set_weight(flag=flag, is2d=is2d)
[55bb249c]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                 
[3e3ab46]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
380class FitProblem(FitProblemComponent):
[d89f09b]381    """ 
[5062bbf]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.
[d89f09b]385    """
386    def __init__(self):
[3e3ab46]387        FitProblemComponent.__init__(self)
[d89f09b]388        """
[5062bbf]389        contains information about data and model to fit
[d89f09b]390        """
[925a30e]391        ## data used for fitting
[6bbeacd4]392        self.fit_data = None
393        self.theory_data = None
[05325ec4]394        # original data: should not be modified
395        self.original_data = None
[2140e68]396        ## the current model
397        self.model = None
[925a30e]398        ## if 1 this fit problem will be selected to fit , if 0
399        ## it will not be selected for fit
[6bbeacd4]400        self.schedule = 0
[925a30e]401        ##list containing parameter name and value
[6bbeacd4]402        self.list_param = []
[925a30e]403        ## smear object to smear or not data1D
[7afcae8]404        self.smearer_computed = False
[3e3ab46]405        self.smearer_enable = False
406        self.smearer_computer_value = None
[2140e68]407        ## fitting range
408        self.qmin = None
409        self.qmax = None
[55bb249c]410        # fit weight
411        self.weight = None
[08b9c6c8]412       
[ba1f0b2]413       
[3e3ab46]414    def enable_smearing(self, flag=False):
[9853ad0]415        """
[3e3ab46]416        :param flag: bool.When flag is 1 get the computer smear value. When
417        flag is 0 ingore smear value.
[9853ad0]418        """
[3e3ab46]419        self.smearer_enable = flag
[9853ad0]420       
[08b9c6c8]421    def set_smearer(self, smearer):
[925a30e]422        """
[5062bbf]423        save reference of  smear object on fitdata
424       
425        :param smear: smear object from DataLoader
426       
[925a30e]427        """
[3e3ab46]428        self.smearer_computer_value = smearer
[925a30e]429       
[08b9c6c8]430    def get_smearer(self):
[925a30e]431        """
[5062bbf]432        return smear object
[925a30e]433        """
[3e3ab46]434        if not self.smearer_enable:
435            return None
[7afcae8]436        if not self.smearer_computed:
[3e3ab46]437            #smeari_selection should be call only once per fitproblem
[7afcae8]438            self.smearer_computer_value = smear_selection(self.fit_data,
[3e3ab46]439                                                           self.model)
[7afcae8]440            self.smearer_computed = True
[3e3ab46]441        return self.smearer_computer_value
[08b9c6c8]442   
[5062bbf]443    def save_model_name(self, name):
444        """
445        """ 
[bb18ef1]446        self.name_per_page= name
447       
448    def get_name(self):
[5062bbf]449        """
450        """
[bb18ef1]451        return self.name_per_page
[08b9c6c8]452   
[8aa5788]453    def set_model(self, model):
[d89f09b]454        """
[5062bbf]455        associates each model with its new created name
456        :param model: model selected
457        :param name: name created for model
[d89f09b]458        """
[7afcae8]459        self.model = model
460        self.smearer_computer_value = smear_selection(self.fit_data,
461                                                           self.model)
462        self.smearer_computed = True
[bb18ef1]463       
[2140e68]464    def get_model(self):
[5062bbf]465        """
466        :return: saved model
467        """
[2140e68]468        return self.model
469 
[6bbeacd4]470    def set_theory_data(self, data):
[d89f09b]471        """
[5062bbf]472        save a copy of the data select to fit
473       
474        :param data: data selected
475       
[d89f09b]476        """
[e88ebfd]477        self.theory_data = copy.deepcopy(data)
[ba1f0b2]478       
[3e3ab46]479 
[ba1f0b2]480         
[6bbeacd4]481    def get_theory_data(self):
[5062bbf]482        """
[3e3ab46]483        :return: theory generated with the current model and data of this class
[5062bbf]484        """
[6bbeacd4]485        return self.theory_data
[5062bbf]486
[3e3ab46]487    def set_fit_data(self, data):
[2a8fac1]488        """
[3e3ab46]489        Store data associated with this class
490        :param data: list of data selected
[2a8fac1]491        """
[05325ec4]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
[55bb249c]495        self.fit_data = copy.deepcopy(data)
[7afcae8]496        self.smearer_computer_value = smear_selection(self.fit_data,
497                                                           self.model)
498        self.smearer_computed = True
[f7ef313]499       
[2a8fac1]500    def get_fit_data(self):
[5062bbf]501        """
[3e3ab46]502        :return: data associate with this class
[5062bbf]503        """
[2a8fac1]504        return self.fit_data
505   
[f7ef313]506    def set_weight(self, is2d, flag=None):
[55bb249c]507        """
[f7ef313]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
[55bb249c]511        """
[f7ef313]512        from .utils import get_weight
[05325ec4]513        # send original data for weighting
514        self.weight = get_weight(data=self.original_data, is2d=is2d, flag=flag)
[55bb249c]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       
[2140e68]526    def set_model_param(self,name,value=None):
[d89f09b]527        """
[5062bbf]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
[d89f09b]531        """
[8e81af0]532        self.list_param.append([name,value])
[925a30e]533       
[00561739]534    def get_model_param(self):
535        """
[5062bbf]536        return list of couple of parameter name and value
[00561739]537        """
[8e81af0]538        return self.list_param
[d89f09b]539       
[948add7]540    def schedule_tofit(self, schedule=0):
[3b19ac9]541        """
[5062bbf]542        set schedule to true to decide if this fit  must be performed
[3b19ac9]543        """
[3e3ab46]544        self.schedule = schedule
[6bcdad1]545       
[3b19ac9]546    def get_scheduled(self):
[5062bbf]547        """
548        return true or false if a problem as being schedule for fitting
549        """
[3b19ac9]550        return self.schedule
[925a30e]551   
[2140e68]552    def set_range(self, qmin=None, qmax=None):
553        """
[3e3ab46]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
[2140e68]557        """
558        self.qmin = qmin
559        self.qmax = qmax
560       
561    def get_range(self):
562        """
[5062bbf]563        :return: fitting range
564       
[2140e68]565        """
566        return self.qmin, self.qmax
[925a30e]567   
[9e27de9]568    def clear_model_param(self):
569        """
570        clear constraint info
571        """
[3e3ab46]572        self.list_param = []
[6bbeacd4]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
[ba1f0b2]583   
[3e3ab46]584 
[5062bbf]585   
Note: See TracBrowser for help on using the repository browser.