source: sasview/src/sas/perspectives/fitting/plugin_models/testmodel_2.py @ 5e880fe1

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 5e880fe1 was b9a5f0e, checked in by krzywon, 10 years ago

90% complete with the conversion.

  • Property mode set to 100644
File size: 8.2 KB
RevLine 
[96814e1]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"""
[22b1d38]8
[e9bd127]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.
[b9a5f0e]13Otherwise, it could be removed in the future on re-installation of the SasView.
[22b1d38]14## *****************************************************************************
[e9bd127]15"""
[22b1d38]16
[79492222]17from sas.models.pluginmodel import Model1DPlugin  ##DO NOT CHANGE THIS LINE!!!
[6f140f2]18import math                     ##DO NOT CHANGE THIS LINE!!!
19import numpy                    ##DO NOT CHANGE THIS LINE!!!
[316e231]20import os
21import sys
[96814e1]22##PLEASE READ COMMENTS CAREFULLY !!! COMMENT ARE IN CAPITAL LETTERS AND AFTER ##
23## THESE COMMENTS ARE THERE TO GUIDE YOU. YOU CAN REMOVE THEM ONLY WHEN YOU ARE
24## CONFORTABLE ENOUGH WITH OUR MODEL PLUGIN OPTION
25
26
27## <-----  SIGN DEFINES WHERE YOU CAN MODIFY THE CODE
28
29class Model(Model1DPlugin): ##DO NOT CHANGE THIS LINE!!!
30    """
31    ##YOU CAN BE MODIFY ANYTHING BETWEEN """ """
32    ##DESCRIPTION OF MODEL PLUG-IN GOES HERE
33   
34    ##EXAMPLE: Class that evaluates a polynomial model.
35    """
[316e231]36    name = "" 
[96814e1]37                               
38    def __init__(self):      ##DO NOT CHANGE THIS LINE!!!
39        """
40        Initialization
41        """
42        Model1DPlugin.__init__(self, name=self.name) ##DO NOT CHANGE THIS LINE!!!
43       
44        ## EDIT PARAMETERS' NAMES AND VALUE
45        ## DELETE MODIFIABLE LINE HERE WILL REDUCE THE NUMBER OF PARAMETERS
46        self.params = {}                ##DO NOT CHANGE THIS LINE!!!
[316e231]47        # Set the name same as the file name
48        self.name = self.get_fname()     ##DO NOT CHANGE THIS LINE!!!
[96814e1]49        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER
50        ## YOU CAN ALSO DELETE THIS LINE
51        self.params['scale'] = 1.0   ## <-----   
52        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER
53        ## YOU CAN ALSO DELETE THIS LINE                                                     
54        self.params['A'] = 0.0       ## <-----   
55        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER
56        ## YOU CAN ALSO DELETE THIS LINE                 
57        self.params['B'] = 10.0      ## <-----   
58        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER
59        ## YOU CAN ALSO DELETE THIS LINE                 
60        self.params['C'] = 0.0       ## <-----   
61        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER
62        ## YOU CAN ALSO DELETE THIS LINE               
63        self.params['D'] = 0.0     ## <-----   
64        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER
65        ## YOU CAN ALSO DELETE THIS LINE                   
66        self.params['E'] = 0.0     ## <-----   
67        ## YOU CAN MODIFY THELINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER
68        ## YOU CAN ALSO DELETE THIS LINE                 
69        self.params['F'] = 0.0     ## <-----         
70
71        ## STORING PARAMETERS  [UNIT, MINIMUM VALUE, MAXIMUM VALUE]
72        self.details = {}    ##DO NOT CHANGE THIS LINE!!!
73        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ',WORD BETWEEN
74        ## ' ', TWO OTHER NUMBESR TO NEW VALUE OR YOU CAN ALSO DELETE TH LINE     
75        self.details['scale'] = ['',None, None]    ## <-----   
76        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ',WORD BETWEEN
77        ## ' ', TWO OTHER NUMBERS TO NEW VALUE OR YOU CAN ALSO DELETE TH LINE     
78        self.details['A'] = ['', None, None]        ## <-----   
79        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ',WORD BETWEEN
80        ## ' ', TWO OTHER NUMBERS TO NEW VALUE OR YOU CAN ALSO DELETE TH LINE     
81        self.details['B'] = ['', None, None]        ## <-----   
82        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ',WORD BETWEEN
83        ## ' ', TWO OTHER NUMBERS TO NEW VALUE OR YOU CAN ALSO DELETE TH LINE     
84        self.details['C'] = ['', None, None]        ## <-----   
85        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ',WORD BETWEEN
86        ## ' ', TWO OTHER NUMBERS TO NEW VALUE OR YOU CAN ALSO DELETE TH LINE     
87        self.details['D'] = ['', None, None]        ## <-----   
88        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ',WORD BETWEEN
89        ## ' ', TWO OTHER NUMBERS TO NEW VALUE OR YOU CAN ALSO DELETE TH LINE     
90        self.details['E'] = ['', None, None]        ## <-----   
91        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ',WORD BETWEEN
92        ## ' ', TWO OTHER NUMBERS TO NEW VALUE OR YOU CAN ALSO DELETE TH LINE     
93        self.details['F'] = ['', 0, 1e16]           ## <-----   
94        ## YOU CAN MODIFY THE LINE BELLOW.MODIFY WORDS BETWEEN """   """  ONLY!!!!
95        self.description = """
96            scale * sin(F(x)/F(x)) \n where F(x)=A+B*x+C*x^2+D*x^3+E*x^4+F*x^5
97                 """                        ## <-----   
98   
99    def function(self, x = 0.0): ##DO NOT CHANGE THIS LINE!!!
100        """
101        Evaluate the model
102       
103        :param x: input x
104       
105        :return: function value
106       
107        """
108        ## ADD YOUR FUNCTION HERE.
109        ## REUSE THE PARAMETERS DEFINED PREVIOUSLY TO WRITE YOUR FUNCTION.
110       
111        ## IN THIS EXAMPLE THE FUNTION IS:
112        ## scale * sin(F(x)/F(x)) \n where F(x)=A+B*x+C*x^2+D*x^3+E*x^4+F*x^5
113       
114        ## YOU CAN REWRITE OUR EXAMPLE WITH EVERYTING INSIDE " " :
115        ## "RETURN self.params['scale']* (self.params['A'] + self.params['B']*x + \
116        ##   self.params['C']* pow(x,2) + self.params['D']*pow(x,3)+\
117        ## self.params['E']*pow(x,4) +self.params['F']*pow(x,5)  ) " 
118         
119        ## NOTE: pow IS A FUNCTION IMPORTED FROM PYTHON MATH LIBRARY
120        ## FOR MORE INFORMATION CHECK http://docs.python.org/library/math.html     
121        ## OTHER FUNCTIONS ARE ALSO
122        ###  AVAILABLE http://www.scipy.org/Numpy_Functions_by_Category
123        ## numpy FUNCTIONS ARE FOR EXPERT USER
124       
125         
126        ## YOU CAN ERASE EVERYTHING BELLOW FOR YOUR OWN FUNCTION
127        #Redefine parameters as local parameters
128        a = self.params['A']       ## <-----   
129        b = self.params['B']       ## <-----   
130        c = self.params['C']       ## <-----   
131        d = self.params['D']       ## <-----   
132        e = self.params['E']       ## <-----   
133        f = self.params['F']        ## <-----   
134        scl = self.params['scale']   ## <-----   
135        ##THIS OUR FUNCTION TEMPLATE
136        poly = a + b*x + c*math.pow(x,2) + d*math.pow(x,3) \
137             + e*math.pow(x,4) +f*math.pow(x,5)      ## <-----               
138       
139        #Remove a singular point (lim poly --> 0) for sin(poly)/poly
140        #(Just note: In Python, indentation defines the belongings of 'if', 'for', and so on..)
141        if poly == 0:                       ## <-----   
142            result = 1                      ## <-----   
143        else:                               ## <-----   
[7c8d3093]144            result = math.sin(poly)/poly    ## <-----   
[96814e1]145           
146        #Re-scale                          ## <-----   
147        result *=scl                       ## <-----   
148
149        return result ## MODIFY ONLY RESULT. DON'T DELETE RETURN!!!!
[316e231]150
151    ## DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!!   
152    def get_fname(self):
153        """
154        Get the model name same as the file name
155        """
156        path = sys._getframe().f_code.co_filename
157        basename  = os.path.basename(path)
158        name, _ = os.path.splitext(basename)
159        return name
160               
[7c8d3093]161## DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!!       
162if __name__ == "__main__": 
163    m= Model() 
164    out1 = m.runXY(0.0)
165    out2 = m.runXY(0.01)
166    isfine1 = numpy.isfinite(out1)
167    isfine2 = numpy.isfinite(out2)
168    print "Testing the value at Q = 0.0:"
169    print out1, " : finite? ", isfine1
170    print "Testing the value at Q = 0.01:"
171    print out2, " : finite? ", isfine2
172    if isfine1 and isfine2:
173        print "===> Simple Test: Passed!"
174    else:
[b9a5f0e]175        print "===> Simple Test: Failed!"
Note: See TracBrowser for help on using the repository browser.