source: sasview/src/sas/perspectives/fitting/plugin_models/polynomial5.py @ 3a39c2e

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 3a39c2e was 79492222, checked in by krzywon, 10 years ago

Changed the file and folder names to remove all SANS references.

  • Property mode set to 100644
File size: 4.9 KB
RevLine 
[7c8d3093]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"""
[e9bd127]8
9"""
[22b1d38]10## *****************************************************************************
[e9bd127]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 SansView.
[22b1d38]14## *****************************************************************************
[e9bd127]15"""
[79492222]16from sas.models.pluginmodel import Model1DPlugin  ##DO NOT CHANGE THIS LINE!!!
[7c8d3093]17
[ed8fa6a3]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!!!
[316e231]22import os
23import sys
[7c8d3093]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    """
[316e231]33    name = "" 
[7c8d3093]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       
[ed8fa6a3]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'] = 0.0       ## <-----               
46        self.params['D'] = 0.0       ## <-----                   
47        self.params['E'] = 0.0       ## <-----                 
48        self.params['F'] = 0.0       ## <-----   
[316e231]49        # Set the name same as the file name
50        self.name = self.get_fname()     ##DO NOT CHANGE THIS LINE!!!
[19e614a]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        """                        ## <-----   
[ed8fa6a3]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             
[7c8d3093]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        """
[ed8fa6a3]68        ## DEFINE YOUR FUNCTION HERE.
[7c8d3093]69        ## YOU CAN ERASE EVERYTHING BELLOW FOR YOUR OWN FUNCTION
[ed8fa6a3]70        #Redefine parameters as local parameters, or skip and use long name
[7c8d3093]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']       ## <-----   
[ed8fa6a3]76        f = self.params['F']       ## <-----   
[7c8d3093]77 
78        ##THIS OUR FUNCTION TEMPLATE
[ed8fa6a3]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)              ## <-----               
[7c8d3093]81       
[ed8fa6a3]82        #(Just note: In Python, indentation defines the belongings
83        # of 'if', 'for', and so on..)
[7c8d3093]84        #(lim x --> 0)
[ed8fa6a3]85        if x == 0:                          ## <-----   
[7c8d3093]86            result = a                      ## <-----   
87        else:                               ## <-----   
[ed8fa6a3]88            result = poly                   ## <-----   
[7c8d3093]89
[ed8fa6a3]90        return result       ## MODIFY ONLY RESULT. DON'T DELETE RETURN!!!!
[316e231]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           
[ed8fa6a3]102###############################################################################
103## THIS IS FOR TEST. DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!!       
[7c8d3093]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!"
Note: See TracBrowser for help on using the repository browser.