[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='' |
---|
[988130c6] | 33 | #list of parameter that cannot be fitted |
---|
| 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): |
---|
| 43 | return par_name.lower() not in self.fixed |
---|
| 44 | #For the future |
---|
[836fe6e] | 45 | #return self.params[str(par_name)].is_fittable() |
---|
[988130c6] | 46 | |
---|
[ae60f86] | 47 | def run(self, x): return NotImplemented |
---|
| 48 | def runXY(self, x): return NotImplemented |
---|
[ae3ce4e] | 49 | |
---|
| 50 | def clone(self): |
---|
| 51 | """ Returns a new object identical to the current object """ |
---|
| 52 | obj = copy.deepcopy(self) |
---|
[8809e48] | 53 | return self._clone(obj) |
---|
| 54 | |
---|
| 55 | def _clone(self, obj): |
---|
| 56 | """ |
---|
| 57 | Internal utility function to copy the internal |
---|
| 58 | data members to a fresh copy. |
---|
| 59 | """ |
---|
[ae60f86] | 60 | obj.params = copy.deepcopy(self.params) |
---|
| 61 | obj.details = copy.deepcopy(self.details) |
---|
| 62 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
[ae3ce4e] | 63 | return obj |
---|
| 64 | |
---|
| 65 | def setParam(self, name, value): |
---|
[ae60f86] | 66 | """ |
---|
| 67 | Set the value of a model parameter |
---|
[ae3ce4e] | 68 | |
---|
| 69 | @param name: name of the parameter |
---|
| 70 | @param value: value of the parameter |
---|
| 71 | """ |
---|
[ae60f86] | 72 | # Look for dispersion parameters |
---|
| 73 | toks = name.split('.') |
---|
[2caecd5] | 74 | #print "dispersion",self.dispersion |
---|
[ae60f86] | 75 | if len(toks)==2: |
---|
| 76 | for item in self.dispersion.keys(): |
---|
| 77 | if item.lower()==toks[0].lower(): |
---|
| 78 | for par in self.dispersion[item]: |
---|
| 79 | if par.lower() == toks[1].lower(): |
---|
| 80 | self.dispersion[item][par] = value |
---|
| 81 | return |
---|
| 82 | else: |
---|
| 83 | # Look for standard parameter |
---|
| 84 | for item in self.params.keys(): |
---|
| 85 | if item.lower()==name.lower(): |
---|
| 86 | self.params[item] = value |
---|
[836fe6e] | 87 | #self.params[item] = Parameter(name= item,value=value, is_fixed=False) |
---|
| 88 | #print "setparam,self.param",self.params.values() |
---|
[ae60f86] | 89 | return |
---|
| 90 | |
---|
| 91 | raise ValueError, "Model does not contain parameter %s" % name |
---|
[ae3ce4e] | 92 | |
---|
[ae60f86] | 93 | def getParam(self, name): |
---|
| 94 | """ |
---|
| 95 | Set the value of a model parameter |
---|
| 96 | |
---|
[ae3ce4e] | 97 | @param name: name of the parameter |
---|
| 98 | """ |
---|
[ae60f86] | 99 | # Look for dispersion parameters |
---|
[ae3ce4e] | 100 | toks = name.split('.') |
---|
[ae60f86] | 101 | if len(toks)==2: |
---|
| 102 | for item in self.dispersion.keys(): |
---|
| 103 | if item.lower()==toks[0].lower(): |
---|
| 104 | for par in self.dispersion[item]: |
---|
| 105 | if par.lower() == toks[1].lower(): |
---|
| 106 | return self.dispersion[item][par] |
---|
| 107 | else: |
---|
| 108 | # Look for standard parameter |
---|
| 109 | for item in self.params.keys(): |
---|
| 110 | if item.lower()==name.lower(): |
---|
[836fe6e] | 111 | #print "getparam", self.params |
---|
| 112 | #return self.params[item].value |
---|
[ae60f86] | 113 | return self.params[item] |
---|
| 114 | |
---|
| 115 | raise ValueError, "Model does not contain parameter %s" % name |
---|
| 116 | |
---|
[ae3ce4e] | 117 | def getParamList(self): |
---|
| 118 | """ |
---|
[ae60f86] | 119 | Return a list of all available parameters for the model |
---|
| 120 | """ |
---|
| 121 | list = self.params.keys() |
---|
| 122 | # WARNING: Extending the list with the dispersion parameters |
---|
| 123 | list.extend(self.getDispParamList()) |
---|
| 124 | return list |
---|
| 125 | |
---|
| 126 | def getDispParamList(self): |
---|
| 127 | """ |
---|
| 128 | Return a list of all available parameters for the model |
---|
| 129 | """ |
---|
| 130 | list = [] |
---|
| 131 | |
---|
| 132 | for item in self.dispersion.keys(): |
---|
| 133 | for p in self.dispersion[item].keys(): |
---|
| 134 | if p not in ['type']: |
---|
| 135 | list.append('%s.%s' % (item.lower(), p.lower())) |
---|
| 136 | |
---|
| 137 | return list |
---|
| 138 | |
---|
| 139 | # Old-style methods that are no longer used |
---|
| 140 | def setParamWithToken(self, name, value, token, member): return NotImplemented |
---|
| 141 | def getParamWithToken(self, name, token, member): return NotImplemented |
---|
| 142 | def getParamListWithToken(self, token, member): return NotImplemented |
---|
| 143 | def __add__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
| 144 | def __sub__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
| 145 | def __mul__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
| 146 | def __div__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
| 147 | |
---|