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