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