[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 | |
---|
| 7 | from sans.models.BaseComponent import BaseComponent |
---|
| 8 | import math |
---|
| 9 | |
---|
| 10 | class 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. |
---|
[f0ffb873] | 38 | F(x) = scale* (|x|)^(-m) + bkd |
---|
[753552d] | 39 | The model has three parameters: |
---|
| 40 | m = power |
---|
| 41 | scale = scale factor |
---|
| 42 | bkd = incoherent background""" |
---|
[829eee9] | 43 | ## Parameter details [units, min, max] |
---|
| 44 | self.details = {} |
---|
[36948c92] | 45 | self.details['m'] = ['', 0, None] |
---|
[3db3895] | 46 | self.details['scale'] = ['', None, None] |
---|
[0824909] | 47 | self.details['background'] = ['[1/cm]', None, None] |
---|
[988130c6] | 48 | #list of parameter that cannot be fitted |
---|
| 49 | self.fixed= [] |
---|
[829eee9] | 50 | def _PowerLaw(self, x): |
---|
| 51 | """ |
---|
[f0ffb873] | 52 | Evaluate F(x) = scale* (|x|)^(-m) + bkd |
---|
[829eee9] | 53 | |
---|
| 54 | """ |
---|
[f0ffb873] | 55 | #if x!=0 and self.params['m']!=0: |
---|
| 56 | # raise ValueError, "negative number cannot be raised to a fractional power" |
---|
| 57 | if self.params['m']>0 and x==0: |
---|
| 58 | return 1e+32 |
---|
| 59 | elif self.params['m']==0 and x==0: |
---|
| 60 | return 1 |
---|
| 61 | else: |
---|
| 62 | return self.params['scale']*math.pow(x ,-1.0*self.params['m'])\ |
---|
[3db3895] | 63 | + self.params['background'] |
---|
[829eee9] | 64 | |
---|
| 65 | def run(self, x = 0.0): |
---|
| 66 | """ Evaluate the model |
---|
[3db3895] | 67 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
[829eee9] | 68 | @return: (PowerLaw value) |
---|
| 69 | """ |
---|
| 70 | if x.__class__.__name__ == 'list': |
---|
[36948c92] | 71 | # Take absolute value of Q, since this model is really meant to |
---|
| 72 | # be defined in 1D for a given length of Q |
---|
[a55fac1] | 73 | #qx = math.fabs(x[0]*math.cos(x[1])) |
---|
| 74 | #qy = math.fabs(x[0]*math.sin(x[1])) |
---|
| 75 | return self._PowerLaw(math.fabs(x[0])) |
---|
[829eee9] | 76 | elif x.__class__.__name__ == 'tuple': |
---|
| 77 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 78 | else: |
---|
| 79 | return self._PowerLaw(x) |
---|
| 80 | |
---|
| 81 | def runXY(self, x = 0.0): |
---|
| 82 | """ Evaluate the model |
---|
[3db3895] | 83 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
[829eee9] | 84 | @return: PowerLaw value |
---|
| 85 | """ |
---|
| 86 | if x.__class__.__name__ == 'list': |
---|
[a55fac1] | 87 | q = math.sqrt(x[0]**2 + x[1]**2) |
---|
| 88 | return self._PowerLaw(q) |
---|
[829eee9] | 89 | elif x.__class__.__name__ == 'tuple': |
---|
| 90 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 91 | else: |
---|
| 92 | return self._PowerLaw(x) |
---|