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

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 2b440504 was 5e48acb, checked in by Jae Cho <jhjcho@…>, 13 years ago

fixed group_id related problem when set data in fitpage and added feature that send multiple data sets on import

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