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