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

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 ae60f86 was ae60f86, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

changing base model

  • 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
42class ParameterProperty(object):
43    """
44        Parameter property allow direct access to
45        the parameter values
46    """
47    def __init__(self,name,**kw):
48        self.name = name
49       
50    def __get__(self,instance,cls):
51        return instance.parameters[self.name].value
52
53    def __set__(self,instance,value):
54        instance.parameters[self.name].value = value
55
56
57   
58from ModelAdaptor import ModelAdaptor
59
60class BaseModel(ModelAdaptor):
61    """
62        Basic model component
63    """
64    ## Name of the model
65    name = "BaseModel"
66   
67    def __init__(self):
68        ## Dictionary of Parameter objects
69        self.parameters = {}
70       
71        for item in self.params:
72            self.parameters[item] = Parameter(item, self.params[item])
73
74       
75
76    # Evaluation methods to be implemented by the models
77    def run(self, x=0):  return NotImplemented
78    def runXY(self, x=0):  return NotImplemented
79   
80    def __call__(self, x=0):
81        """     
82            Evaluate the model. Equivalent to runXY(x)
83            @param x: input value
84            @return: value of the model
85        """
86        return self.runXY(x)
87   
88    def clone(self):
89        """ Returns a new object identical to the current object """
90       
91        obj = copy.deepcopy(self)
92        obj.params = copy.deepcopy(self.params)
93        obj.details = copy.deepcopy(self.details)
94   
95        return obj
96   
97    def setParam(self, name, value):
98        """
99            Set the value of a model parameter
100       
101            @param name: name of the parameter
102            @param value: value of the parameter
103        """
104        # Lowercase for case insensitivity
105        name = name.lower()
106        if name in self.parameters:
107            print "found"
108        return object.__setattr__(self, name, value)
109       
110    def getParam(self, name):
111        """
112            Set the value of a model parameter
113
114            @param name: name of the parameter
115            @param value: value of the parameter
116        """
117        # Lowercase for case insensitivity
118        name = name.lower()
119        return object.__getattribute__(self, name)
120       
121    def getParamList(self):
122        """
123            Return a list of all available parameters for the model
124        """
125        param_list = self.params.keys()
126        return param_list
127       
128   
129if __name__ == "__main__":
130    b = BaseModel() 
131    print b.operateOn
132   
Note: See TracBrowser for help on using the repository browser.