source: sasview/src/sans/models/PowerLawModel.py @ 98816c43

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 98816c43 was 5777106, checked in by Mathieu Doucet <doucetm@…>, 11 years ago

Moving things around. Will definitely not build.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1"""
2    Provide F(x) = scale* (x)^(-m) + bkd
3    Power law function as a BaseComponent model
4"""
5from sans.models.BaseComponent import BaseComponent
6import math
7
8class PowerLawModel(BaseComponent):
9    """
10        Class that evaluates a Power_Law model.
11       
12        F(x) = scale* (x)^(-m) + bkd
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 = {}
31        self.params['m']            = 4.0
32        self.params['scale']        = 1.0
33        self.params['background']   = 0.0
34        self.description = """ The Power_Law model.
35        F(x) = scale* (x)^(-m) + bkd
36       
37        The model has three parameters:
38        m     =  power
39        scale  =  scale factor
40        bkd    =  incoherent background"""
41        ## Parameter details [units, min, max]
42        self.details = {}
43        self.details['m']           = ['', 0,    None]
44        self.details['scale']       = ['', None, None]
45        self.details['background']  = ['[1/cm]', None, None]
46        #list of parameter that cannot be fitted
47        self.fixed = []   
48    def _PowerLaw(self, x):
49        """
50            Evaluate  F(x) = scale* (x)^(-m) + bkd
51            :param x: q-value
52        """
53        if self.params['m'] > 0 and x == 0:
54            return 1e+32
55        elif self.params['m'] == 0 and x == 0:
56            return 1
57        else:
58            return self.params['scale']*math.pow(x, -1.0*self.params['m'])\
59                + self.params['background']
60       
61    def run(self, x = 0.0):
62        """ Evaluate the model
63            :param x: input q-value (float or [float, float] as [r, theta])
64            :return: (PowerLaw value)
65        """
66        if x.__class__.__name__ == 'list':
67            # Take absolute value of Q, since this model is really meant to
68            # be defined in 1D for a given length of Q
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]))
72        elif x.__class__.__name__ == 'tuple':
73            raise ValueError, "Tuples are not allowed as input to models"
74        else:
75            return self._PowerLaw(x)
76   
77    def runXY(self, x = 0.0):
78        """ Evaluate the model
79            @param x: input q-value (float or [float, float] as [qx, qy])
80            @return: PowerLaw value
81        """
82        if x.__class__.__name__ == 'list':
83            q = math.sqrt(x[0]**2 + x[1]**2)
84            return self._PowerLaw(q)
85        elif x.__class__.__name__ == 'tuple':
86            raise ValueError, "Tuples are not allowed as input to models"
87        else:
88            return self._PowerLaw(x)
Note: See TracBrowser for help on using the repository browser.