1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
4 | Provide base functionality for all model components |
---|
5 | """ |
---|
6 | |
---|
7 | # imports |
---|
8 | import copy |
---|
9 | import numpy |
---|
10 | #TO DO: that about a way to make the parameter |
---|
11 | #is self return if it is fittable or not |
---|
12 | |
---|
13 | class BaseComponent: |
---|
14 | """ |
---|
15 | Basic model component |
---|
16 | |
---|
17 | Since version 0.5.0, basic operations are no longer supported. |
---|
18 | """ |
---|
19 | |
---|
20 | def __init__(self): |
---|
21 | """ Initialization""" |
---|
22 | |
---|
23 | ## Name of the model |
---|
24 | self.name = "BaseComponent" |
---|
25 | |
---|
26 | ## Parameters to be accessed by client |
---|
27 | self.params = {} |
---|
28 | self.details = {} |
---|
29 | ## Dictionary used to store the dispersity/averaging |
---|
30 | # parameters of dispersed/averaged parameters. |
---|
31 | self.dispersion = {} |
---|
32 | # string containing information about the model such as the equation |
---|
33 | #of the given model, exception or possible use |
---|
34 | self.description = '' |
---|
35 | #list of parameter that can be fitted |
---|
36 | self.fixed = [] |
---|
37 | #list of non-fittable parameter |
---|
38 | self.non_fittable = [] |
---|
39 | ## parameters with orientation |
---|
40 | self.orientation_params = [] |
---|
41 | ## magnetic parameters |
---|
42 | self.magnetic_params = [] |
---|
43 | ## store dispersity reference |
---|
44 | self._persistency_dict = {} |
---|
45 | ## independent parameter name and unit [string] |
---|
46 | self.input_name = "Q" |
---|
47 | self.input_unit = "A^{-1}" |
---|
48 | ## output name and unit [string] |
---|
49 | self.output_name = "Intensity" |
---|
50 | self.output_unit = "cm^{-1}" |
---|
51 | |
---|
52 | def __str__(self): |
---|
53 | """ |
---|
54 | :return: string representatio |
---|
55 | """ |
---|
56 | return self.name |
---|
57 | |
---|
58 | def is_fittable(self, par_name): |
---|
59 | """ |
---|
60 | Check if a given parameter is fittable or not |
---|
61 | |
---|
62 | :param par_name: the parameter name to check |
---|
63 | |
---|
64 | """ |
---|
65 | return par_name.lower() in self.fixed |
---|
66 | #For the future |
---|
67 | #return self.params[str(par_name)].is_fittable() |
---|
68 | |
---|
69 | def run(self, x): |
---|
70 | """ |
---|
71 | run 1d |
---|
72 | """ |
---|
73 | return NotImplemented |
---|
74 | |
---|
75 | def runXY(self, x): |
---|
76 | """ |
---|
77 | run 2d |
---|
78 | """ |
---|
79 | return NotImplemented |
---|
80 | |
---|
81 | def calculate_ER(self): |
---|
82 | """ |
---|
83 | Calculate effective radius |
---|
84 | """ |
---|
85 | return NotImplemented |
---|
86 | |
---|
87 | def calculate_VR(self): |
---|
88 | """ |
---|
89 | Calculate volume fraction ratio |
---|
90 | """ |
---|
91 | return NotImplemented |
---|
92 | |
---|
93 | def evalDistribution(self, qdist): |
---|
94 | """ |
---|
95 | Evaluate a distribution of q-values. |
---|
96 | |
---|
97 | * For 1D, a numpy array is expected as input: :: |
---|
98 | |
---|
99 | evalDistribution(q) |
---|
100 | |
---|
101 | where q is a numpy array. |
---|
102 | |
---|
103 | |
---|
104 | * For 2D, a list of numpy arrays are expected: [qx_prime,qy_prime], |
---|
105 | where 1D arrays, :: |
---|
106 | |
---|
107 | qx_prime = [ qx[0], qx[1], qx[2], ....] |
---|
108 | |
---|
109 | and :: |
---|
110 | |
---|
111 | qy_prime = [ qy[0], qy[1], qy[2], ....] |
---|
112 | |
---|
113 | Then get :: |
---|
114 | |
---|
115 | q = numpy.sqrt(qx_prime^2+qy_prime^2) |
---|
116 | |
---|
117 | that is a qr in 1D array; :: |
---|
118 | |
---|
119 | q = [q[0], q[1], q[2], ....] |
---|
120 | |
---|
121 | ..note:: |
---|
122 | Due to 2D speed issue, no anisotropic scattering |
---|
123 | is supported for python models, thus C-models should have |
---|
124 | their own evalDistribution methods. |
---|
125 | |
---|
126 | The method is then called the following way: :: |
---|
127 | |
---|
128 | evalDistribution(q) |
---|
129 | |
---|
130 | where q is a numpy array. |
---|
131 | |
---|
132 | :param qdist: ndarray of scalar q-values or list [qx,qy] where qx,qy are 1D ndarrays |
---|
133 | """ |
---|
134 | if qdist.__class__.__name__ == 'list': |
---|
135 | # Check whether we have a list of ndarrays [qx,qy] |
---|
136 | if len(qdist)!=2 or \ |
---|
137 | qdist[0].__class__.__name__ != 'ndarray' or \ |
---|
138 | qdist[1].__class__.__name__ != 'ndarray': |
---|
139 | msg = "evalDistribution expects a list of 2 ndarrays" |
---|
140 | raise RuntimeError, msg |
---|
141 | |
---|
142 | # Extract qx and qy for code clarity |
---|
143 | qx = qdist[0] |
---|
144 | qy = qdist[1] |
---|
145 | |
---|
146 | # calculate q_r component for 2D isotropic |
---|
147 | q = numpy.sqrt(qx**2+qy**2) |
---|
148 | # vectorize the model function runXY |
---|
149 | v_model = numpy.vectorize(self.runXY, otypes=[float]) |
---|
150 | # calculate the scattering |
---|
151 | iq_array = v_model(q) |
---|
152 | |
---|
153 | return iq_array |
---|
154 | |
---|
155 | elif qdist.__class__.__name__ == 'ndarray': |
---|
156 | # We have a simple 1D distribution of q-values |
---|
157 | v_model = numpy.vectorize(self.runXY, otypes=[float]) |
---|
158 | iq_array = v_model(qdist) |
---|
159 | return iq_array |
---|
160 | |
---|
161 | else: |
---|
162 | mesg = "evalDistribution is expecting an ndarray of scalar q-values" |
---|
163 | mesg += " or a list [qx,qy] where qx,qy are 2D ndarrays." |
---|
164 | raise RuntimeError, mesg |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | def clone(self): |
---|
169 | """ Returns a new object identical to the current object """ |
---|
170 | obj = copy.deepcopy(self) |
---|
171 | return self._clone(obj) |
---|
172 | |
---|
173 | def _clone(self, obj): |
---|
174 | """ |
---|
175 | Internal utility function to copy the internal |
---|
176 | data members to a fresh copy. |
---|
177 | """ |
---|
178 | obj.params = copy.deepcopy(self.params) |
---|
179 | obj.details = copy.deepcopy(self.details) |
---|
180 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
181 | obj._persistency_dict = copy.deepcopy( self._persistency_dict) |
---|
182 | return obj |
---|
183 | |
---|
184 | def set_dispersion(self, parameter, dispersion): |
---|
185 | """ |
---|
186 | model dispersions |
---|
187 | """ |
---|
188 | ##Not Implemented |
---|
189 | return None |
---|
190 | |
---|
191 | def getProfile(self): |
---|
192 | """ |
---|
193 | Get SLD profile |
---|
194 | |
---|
195 | : return: (z, beta) where z is a list of depth of the transition points |
---|
196 | beta is a list of the corresponding SLD values |
---|
197 | """ |
---|
198 | #Not Implemented |
---|
199 | return None, None |
---|
200 | |
---|
201 | def setParam(self, name, value): |
---|
202 | """ |
---|
203 | Set the value of a model parameter |
---|
204 | |
---|
205 | :param name: name of the parameter |
---|
206 | :param value: value of the parameter |
---|
207 | |
---|
208 | """ |
---|
209 | # Look for dispersion parameters |
---|
210 | toks = name.split('.') |
---|
211 | if len(toks)==2: |
---|
212 | for item in self.dispersion.keys(): |
---|
213 | if item.lower()==toks[0].lower(): |
---|
214 | for par in self.dispersion[item]: |
---|
215 | if par.lower() == toks[1].lower(): |
---|
216 | self.dispersion[item][par] = value |
---|
217 | return |
---|
218 | else: |
---|
219 | # Look for standard parameter |
---|
220 | for item in self.params.keys(): |
---|
221 | if item.lower()==name.lower(): |
---|
222 | self.params[item] = value |
---|
223 | return |
---|
224 | |
---|
225 | raise ValueError, "Model does not contain parameter %s" % name |
---|
226 | |
---|
227 | def getParam(self, name): |
---|
228 | """ |
---|
229 | Set the value of a model parameter |
---|
230 | :param name: name of the parameter |
---|
231 | |
---|
232 | """ |
---|
233 | # Look for dispersion parameters |
---|
234 | toks = name.split('.') |
---|
235 | if len(toks)==2: |
---|
236 | for item in self.dispersion.keys(): |
---|
237 | if item.lower()==toks[0].lower(): |
---|
238 | for par in self.dispersion[item]: |
---|
239 | if par.lower() == toks[1].lower(): |
---|
240 | return self.dispersion[item][par] |
---|
241 | else: |
---|
242 | # Look for standard parameter |
---|
243 | for item in self.params.keys(): |
---|
244 | if item.lower()==name.lower(): |
---|
245 | return self.params[item] |
---|
246 | |
---|
247 | raise ValueError, "Model does not contain parameter %s" % name |
---|
248 | |
---|
249 | def getParamList(self): |
---|
250 | """ |
---|
251 | Return a list of all available parameters for the model |
---|
252 | """ |
---|
253 | list = self.params.keys() |
---|
254 | # WARNING: Extending the list with the dispersion parameters |
---|
255 | list.extend(self.getDispParamList()) |
---|
256 | return list |
---|
257 | |
---|
258 | def getDispParamList(self): |
---|
259 | """ |
---|
260 | Return a list of all available parameters for the model |
---|
261 | """ |
---|
262 | list = [] |
---|
263 | |
---|
264 | for item in self.dispersion.keys(): |
---|
265 | for p in self.dispersion[item].keys(): |
---|
266 | if p not in ['type']: |
---|
267 | list.append('%s.%s' % (item.lower(), p.lower())) |
---|
268 | |
---|
269 | return list |
---|
270 | |
---|
271 | # Old-style methods that are no longer used |
---|
272 | def setParamWithToken(self, name, value, token, member): |
---|
273 | """ |
---|
274 | set Param With Token |
---|
275 | """ |
---|
276 | return NotImplemented |
---|
277 | def getParamWithToken(self, name, token, member): |
---|
278 | """ |
---|
279 | get Param With Token |
---|
280 | """ |
---|
281 | return NotImplemented |
---|
282 | |
---|
283 | def getParamListWithToken(self, token, member): |
---|
284 | """ |
---|
285 | get Param List With Token |
---|
286 | """ |
---|
287 | return NotImplemented |
---|
288 | def __add__(self, other): |
---|
289 | """ |
---|
290 | add |
---|
291 | """ |
---|
292 | raise ValueError, "Model operation are no longer supported" |
---|
293 | def __sub__(self, other): |
---|
294 | """ |
---|
295 | sub |
---|
296 | """ |
---|
297 | raise ValueError, "Model operation are no longer supported" |
---|
298 | def __mul__(self, other): |
---|
299 | """ |
---|
300 | mul |
---|
301 | """ |
---|
302 | raise ValueError, "Model operation are no longer supported" |
---|
303 | def __div__(self, other): |
---|
304 | """ |
---|
305 | div |
---|
306 | """ |
---|
307 | raise ValueError, "Model operation are no longer supported" |
---|