source: sasview/src/sas/sasgui/plottools/LineModel.py @ b9d74f3

Last change on this file since b9d74f3 was b9d74f3, checked in by andyfaff, 8 years ago

MAINT: use raise Exception() not raise Exception

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