[a9d5684] | 1 | #!/usr/bin/env python |
---|
[2df0b74] | 2 | """ |
---|
[e3ae090] | 3 | Provide Line function (y= Ax + B). Until July 10, 2016 this function provided |
---|
| 4 | (y= A + Bx). This however was contrary to all the other code using it which |
---|
| 5 | assumed (y= mx+b) or in this nomenclature (y=Ax + B). This lead to some |
---|
| 6 | contortions in the code and worse incorrect calculations until now for at least |
---|
| 7 | some of the functions. This seemed the easiest to fix particularly since this |
---|
| 8 | function should disappear in a future iteration (see notes in fitDialog) |
---|
| 9 | |
---|
| 10 | -PDB July 10, 2016 |
---|
[a9d5684] | 11 | """ |
---|
| 12 | |
---|
| 13 | import math |
---|
| 14 | |
---|
| 15 | class LineModel(object): |
---|
[2df0b74] | 16 | """ |
---|
[a9d5684] | 17 | Class that evaluates a linear model. |
---|
[2df0b74] | 18 | |
---|
[e3ae090] | 19 | f(x) = Ax + B |
---|
[2df0b74] | 20 | |
---|
[a9d5684] | 21 | List of default parameters: |
---|
[e3ae090] | 22 | A = 1.0 |
---|
| 23 | B = 1.0 |
---|
[a9d5684] | 24 | """ |
---|
[2df0b74] | 25 | |
---|
[a9d5684] | 26 | def __init__(self): |
---|
| 27 | """ Initialization """ |
---|
[2df0b74] | 28 | # # Name of the model |
---|
[a9d5684] | 29 | self.name = "LineModel" |
---|
| 30 | |
---|
[2df0b74] | 31 | # # Define parameters |
---|
[a9d5684] | 32 | self.params = {} |
---|
| 33 | self.params['A'] = 1.0 |
---|
| 34 | self.params['B'] = 1.0 |
---|
| 35 | |
---|
[2df0b74] | 36 | # # Parameter details [units, min, max] |
---|
[a9d5684] | 37 | self.details = {} |
---|
| 38 | self.details['A'] = ['', None, None] |
---|
| 39 | self.details['B'] = ['', None, None] |
---|
[2df0b74] | 40 | |
---|
[a9d5684] | 41 | def getParam(self, name): |
---|
| 42 | """ |
---|
[2df0b74] | 43 | Return parameter value |
---|
[a9d5684] | 44 | """ |
---|
| 45 | return self.params[name.upper()] |
---|
[2df0b74] | 46 | |
---|
[a9d5684] | 47 | def setParam(self, name, value): |
---|
| 48 | """ |
---|
[2df0b74] | 49 | Set parameter value |
---|
[a9d5684] | 50 | """ |
---|
| 51 | self.params[name.upper()] = value |
---|
[2df0b74] | 52 | |
---|
[a9d5684] | 53 | def _line(self, x): |
---|
| 54 | """ |
---|
| 55 | Evaluate the function |
---|
[2df0b74] | 56 | |
---|
[a9d5684] | 57 | :param x: x-value |
---|
[2df0b74] | 58 | |
---|
[a9d5684] | 59 | :return: function value |
---|
[2df0b74] | 60 | |
---|
[a9d5684] | 61 | """ |
---|
[e3ae090] | 62 | return (self.params['A'] * x) + self.params['B'] |
---|
[2df0b74] | 63 | |
---|
| 64 | def run(self, x=0.0): |
---|
| 65 | """ |
---|
[a9d5684] | 66 | Evaluate the model |
---|
[2df0b74] | 67 | |
---|
[e3ae090] | 68 | :note: This is the function called by fitDialog to calculate the |
---|
| 69 | the y(xmin) and y(xmax), but the only difference between this and |
---|
| 70 | runXY is when the if statement is true. I however cannot see what that |
---|
| 71 | function is for. It needs to be documented here or removed. |
---|
| 72 | -PDB 7/10/16 |
---|
| 73 | |
---|
[a9d5684] | 74 | :param x: simple value |
---|
[2df0b74] | 75 | |
---|
[a9d5684] | 76 | :return: (Line value) |
---|
| 77 | """ |
---|
| 78 | if x.__class__.__name__ == 'list': |
---|
| 79 | return self._line(x[0] * math.cos(x[1])) * \ |
---|
| 80 | self._line(x[0] * math.sin(x[1])) |
---|
| 81 | elif x.__class__.__name__ == 'tuple': |
---|
[2df0b74] | 82 | msg = "Tuples are not allowed as input to BaseComponent models" |
---|
[a9d5684] | 83 | raise ValueError, msg |
---|
| 84 | else: |
---|
| 85 | return self._line(x) |
---|
[2df0b74] | 86 | |
---|
| 87 | def runXY(self, x=0.0): |
---|
| 88 | """ |
---|
[e3ae090] | 89 | Evaluate the model. |
---|
| 90 | |
---|
| 91 | :note: This is to be what is called by fitDialog for the actual fit |
---|
| 92 | but the only difference between this and run is when the if |
---|
| 93 | statement is true. I however cannot see what that function |
---|
| 94 | is for. It needs to be documented here or removed. -PDB 7/10/16 |
---|
[2df0b74] | 95 | |
---|
[a9d5684] | 96 | :param x: simple value |
---|
[2df0b74] | 97 | |
---|
[a9d5684] | 98 | :return: Line value |
---|
[2df0b74] | 99 | |
---|
[a9d5684] | 100 | """ |
---|
| 101 | if x.__class__.__name__ == 'list': |
---|
| 102 | return self._line(x[0]) * self._line(x[1]) |
---|
| 103 | elif x.__class__.__name__ == 'tuple': |
---|
| 104 | msg = "Tuples are not allowed as input to BaseComponent models" |
---|
| 105 | raise ValueError, msg |
---|
| 106 | else: |
---|
| 107 | return self._line(x) |
---|