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

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

added EZ setall

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