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 |
---|
29 | import copy |
---|
30 | |
---|
31 | class 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 | |
---|
42 | class 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 | |
---|
58 | from ModelAdaptor import ModelAdaptor |
---|
59 | |
---|
60 | class BaseModel(ModelAdaptor): |
---|
61 | """ |
---|
62 | Basic model component |
---|
63 | """ |
---|
64 | ## Name of the model |
---|
65 | name = "BaseModel" |
---|
66 | ## Dictionary of Parameter objects |
---|
67 | parameters = {} |
---|
68 | |
---|
69 | # Evaluation methods to be implemented by the models |
---|
70 | def run(self, x=0): return NotImplemented |
---|
71 | def runXY(self, x=0): return NotImplemented |
---|
72 | |
---|
73 | def __call__(self, x=0): |
---|
74 | """ |
---|
75 | Evaluate the model. Equivalent to runXY(x) |
---|
76 | @param x: input value |
---|
77 | @return: value of the model |
---|
78 | """ |
---|
79 | return self.runXY(x) |
---|
80 | |
---|
81 | def clone(self): |
---|
82 | """ Returns a new object identical to the current object """ |
---|
83 | |
---|
84 | obj = copy.deepcopy(self) |
---|
85 | obj.params = copy.deepcopy(self.params) |
---|
86 | obj.details = copy.deepcopy(self.details) |
---|
87 | |
---|
88 | return obj |
---|
89 | |
---|
90 | def setParam(self, name, value): |
---|
91 | """ |
---|
92 | Set the value of a model parameter |
---|
93 | |
---|
94 | @param name: name of the parameter |
---|
95 | @param value: value of the parameter |
---|
96 | """ |
---|
97 | # Lowercase for case insensitivity |
---|
98 | name = name.lower() |
---|
99 | return object.__setattribute__(self, name, value) |
---|
100 | |
---|
101 | def getParam(self, name): |
---|
102 | """ |
---|
103 | Set the value of a model parameter |
---|
104 | |
---|
105 | @param name: name of the parameter |
---|
106 | @param value: value of the parameter |
---|
107 | """ |
---|
108 | # Lowercase for case insensitivity |
---|
109 | name = name.lower() |
---|
110 | return object.__getattribute__(self, name) |
---|
111 | |
---|
112 | def getParamList(self): |
---|
113 | """ |
---|
114 | Return a list of all available parameters for the model |
---|
115 | """ |
---|
116 | param_list = self.params.keys() |
---|
117 | return param_list |
---|
118 | |
---|
119 | |
---|
120 | if __name__ == "__main__": |
---|
121 | b = BaseModel() |
---|
122 | print b.operateOn |
---|
123 | |
---|