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 | ## Dictionary used to store the dispersity/averaging |
---|
51 | # parameters of dispersed/averaged parameters. |
---|
52 | self.dispersion = {} |
---|
53 | |
---|
54 | def __str__(self): |
---|
55 | """ Prints an XML representation of the model |
---|
56 | @return: string representation |
---|
57 | """ |
---|
58 | return self.name |
---|
59 | |
---|
60 | def run(self, x=0): #pylint: disable-msg=R0201 |
---|
61 | """ Evaluate the model |
---|
62 | Dummy function |
---|
63 | @param x: input value |
---|
64 | @return: value of the model |
---|
65 | """ |
---|
66 | return 0 |
---|
67 | |
---|
68 | def clone(self): |
---|
69 | """ Returns a new object identical to the current object """ |
---|
70 | |
---|
71 | obj = copy.deepcopy(self) |
---|
72 | obj.params = copy.deepcopy(self.params) |
---|
73 | obj.details = copy.deepcopy(self.details) |
---|
74 | |
---|
75 | # Check for standard data members of arithmetics sub-classes |
---|
76 | if hasattr(self, 'operateOn') and \ |
---|
77 | not self.operateOn == None: |
---|
78 | obj.operateOn = self.operateOn.clone() |
---|
79 | |
---|
80 | if hasattr(self, 'other') and \ |
---|
81 | not self.other == None: |
---|
82 | obj.other = self.other.clone() |
---|
83 | |
---|
84 | return obj |
---|
85 | |
---|
86 | def __add__(self, other): |
---|
87 | """ Overload addition operator |
---|
88 | @param other: model to add |
---|
89 | """ |
---|
90 | from sans.models.AddComponent import AddComponent |
---|
91 | return AddComponent(self.clone(), other) |
---|
92 | |
---|
93 | def __sub__(self, other): |
---|
94 | """ Overload subtraction operator |
---|
95 | @param other: model to subtract |
---|
96 | """ |
---|
97 | from sans.models.SubComponent import SubComponent |
---|
98 | return SubComponent(self.clone(), other) |
---|
99 | |
---|
100 | def __mul__(self, other): |
---|
101 | """ Overload multiplication operator |
---|
102 | @param other: model to multiply |
---|
103 | """ |
---|
104 | from sans.models.MulComponent import MulComponent |
---|
105 | return MulComponent(self.clone(), other) |
---|
106 | |
---|
107 | def __div__(self, other): |
---|
108 | """ Overload division operator |
---|
109 | @param other: mode to divide by |
---|
110 | """ |
---|
111 | from sans.models.DivComponent import DivComponent |
---|
112 | return DivComponent(self.clone(), other) |
---|
113 | |
---|
114 | def setParam(self, name, value): |
---|
115 | """ Set the value of a model parameter |
---|
116 | |
---|
117 | A list of all the parameters of a model can |
---|
118 | be obtained with the getParamList() method. |
---|
119 | For models resulting from an arithmetic |
---|
120 | operation between two models, the names of |
---|
121 | the parameters are modified according to |
---|
122 | the following rule: |
---|
123 | |
---|
124 | For parameter 'a' of model A and |
---|
125 | parameter 'b' of model B: |
---|
126 | |
---|
127 | C = A + B |
---|
128 | C.setParam('base.a') to access 'a' |
---|
129 | C.setParam('add.b') to access 'b' |
---|
130 | |
---|
131 | C = A - B |
---|
132 | C.setParam('base.a') to access 'a' |
---|
133 | C.setParam('sub.b') to access 'b' |
---|
134 | |
---|
135 | C = A * B |
---|
136 | C.setParam('base.a') to access 'a' |
---|
137 | C.setParam('mul.b') to access 'b' |
---|
138 | |
---|
139 | C = A / B |
---|
140 | C.setParam('base.a') to access 'a' |
---|
141 | C.setParam('div.b') to access 'b' |
---|
142 | |
---|
143 | @param name: name of the parameter |
---|
144 | @param value: value of the parameter |
---|
145 | """ |
---|
146 | # Lowercase for case insensitivity |
---|
147 | name = name.lower() |
---|
148 | |
---|
149 | keys = self.params.keys() |
---|
150 | found = False |
---|
151 | |
---|
152 | # Loop through the keys and find the right one |
---|
153 | # The case should not matter |
---|
154 | for key in keys: |
---|
155 | if name == key.lower(): |
---|
156 | self.params[key] = value |
---|
157 | found = True |
---|
158 | break |
---|
159 | |
---|
160 | # Check whether we found the key |
---|
161 | if not found: |
---|
162 | raise ValueError, "Model does not contain parameter %s" % name |
---|
163 | |
---|
164 | def setParamWithToken(self, name, value, token, member): |
---|
165 | """ |
---|
166 | Returns value of a parameter that is part of an internal component |
---|
167 | @param name: name of the parameter |
---|
168 | @param value: value of the parameter |
---|
169 | @param token: parameter prefix |
---|
170 | @param member: data member pointing to the component |
---|
171 | """ |
---|
172 | # Lowercase for case insensitivity |
---|
173 | name = name.lower() |
---|
174 | |
---|
175 | # Look for sub-model access |
---|
176 | toks = name.split('.') |
---|
177 | if len(toks)>1: |
---|
178 | short_name = '.'.join(toks[1:]) |
---|
179 | if toks[0] == 'base': |
---|
180 | return self.operateOn.setParam(short_name, value) |
---|
181 | elif toks[0] == token: |
---|
182 | return member.setParam(short_name, value) |
---|
183 | else: |
---|
184 | raise ValueError, "Model does not contain parameter %s" % name |
---|
185 | |
---|
186 | # If we didn't find a token we know, call the default behavior |
---|
187 | return BaseComponent.setParam(self, name, value) |
---|
188 | |
---|
189 | |
---|
190 | def getParam(self, name): |
---|
191 | """ Set the value of a model parameter |
---|
192 | |
---|
193 | A list of all the parameters of a model can |
---|
194 | be obtained with the getParamList() method. |
---|
195 | For models resulting from an arithmetic |
---|
196 | operation between two models, the names of |
---|
197 | the parameters are modified according to |
---|
198 | the following rule: |
---|
199 | |
---|
200 | For parameter 'a' of model A and |
---|
201 | parameter 'b' of model B: |
---|
202 | |
---|
203 | C = A + B |
---|
204 | C.getParam('base.a') to access 'a' |
---|
205 | C.getParam('add.b') to access 'b' |
---|
206 | |
---|
207 | C = A - B |
---|
208 | C.getParam('base.a') to access 'a' |
---|
209 | C.getParam('sub.b') to access 'b' |
---|
210 | |
---|
211 | C = A * B |
---|
212 | C.getParam('base.a') to access 'a' |
---|
213 | C.getParam('mul.b') to access 'b' |
---|
214 | |
---|
215 | C = A / B |
---|
216 | C.getParam('base.a') to access 'a' |
---|
217 | C.getParam('div.b') to access 'b' |
---|
218 | |
---|
219 | @param name: name of the parameter |
---|
220 | @param value: value of the parameter |
---|
221 | """ |
---|
222 | # Lowercase for case insensitivity |
---|
223 | name = name.lower() |
---|
224 | |
---|
225 | keys = self.params.keys() |
---|
226 | value = None |
---|
227 | |
---|
228 | # Loop through the keys and find the right one |
---|
229 | # The case should not matter |
---|
230 | for key in keys: |
---|
231 | if name == key.lower(): |
---|
232 | value = self.params[key] |
---|
233 | break |
---|
234 | |
---|
235 | # Check whether we found the key |
---|
236 | if value == None: |
---|
237 | raise ValueError, "Model does not contain parameter %s" % name |
---|
238 | |
---|
239 | return value |
---|
240 | |
---|
241 | def getParamWithToken(self, name, token, member): |
---|
242 | """ |
---|
243 | Returns value of a parameter that is part of an internal component |
---|
244 | @param name: name of the parameter |
---|
245 | @param token: parameter prefix |
---|
246 | @param member: data member pointing to the component |
---|
247 | @return: value of the parameter |
---|
248 | """ |
---|
249 | # Lowercase for case insensitivity |
---|
250 | name = name.lower() |
---|
251 | |
---|
252 | # Look for sub-model access |
---|
253 | toks = name.split('.') |
---|
254 | if len(toks)>1: |
---|
255 | #short_name = string.join(toks[1:],'.') |
---|
256 | short_name = '.'.join(toks[1:]) |
---|
257 | if toks[0] == 'base': |
---|
258 | return self.operateOn.getParam(short_name) |
---|
259 | elif toks[0] == token: |
---|
260 | return member.getParam(short_name) |
---|
261 | else: |
---|
262 | raise ValueError, "Model does not contain parameter %s" % name |
---|
263 | |
---|
264 | # If we didn't find a token we know, call the default behavior |
---|
265 | return BaseComponent.getParam(self, name) |
---|
266 | |
---|
267 | |
---|
268 | def getParamList(self): |
---|
269 | """ Return a list of all available parameters for the model |
---|
270 | |
---|
271 | For models resulting from an arithmetic |
---|
272 | operation between two models, the names of |
---|
273 | the parameters are modified according to |
---|
274 | the following rule: |
---|
275 | |
---|
276 | For parameter 'a' of model A and |
---|
277 | parameter 'b' of model B: |
---|
278 | |
---|
279 | C = A + B |
---|
280 | C.getParam('base.a') to access 'a' |
---|
281 | C.getParam('add.b') to access 'b' |
---|
282 | |
---|
283 | C = A - B |
---|
284 | C.getParam('base.a') to access 'a' |
---|
285 | C.getParam('sub.b') to access 'b' |
---|
286 | |
---|
287 | C = A * B |
---|
288 | C.getParam('base.a') to access 'a' |
---|
289 | C.getParam('mul.b') to access 'b' |
---|
290 | |
---|
291 | C = A / B |
---|
292 | C.getParam('base.a') to access 'a' |
---|
293 | C.getParam('div.b') to access 'b' |
---|
294 | |
---|
295 | """ |
---|
296 | param_list = self.params.keys() |
---|
297 | return param_list |
---|
298 | |
---|
299 | def getParamListWithToken(self, token, member): |
---|
300 | """ |
---|
301 | Return a list of all available parameters for the model |
---|
302 | |
---|
303 | @param token: parameter prefix |
---|
304 | @param member: data member pointing to internal component |
---|
305 | @return: list of parameters |
---|
306 | """ |
---|
307 | param_list = self.params.keys() |
---|
308 | if not self.operateOn == None: |
---|
309 | sub_list = self.operateOn.params.keys() |
---|
310 | for p in sub_list: |
---|
311 | param_list.append("base.%s" % p) |
---|
312 | if not member == None: |
---|
313 | sub_list = member.params.keys() |
---|
314 | for p in sub_list: |
---|
315 | param_list.append("%s.%s" % (token, p)) |
---|
316 | return param_list |
---|
317 | |
---|
318 | # End of file |
---|