source: sasview/sansmodels/src/sans/models/BaseModel.py @ f7a5c7e

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 f7a5c7e was f9bf661, checked in by Jae Cho <jhjcho@…>, 15 years ago

updated documents

  • Property mode set to 100644
File size: 3.7 KB
Line 
1#!/usr/bin/env python
2"""
3    This software was developed by the University of Tennessee as part of the
4    Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
5    project funded by the US National Science Foundation.
6
7    If you use DANSE applications to do scientific research that leads to
8    publication, we ask that you acknowledge the use of the software with the
9    following sentence:
10
11    "This work benefited from DANSE software developed under NSF award DMR-0520547."
12
13    copyright 2008, University of Tennessee
14"""
15
16"""
17    Provide base functionality for all model components
18   
19    The following has changed since going from BaseComponent to BaseModel:
20   
21        - Arithmetic operation between models is no longer supported.
22          It was found to be of little use and not very flexible.
23       
24        - Parameters are now stored as Parameter object to provide
25          the necessary extra information like limits, units, etc...
26"""
27
28# imports   
29import copy
30   
31class Parameter(object):
32    """
33        Parameter class
34    """
35    name  = ''
36    value = 0.0
37    def __init__(self, name, value):
38        self.name = name
39        self.value = value
40       
41    def __str__(self):
42        return "%s: %g" % (self.name, self.value)
43
44class ParameterProperty(object):
45    """
46        Parameter property allow direct access to
47        the parameter values
48    """
49    def __init__(self,name,**kw):
50        self.name = name
51       
52    def __get__(self,instance,cls):
53        return instance.parameters[self.name].value
54
55    def __set__(self,instance,value):
56        instance.parameters[self.name].value = value
57
58
59   
60from ModelAdaptor import ModelAdaptor
61
62class BaseModel(ModelAdaptor):
63    """
64        Basic model component
65    """
66    ## Name of the model
67    name = "BaseModel"
68   
69    def __init__(self):
70        ## Dictionary of Parameter objects
71        self.parameters = {}
72
73    # Evaluation methods to be implemented by the models
74    def run(self, x=0):  return NotImplemented
75    def runXY(self, x=0):  return NotImplemented
76    def calculate_ER(self):  return NotImplemented
77    def __call__(self, x=0):
78        """     
79            Evaluate the model. Equivalent to runXY(x)
80            @param x: input value
81            @return: value of the model
82        """
83        return self.runXY(x)
84   
85    def clone(self):
86        """ Returns a new object identical to the current object """
87       
88        obj = copy.deepcopy(self)
89        obj.params = copy.deepcopy(self.params)
90        obj.details = copy.deepcopy(self.details)
91   
92        return obj
93   
94    def setParam(self, name, value):
95        """
96            Set the value of a model parameter
97       
98            @param name: name of the parameter
99            @param value: value of the parameter
100        """
101        # Lowercase for case insensitivity
102        name = name.lower()
103        if name in self.parameters:
104            print "found"
105            #self.parameters[name].value = value
106        return object.__setattr__(self, name, value)
107       
108    def getParam(self, name):
109        """
110            Set the value of a model parameter
111
112            @param name: name of the parameter
113            @param value: value of the parameter
114        """
115        # Lowercase for case insensitivity
116        name = name.lower()
117        return object.__getattribute__(self, name)
118       
119    def getParamList(self):
120        """
121            Return a list of all available parameters for the model
122        """
123        param_list = self.params.keys()
124        return param_list
125       
126   
127if __name__ == "__main__":
128    b = BaseModel() 
129    print b.operateOn
130   
Note: See TracBrowser for help on using the repository browser.