source: sasview/src/sas/perspectives/fitting/plugin_models/testmodel.py @ 7cd87c2

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

90% complete with the conversion.

  • Property mode set to 100644
File size: 4.2 KB
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"""
16
17from sas.models.pluginmodel import Model1DPlugin  ##DO NOT CHANGE THIS LINE!!!
18import math                  ##DO NOT CHANGE THIS LINE!!!
19import numpy               ##DO NOT CHANGE THIS LINE!!!
20import os
21import sys
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 cos(x) model.
35    """
36    name = ""     
37   
38    def __init__(self):
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!!!
47        # Set the name same as the file name
48        self.name = self.get_fname()     ##DO NOT CHANGE THIS LINE!!!
49        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER
50        ## YOU CAN ALSO DELETE THIS LINE
51        self.params['A'] = 1.0       ## <----- 
52        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER
53        ## YOU CAN ALSO DELETE THIS LINE   
54        self.params['B'] = 1.0       ## <-----
55        ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER
56        ## YOU CAN ALSO DELETE THIS LINE   
57        self.params['C'] = 10.0      ## <----- 
58
59        ## DEFINE DEFAULT DETAILS
60        self.set_details()      ##DO NOT DELETE OR CHANGE THIS LINE!!!
61       
62        ## YOU CAN MODIFY THE LINE BELLOW.MODIFY WORDS BETWEEN """   """  ONLY!!!!
63        self.description = "F(x)=A+Bcos(2x)+Csin(2x) "     ## <-----
64   
65    def function(self, x = 0.0):  ##DO NOT CHANGE THIS LINE!!!
66        """
67        Evaluate the model
68       
69        :param x: input x
70       
71        :return: function value
72       
73        """
74        ## ADD YOUR FUNCTION HERE.
75        ## REUSE THE PARAMETERS DEFINED PREVIOUSLY TO WRITE YOUR FUNCTION.
76       
77        ## IN THIS EXAMPLE THE FUNTION IS:
78        ## A+Bcos(2x)+Csin(2x)
79        ## YOU CAN USE math.sin or sin directly
80        ## NOTE: sin, cos ARE FUNCTIONS  IMPORTED FROM PYTHON MATH LIBRARY
81        ## FOR MORE INFORMATION CHECK http://docs.python.org/library/math.html     
82        ## OTHER FUNCTIONS ARE ALSO
83        ##  AVAILABLE http://www.scipy.org/Numpy_Functions_by_Category
84        ## numpy FUNCTIONS ARE FOR EXPERT USER
85       
86        return self.params['A']+self.params['B']*math.cos(2.0*x)+self.params['C']*math.sin(2.0*x)
87   
88    ## DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!!   
89    def get_fname(self):
90        """
91        Get the model name same as the file name
92        """
93        path = sys._getframe().f_code.co_filename
94        basename  = os.path.basename(path)
95        name, _ = os.path.splitext(basename)
96        return name
97   
98## DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!!       
99if __name__ == "__main__": 
100    m= Model() 
101    out1 = m.runXY(0.0)
102    out2 = m.runXY(0.01)
103    isfine1 = numpy.isfinite(out1)
104    isfine2 = numpy.isfinite(out2)
105    print "Testing the value at Q = 0.0:"
106    print out1, " : finite? ", isfine1
107    print "Testing the value at Q = 0.01:"
108    print out2, " : finite? ", isfine2
109    if isfine1 and isfine2:
110        print "===> Simple Test: Passed!"
111    else:
112        print "===> Simple Test: Failed!"
Note: See TracBrowser for help on using the repository browser.