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

ticket-1249
Last change on this file since a5e1b6ca was 5251ec6, checked in by Paul Kienzle <pkienzle@…>, 6 years ago

improved support for py37 in sasgui

  • Property mode set to 100644
File size: 3.1 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        :param x: simple value
70
71        :return: (Line value)
72
73        .. note::
74            This is the function called by fitDialog to calculate the
75            the y(xmin) and y(xmax), but the only difference between this and
76            runXY is when the if statement is true. I however cannot see what
77            that function is for.  It needs to be documented here or removed.
78            PDB 7/10/16
79        """
80        if x.__class__.__name__ == 'list':
81            return self._line(x[0] * math.cos(x[1])) * \
82                                self._line(x[0] * math.sin(x[1]))
83        elif x.__class__.__name__ == 'tuple':
84            msg = "Tuples are not allowed as input to BaseComponent models"
85            raise ValueError(msg)
86        else:
87            return self._line(x)
88
89    def runXY(self, x=0.0):
90        """
91        Evaluate the model.
92
93        :param x: simple value
94
95        :return: Line value
96
97        .. note::
98            This is to be what is called by fitDialog for the actual fit
99            the only difference between this and run is when the if
100            statement is true. I however cannot see what that function
101            is for.  It needs to be documented here or removed. PDB 7/10/16
102        """
103        if x.__class__.__name__ == 'list':
104            return self._line(x[0]) * self._line(x[1])
105        elif x.__class__.__name__ == 'tuple':
106            msg = "Tuples are not allowed as input to BaseComponent models"
107            raise ValueError(msg)
108        else:
109            return self._line(x)
Note: See TracBrowser for help on using the repository browser.