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