source: sasview/sansmodels/src/sans/models/PowerLawModel.py @ bd365666

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 bd365666 was 499fe7a, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Fixing code style problems

  • Property mode set to 100644
File size: 3.0 KB
RevLine 
[829eee9]1"""
[3db3895]2    Provide F(x) = scale* (x)^(-m) + bkd
3    Power law function as a BaseComponent model
[829eee9]4"""
5from sans.models.BaseComponent import BaseComponent
6import math
7
8class PowerLawModel(BaseComponent):
9    """
10        Class that evaluates a Power_Law model.
11       
[3db3895]12        F(x) = scale* (x)^(-m) + bkd
[829eee9]13       
14        The model has three parameters:
15            m     =  power
16            scale  =  scale factor
17            bkd    =  incoherent background
18    """
19       
20    def __init__(self):
21        """ Initialization """
22       
23        # Initialize BaseComponent first, then sphere
24        BaseComponent.__init__(self)
25       
26        ## Name of the model
27        self.name = "Power_Law"
28
29        ## Define parameters
30        self.params = {}
[3db3895]31        self.params['m']            = 4.0
32        self.params['scale']        = 1.0
33        self.params['background']   = 0.0
[499fe7a]34        self.description = """ The Power_Law model.
[9ce41c6]35        F(x) = scale* (x)^(-m) + bkd
[1ed3834]36       
[753552d]37        The model has three parameters:
38        m     =  power
39        scale  =  scale factor
40        bkd    =  incoherent background"""
[829eee9]41        ## Parameter details [units, min, max]
42        self.details = {}
[36948c92]43        self.details['m']           = ['', 0,    None]
[3db3895]44        self.details['scale']       = ['', None, None]
[0824909]45        self.details['background']  = ['[1/cm]', None, None]
[988130c6]46        #list of parameter that cannot be fitted
[499fe7a]47        self.fixed = []   
[829eee9]48    def _PowerLaw(self, x):
49        """
[9ce41c6]50            Evaluate  F(x) = scale* (x)^(-m) + bkd
[499fe7a]51            :param x: q-value
[829eee9]52        """
[499fe7a]53        if self.params['m'] > 0 and x == 0:
[f0ffb873]54            return 1e+32
[499fe7a]55        elif self.params['m'] == 0 and x == 0:
[f0ffb873]56            return 1
57        else:
[499fe7a]58            return self.params['scale']*math.pow(x, -1.0*self.params['m'])\
[3db3895]59                + self.params['background']
[829eee9]60       
61    def run(self, x = 0.0):
62        """ Evaluate the model
[499fe7a]63            :param x: input q-value (float or [float, float] as [r, theta])
64            :return: (PowerLaw value)
[829eee9]65        """
66        if x.__class__.__name__ == 'list':
[36948c92]67            # Take absolute value of Q, since this model is really meant to
68            # be defined in 1D for a given length of Q
[a55fac1]69            #qx = math.fabs(x[0]*math.cos(x[1]))
70            #qy = math.fabs(x[0]*math.sin(x[1]))
71            return self._PowerLaw(math.fabs(x[0]))
[829eee9]72        elif x.__class__.__name__ == 'tuple':
[499fe7a]73            raise ValueError, "Tuples are not allowed as input to models"
[829eee9]74        else:
75            return self._PowerLaw(x)
76   
77    def runXY(self, x = 0.0):
78        """ Evaluate the model
[3db3895]79            @param x: input q-value (float or [float, float] as [qx, qy])
[829eee9]80            @return: PowerLaw value
81        """
82        if x.__class__.__name__ == 'list':
[a55fac1]83            q = math.sqrt(x[0]**2 + x[1]**2)
84            return self._PowerLaw(q)
[829eee9]85        elif x.__class__.__name__ == 'tuple':
[499fe7a]86            raise ValueError, "Tuples are not allowed as input to models"
[829eee9]87        else:
88            return self._PowerLaw(x)
Note: See TracBrowser for help on using the repository browser.