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