Changeset 499fe7a in sasview


Ignore:
Timestamp:
Apr 18, 2012 11:51:32 AM (12 years ago)
Author:
Mathieu Doucet <doucetm@…>
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, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
2dda7ae1
Parents:
279e371
Message:

Fixing code style problems

Location:
sansmodels/src/sans/models
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • sansmodels/src/sans/models/PorodModel.py

    r411d8bf r499fe7a  
    1 #!/usr/bin/env python 
    21"""  
    32    Provide I(q) = C/q^4, 
    43    Porod function as a BaseComponent model 
    54""" 
    6  
    75from sans.models.BaseComponent import BaseComponent 
    86import math 
    97 
    108class PorodModel(BaseComponent): 
    11     """ Class that evaluates a Porod model. 
    12      
    13        I(q) = scale/q^4 +background 
    14          
     9    """  
     10        Class that evaluates a Porod model. 
     11        I(q) = scale/q^4 +background 
    1512    """ 
    1613         
     
    2825        self.params['scale'] = 1.0 
    2926        self.params['background'] = 0.0 
    30         self.description= """The Porod model. 
     27        self.description = """The Porod model. 
    3128        I(q) = scale/q^4 +background""" 
    3229 
     
    3936                
    4037    def _porod(self, x): 
     38        """ 
     39            Evaluate Porod function 
     40            :param x: q-value 
     41        """ 
    4142        return self.params['scale']/x**4.0 + self.params['background'] 
    4243    
     
    4950            return self._porod(x[0]) 
    5051        elif x.__class__.__name__ == 'tuple': 
    51             raise ValueError, "Tuples are not allowed as input to BaseComponent models" 
     52            raise ValueError, "Tuples are not allowed as input to models" 
    5253        else: 
    5354            return self._porod(x) 
     
    6263            return self._porod(q) 
    6364        elif x.__class__.__name__ == 'tuple': 
    64             raise ValueError, "Tuples are not allowed as input to BaseComponent models" 
     65            raise ValueError, "Tuples are not allowed as input to models" 
    6566        else: 
    6667            return self._porod(x) 
  • sansmodels/src/sans/models/PowerLawAbsModel.py

    r9ce41c6 r499fe7a  
    1 #!/usr/bin/env python 
    21"""  
    32    Provide F(x) = scale* (|x|)^(-m) + bkd 
    43    Power law function as a BaseComponent model 
    54""" 
    6  
    75from sans.models.PowerLawModel import PowerLawModel 
    86import math 
     7 
    98class PowerLawAbsModel(PowerLawModel): 
    109    """ 
     
    2524        ## Name of the model 
    2625        self.name = "Absolute Power_Law" 
    27         self.description=""" The Power_Law model. 
     26        self.description = """ The Power_Law model. 
    2827        F(x) = scale* (|x|)^(-m) + bkd 
    2928         
  • sansmodels/src/sans/models/PowerLawModel.py

    r9ce41c6 r499fe7a  
    1 #!/usr/bin/env python 
    21"""  
    32    Provide F(x) = scale* (x)^(-m) + bkd 
    43    Power law function as a BaseComponent model 
    54""" 
    6  
    75from sans.models.BaseComponent import BaseComponent 
    86import math 
    97 
    108class PowerLawModel(BaseComponent): 
    11     
    129    """ 
    1310        Class that evaluates a Power_Law model. 
     
    3532        self.params['scale']        = 1.0 
    3633        self.params['background']   = 0.0 
    37         self.description=""" The Power_Law model. 
     34        self.description = """ The Power_Law model. 
    3835        F(x) = scale* (x)^(-m) + bkd 
    3936         
     
    4845        self.details['background']  = ['[1/cm]', None, None] 
    4946        #list of parameter that cannot be fitted 
    50         self.fixed= []     
     47        self.fixed = []     
    5148    def _PowerLaw(self, x): 
    5249        """ 
    5350            Evaluate  F(x) = scale* (x)^(-m) + bkd 
    54             
     51            :param x: q-value 
    5552        """ 
    56         #if x!=0 and self.params['m']!=0: 
    57         #   raise ValueError, "negative number cannot be raised to a fractional power" 
    58         if self.params['m']>0  and x==0: 
     53        if self.params['m'] > 0 and x == 0: 
    5954            return 1e+32 
    60         elif self.params['m']==0  and x==0: 
     55        elif self.params['m'] == 0 and x == 0: 
    6156            return 1 
    6257        else: 
    63             return self.params['scale']*math.pow(x ,-1.0*self.params['m'])\ 
     58            return self.params['scale']*math.pow(x, -1.0*self.params['m'])\ 
    6459                + self.params['background'] 
    6560        
    6661    def run(self, x = 0.0): 
    6762        """ Evaluate the model 
    68             @param x: input q-value (float or [float, float] as [r, theta]) 
    69             @return: (PowerLaw value) 
     63            :param x: input q-value (float or [float, float] as [r, theta]) 
     64            :return: (PowerLaw value) 
    7065        """ 
    7166        if x.__class__.__name__ == 'list': 
     
    7671            return self._PowerLaw(math.fabs(x[0])) 
    7772        elif x.__class__.__name__ == 'tuple': 
    78             raise ValueError, "Tuples are not allowed as input to BaseComponent models" 
     73            raise ValueError, "Tuples are not allowed as input to models" 
    7974        else: 
    8075            return self._PowerLaw(x) 
     
    8984            return self._PowerLaw(q) 
    9085        elif x.__class__.__name__ == 'tuple': 
    91             raise ValueError, "Tuples are not allowed as input to BaseComponent models" 
     86            raise ValueError, "Tuples are not allowed as input to models" 
    9287        else: 
    9388            return self._PowerLaw(x) 
  • sansmodels/src/sans/models/ReflectivityIIModel.py

    r1352c78 r499fe7a  
    55from math import floor 
    66from math import fabs 
    7 from scipy.special import erf 
    87func_list = {'Erf(|nu|*z)':0, 'RPower(z^|nu|)':1, 'LPower(z^|nu|)':2, \ 
    98                     'RExp(-|nu|*z)':3, 'LExp(-|nu|*z)':4} 
     
    1514    """ 
    1615    def __init__(self, multfactor=1): 
     16        """ 
     17            :param multfactor: number of layers in the model,  
     18            assumes 0<= n_layers <=10. 
     19        """ 
    1720        BaseComponent.__init__(self) 
    18         """ 
    19         :param multfactor: number of layers in the model,  
    20         assumes 0<= n_layers <=10. 
    21         """ 
    2221 
    2322        ## Setting  model name model description 
    24         self.description="" 
     23        self.description = "" 
    2524        model = ReflAdvModel() 
    2625        self.model = model 
    2726        self.name = "ReflectivityIIModel" 
    28         self.description=model.description 
     27        self.description = model.description 
    2928        self.n_layers = int(multfactor) 
    3029        ## Define parameters 
     
    5453        # [int(maximum no. of functionality),"str(Titl), 
    5554        # [str(name of function0),...], [str(x-asix name of sld),...]] 
    56         self.multiplicity_info = [max_nshells,"No. of Layers:",[],['Depth']] 
     55        self.multiplicity_info = [max_nshells, "No. of Layers:", [], ['Depth']] 
    5756        ## independent parameter name and unit [string] 
    5857        self.input_name = "Q" 
     
    9291        """ 
    9392        # rearrange the parameters for the given # of shells 
    94         for name , value in self.model.params.iteritems(): 
     93        for name, value in self.model.params.iteritems(): 
    9594            n = 0 
    9695            pos = len(name.split('_'))-1 
     
    103102                continue 
    104103            elif first_name == 'func': 
    105                 n= -1 
    106                 while n<self.n_layers: 
     104                n = -1 
     105                while n < self.n_layers: 
    107106                    n += 1 
    108107                    if last_name == 'inter%s' % str(n):  
    109                         self.params[name]=value 
     108                        self.params[name] = value 
    110109                        continue 
    111110             
    112111                #continue 
    113112            elif last_name[0:5] == 'inter': 
    114                 n= -1 
    115                 while n<self.n_layers: 
     113                n = -1 
     114                while n < self.n_layers: 
    116115                    n += 1 
    117116                    if last_name == 'inter%s' % str(n): 
    118                         self.params[name]= value 
     117                        self.params[name] = value 
    119118                        continue 
    120119            elif last_name[0:4] == 'flat': 
    121                 while n<self.n_layers: 
     120                while n < self.n_layers: 
    122121                    n += 1 
    123122                    if last_name == 'flat%s' % str(n): 
    124                         self.params[name]= value 
     123                        self.params[name] = value 
    125124                        continue 
    126125            elif name == 'n_layers': 
    127126                continue 
    128127            else: 
    129                 self.params[name]= value 
     128                self.params[name] = value 
    130129   
    131130        self.model.params['n_layers'] = self.n_layers     
     
    139138        this model details  
    140139        """ 
    141         for name ,detail in self.model.details.iteritems(): 
     140        for name, detail in self.model.details.iteritems(): 
    142141            if name in self.params.iterkeys(): 
    143                 self.details[name]= detail 
     142                self.details[name] = detail 
    144143             
    145144     
     
    155154                    continue 
    156155                if  key.split('_')[0] == 'func':  
    157                         self.model.setParam(key, 0) 
    158                         continue 
     156                    self.model.setParam(key, 0) 
     157                    continue 
    159158 
    160159                for nshell in range(self.n_layers,max_nshells): 
     
    166165                                value = self.model.params['sldIM_medium'] 
    167166                            self.model.setParam(key, value) 
    168                         except: pass 
     167                        except: 
     168                            message = "ReflectivityIIModel evaluation problem" 
     169                            raise RuntimeError, message 
    169170     
    170171    def _get_func_list(self): 
    171172        """ 
    172         Get the list of functions in each layer (shell)  
    173         """ 
    174         #func_list = {} 
     173            Get the list of functions in each layer (shell)  
     174        """ 
    175175        return func_list 
    176176         
     
    186186        z = [] 
    187187        beta = [] 
    188         sub_range = int(floor(n_sub/2.0)) 
    189188        z.append(0) 
    190189        beta.append(self.params['sld_bottom0'])  
     
    193192        dz = 0.0 
    194193        # for layers from the top 
    195         for n_lyr in range(1,self.n_layers+2): 
     194        for n_lyr in range(1, self.n_layers+2): 
    196195            i = n_lyr 
    197196            # j=0 for interface, j=1 for flat layer  
    198             for j in range(0,2): 
     197            for j in range(0, 2): 
    199198                # interation for sub-layers 
    200                 for n_s in range(0,n_sub): 
     199                for n_s in range(0, n_sub): 
    201200                    # for flat layer 
    202                     if j==1: 
    203                         if i==self.n_layers+1: 
     201                    if j == 1: 
     202                        if i == self.n_layers+1: 
    204203                            break 
    205204                        # shift half sub thickness for the first point 
    206205                        z0 -= dz/2.0 
    207206                        z.append(z0) 
    208                         sld_i = self.params['sld_flat%s'% str(i)] 
     207                        sld_i = self.params['sld_flat%s' % str(i)] 
    209208                        beta.append(sld_i) 
    210                         dz = self.params['thick_flat%s'% str(i)] 
     209                        dz = self.params['thick_flat%s' % str(i)] 
    211210                        z0 += dz 
    212211                    else: 
    213                         dz = self.params['thick_inter%s'% str(i-1)]/n_sub 
    214                         nu = fabs(self.params['nu_inter%s'% str(i-1)]) 
     212                        dz = self.params['thick_inter%s' % str(i-1)]/n_sub 
     213                        nu = fabs(self.params['nu_inter%s' % str(i-1)]) 
    215214                        if n_s == 0: 
    216215                            # shift half sub thickness for the point 
     
    220219                            sld_l = self.params['sld_bottom0'] 
    221220                        else: 
    222                             sld_l = self.params['sld_flat%s'% str(i-1)] 
     221                            sld_l = self.params['sld_flat%s' % str(i-1)] 
    223222                        if i == self.n_layers+1: 
    224223                            sld_r = self.params['sld_medium'] 
    225224                        else: 
    226                             sld_r = self.params['sld_flat%s'% str(i)] 
     225                            sld_r = self.params['sld_flat%s' % str(i)] 
    227226                        if sld_r == sld_l: 
    228227                            sld_i = sld_r 
    229228                        else: 
    230                             func_idx = self.params['func_inter%s'% str(i-1)] 
     229                            func_idx = self.params['func_inter%s' % str(i-1)] 
    231230                            # calculate the sld 
    232231                            sld_i = self._get_sld(func_idx, n_sub, n_s+0.5, nu, 
     
    235234                    z.append(z0) 
    236235                    beta.append(sld_i) 
    237                     if j==1: break 
    238                     else: z0 += dz 
     236                    if j == 1:  
     237                        break 
     238                    else:  
     239                        z0 += dz 
    239240        # put substrate and superstrate profile 
    240241        z.append(z0) 
     
    246247        z.append(z0+z_ext) 
    247248        beta.append(self.params['sld_medium'])  
    248         z.insert(0,-z_ext) 
    249         beta.insert(0,self.params['sld_bottom0'])  
     249        z.insert(0, -z_ext) 
     250        beta.insert(0, self.params['sld_bottom0'])  
    250251        # rearrange the profile for NR sld profile style 
    251252        z = [z0 - x for x in z] 
     
    269270        sld_cal = SLDCalFunc() 
    270271        # set params 
    271         sld_cal.setParam('fun_type',func_idx) 
    272         sld_cal.setParam('npts_inter',n_sub) 
    273         sld_cal.setParam('shell_num',n_s) 
    274         sld_cal.setParam('nu_inter',nu) 
    275         sld_cal.setParam('sld_left',sld_l) 
    276         sld_cal.setParam('sld_right',sld_r) 
     272        sld_cal.setParam('fun_type', func_idx) 
     273        sld_cal.setParam('npts_inter', n_sub) 
     274        sld_cal.setParam('shell_num', n_s) 
     275        sld_cal.setParam('nu_inter', nu) 
     276        sld_cal.setParam('sld_left', sld_l) 
     277        sld_cal.setParam('sld_right', sld_r) 
    277278        # return sld value 
    278279        return sld_cal.run() 
     
    289290         
    290291        ## setParam to model  
    291         if name=='sld_medium': 
     292        if name == 'sld_medium': 
    292293            # the sld_*** model.params not in params must set  
    293294            # to value of sld_solv 
    294295            for key in self.model.params.iterkeys(): 
    295296                if key not in self.params.keys()and key.split('_')[0] == 'sld': 
    296                         self.model.setParam(key, value)    
     297                    self.model.setParam(key, value)    
    297298         
    298299        self.model.setParam( name, value) 
     
    342343    ## Now (May27,10) directly uses the model eval function  
    343344    ## instead of the for-loop in Base Component. 
    344     def evalDistribution(self, x = []): 
     345    def evalDistribution(self, x): 
    345346        """  
    346347        Evaluate the model in cartesian coordinates 
  • sansmodels/src/sans/models/ReflectivityModel.py

    r1352c78 r499fe7a  
    77func_list = {'Erf':0, 'Linear':1} 
    88max_nshells = 10 
     9 
    910class ReflectivityModel(BaseComponent): 
    1011    """ 
     
    1314    """ 
    1415    def __init__(self, multfactor=1): 
     16        """ 
     17            :param multfactor: number of layers in the model,  
     18            assumes 0<= n_shells <=10. 
     19        """ 
    1520        BaseComponent.__init__(self) 
    16         """ 
    17         :param multfactor: number of layers in the model,  
    18         assumes 0<= n_shells <=10. 
    19         """ 
    2021 
    2122        ## Setting  model name model description 
    22         self.description="" 
     23        self.description = "" 
    2324        model = ReflModel() 
    2425        self.model = model 
    2526        self.name = "ReflectivityModel" 
    26         self.description=model.description 
     27        self.description = model.description 
    2728        self.n_layers = int(multfactor) 
    2829        ## Define parameters 
     
    5253        # [int(maximum no. of functionality),"str(Titl), 
    5354        # [str(name of function0),...], [str(x-asix name of sld),...]] 
    54         self.multiplicity_info = [max_nshells,"No. of Layers:",[],['Depth']] 
     55        self.multiplicity_info = [max_nshells, "No. of Layers:", [], ['Depth']] 
    5556        ## independent parameter name and unit [string] 
    5657        self.input_name = "Q" 
     
    9596                continue 
    9697            elif name.split('_')[0] == 'func': 
    97                 n= -1 
    98                 while n<self.n_layers: 
     98                n = -1 
     99                while n < self.n_layers: 
    99100                    n += 1 
    100101                    if name.split('_')[pos] == 'inter%s' % str(n): 
    101                         self.params[name]=value 
     102                        self.params[name] = value 
    102103                        continue 
    103104                #continue 
    104105            elif name.split('_')[pos][0:5] == 'inter': 
    105                 n= -1 
    106                 while n<self.n_layers: 
     106                n = -1 
     107                while n < self.n_layers: 
    107108                    n += 1 
    108109                    if name.split('_')[pos] == 'inter%s' % str(n): 
    109                         self.params[name]= value 
     110                        self.params[name] = value 
    110111                        continue 
    111112            elif name.split('_')[pos][0:4] == 'flat': 
    112                 while n<self.n_layers: 
     113                while n < self.n_layers: 
    113114                    n += 1 
    114115                    if name.split('_')[pos] == 'flat%s' % str(n): 
    115                         self.params[name]= value 
     116                        self.params[name] = value 
    116117                        continue 
    117118            elif name == 'n_layers': 
    118119                continue 
    119120            else: 
    120                 self.params[name]= value 
     121                self.params[name] = value 
    121122                
    122123        self.model.params['n_layers'] = self.n_layers     
     
    130131        this model details  
    131132        """ 
    132         for name ,detail in self.model.details.iteritems(): 
     133        for name, detail in self.model.details.iteritems(): 
    133134            if name in self.params.iterkeys(): 
    134                 self.details[name]= detail 
     135                self.details[name] = detail 
    135136             
    136137     
     
    156157                                value = self.model.params['sldIM_medium'] 
    157158                            self.model.setParam(key, value) 
    158                         except: pass 
     159                        except: 
     160                            raise RuntimeError, "ReflectivityModel problem" 
    159161     
    160162    def _get_func_list(self): 
     
    182184        z0 = 0 
    183185        # for layers from the top 
    184         for n in range(1,self.n_layers+2): 
     186        for n in range(1, self.n_layers+2): 
    185187            i = n 
    186188 
    187             for j in range(0,2): 
    188                 for n_s in range(-sub_range,sub_range+1): 
    189                     if j==1: 
    190                         if i==self.n_layers+1: 
     189            for j in range(0, 2): 
     190                for n_s in range(-sub_range, sub_range+1):  
     191                    dz = self.params['thick_inter%s' % str(i-1)]/n_sub 
     192                    if j == 1: 
     193                        if i == self.n_layers+1: 
    191194                            break 
    192195                        # shift half sub thickness for the first point 
     
    194197                        z.append(z0) 
    195198                        #z0 -= dz/2.0 
    196                         z0 += self.params['thick_flat%s'% str(i)] 
     199                        z0 += self.params['thick_flat%s' % str(i)] 
    197200                         
    198                         sld_i = self.params['sld_flat%s'% str(i)] 
    199                         beta.append(self.params['sld_flat%s'% str(i)]) 
     201                        sld_i = self.params['sld_flat%s' % str(i)] 
     202                        beta.append(self.params['sld_flat%s' % str(i)]) 
    200203                    else: 
    201                          
    202                         dz = self.params['thick_inter%s'% str(i-1)]/n_sub 
    203                          
    204204                        if n_s == -sub_range: 
    205205                            # shift half sub thickness for the first point 
     
    212212                            sld_l = self.params['sld_bottom0'] 
    213213                        else: 
    214                             sld_l = self.params['sld_flat%s'% str(i-1)] 
     214                            sld_l = self.params['sld_flat%s' % str(i-1)] 
    215215                        if i == self.n_layers+1: 
    216216                            sld_r = self.params['sld_medium'] 
    217217                        else: 
    218                             sld_r = self.params['sld_flat%s'% str(i)] 
    219                         func_idx = self.params['func_inter%s'% str(i-1)] 
     218                            sld_r = self.params['sld_flat%s' % str(i)] 
     219                        func_idx = self.params['func_inter%s' % str(i-1)] 
    220220                        func = self._get_func(n_s, n_sub, func_idx) 
    221                         if sld_r>sld_l: 
     221                        if sld_r > sld_l: 
    222222                            sld_i = (sld_r-sld_l)*func+sld_l 
    223                         elif sld_r<sld_l: 
     223                        elif sld_r < sld_l: 
    224224                            sld_i = (sld_l-sld_r)*(1-func)+sld_r 
    225225                        else: 
    226                              sld_i = sld_r 
     226                            sld_i = sld_r 
    227227                    z.append(z0) 
    228228                    beta.append(sld_i) 
    229                     if j==1: break 
     229                    if j == 1: 
     230                        break 
    230231        # put substrate and superstrate profile 
    231232        # shift half sub thickness for the first point 
     
    239240        z.append(z0+z_ext) 
    240241        beta.append(self.params['sld_medium'])  
    241         z.insert(0,-z_ext) 
    242         beta.insert(0,self.params['sld_bottom0'])  
     242        z.insert(0, -z_ext) 
     243        beta.insert(0, self.params['sld_bottom0'])  
    243244        z = [z0 - x for x in z] 
    244245        z.reverse() 
     
    298299         
    299300        ## setParam to model  
    300         if name=='sld_medium': 
     301        if name == 'sld_medium': 
    301302            # the sld_*** model.params not in params must set  
    302303            # to value of sld_solv 
    303304            for key in self.model.params.iterkeys(): 
    304305                if key not in self.params.keys()and key.split('_')[0] == 'sld': 
    305                         self.model.setParam(key, value) 
     306                    self.model.setParam(key, value) 
    306307             
    307308        self.model.setParam( name, value) 
     
    351352    ## Now (May27,10) directly uses the model eval function  
    352353    ## instead of the for-loop in Base Component. 
    353     def evalDistribution(self, x = []): 
     354    def evalDistribution(self, x): 
    354355        """  
    355356        Evaluate the model in cartesian coordinates 
     
    360361        # set effective radius and scaling factor before run 
    361362        return self.model.evalDistribution(x) 
     363     
    362364    def calculate_ER(self): 
    363365        """ 
    364366        """ 
    365367        return self.model.calculate_ER() 
     368     
    366369    def set_dispersion(self, parameter, dispersion): 
    367370        """ 
Note: See TracChangeset for help on using the changeset viewer.