1 | #!/usr/bin/env python |
---|
2 | """ Provide base functionality for all model components |
---|
3 | @author: Mathieu Doucet / UTK |
---|
4 | @contact: mathieu.doucet@nist.gov |
---|
5 | """ |
---|
6 | |
---|
7 | # info |
---|
8 | __author__ = "Mathieu Doucet / UTK" |
---|
9 | __id__ = "$Id: BaseComponent.py,v 1.2 2007/03/14 21:04:40 doucet Exp $" |
---|
10 | |
---|
11 | # imports |
---|
12 | import copy |
---|
13 | |
---|
14 | class BaseComponent: |
---|
15 | """ Basic model component |
---|
16 | Provides basic arithmetics |
---|
17 | |
---|
18 | The supported operations are: add (+), subtract (-), multiply (*) and divide (/). |
---|
19 | |
---|
20 | Here's an arithmetics example to define a function |
---|
21 | f = ( 1+sin(x) )**2 + 3.0 |
---|
22 | and evaluate it at 1.4. |
---|
23 | |
---|
24 | |
---|
25 | sin = Sin() |
---|
26 | const1 = Constant() |
---|
27 | const2 = Constant() |
---|
28 | const2.setParam('value', 3.0) |
---|
29 | |
---|
30 | func = (const1 + sin)*(const1 + sin) + const2 |
---|
31 | |
---|
32 | print func(1.4) |
---|
33 | |
---|
34 | """ |
---|
35 | |
---|
36 | def __init__(self): |
---|
37 | """ Initialization""" |
---|
38 | |
---|
39 | ## Name of the model |
---|
40 | self.name = "BaseComponent" |
---|
41 | ## Component left of operator |
---|
42 | self.operateOn = None |
---|
43 | ## Component right of operator |
---|
44 | self.other = None |
---|
45 | |
---|
46 | |
---|
47 | ## Parameters to be accessed by client |
---|
48 | self.params = {} |
---|
49 | self.details = {} |
---|
50 | |
---|
51 | def __str__(self): |
---|
52 | """ Prints an XML representation of the model |
---|
53 | @return: string representation |
---|
54 | """ |
---|
55 | return self.name |
---|
56 | |
---|
57 | def run(self, x=0): #pylint: disable-msg=R0201 |
---|
58 | """ Evaluate the model |
---|
59 | Dummy function |
---|
60 | @param x: input value |
---|
61 | @return: value of the model |
---|
62 | """ |
---|
63 | return 0 |
---|
64 | |
---|
65 | def clone(self): |
---|
66 | """ Returns a new object identical to the current object """ |
---|
67 | |
---|
68 | obj = copy.deepcopy(self) |
---|
69 | obj.params = copy.deepcopy(self.params) |
---|
70 | obj.details = copy.deepcopy(self.details) |
---|
71 | |
---|
72 | # Check for standard data members of arithmetics sub-classes |
---|
73 | if hasattr(self, 'operateOn') and \ |
---|
74 | not self.operateOn == None: |
---|
75 | obj.operateOn = self.operateOn.clone() |
---|
76 | |
---|
77 | if hasattr(self, 'other') and \ |
---|
78 | not self.other == None: |
---|
79 | obj.other = self.other.clone() |
---|
80 | |
---|
81 | return obj |
---|
82 | |
---|
83 | def __add__(self, other): |
---|
84 | """ Overload addition operator |
---|
85 | @param other: model to add |
---|
86 | """ |
---|
87 | from sans.models.AddComponent import AddComponent |
---|
88 | return AddComponent(self.clone(), other) |
---|
89 | |
---|
90 | def __sub__(self, other): |
---|
91 | """ Overload subtraction operator |
---|
92 | @param other: model to subtract |
---|
93 | """ |
---|
94 | from sans.models.SubComponent import SubComponent |
---|
95 | return SubComponent(self.clone(), other) |
---|
96 | |
---|
97 | def __mul__(self, other): |
---|
98 | """ Overload multiplication operator |
---|
99 | @param other: model to multiply |
---|
100 | """ |
---|
101 | from sans.models.MulComponent import MulComponent |
---|
102 | return MulComponent(self.clone(), other) |
---|
103 | |
---|
104 | def __div__(self, other): |
---|
105 | """ Overload division operator |
---|
106 | @param other: mode to divide by |
---|
107 | """ |
---|
108 | from sans.models.DivComponent import DivComponent |
---|
109 | return DivComponent(self.clone(), other) |
---|
110 | |
---|
111 | def setParam(self, name, value): |
---|
112 | """ Set the value of a model parameter |
---|
113 | |
---|
114 | A list of all the parameters of a model can |
---|
115 | be obtained with the getParamList() method. |
---|
116 | For models resulting from an arithmetic |
---|
117 | operation between two models, the names of |
---|
118 | the parameters are modified according to |
---|
119 | the following rule: |
---|
120 | |
---|
121 | For parameter 'a' of model A and |
---|
122 | parameter 'b' of model B: |
---|
123 | |
---|
124 | C = A + B |
---|
125 | C.setParam('base.a') to access 'a' |
---|
126 | C.setParam('add.b') to access 'b' |
---|
127 | |
---|
128 | C = A - B |
---|
129 | C.setParam('base.a') to access 'a' |
---|
130 | C.setParam('sub.b') to access 'b' |
---|
131 | |
---|
132 | C = A * B |
---|
133 | C.setParam('base.a') to access 'a' |
---|
134 | C.setParam('mul.b') to access 'b' |
---|
135 | |
---|
136 | C = A / B |
---|
137 | C.setParam('base.a') to access 'a' |
---|
138 | C.setParam('div.b') to access 'b' |
---|
139 | |
---|
140 | @param name: name of the parameter |
---|
141 | @param value: value of the parameter |
---|
142 | """ |
---|
143 | # Lowercase for case insensitivity |
---|
144 | name = name.lower() |
---|
145 | |
---|
146 | keys = self.params.keys() |
---|
147 | found = False |
---|
148 | |
---|
149 | # Loop through the keys and find the right one |
---|
150 | # The case should not matter |
---|
151 | for key in keys: |
---|
152 | if name == key.lower(): |
---|
153 | self.params[key] = value |
---|
154 | found = True |
---|
155 | break |
---|
156 | |
---|
157 | # Check whether we found the key |
---|
158 | if not found: |
---|
159 | raise ValueError, "Model does not contain parameter %s" % name |
---|
160 | |
---|
161 | def setParamWithToken(self, name, value, token, member): |
---|
162 | """ |
---|
163 | Returns value of a parameter that is part of an internal component |
---|
164 | @param name: name of the parameter |
---|
165 | @param value: value of the parameter |
---|
166 | @param token: parameter prefix |
---|
167 | @param member: data member pointing to the component |
---|
168 | """ |
---|
169 | # Lowercase for case insensitivity |
---|
170 | name = name.lower() |
---|
171 | |
---|
172 | # Look for sub-model access |
---|
173 | toks = name.split('.') |
---|
174 | if len(toks)>1: |
---|
175 | short_name = '.'.join(toks[1:]) |
---|
176 | if toks[0] == 'base': |
---|
177 | return self.operateOn.setParam(short_name, value) |
---|
178 | elif toks[0] == token: |
---|
179 | return member.setParam(short_name, value) |
---|
180 | else: |
---|
181 | raise ValueError, "Model does not contain parameter %s" % name |
---|
182 | |
---|
183 | # If we didn't find a token we know, call the default behavior |
---|
184 | return BaseComponent.setParam(self, name, value) |
---|
185 | |
---|
186 | |
---|
187 | def getParam(self, name): |
---|
188 | """ Set the value of a model parameter |
---|
189 | |
---|
190 | A list of all the parameters of a model can |
---|
191 | be obtained with the getParamList() method. |
---|
192 | For models resulting from an arithmetic |
---|
193 | operation between two models, the names of |
---|
194 | the parameters are modified according to |
---|
195 | the following rule: |
---|
196 | |
---|
197 | For parameter 'a' of model A and |
---|
198 | parameter 'b' of model B: |
---|
199 | |
---|
200 | C = A + B |
---|
201 | C.getParam('base.a') to access 'a' |
---|
202 | C.getParam('add.b') to access 'b' |
---|
203 | |
---|
204 | C = A - B |
---|
205 | C.getParam('base.a') to access 'a' |
---|
206 | C.getParam('sub.b') to access 'b' |
---|
207 | |
---|
208 | C = A * B |
---|
209 | C.getParam('base.a') to access 'a' |
---|
210 | C.getParam('mul.b') to access 'b' |
---|
211 | |
---|
212 | C = A / B |
---|
213 | C.getParam('base.a') to access 'a' |
---|
214 | C.getParam('div.b') to access 'b' |
---|
215 | |
---|
216 | @param name: name of the parameter |
---|
217 | @param value: value of the parameter |
---|
218 | """ |
---|
219 | # Lowercase for case insensitivity |
---|
220 | name = name.lower() |
---|
221 | |
---|
222 | keys = self.params.keys() |
---|
223 | value = None |
---|
224 | |
---|
225 | # Loop through the keys and find the right one |
---|
226 | # The case should not matter |
---|
227 | for key in keys: |
---|
228 | if name == key.lower(): |
---|
229 | value = self.params[key] |
---|
230 | break |
---|
231 | |
---|
232 | # Check whether we found the key |
---|
233 | if value == None: |
---|
234 | raise ValueError, "Model does not contain parameter %s" % name |
---|
235 | |
---|
236 | return value |
---|
237 | |
---|
238 | def getParamWithToken(self, name, token, member): |
---|
239 | """ |
---|
240 | Returns value of a parameter that is part of an internal component |
---|
241 | @param name: name of the parameter |
---|
242 | @param token: parameter prefix |
---|
243 | @param member: data member pointing to the component |
---|
244 | @return: value of the parameter |
---|
245 | """ |
---|
246 | # Lowercase for case insensitivity |
---|
247 | name = name.lower() |
---|
248 | |
---|
249 | # Look for sub-model access |
---|
250 | toks = name.split('.') |
---|
251 | if len(toks)>1: |
---|
252 | #short_name = string.join(toks[1:],'.') |
---|
253 | short_name = '.'.join(toks[1:]) |
---|
254 | if toks[0] == 'base': |
---|
255 | return self.operateOn.getParam(short_name) |
---|
256 | elif toks[0] == token: |
---|
257 | return member.getParam(short_name) |
---|
258 | else: |
---|
259 | raise ValueError, "Model does not contain parameter %s" % name |
---|
260 | |
---|
261 | # If we didn't find a token we know, call the default behavior |
---|
262 | return BaseComponent.getParam(self, name) |
---|
263 | |
---|
264 | |
---|
265 | def getParamList(self): |
---|
266 | """ Return a list of all available parameters for the model |
---|
267 | |
---|
268 | For models resulting from an arithmetic |
---|
269 | operation between two models, the names of |
---|
270 | the parameters are modified according to |
---|
271 | the following rule: |
---|
272 | |
---|
273 | For parameter 'a' of model A and |
---|
274 | parameter 'b' of model B: |
---|
275 | |
---|
276 | C = A + B |
---|
277 | C.getParam('base.a') to access 'a' |
---|
278 | C.getParam('add.b') to access 'b' |
---|
279 | |
---|
280 | C = A - B |
---|
281 | C.getParam('base.a') to access 'a' |
---|
282 | C.getParam('sub.b') to access 'b' |
---|
283 | |
---|
284 | C = A * B |
---|
285 | C.getParam('base.a') to access 'a' |
---|
286 | C.getParam('mul.b') to access 'b' |
---|
287 | |
---|
288 | C = A / B |
---|
289 | C.getParam('base.a') to access 'a' |
---|
290 | C.getParam('div.b') to access 'b' |
---|
291 | |
---|
292 | """ |
---|
293 | param_list = self.params.keys() |
---|
294 | return param_list |
---|
295 | |
---|
296 | def getParamListWithToken(self, token, member): |
---|
297 | """ |
---|
298 | Return a list of all available parameters for the model |
---|
299 | |
---|
300 | @param token: parameter prefix |
---|
301 | @param member: data member pointing to internal component |
---|
302 | @return: list of parameters |
---|
303 | """ |
---|
304 | param_list = self.params.keys() |
---|
305 | if not self.operateOn == None: |
---|
306 | sub_list = self.operateOn.params.keys() |
---|
307 | for p in sub_list: |
---|
308 | param_list.append("base.%s" % p) |
---|
309 | if not member == None: |
---|
310 | sub_list = member.params.keys() |
---|
311 | for p in sub_list: |
---|
312 | param_list.append("%s.%s" % (token, p)) |
---|
313 | return param_list |
---|
314 | |
---|
315 | # End of file |
---|