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