1 | #!/usr/bin/env python |
---|
2 | """ |
---|
3 | Provide F(x) = scale* (x)^(-m) + bkd |
---|
4 | Power law function as a BaseComponent model |
---|
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 | |
---|
15 | F(x) = scale* (x)^(-m) + bkd |
---|
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 = {} |
---|
34 | self.params['m'] = 4.0 |
---|
35 | self.params['scale'] = 1.0 |
---|
36 | self.params['background'] = 0.0 |
---|
37 | self.description=""" The Power_Law model. |
---|
38 | F(x) = scale* (x)^(-m) + bkd |
---|
39 | The model has three parameters: |
---|
40 | m = power |
---|
41 | scale = scale factor |
---|
42 | bkd = incoherent background""" |
---|
43 | ## Parameter details [units, min, max] |
---|
44 | self.details = {} |
---|
45 | self.details['m'] = ['', 0, None] |
---|
46 | self.details['scale'] = ['', None, None] |
---|
47 | self.details['background'] = ['', None, None] |
---|
48 | |
---|
49 | def _PowerLaw(self, x): |
---|
50 | """ |
---|
51 | Evaluate F(x) = scale* (x)^(-m) + bkd |
---|
52 | |
---|
53 | """ |
---|
54 | if x<0 and self.params['m']>0: |
---|
55 | raise ValueError, "negative number cannot be raised to a fractional power" |
---|
56 | |
---|
57 | return self.params['scale']*math.pow(x ,-1.0*self.params['m'])\ |
---|
58 | + self.params['background'] |
---|
59 | |
---|
60 | def run(self, x = 0.0): |
---|
61 | """ Evaluate the model |
---|
62 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
63 | @return: (PowerLaw value) |
---|
64 | """ |
---|
65 | if x.__class__.__name__ == 'list': |
---|
66 | # Take absolute value of Q, since this model is really meant to |
---|
67 | # be defined in 1D for a given length of Q |
---|
68 | #qx = math.fabs(x[0]*math.cos(x[1])) |
---|
69 | #qy = math.fabs(x[0]*math.sin(x[1])) |
---|
70 | return self._PowerLaw(math.fabs(x[0])) |
---|
71 | elif x.__class__.__name__ == 'tuple': |
---|
72 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
73 | else: |
---|
74 | return self._PowerLaw(x) |
---|
75 | |
---|
76 | def runXY(self, x = 0.0): |
---|
77 | """ Evaluate the model |
---|
78 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
79 | @return: PowerLaw value |
---|
80 | """ |
---|
81 | if x.__class__.__name__ == 'list': |
---|
82 | q = math.sqrt(x[0]**2 + x[1]**2) |
---|
83 | return self._PowerLaw(q) |
---|
84 | elif x.__class__.__name__ == 'tuple': |
---|
85 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
86 | else: |
---|
87 | return self._PowerLaw(x) |
---|