Changeset 50fcb09 in sasview for src/sas/sascalc


Ignore:
Timestamp:
Jun 27, 2017 10:18:33 AM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
251ef684
Parents:
65f3930
Message:

move qsmearing to fit package

Location:
src/sas/sascalc
Files:
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/fit/AbstractFitEngine.py

    ra1b8fee r50fcb09  
    137137               that will smear the theory data (slit smearing or resolution 
    138138               smearing) when set. 
    139              
     139 
    140140            The proper way to set the smearing object would be to 
    141141            do the following: :: 
    142              
    143                 from sas.sascalc.data_util.qsmearing import smear_selection 
     142 
     143                from sas.sascalc.fit.qsmearing import smear_selection 
    144144                smearer = smear_selection(some_data) 
    145145                fitdata1d = FitData1D( x= [1,3,..,], 
     
    147147                                        dx=None, 
    148148                                        dy=[1,2...], smearer= smearer) 
    149             
     149 
    150150            :Note: that some_data _HAS_ to be of 
    151151                class DataLoader.data_info.Data1D 
    152152                Setting it back to None will turn smearing off. 
    153                  
     153 
    154154        """ 
    155155        Data1D.__init__(self, x=x, y=y, dx=dx, dy=dy, lam=lam, dlam=dlam) 
     
    176176        ## Max Q-value 
    177177        self.qmax = max(self.x) 
    178          
     178 
    179179        # Range used for input to smearing 
    180180        self._qmin_unsmeared = self.qmin 
     
    184184        self.idx_unsmeared = (self.x >= self._qmin_unsmeared) \ 
    185185                            & (self.x <= self._qmax_unsmeared) 
    186    
     186 
    187187    def set_fit_range(self, qmin=None, qmax=None): 
    188188        """ to set the fit range""" 
     
    199199        self._qmin_unsmeared = self.qmin 
    200200        self._qmax_unsmeared = self.qmax 
    201          
     201 
    202202        self._first_unsmeared_bin = 0 
    203203        self._last_unsmeared_bin = len(self.x) - 1 
    204          
     204 
    205205        if self.smearer is not None: 
    206206            self._first_unsmeared_bin, self._last_unsmeared_bin = \ 
     
    208208            self._qmin_unsmeared = self.x[self._first_unsmeared_bin] 
    209209            self._qmax_unsmeared = self.x[self._last_unsmeared_bin] 
    210              
     210 
    211211        # Identify the bin range for the unsmeared and smeared spaces 
    212212        self.idx = (self.x >= self.qmin) & (self.x <= self.qmax) 
     
    231231        """ 
    232232            Compute residuals. 
    233              
     233 
    234234            If self.smearer has been set, use if to smear 
    235235            the data before computing chi squared. 
    236              
     236 
    237237            :param fn: function that return model value 
    238              
     238 
    239239            :return: residuals 
    240240        """ 
     
    242242        fx = np.zeros(len(self.x)) 
    243243        fx[self.idx_unsmeared] = fn(self.x[self.idx_unsmeared]) 
    244         
     244 
    245245        ## Smear theory data 
    246246        if self.smearer is not None: 
     
    253253            raise RuntimeError, msg 
    254254        return (self.y[self.idx] - fx[self.idx]) / self.dy[self.idx], fx[self.idx] 
    255              
     255 
    256256    def residuals_deriv(self, model, pars=[]): 
    257257        """ 
    258258            :return: residuals derivatives . 
    259              
    260             :note: in this case just return empty array  
     259 
     260            :note: in this case just return empty array 
    261261        """ 
    262262        return [] 
     
    293293        x_max = max(math.fabs(sas_data2d.xmin), math.fabs(sas_data2d.xmax)) 
    294294        y_max = max(math.fabs(sas_data2d.ymin), math.fabs(sas_data2d.ymax)) 
    295          
     295 
    296296        ## fitting range 
    297297        if qmin is None: 
     
    305305            self.res_err_data = copy.deepcopy(self.err_data) 
    306306        #self.res_err_data[self.res_err_data==0]=1 
    307          
     307 
    308308        self.radius = np.sqrt(self.qx_data**2 + self.qy_data**2) 
    309          
     309 
    310310        # Note: mask = True: for MASK while mask = False for NOT to mask 
    311311        self.idx = ((self.qmin <= self.radius) &\ 
     
    368368 
    369369        return res, gn 
    370          
     370 
    371371    def residuals_deriv(self, model, pars=[]): 
    372372        """ 
    373373        :return: residuals derivatives . 
    374          
     374 
    375375        :note: in this case just return empty array 
    376          
     376 
    377377        """ 
    378378        return [] 
    379      
    380      
     379 
     380 
    381381class FitAbort(Exception): 
    382382    """ 
     
    396396        self.fit_arrange_dict = {} 
    397397        self.fitter_id = None 
    398          
     398 
    399399    def set_model(self, model, id, pars=[], constraints=[], data=None): 
    400400        """ 
    401401        set a model on a given  in the fit engine. 
    402          
    403         :param model: sas.models type  
     402 
     403        :param model: sas.models type 
    404404        :param id: is the key of the fitArrange dictionary where model is saved as a value 
    405         :param pars: the list of parameters to fit  
    406         :param constraints: list of  
     405        :param pars: the list of parameters to fit 
     406        :param constraints: list of 
    407407            tuple (name of parameter, value of parameters) 
    408408            the value of parameter must be a string to constraint 2 different 
    409409            parameters. 
    410             Example:   
     410            Example: 
    411411            we want to fit 2 model M1 and M2 both have parameters A and B. 
    412412            constraints can be ``constraints = [(M1.A, M2.B+2), (M1.B= M2.A *5),...,]`` 
    413              
    414               
     413 
     414 
    415415        :note: pars must contains only name of existing model's parameters 
    416          
     416 
    417417        """ 
    418418        if not pars: 
     
    445445        in a FitArrange object and adds that object in a dictionary 
    446446        with key id. 
    447          
     447 
    448448        :param data: data added 
    449449        :param id: unique key corresponding to a fitArrange object with data 
     
    456456                                 dx=data.dx, dy=data.dy, smearer=smearer) 
    457457        fitdata.sas_data = data 
    458         
     458 
    459459        fitdata.set_fit_range(qmin=qmin, qmax=qmax) 
    460460        #A fitArrange is already created but contains model only at id 
     
    466466            fitproblem.add_data(fitdata) 
    467467            self.fit_arrange_dict[id] = fitproblem 
    468     
     468 
    469469    def get_model(self, id): 
    470470        """ 
    471471        :param id: id is key in the dictionary containing the model to return 
    472          
     472 
    473473        :return:  a model at this id or None if no FitArrange element was 
    474474            created with this id 
     
    478478        else: 
    479479            return None 
    480      
     480 
    481481    def remove_fit_problem(self, id): 
    482482        """remove   fitarrange in id""" 
    483483        if id in self.fit_arrange_dict: 
    484484            del self.fit_arrange_dict[id] 
    485              
     485 
    486486    def select_problem_for_fit(self, id, value): 
    487487        """ 
    488488        select a couple of model and data at the id position in dictionary 
    489489        and set in self.selected value to value 
    490          
     490 
    491491        :param value: the value to allow fitting. 
    492492                can only have the value one or zero 
     
    494494        if id in self.fit_arrange_dict: 
    495495            self.fit_arrange_dict[id].set_to_fit(value) 
    496               
     496 
    497497    def get_problem_to_fit(self, id): 
    498498        """ 
    499499        return the self.selected value of the fit problem of id 
    500          
     500 
    501501        :param id: the id of the problem 
    502502        """ 
    503503        if id in self.fit_arrange_dict: 
    504504            self.fit_arrange_dict[id].get_to_fit() 
    505      
    506      
     505 
     506 
    507507class FitArrange: 
    508508    def __init__(self): 
     
    511511        to perform the Fit.FitArrange must contain exactly one model 
    512512        and at least one data for the fit to be performed. 
    513          
     513 
    514514        model: the model selected by the user 
    515515        Ldata: a list of data what the user wants to fit 
    516              
     516 
    517517        """ 
    518518        self.model = None 
     
    525525        """ 
    526526        set_model save a copy of the model 
    527          
     527 
    528528        :param model: the model being set 
    529529        """ 
    530530        self.model = model 
    531          
     531 
    532532    def add_data(self, data): 
    533533        """ 
    534534        add_data fill a self.data_list with data to fit 
    535          
     535 
    536536        :param data: Data to add in the list 
    537537        """ 
    538538        if not data in self.data_list: 
    539539            self.data_list.append(data) 
    540              
     540 
    541541    def get_model(self): 
    542542        """ 
     
    544544        """ 
    545545        return self.model 
    546       
     546 
    547547    def get_data(self): 
    548548        """ 
     
    550550        """ 
    551551        return self.data_list[0] 
    552        
     552 
    553553    def remove_data(self, data): 
    554554        """ 
    555555        Remove one element from the list 
    556          
     556 
    557557        :param data: Data to remove from data_list 
    558558        """ 
    559559        if data in self.data_list: 
    560560            self.data_list.remove(data) 
    561              
     561 
    562562    def set_to_fit(self, value=0): 
    563563        """ 
    564564        set self.selected to 0 or 1  for other values raise an exception 
    565          
     565 
    566566        :param value: integer between 0 or 1 
    567567        """ 
    568568        self.selected = value 
    569          
     569 
    570570    def get_to_fit(self): 
    571571        """ 
     
    599599        if self.model is not None and self.data is not None: 
    600600            self.inputs = [(self.model, self.data)] 
    601       
     601 
    602602    def set_model(self, model): 
    603603        """ 
    604604        """ 
    605605        self.model = model 
    606          
     606 
    607607    def set_fitness(self, fitness): 
    608608        """ 
    609609        """ 
    610610        self.fitness = fitness 
    611          
     611 
    612612    def __str__(self): 
    613613        """ 
     
    624624        msg = [msg1, msg3] + msg2 
    625625        return "\n".join(msg) 
    626      
     626 
    627627    def print_summary(self): 
    628628        """ 
  • src/sas/sascalc/fit/qsmearing.py

    r8938502 r50fcb09  
    55#This software was developed by the University of Tennessee as part of the 
    66#Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
    7 #project funded by the US National Science Foundation.  
     7#project funded by the US National Science Foundation. 
    88#See the license text in license.txt 
    99#copyright 2008, University of Tennessee 
     
    1919from sasmodels.sesans import SesansTransform 
    2020from sasmodels.resolution2d import Pinhole2D 
    21 from .nxsunit import Converter 
     21 
     22from sas.sascalc.data_util.nxsunit import Converter 
    2223 
    2324def smear_selection(data, model = None): 
  • src/sas/sascalc/pr/fit/AbstractFitEngine.py

    ra1b8fee r50fcb09  
    137137               that will smear the theory data (slit smearing or resolution 
    138138               smearing) when set. 
    139              
     139 
    140140            The proper way to set the smearing object would be to 
    141141            do the following: :: 
    142              
    143                 from sas.sascalc.data_util.qsmearing import smear_selection 
     142 
     143                from sas.sascalc.fit.qsmearing import smear_selection 
    144144                smearer = smear_selection(some_data) 
    145145                fitdata1d = FitData1D( x= [1,3,..,], 
     
    147147                                        dx=None, 
    148148                                        dy=[1,2...], smearer= smearer) 
    149             
     149 
    150150            :Note: that some_data _HAS_ to be of 
    151151                class DataLoader.data_info.Data1D 
    152152                Setting it back to None will turn smearing off. 
    153                  
     153 
    154154        """ 
    155155        Data1D.__init__(self, x=x, y=y, dx=dx, dy=dy) 
     
    176176        ## Max Q-value 
    177177        self.qmax = max(self.x) 
    178          
     178 
    179179        # Range used for input to smearing 
    180180        self._qmin_unsmeared = self.qmin 
     
    184184        self.idx_unsmeared = (self.x >= self._qmin_unsmeared) \ 
    185185                            & (self.x <= self._qmax_unsmeared) 
    186    
     186 
    187187    def set_fit_range(self, qmin=None, qmax=None): 
    188188        """ to set the fit range""" 
     
    199199        self._qmin_unsmeared = self.qmin 
    200200        self._qmax_unsmeared = self.qmax 
    201          
     201 
    202202        self._first_unsmeared_bin = 0 
    203203        self._last_unsmeared_bin = len(self.x) - 1 
    204          
     204 
    205205        if self.smearer is not None: 
    206206            self._first_unsmeared_bin, self._last_unsmeared_bin = \ 
     
    208208            self._qmin_unsmeared = self.x[self._first_unsmeared_bin] 
    209209            self._qmax_unsmeared = self.x[self._last_unsmeared_bin] 
    210              
     210 
    211211        # Identify the bin range for the unsmeared and smeared spaces 
    212212        self.idx = (self.x >= self.qmin) & (self.x <= self.qmax) 
     
    231231        """ 
    232232            Compute residuals. 
    233              
     233 
    234234            If self.smearer has been set, use if to smear 
    235235            the data before computing chi squared. 
    236              
     236 
    237237            :param fn: function that return model value 
    238              
     238 
    239239            :return: residuals 
    240240        """ 
     
    242242        fx = np.zeros(len(self.x)) 
    243243        fx[self.idx_unsmeared] = fn(self.x[self.idx_unsmeared]) 
    244         
     244 
    245245        ## Smear theory data 
    246246        if self.smearer is not None: 
     
    253253            raise RuntimeError, msg 
    254254        return (self.y[self.idx] - fx[self.idx]) / self.dy[self.idx], fx[self.idx] 
    255              
     255 
    256256    def residuals_deriv(self, model, pars=[]): 
    257257        """ 
    258258            :return: residuals derivatives . 
    259              
    260             :note: in this case just return empty array  
     259 
     260            :note: in this case just return empty array 
    261261        """ 
    262262        return [] 
     
    293293        x_max = max(math.fabs(sas_data2d.xmin), math.fabs(sas_data2d.xmax)) 
    294294        y_max = max(math.fabs(sas_data2d.ymin), math.fabs(sas_data2d.ymax)) 
    295          
     295 
    296296        ## fitting range 
    297297        if qmin is None: 
     
    305305            self.res_err_data = copy.deepcopy(self.err_data) 
    306306        #self.res_err_data[self.res_err_data==0]=1 
    307          
     307 
    308308        self.radius = np.sqrt(self.qx_data**2 + self.qy_data**2) 
    309          
     309 
    310310        # Note: mask = True: for MASK while mask = False for NOT to mask 
    311311        self.idx = ((self.qmin <= self.radius) &\ 
     
    371371 
    372372        return res, gn 
    373          
     373 
    374374    def residuals_deriv(self, model, pars=[]): 
    375375        """ 
    376376        :return: residuals derivatives . 
    377          
     377 
    378378        :note: in this case just return empty array 
    379          
     379 
    380380        """ 
    381381        return [] 
    382      
    383      
     382 
     383 
    384384class FitAbort(Exception): 
    385385    """ 
     
    399399        self.fit_arrange_dict = {} 
    400400        self.fitter_id = None 
    401          
     401 
    402402    def set_model(self, model, id, pars=[], constraints=[], data=None): 
    403403        """ 
    404404        set a model on a given  in the fit engine. 
    405          
    406         :param model: sas.models type  
     405 
     406        :param model: sas.models type 
    407407        :param id: is the key of the fitArrange dictionary where model is saved as a value 
    408         :param pars: the list of parameters to fit  
    409         :param constraints: list of  
     408        :param pars: the list of parameters to fit 
     409        :param constraints: list of 
    410410            tuple (name of parameter, value of parameters) 
    411411            the value of parameter must be a string to constraint 2 different 
    412412            parameters. 
    413             Example:   
     413            Example: 
    414414            we want to fit 2 model M1 and M2 both have parameters A and B. 
    415415            constraints can be ``constraints = [(M1.A, M2.B+2), (M1.B= M2.A *5),...,]`` 
    416              
    417               
     416 
     417 
    418418        :note: pars must contains only name of existing model's parameters 
    419          
     419 
    420420        """ 
    421421        if not pars: 
     
    448448        in a FitArrange object and adds that object in a dictionary 
    449449        with key id. 
    450          
     450 
    451451        :param data: data added 
    452452        :param id: unique key corresponding to a fitArrange object with data 
     
    459459                                 dx=data.dx, dy=data.dy, smearer=smearer) 
    460460        fitdata.sas_data = data 
    461         
     461 
    462462        fitdata.set_fit_range(qmin=qmin, qmax=qmax) 
    463463        #A fitArrange is already created but contains model only at id 
     
    469469            fitproblem.add_data(fitdata) 
    470470            self.fit_arrange_dict[id] = fitproblem 
    471     
     471 
    472472    def get_model(self, id): 
    473473        """ 
    474474        :param id: id is key in the dictionary containing the model to return 
    475          
     475 
    476476        :return:  a model at this id or None if no FitArrange element was 
    477477            created with this id 
     
    481481        else: 
    482482            return None 
    483      
     483 
    484484    def remove_fit_problem(self, id): 
    485485        """remove   fitarrange in id""" 
    486486        if id in self.fit_arrange_dict: 
    487487            del self.fit_arrange_dict[id] 
    488              
     488 
    489489    def select_problem_for_fit(self, id, value): 
    490490        """ 
    491491        select a couple of model and data at the id position in dictionary 
    492492        and set in self.selected value to value 
    493          
     493 
    494494        :param value: the value to allow fitting. 
    495495                can only have the value one or zero 
     
    497497        if id in self.fit_arrange_dict: 
    498498            self.fit_arrange_dict[id].set_to_fit(value) 
    499               
     499 
    500500    def get_problem_to_fit(self, id): 
    501501        """ 
    502502        return the self.selected value of the fit problem of id 
    503          
     503 
    504504        :param id: the id of the problem 
    505505        """ 
    506506        if id in self.fit_arrange_dict: 
    507507            self.fit_arrange_dict[id].get_to_fit() 
    508      
    509      
     508 
     509 
    510510class FitArrange: 
    511511    def __init__(self): 
     
    514514        to perform the Fit.FitArrange must contain exactly one model 
    515515        and at least one data for the fit to be performed. 
    516          
     516 
    517517        model: the model selected by the user 
    518518        Ldata: a list of data what the user wants to fit 
    519              
     519 
    520520        """ 
    521521        self.model = None 
     
    528528        """ 
    529529        set_model save a copy of the model 
    530          
     530 
    531531        :param model: the model being set 
    532532        """ 
    533533        self.model = model 
    534          
     534 
    535535    def add_data(self, data): 
    536536        """ 
    537537        add_data fill a self.data_list with data to fit 
    538          
     538 
    539539        :param data: Data to add in the list 
    540540        """ 
    541541        if not data in self.data_list: 
    542542            self.data_list.append(data) 
    543              
     543 
    544544    def get_model(self): 
    545545        """ 
     
    547547        """ 
    548548        return self.model 
    549       
     549 
    550550    def get_data(self): 
    551551        """ 
     
    553553        """ 
    554554        return self.data_list[0] 
    555        
     555 
    556556    def remove_data(self, data): 
    557557        """ 
    558558        Remove one element from the list 
    559          
     559 
    560560        :param data: Data to remove from data_list 
    561561        """ 
    562562        if data in self.data_list: 
    563563            self.data_list.remove(data) 
    564              
     564 
    565565    def set_to_fit(self, value=0): 
    566566        """ 
    567567        set self.selected to 0 or 1  for other values raise an exception 
    568          
     568 
    569569        :param value: integer between 0 or 1 
    570570        """ 
    571571        self.selected = value 
    572          
     572 
    573573    def get_to_fit(self): 
    574574        """ 
     
    602602        if self.model is not None and self.data is not None: 
    603603            self.inputs = [(self.model, self.data)] 
    604       
     604 
    605605    def set_model(self, model): 
    606606        """ 
    607607        """ 
    608608        self.model = model 
    609          
     609 
    610610    def set_fitness(self, fitness): 
    611611        """ 
    612612        """ 
    613613        self.fitness = fitness 
    614          
     614 
    615615    def __str__(self): 
    616616        """ 
     
    627627        msg = [msg1, msg3] + msg2 
    628628        return "\n".join(msg) 
    629      
     629 
    630630    def print_summary(self): 
    631631        """ 
Note: See TracChangeset for help on using the changeset viewer.