Ticket #751: polynomial5.py

File polynomial5.py, 4.9 KB (added by richardh, 8 years ago)
Line 
1"""
2Test plug-in model
3These are links of available functions:
4
5http://docs.python.org/library/math.html
6http://www.scipy.org/Numpy_Functions_by_Category
7"""
8
9"""
10## *****************************************************************************
11Please select the 'Compile' from the menubar after the modification and saving.
12Note that we recommend to save the file as a different file name.
13Otherwise, it could be removed in the future on re-installation of the SasView.
14## *****************************************************************************
15"""
16from sas.models.pluginmodel import Model1DPlugin  ##DO NOT CHANGE THIS LINE!!!
17
18## FOR MORE INFORMATION CHECK http://docs.python.org/library/math.html     
19## AND http://www.scipy.org/Numpy_Functions_by_Category
20import math                    ##DO NOT CHANGE THIS LINE!!!
21import numpy                   ##DO NOT CHANGE THIS LINE!!!
22import os
23import sys
24## <-----  SIGN DEFINES WHERE YOU CAN MODIFY THE CODE
25
26class Model(Model1DPlugin): ##DO NOT CHANGE THIS LINE!!!
27    """
28    ##YOU CAN BE MODIFY ANYTHING BETWEEN """ """
29    ##DESCRIPTION OF MODEL PLUG-IN GOES HERE
30   
31    ##EXAMPLE: Class that evaluates a polynomial model.
32    """
33    name = "" 
34                               
35    def __init__(self):      ##DO NOT CHANGE THIS LINE!!!
36        """
37        Initialization
38        """
39        Model1DPlugin.__init__(self, name=self.name) ##DO NOT CHANGE THIS LINE!!!
40       
41        ## HERE WE DEFINE THE PARAM NAME AND ITS INITIAL VALUE
42        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER                                                     
43        self.params['A'] = 0.1       ## <-----               
44        self.params['B'] = 10.0      ## <-----                 
45        self.params['C'] = 11.0       ## <-----               
46        self.params['D'] = 0.0       ## <-----                   
47        self.params['E'] = 0.0       ## <-----                 
48        self.params['F'] = 0.0       ## <-----   
49        # Set the name same as the file name
50        self.name = self.get_fname()     ##DO NOT CHANGE THIS LINE!!!
51        ## YOU CAN MODIFY THE LINE BELLOW.MODIFY WORDS BETWEEN """   """  ONLY!!!!
52        self.description = """
53            a + b * x + c * math.pow(x,2) + d * math.pow(x,3) \n
54             + e * math.pow(x,4) + f * math.pow(x,5) 
55        """                        ## <-----   
56        ## DEFINE DEFAULT DETAILS
57        self.set_details()      ##DO NOT DELETE OR CHANGE THIS LINE!!!
58       
59        ## IN THIS EXAMPLE THE FUNTION IS:
60        ## F(x)=A+B*x+C*x^2+D*x^3+E*x^4+F*x^5             
61   
62    def function(self, x = 0.0): ##DO NOT CHANGE THIS LINE!!!
63        """
64        Evaluate the model
65        :param x: input x
66        :return: function value
67        """
68        ## DEFINE YOUR FUNCTION HERE.
69        ## YOU CAN ERASE EVERYTHING BELLOW FOR YOUR OWN FUNCTION
70        #Redefine parameters as local parameters, or skip and use long name
71        a = self.params['A']       ## <-----   
72        b = self.params['B']       ## <-----   
73        c = self.params['C']       ## <-----   
74        d = self.params['D']       ## <-----   
75        e = self.params['E']       ## <-----   
76        f = self.params['F']       ## <-----   
77 
78        ##THIS OUR FUNCTION TEMPLATE
79        poly = a + b * x + c * math.pow(x,2) + d * math.pow(x,3) \
80                + e * math.pow(x,4) + f * math.pow(x,5)              ## <-----               
81       
82        #(Just note: In Python, indentation defines the belongings
83        # of 'if', 'for', and so on..)
84        #(lim x --> 0)
85        if x == 0:                          ## <-----   
86            result = a                      ## <-----   
87        else:                               ## <-----   
88            result = poly                   ## <-----   
89
90        return result       ## MODIFY ONLY RESULT. DON'T DELETE RETURN!!!!
91   
92    ## DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!!       
93    def get_fname(self):
94        """
95        Get the model name same as the file name
96        """
97        path = sys._getframe().f_code.co_filename
98        basename  = os.path.basename(path)
99        name, _ = os.path.splitext(basename)
100        return name
101           
102###############################################################################
103## THIS IS FOR TEST. DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!!       
104if __name__ == "__main__": 
105    m= Model() 
106    out1 = m.runXY(0.0)
107    out2 = m.runXY(0.01)
108    isfine1 = numpy.isfinite(out1)
109    isfine2 = numpy.isfinite(out2)
110    print "Testing the value at Q = 0.0:"
111    print out1, " : finite? ", isfine1
112    print "Testing the value at Q = 0.01:"
113    print out2, " : finite? ", isfine2
114    if isfine1 and isfine2:
115        print "===> Simple Test: Passed!"
116    else:
117        print "===> Simple Test: Failed!"