source: sasview/sansview/plugins/testmodel.py @ c7bc3e7

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 c7bc3e7 was b30f001, checked in by Gervaise Alina <gervyh@…>, 16 years ago

working on model menu

  • Property mode set to 100644
File size: 2.5 KB
Line 
1"""
2    Test plug-in model
3"""
4#try:
5#    from DataPlugin import *
6#except:
7#    import sys, os
8#    print os.path.abspath('..')
9#    sys.path.insert(1,os.path.abspath('..'))
10#    from DataPlugin import *
11#    print "Running independently"
12#
13import math
14from sans.models.BaseComponent import BaseComponent
15import math
16class Model1DPlugin(BaseComponent):
17    ## Name of the model
18    name = "Plugin Model"
19
20    def __init__(self):
21        """ Initialization """
22        self.details = {}
23        self.params  = {}
24       
25    def function(self, x):
26        """
27            Function to be implemented by the plug-in writer
28        """
29        return x
30       
31    def run(self, x = 0.0):
32        """ Evaluate the model
33            @param x: input x, or [x, phi] [radian]
34            @return: function value
35        """
36        if x.__class__.__name__ == 'list':
37            x_val = x[0]*math.cos(x[1])
38            y_val = x[0]*math.sin(x[1])
39            return self.function(x_val)*self.function(y_val)
40        elif x.__class__.__name__ == 'tuple':
41            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
42        else:
43            return self.function(x)
44   
45    def runXY(self, x = 0.0):
46        """ Evaluate the model
47            @param x: input x, or [x, y]
48            @return: function value
49        """
50        if x.__class__.__name__ == 'list':
51            return self.function(x[0])*self.function(x[1])
52        elif x.__class__.__name__ == 'tuple':
53            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
54        else:
55            return self.function(x)
56
57# Your model HAS to be called Model
58class Model(Model1DPlugin):
59    """ Class that evaluates a cos(x) model.
60    """
61   
62    ## Name of the model
63    name = "A+Bcos(2x)+Csin(2x)"
64   
65    def __init__(self):
66        """ Initialization """
67     
68       
69        ## Parameters definition and defaults
70        self.params = {}
71        self.params['A'] = 0.0
72        self.params['B'] = 1.0
73        self.params['C'] = 0.0
74
75        ## Parameter details [units, min, max]
76        self.details = {}
77        self.details['A'] = ['', -1e16, 1e16]
78        self.details['B'] = ['', -1e16, 1e16]
79        self.details['C'] = ['', -1e16, 1e16]
80   
81    def function(self, x = 0.0):
82        """ Evaluate the model
83            @param x: input x
84            @return: function value
85        """
86        return self.params['A']+self.params['B']*math.cos(2.0*x)+self.params['C']*math.sin(2.0*x)
87   
Note: See TracBrowser for help on using the repository browser.