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

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 500be82 was 9ce41c6, checked in by Gervaise Alina <gervyh@…>, 15 years ago

wrote unittest for all model untested , haven' test critial point error handling

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