source: sasview/sansmodels/src/sans/models/LineModel.py @ 95fe70d

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 95fe70d was d9341f2, checked in by Jae Cho <jhjcho@…>, 13 years ago

fixed 2D line runxy

  • Property mode set to 100644
File size: 4.1 KB
Line 
1#!/usr/bin/env python
2"""
3    Provide Line function (y= A + Bx) as a BaseComponent model
4"""
5
6from sans.models.BaseComponent import BaseComponent
7import math
8import numpy
9
10
11class LineModel(BaseComponent):
12    """
13        Class that evaluates a linear model.
14       
15        f(x) = A + Bx
16         
17        List of default parameters:
18          A = 1.0
19          B = 1.0
20    """
21       
22    def __init__(self):
23        """ Initialization """
24       
25        # Initialize BaseComponent first, then sphere
26        BaseComponent.__init__(self)
27       
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        self.description='f(x) = A + Bx'
36        ## Parameter details [units, min, max]
37        self.details = {}
38        self.details['A'] = ['', None, None]
39        self.details['B'] = ['', None, None]
40        # fixed paramaters
41        self.fixed=[]
42    def _line(self, x):
43        """
44            Evaluate the function
45            @param x: x-value
46            @return: function value
47        """
48        return  self.params['A'] + x *self.params['B']
49   
50    def run(self, x = 0.0):
51        """ Evaluate the model
52            @param x: simple value
53            @return: (Line value)
54        """
55        if x.__class__.__name__ == 'list':
56            return self._line(x[0]*math.cos(x[1]))*self._line(x[0]*math.sin(x[1]))
57        elif x.__class__.__name__ == 'tuple':
58            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
59        else:
60            return self._line(x)
61   
62    def runXY(self, x = 0.0):
63        """ Evaluate the model
64            @param x: simple value
65            @return: Line value
66        """
67        if x.__class__.__name__ == 'list':
68            return self._line(x[1])
69        elif x.__class__.__name__ == 'tuple':
70            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
71        else:
72            return self._line(x)
73       
74    def evalDistribution(self, qdist):
75        """
76        Evaluate a distribution of q-values.
77       
78        * For 1D, a numpy array is expected as input:
79       
80            evalDistribution(q)
81           
82        where q is a numpy array.
83       
84       
85        * For 2D, a list of numpy arrays are expected: [qx_prime,qy_prime],
86          where 1D arrays,
87               
88        :param qdist: ndarray of scalar q-values or list [qx,qy]
89                    where qx,qy are 1D ndarrays
90       
91        """
92        if qdist.__class__.__name__ == 'list':
93            # Check whether we have a list of ndarrays [qx,qy]
94            if len(qdist)!=2 or \
95                qdist[0].__class__.__name__ != 'ndarray' or \
96                qdist[1].__class__.__name__ != 'ndarray':
97                    raise RuntimeError, "evalDistribution expects a list of 2 ndarrays"
98               
99            # Extract qx and qy for code clarity
100            qx = qdist[0]
101            qy = qdist[1]
102            #For 2D, Z = A + B * Y,
103            # so that it keeps its linearity in y-direction.
104            # calculate q_r component for 2D isotropic
105            q =  qy
106            # vectorize the model function runXY
107            v_model = numpy.vectorize(self.runXY,otypes=[float])
108            # calculate the scattering
109            iq_array = v_model(q)
110
111            return iq_array
112               
113        elif qdist.__class__.__name__ == 'ndarray':
114                # We have a simple 1D distribution of q-values
115                v_model = numpy.vectorize(self.runXY,otypes=[float])
116                iq_array = v_model(qdist)
117
118                return iq_array
119           
120        else:
121            mesg = "evalDistribution is expecting an ndarray of scalar q-values"
122            mesg += " or a list [qx,qy] where qx,qy are 2D ndarrays."
123            raise RuntimeError, mesg
124       
125   
126   
127
128if __name__ == "__main__": 
129    l = Line()
130    print "hello"
131       
132# End of file
Note: See TracBrowser for help on using the repository browser.