[e7a0b2f] | 1 | #!/usr/bin/env python |
---|
| 2 | """ |
---|
| 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 |
---|
| 11 | """ |
---|
| 12 | |
---|
| 13 | import math |
---|
| 14 | |
---|
| 15 | class LineModel(object): |
---|
| 16 | """ |
---|
| 17 | Class that evaluates a linear model. |
---|
| 18 | |
---|
| 19 | f(x) = Ax + B |
---|
| 20 | |
---|
| 21 | List of default parameters: |
---|
| 22 | A = 1.0 |
---|
| 23 | B = 1.0 |
---|
| 24 | """ |
---|
| 25 | |
---|
| 26 | def __init__(self): |
---|
| 27 | """ Initialization """ |
---|
| 28 | # # Name of the model |
---|
| 29 | self.name = "LineModel" |
---|
| 30 | |
---|
| 31 | # # Define parameters |
---|
| 32 | self.params = {} |
---|
| 33 | self.params['A'] = 1.0 |
---|
| 34 | self.params['B'] = 1.0 |
---|
| 35 | |
---|
| 36 | # # Parameter details [units, min, max] |
---|
| 37 | self.details = {} |
---|
| 38 | self.details['A'] = ['', None, None] |
---|
| 39 | self.details['B'] = ['', None, None] |
---|
| 40 | |
---|
| 41 | def getParam(self, name): |
---|
| 42 | """ |
---|
| 43 | Return parameter value |
---|
| 44 | """ |
---|
| 45 | return self.params[name.upper()] |
---|
| 46 | |
---|
| 47 | def setParam(self, name, value): |
---|
| 48 | """ |
---|
| 49 | Set parameter value |
---|
| 50 | """ |
---|
| 51 | self.params[name.upper()] = value |
---|
| 52 | |
---|
| 53 | def _line(self, x): |
---|
| 54 | """ |
---|
| 55 | Evaluate the function |
---|
| 56 | |
---|
| 57 | :param x: x-value |
---|
| 58 | |
---|
| 59 | :return: function value |
---|
| 60 | |
---|
| 61 | """ |
---|
| 62 | return (self.params['A'] * x) + self.params['B'] |
---|
| 63 | |
---|
| 64 | def run(self, x=0.0): |
---|
| 65 | """ |
---|
| 66 | Evaluate the model |
---|
| 67 | |
---|
| 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 | |
---|
| 74 | :param x: simple value |
---|
| 75 | |
---|
| 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': |
---|
| 82 | msg = "Tuples are not allowed as input to BaseComponent models" |
---|
| 83 | raise ValueError, msg |
---|
| 84 | else: |
---|
| 85 | return self._line(x) |
---|
| 86 | |
---|
| 87 | def runXY(self, x=0.0): |
---|
| 88 | """ |
---|
| 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 |
---|
| 95 | |
---|
| 96 | :param x: simple value |
---|
| 97 | |
---|
| 98 | :return: Line value |
---|
| 99 | |
---|
| 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) |
---|
| 108 | |
---|