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