[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 = {} |
---|
[5f89fb8] | 28 | # string containing information about the model such as the equation |
---|
| 29 | #of the given model, exception or possible use |
---|
| 30 | self.description='' |
---|
[ae3ce4e] | 31 | |
---|
| 32 | def __str__(self): |
---|
[ae60f86] | 33 | """ |
---|
[ae3ce4e] | 34 | @return: string representation |
---|
| 35 | """ |
---|
| 36 | return self.name |
---|
| 37 | |
---|
[ae60f86] | 38 | def run(self, x): return NotImplemented |
---|
| 39 | def runXY(self, x): return NotImplemented |
---|
[ae3ce4e] | 40 | |
---|
| 41 | def clone(self): |
---|
| 42 | """ Returns a new object identical to the current object """ |
---|
| 43 | obj = copy.deepcopy(self) |
---|
[8809e48] | 44 | return self._clone(obj) |
---|
| 45 | |
---|
| 46 | def _clone(self, obj): |
---|
| 47 | """ |
---|
| 48 | Internal utility function to copy the internal |
---|
| 49 | data members to a fresh copy. |
---|
| 50 | """ |
---|
[ae60f86] | 51 | obj.params = copy.deepcopy(self.params) |
---|
| 52 | obj.details = copy.deepcopy(self.details) |
---|
| 53 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
[ae3ce4e] | 54 | return obj |
---|
| 55 | |
---|
| 56 | def setParam(self, name, value): |
---|
[ae60f86] | 57 | """ |
---|
| 58 | Set the value of a model parameter |
---|
[ae3ce4e] | 59 | |
---|
| 60 | @param name: name of the parameter |
---|
| 61 | @param value: value of the parameter |
---|
| 62 | """ |
---|
[ae60f86] | 63 | # Look for dispersion parameters |
---|
| 64 | toks = name.split('.') |
---|
| 65 | if len(toks)==2: |
---|
| 66 | for item in self.dispersion.keys(): |
---|
| 67 | if item.lower()==toks[0].lower(): |
---|
| 68 | for par in self.dispersion[item]: |
---|
| 69 | if par.lower() == toks[1].lower(): |
---|
| 70 | self.dispersion[item][par] = value |
---|
| 71 | return |
---|
| 72 | else: |
---|
| 73 | # Look for standard parameter |
---|
| 74 | for item in self.params.keys(): |
---|
| 75 | if item.lower()==name.lower(): |
---|
| 76 | self.params[item] = value |
---|
| 77 | return |
---|
| 78 | |
---|
| 79 | raise ValueError, "Model does not contain parameter %s" % name |
---|
[ae3ce4e] | 80 | |
---|
[ae60f86] | 81 | def getParam(self, name): |
---|
| 82 | """ |
---|
| 83 | Set the value of a model parameter |
---|
| 84 | |
---|
[ae3ce4e] | 85 | @param name: name of the parameter |
---|
| 86 | """ |
---|
[ae60f86] | 87 | # Look for dispersion parameters |
---|
[ae3ce4e] | 88 | toks = name.split('.') |
---|
[ae60f86] | 89 | if len(toks)==2: |
---|
| 90 | for item in self.dispersion.keys(): |
---|
| 91 | if item.lower()==toks[0].lower(): |
---|
| 92 | for par in self.dispersion[item]: |
---|
| 93 | if par.lower() == toks[1].lower(): |
---|
| 94 | return self.dispersion[item][par] |
---|
| 95 | else: |
---|
| 96 | # Look for standard parameter |
---|
| 97 | for item in self.params.keys(): |
---|
| 98 | if item.lower()==name.lower(): |
---|
| 99 | return self.params[item] |
---|
| 100 | |
---|
| 101 | raise ValueError, "Model does not contain parameter %s" % name |
---|
| 102 | |
---|
[ae3ce4e] | 103 | def getParamList(self): |
---|
| 104 | """ |
---|
[ae60f86] | 105 | Return a list of all available parameters for the model |
---|
| 106 | """ |
---|
| 107 | list = self.params.keys() |
---|
| 108 | # WARNING: Extending the list with the dispersion parameters |
---|
| 109 | list.extend(self.getDispParamList()) |
---|
| 110 | return list |
---|
| 111 | |
---|
| 112 | def getDispParamList(self): |
---|
| 113 | """ |
---|
| 114 | Return a list of all available parameters for the model |
---|
| 115 | """ |
---|
| 116 | list = [] |
---|
| 117 | |
---|
| 118 | for item in self.dispersion.keys(): |
---|
| 119 | for p in self.dispersion[item].keys(): |
---|
| 120 | if p not in ['type']: |
---|
| 121 | list.append('%s.%s' % (item.lower(), p.lower())) |
---|
| 122 | |
---|
| 123 | return list |
---|
| 124 | |
---|
| 125 | # Old-style methods that are no longer used |
---|
| 126 | def setParamWithToken(self, name, value, token, member): return NotImplemented |
---|
| 127 | def getParamWithToken(self, name, token, member): return NotImplemented |
---|
| 128 | def getParamListWithToken(self, token, member): return NotImplemented |
---|
| 129 | def __add__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
| 130 | def __sub__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
| 131 | def __mul__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
| 132 | def __div__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
| 133 | |
---|