1 | #!/usr/bin/env python |
---|
2 | """ |
---|
3 | Provide base functionality for all model components |
---|
4 | """ |
---|
5 | |
---|
6 | # imports |
---|
7 | import copy |
---|
8 | import numpy |
---|
9 | #TO DO: that about a way to make the parameter |
---|
10 | #is self return if it is fittable or not |
---|
11 | |
---|
12 | class BaseComponent: |
---|
13 | """ |
---|
14 | Basic model component |
---|
15 | |
---|
16 | Since version 0.5.0, basic operations are no longer supported. |
---|
17 | """ |
---|
18 | |
---|
19 | def __init__(self): |
---|
20 | """ Initialization""" |
---|
21 | |
---|
22 | ## Name of the model |
---|
23 | self.name = "BaseComponent" |
---|
24 | |
---|
25 | ## Parameters to be accessed by client |
---|
26 | self.params = {} |
---|
27 | self.details = {} |
---|
28 | ## Dictionary used to store the dispersity/averaging |
---|
29 | # parameters of dispersed/averaged parameters. |
---|
30 | self.dispersion = {} |
---|
31 | # string containing information about the model such as the equation |
---|
32 | #of the given model, exception or possible use |
---|
33 | self.description='' |
---|
34 | #list of parameter that can be fitted |
---|
35 | self.fixed= [] |
---|
36 | ## parameters with orientation |
---|
37 | self.orientation_params =[] |
---|
38 | ## store dispersity reference |
---|
39 | self._persistency_dict = {} |
---|
40 | |
---|
41 | def __str__(self): |
---|
42 | """ |
---|
43 | @return: string representation |
---|
44 | """ |
---|
45 | return self.name |
---|
46 | |
---|
47 | def is_fittable(self, par_name): |
---|
48 | """ |
---|
49 | Check if a given parameter is fittable or not |
---|
50 | @param par_name: the parameter name to check |
---|
51 | """ |
---|
52 | return par_name.lower() in self.fixed |
---|
53 | #For the future |
---|
54 | #return self.params[str(par_name)].is_fittable() |
---|
55 | |
---|
56 | def run(self, x): return NotImplemented |
---|
57 | def runXY(self, x): return NotImplemented |
---|
58 | |
---|
59 | def evalDistribution(self, qdist): |
---|
60 | """ |
---|
61 | Evaluate a distribution of q-values. |
---|
62 | A list of either scale q-values or [qx,qy] doublets |
---|
63 | are assumed. The allowed data types for the doublets are |
---|
64 | the same as for run() and runXY(). |
---|
65 | |
---|
66 | Since the difference between scale and vector q-values |
---|
67 | are dealt with in runXY(), we pass along the values |
---|
68 | as-is. |
---|
69 | |
---|
70 | @param qdist: list of scalar q-values or list of [qx,qy] doublets |
---|
71 | """ |
---|
72 | q_array = numpy.asarray(qdist) |
---|
73 | iq_array = numpy.zeros(len(q_array)) |
---|
74 | for i in len(q_array): |
---|
75 | iq_array[i] = self.runXY(q_array[i]) |
---|
76 | |
---|
77 | return iq_array |
---|
78 | |
---|
79 | def clone(self): |
---|
80 | """ Returns a new object identical to the current object """ |
---|
81 | obj = copy.deepcopy(self) |
---|
82 | return self._clone(obj) |
---|
83 | |
---|
84 | def _clone(self, obj): |
---|
85 | """ |
---|
86 | Internal utility function to copy the internal |
---|
87 | data members to a fresh copy. |
---|
88 | """ |
---|
89 | obj.params = copy.deepcopy(self.params) |
---|
90 | obj.details = copy.deepcopy(self.details) |
---|
91 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
92 | obj._persistency_dict = copy.deepcopy( self._persistency_dict) |
---|
93 | return obj |
---|
94 | |
---|
95 | def setParam(self, name, value): |
---|
96 | """ |
---|
97 | Set the value of a model parameter |
---|
98 | |
---|
99 | @param name: name of the parameter |
---|
100 | @param value: value of the parameter |
---|
101 | """ |
---|
102 | # Look for dispersion parameters |
---|
103 | toks = name.split('.') |
---|
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 | self.dispersion[item][par] = value |
---|
110 | return |
---|
111 | else: |
---|
112 | # Look for standard parameter |
---|
113 | for item in self.params.keys(): |
---|
114 | if item.lower()==name.lower(): |
---|
115 | self.params[item] = value |
---|
116 | return |
---|
117 | |
---|
118 | raise ValueError, "Model does not contain parameter %s" % name |
---|
119 | |
---|
120 | def getParam(self, name): |
---|
121 | """ |
---|
122 | Set the value of a model parameter |
---|
123 | |
---|
124 | @param name: name of the parameter |
---|
125 | """ |
---|
126 | # Look for dispersion parameters |
---|
127 | toks = name.split('.') |
---|
128 | if len(toks)==2: |
---|
129 | for item in self.dispersion.keys(): |
---|
130 | if item.lower()==toks[0].lower(): |
---|
131 | for par in self.dispersion[item]: |
---|
132 | if par.lower() == toks[1].lower(): |
---|
133 | return self.dispersion[item][par] |
---|
134 | else: |
---|
135 | # Look for standard parameter |
---|
136 | for item in self.params.keys(): |
---|
137 | if item.lower()==name.lower(): |
---|
138 | return self.params[item] |
---|
139 | |
---|
140 | raise ValueError, "Model does not contain parameter %s" % name |
---|
141 | |
---|
142 | def getParamList(self): |
---|
143 | """ |
---|
144 | Return a list of all available parameters for the model |
---|
145 | """ |
---|
146 | list = self.params.keys() |
---|
147 | # WARNING: Extending the list with the dispersion parameters |
---|
148 | list.extend(self.getDispParamList()) |
---|
149 | return list |
---|
150 | |
---|
151 | def getDispParamList(self): |
---|
152 | """ |
---|
153 | Return a list of all available parameters for the model |
---|
154 | """ |
---|
155 | list = [] |
---|
156 | |
---|
157 | for item in self.dispersion.keys(): |
---|
158 | for p in self.dispersion[item].keys(): |
---|
159 | if p not in ['type']: |
---|
160 | list.append('%s.%s' % (item.lower(), p.lower())) |
---|
161 | |
---|
162 | return list |
---|
163 | |
---|
164 | # Old-style methods that are no longer used |
---|
165 | def setParamWithToken(self, name, value, token, member): return NotImplemented |
---|
166 | def getParamWithToken(self, name, token, member): return NotImplemented |
---|
167 | def getParamListWithToken(self, token, member): return NotImplemented |
---|
168 | def __add__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
169 | def __sub__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
170 | def __mul__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
171 | def __div__(self, other): raise ValueError, "Model operation are no longer supported" |
---|
172 | |
---|