[ae3ce4e] | 1 | #!/usr/bin/env python |
---|
[ae60f86] | 2 | """ |
---|
| 3 | Provide base functionality for all model components |
---|
[ae3ce4e] | 4 | """ |
---|
| 5 | |
---|
| 6 | # imports |
---|
| 7 | import copy |
---|
| 8 | |
---|
| 9 | class BaseComponent: |
---|
[ae60f86] | 10 | """ |
---|
| 11 | Basic model component |
---|
[ae3ce4e] | 12 | |
---|
[ae60f86] | 13 | Since version 0.5.0, basic operations are no longer supported. |
---|
[ae3ce4e] | 14 | """ |
---|
| 15 | |
---|
| 16 | def __init__(self): |
---|
| 17 | """ Initialization""" |
---|
| 18 | |
---|
| 19 | ## Name of the model |
---|
| 20 | self.name = "BaseComponent" |
---|
| 21 | |
---|
| 22 | ## Parameters to be accessed by client |
---|
| 23 | self.params = {} |
---|
[3db3895] | 24 | self.details = {} |
---|
[d30fdde] | 25 | ## Dictionary used to store the dispersity/averaging |
---|
| 26 | # parameters of dispersed/averaged parameters. |
---|
| 27 | self.dispersion = {} |
---|
[ae3ce4e] | 28 | |
---|
| 29 | def __str__(self): |
---|
[ae60f86] | 30 | """ |
---|
[ae3ce4e] | 31 | @return: string representation |
---|
| 32 | """ |
---|
| 33 | return self.name |
---|
| 34 | |
---|
[ae60f86] | 35 | def run(self, x): return NotImplemented |
---|
| 36 | def runXY(self, x): return NotImplemented |
---|
[ae3ce4e] | 37 | |
---|
| 38 | def clone(self): |
---|
| 39 | """ Returns a new object identical to the current object """ |
---|
| 40 | obj = copy.deepcopy(self) |
---|
[8809e48] | 41 | return self._clone(obj) |
---|
| 42 | |
---|
| 43 | def _clone(self, obj): |
---|
| 44 | """ |
---|
| 45 | Internal utility function to copy the internal |
---|
| 46 | data members to a fresh copy. |
---|
| 47 | """ |
---|
[ae60f86] | 48 | obj.params = copy.deepcopy(self.params) |
---|
| 49 | obj.details = copy.deepcopy(self.details) |
---|
| 50 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
[ae3ce4e] | 51 | return obj |
---|
| 52 | |
---|
| 53 | def setParam(self, name, value): |
---|
[ae60f86] | 54 | """ |
---|
| 55 | Set the value of a model parameter |
---|
[ae3ce4e] | 56 | |
---|
| 57 | @param name: name of the parameter |
---|
| 58 | @param value: value of the parameter |
---|
| 59 | """ |
---|
[ae60f86] | 60 | # Look for dispersion parameters |
---|
| 61 | toks = name.split('.') |
---|
| 62 | if len(toks)==2: |
---|
| 63 | for item in self.dispersion.keys(): |
---|
| 64 | if item.lower()==toks[0].lower(): |
---|
| 65 | for par in self.dispersion[item]: |
---|
| 66 | if par.lower() == toks[1].lower(): |
---|
| 67 | self.dispersion[item][par] = value |
---|
| 68 | return |
---|
| 69 | else: |
---|
| 70 | # Look for standard parameter |
---|
| 71 | for item in self.params.keys(): |
---|
| 72 | if item.lower()==name.lower(): |
---|
| 73 | self.params[item] = value |
---|
| 74 | return |
---|
| 75 | |
---|
| 76 | raise ValueError, "Model does not contain parameter %s" % name |
---|
[ae3ce4e] | 77 | |
---|
[ae60f86] | 78 | def getParam(self, name): |
---|
| 79 | """ |
---|
| 80 | Set the value of a model parameter |
---|
| 81 | |
---|
[ae3ce4e] | 82 | @param name: name of the parameter |
---|
| 83 | """ |
---|
[ae60f86] | 84 | # Look for dispersion parameters |
---|
[ae3ce4e] | 85 | toks = name.split('.') |
---|
[ae60f86] | 86 | if len(toks)==2: |
---|
| 87 | for item in self.dispersion.keys(): |
---|
| 88 | if item.lower()==toks[0].lower(): |
---|
| 89 | for par in self.dispersion[item]: |
---|
| 90 | if par.lower() == toks[1].lower(): |
---|
| 91 | return self.dispersion[item][par] |
---|
| 92 | else: |
---|
| 93 | # Look for standard parameter |
---|
| 94 | for item in self.params.keys(): |
---|
| 95 | if item.lower()==name.lower(): |
---|
| 96 | return self.params[item] |
---|
| 97 | |
---|
| 98 | raise ValueError, "Model does not contain parameter %s" % name |
---|
| 99 | |
---|
[ae3ce4e] | 100 | def getParamList(self): |
---|
| 101 | """ |
---|
[ae60f86] | 102 | Return a list of all available parameters for the model |
---|
| 103 | """ |
---|
| 104 | list = self.params.keys() |
---|
| 105 | # WARNING: Extending the list with the dispersion parameters |
---|
| 106 | list.extend(self.getDispParamList()) |
---|
| 107 | return list |
---|
| 108 | |
---|
| 109 | def getDispParamList(self): |
---|
| 110 | """ |
---|
| 111 | Return a list of all available parameters for the model |
---|
| 112 | """ |
---|
| 113 | list = [] |
---|
| 114 | |
---|
| 115 | for item in self.dispersion.keys(): |
---|
| 116 | for p in self.dispersion[item].keys(): |
---|
| 117 | if p not in ['type']: |
---|
| 118 | list.append('%s.%s' % (item.lower(), p.lower())) |
---|
| 119 | |
---|
| 120 | return list |
---|
| 121 | |
---|
| 122 | # Old-style methods that are no longer used |
---|
| 123 | def setParamWithToken(self, name, value, token, member): return NotImplemented |
---|
| 124 | def getParamWithToken(self, name, token, member): return NotImplemented |
---|
| 125 | def getParamListWithToken(self, token, member): return NotImplemented |
---|
| 126 | def __add__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
| 127 | def __sub__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
| 128 | def __mul__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
| 129 | def __div__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
| 130 | |
---|