1 | #!/usr/bin/env python |
---|
2 | """ |
---|
3 | Provide base functionality for all model components |
---|
4 | """ |
---|
5 | |
---|
6 | # imports |
---|
7 | import copy |
---|
8 | #TO DO: that about a way to make the parameter |
---|
9 | #is self return if it is fittable or not |
---|
10 | class BaseComponent: |
---|
11 | """ |
---|
12 | Basic model component |
---|
13 | |
---|
14 | Since version 0.5.0, basic operations are no longer supported. |
---|
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 = {} |
---|
25 | self.details = {} |
---|
26 | ## Dictionary used to store the dispersity/averaging |
---|
27 | # parameters of dispersed/averaged parameters. |
---|
28 | self.dispersion = {} |
---|
29 | # string containing information about the model such as the equation |
---|
30 | #of the given model, exception or possible use |
---|
31 | self.description='' |
---|
32 | #list of parameter that cannot be fitted |
---|
33 | self.fixed= [] |
---|
34 | |
---|
35 | def __str__(self): |
---|
36 | """ |
---|
37 | @return: string representation |
---|
38 | """ |
---|
39 | return self.name |
---|
40 | |
---|
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 | |
---|
46 | def run(self, x): return NotImplemented |
---|
47 | def runXY(self, x): return NotImplemented |
---|
48 | |
---|
49 | def clone(self): |
---|
50 | """ Returns a new object identical to the current object """ |
---|
51 | obj = copy.deepcopy(self) |
---|
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 | """ |
---|
59 | obj.params = copy.deepcopy(self.params) |
---|
60 | obj.details = copy.deepcopy(self.details) |
---|
61 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
62 | return obj |
---|
63 | |
---|
64 | def setParam(self, name, value): |
---|
65 | """ |
---|
66 | Set the value of a model parameter |
---|
67 | |
---|
68 | @param name: name of the parameter |
---|
69 | @param value: value of the parameter |
---|
70 | """ |
---|
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 |
---|
88 | |
---|
89 | def getParam(self, name): |
---|
90 | """ |
---|
91 | Set the value of a model parameter |
---|
92 | |
---|
93 | @param name: name of the parameter |
---|
94 | """ |
---|
95 | # Look for dispersion parameters |
---|
96 | toks = name.split('.') |
---|
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 | |
---|
111 | def getParamList(self): |
---|
112 | """ |
---|
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 | |
---|