[829eee9] | 1 | """ |
---|
[3db3895] | 2 | Provide F(x) = scale* (x)^(-m) + bkd |
---|
| 3 | Power law function as a BaseComponent model |
---|
[829eee9] | 4 | """ |
---|
[79492222] | 5 | from sas.models.BaseComponent import BaseComponent |
---|
[829eee9] | 6 | import math |
---|
| 7 | |
---|
| 8 | class 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) |
---|