1 | |
---|
2 | from sas.sascalc.calculator.BaseComponent import BaseComponent |
---|
3 | #import numpy, math |
---|
4 | import copy |
---|
5 | from sas.sascalc.fit.pluginmodel import Model1DPlugin |
---|
6 | class MultiplicationModel(BaseComponent): |
---|
7 | r""" |
---|
8 | Use for P(Q)\*S(Q); function call must be in the order of P(Q) and then S(Q): |
---|
9 | The model parameters are combined from both models, P(Q) and S(Q), except 1) 'effect_radius' of S(Q) |
---|
10 | which will be calculated from P(Q) via calculate_ER(), |
---|
11 | and 2) 'scale' in P model which is synchronized w/ volfraction in S |
---|
12 | then P*S is multiplied by a new parameter, 'scale_factor'. |
---|
13 | The polydispersion is applicable only to P(Q), not to S(Q). |
---|
14 | |
---|
15 | .. note:: P(Q) refers to 'form factor' model while S(Q) does to 'structure factor'. |
---|
16 | """ |
---|
17 | def __init__(self, p_model, s_model ): |
---|
18 | BaseComponent.__init__(self) |
---|
19 | """ |
---|
20 | :param p_model: form factor, P(Q) |
---|
21 | :param s_model: structure factor, S(Q) |
---|
22 | """ |
---|
23 | |
---|
24 | ## Setting model name model description |
---|
25 | self.description = "" |
---|
26 | self.name = p_model.name +" * "+ s_model.name |
---|
27 | self.description= self.name + "\n" |
---|
28 | self.fill_description(p_model, s_model) |
---|
29 | |
---|
30 | ## Define parameters |
---|
31 | self.params = {} |
---|
32 | |
---|
33 | ## Parameter details [units, min, max] |
---|
34 | self.details = {} |
---|
35 | |
---|
36 | ##models |
---|
37 | self.p_model = p_model |
---|
38 | self.s_model = s_model |
---|
39 | self.magnetic_params = [] |
---|
40 | ## dispersion |
---|
41 | self._set_dispersion() |
---|
42 | ## Define parameters |
---|
43 | self._set_params() |
---|
44 | ## New parameter:Scaling factor |
---|
45 | self.params['scale_factor'] = 1 |
---|
46 | |
---|
47 | ## Parameter details [units, min, max] |
---|
48 | self._set_details() |
---|
49 | self.details['scale_factor'] = ['', None, None] |
---|
50 | |
---|
51 | #list of parameter that can be fitted |
---|
52 | self._set_fixed_params() |
---|
53 | ## parameters with orientation |
---|
54 | for item in self.p_model.orientation_params: |
---|
55 | self.orientation_params.append(item) |
---|
56 | for item in self.p_model.magnetic_params: |
---|
57 | self.magnetic_params.append(item) |
---|
58 | for item in self.s_model.orientation_params: |
---|
59 | if not item in self.orientation_params: |
---|
60 | self.orientation_params.append(item) |
---|
61 | # get multiplicity if model provide it, else 1. |
---|
62 | try: |
---|
63 | multiplicity = p_model.multiplicity |
---|
64 | except: |
---|
65 | multiplicity = 1 |
---|
66 | ## functional multiplicity of the model |
---|
67 | self.multiplicity = multiplicity |
---|
68 | |
---|
69 | # non-fittable parameters |
---|
70 | self.non_fittable = p_model.non_fittable |
---|
71 | self.multiplicity_info = [] |
---|
72 | self.fun_list = {} |
---|
73 | if self.non_fittable > 1: |
---|
74 | try: |
---|
75 | self.multiplicity_info = p_model.multiplicity_info |
---|
76 | self.fun_list = p_model.fun_list |
---|
77 | except: |
---|
78 | pass |
---|
79 | else: |
---|
80 | self.multiplicity_info = [] |
---|
81 | |
---|
82 | def _clone(self, obj): |
---|
83 | """ |
---|
84 | Internal utility function to copy the internal data members to a |
---|
85 | fresh copy. |
---|
86 | """ |
---|
87 | obj.params = copy.deepcopy(self.params) |
---|
88 | obj.description = copy.deepcopy(self.description) |
---|
89 | obj.details = copy.deepcopy(self.details) |
---|
90 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
91 | obj.p_model = self.p_model.clone() |
---|
92 | obj.s_model = self.s_model.clone() |
---|
93 | #obj = copy.deepcopy(self) |
---|
94 | return obj |
---|
95 | |
---|
96 | |
---|
97 | def _set_dispersion(self): |
---|
98 | """ |
---|
99 | combine the two models' dispersions. Polydispersity should not be |
---|
100 | applied to s_model |
---|
101 | """ |
---|
102 | ##set dispersion only from p_model |
---|
103 | for name , value in self.p_model.dispersion.iteritems(): |
---|
104 | self.dispersion[name] = value |
---|
105 | |
---|
106 | def getProfile(self): |
---|
107 | """ |
---|
108 | Get SLD profile of p_model if exists |
---|
109 | |
---|
110 | :return: (r, beta) where r is a list of radius of the transition points\ |
---|
111 | beta is a list of the corresponding SLD values |
---|
112 | |
---|
113 | .. note:: This works only for func_shell num = 2 (exp function). |
---|
114 | """ |
---|
115 | try: |
---|
116 | x, y = self.p_model.getProfile() |
---|
117 | except: |
---|
118 | x = None |
---|
119 | y = None |
---|
120 | |
---|
121 | return x, y |
---|
122 | |
---|
123 | def _set_params(self): |
---|
124 | """ |
---|
125 | Concatenate the parameters of the two models to create |
---|
126 | these model parameters |
---|
127 | """ |
---|
128 | |
---|
129 | for name , value in self.p_model.params.iteritems(): |
---|
130 | if not name in self.params.keys() and name != 'scale': |
---|
131 | self.params[name] = value |
---|
132 | |
---|
133 | for name , value in self.s_model.params.iteritems(): |
---|
134 | #Remove the effect_radius from the (P*S) model parameters. |
---|
135 | if not name in self.params.keys() and name != 'effect_radius': |
---|
136 | self.params[name] = value |
---|
137 | |
---|
138 | # Set "scale and effec_radius to P and S model as initializing |
---|
139 | # since run P*S comes from P and S separately. |
---|
140 | self._set_scale_factor() |
---|
141 | self._set_effect_radius() |
---|
142 | |
---|
143 | def _set_details(self): |
---|
144 | """ |
---|
145 | Concatenate details of the two models to create |
---|
146 | this model's details |
---|
147 | """ |
---|
148 | for name, detail in self.p_model.details.iteritems(): |
---|
149 | if name != 'scale': |
---|
150 | self.details[name] = detail |
---|
151 | |
---|
152 | for name , detail in self.s_model.details.iteritems(): |
---|
153 | if not name in self.details.keys() or name != 'effect_radius': |
---|
154 | self.details[name] = detail |
---|
155 | |
---|
156 | def _set_scale_factor(self): |
---|
157 | """ |
---|
158 | Set scale=volfraction for P model |
---|
159 | """ |
---|
160 | value = self.params['volfraction'] |
---|
161 | if value != None: |
---|
162 | factor = self.p_model.calculate_VR() |
---|
163 | if factor == None or factor == NotImplemented or factor == 0.0: |
---|
164 | val = value |
---|
165 | else: |
---|
166 | val = value / factor |
---|
167 | self.p_model.setParam('scale', value) |
---|
168 | self.s_model.setParam('volfraction', val) |
---|
169 | |
---|
170 | def _set_effect_radius(self): |
---|
171 | """ |
---|
172 | Set effective radius to S(Q) model |
---|
173 | """ |
---|
174 | if not 'effect_radius' in self.s_model.params.keys(): |
---|
175 | return |
---|
176 | effective_radius = self.p_model.calculate_ER() |
---|
177 | #Reset the effective_radius of s_model just before the run |
---|
178 | if effective_radius != None and effective_radius != NotImplemented: |
---|
179 | self.s_model.setParam('effect_radius', effective_radius) |
---|
180 | |
---|
181 | def setParam(self, name, value): |
---|
182 | """ |
---|
183 | Set the value of a model parameter |
---|
184 | |
---|
185 | :param name: name of the parameter |
---|
186 | :param value: value of the parameter |
---|
187 | """ |
---|
188 | # set param to P*S model |
---|
189 | self._setParamHelper( name, value) |
---|
190 | |
---|
191 | ## setParam to p model |
---|
192 | # set 'scale' in P(Q) equal to volfraction |
---|
193 | if name == 'volfraction': |
---|
194 | self._set_scale_factor() |
---|
195 | elif name in self.p_model.getParamList(): |
---|
196 | self.p_model.setParam( name, value) |
---|
197 | |
---|
198 | ## setParam to s model |
---|
199 | # This is a little bit abundant: Todo: find better way |
---|
200 | self._set_effect_radius() |
---|
201 | if name in self.s_model.getParamList(): |
---|
202 | if name != 'volfraction': |
---|
203 | self.s_model.setParam( name, value) |
---|
204 | |
---|
205 | |
---|
206 | #self._setParamHelper( name, value) |
---|
207 | |
---|
208 | def _setParamHelper(self, name, value): |
---|
209 | """ |
---|
210 | Helper function to setparam |
---|
211 | """ |
---|
212 | # Look for dispersion parameters |
---|
213 | toks = name.split('.') |
---|
214 | if len(toks)==2: |
---|
215 | for item in self.dispersion.keys(): |
---|
216 | if item.lower()==toks[0].lower(): |
---|
217 | for par in self.dispersion[item]: |
---|
218 | if par.lower() == toks[1].lower(): |
---|
219 | self.dispersion[item][par] = value |
---|
220 | return |
---|
221 | else: |
---|
222 | # Look for standard parameter |
---|
223 | for item in self.params.keys(): |
---|
224 | if item.lower() == name.lower(): |
---|
225 | self.params[item] = value |
---|
226 | return |
---|
227 | |
---|
228 | raise ValueError, "Model does not contain parameter %s" % name |
---|
229 | |
---|
230 | |
---|
231 | def _set_fixed_params(self): |
---|
232 | """ |
---|
233 | Fill the self.fixed list with the p_model fixed list |
---|
234 | """ |
---|
235 | for item in self.p_model.fixed: |
---|
236 | self.fixed.append(item) |
---|
237 | |
---|
238 | self.fixed.sort() |
---|
239 | |
---|
240 | |
---|
241 | def run(self, x = 0.0): |
---|
242 | """ |
---|
243 | Evaluate the model |
---|
244 | |
---|
245 | :param x: input q-value (float or [float, float] as [r, theta]) |
---|
246 | :return: (scattering function value) |
---|
247 | """ |
---|
248 | # set effective radius and scaling factor before run |
---|
249 | self._set_effect_radius() |
---|
250 | self._set_scale_factor() |
---|
251 | return self.params['scale_factor'] * self.p_model.run(x) * \ |
---|
252 | self.s_model.run(x) |
---|
253 | |
---|
254 | def runXY(self, x = 0.0): |
---|
255 | """ |
---|
256 | Evaluate the model |
---|
257 | |
---|
258 | :param x: input q-value (float or [float, float] as [qx, qy]) |
---|
259 | :return: scattering function value |
---|
260 | """ |
---|
261 | # set effective radius and scaling factor before run |
---|
262 | self._set_effect_radius() |
---|
263 | self._set_scale_factor() |
---|
264 | out = self.params['scale_factor'] * self.p_model.runXY(x) * \ |
---|
265 | self.s_model.runXY(x) |
---|
266 | return out |
---|
267 | |
---|
268 | ## Now (May27,10) directly uses the model eval function |
---|
269 | ## instead of the for-loop in Base Component. |
---|
270 | def evalDistribution(self, x = []): |
---|
271 | """ |
---|
272 | Evaluate the model in cartesian coordinates |
---|
273 | |
---|
274 | :param x: input q[], or [qx[], qy[]] |
---|
275 | :return: scattering function P(q[]) |
---|
276 | """ |
---|
277 | # set effective radius and scaling factor before run |
---|
278 | self._set_effect_radius() |
---|
279 | self._set_scale_factor() |
---|
280 | out = self.params['scale_factor'] * self.p_model.evalDistribution(x) * \ |
---|
281 | self.s_model.evalDistribution(x) |
---|
282 | return out |
---|
283 | |
---|
284 | def set_dispersion(self, parameter, dispersion): |
---|
285 | """ |
---|
286 | Set the dispersion object for a model parameter |
---|
287 | |
---|
288 | :param parameter: name of the parameter [string] |
---|
289 | :dispersion: dispersion object of type DispersionModel |
---|
290 | """ |
---|
291 | value = None |
---|
292 | try: |
---|
293 | if parameter in self.p_model.dispersion.keys(): |
---|
294 | value = self.p_model.set_dispersion(parameter, dispersion) |
---|
295 | self._set_dispersion() |
---|
296 | return value |
---|
297 | except: |
---|
298 | raise |
---|
299 | |
---|
300 | def fill_description(self, p_model, s_model): |
---|
301 | """ |
---|
302 | Fill the description for P(Q)*S(Q) |
---|
303 | """ |
---|
304 | description = "" |
---|
305 | description += "Note:1) The effect_radius (effective radius) of %s \n"%\ |
---|
306 | (s_model.name) |
---|
307 | description += " is automatically calculated " |
---|
308 | description += "from size parameters (radius...).\n" |
---|
309 | description += " 2) For non-spherical shape, " |
---|
310 | description += "this approximation is valid \n" |
---|
311 | description += " only for limited systems. " |
---|
312 | description += "Thus, use it at your own risk.\n" |
---|
313 | description += "See %s description and %s description \n"% \ |
---|
314 | ( p_model.name, s_model.name ) |
---|
315 | description += " for details of individual models." |
---|
316 | self.description += description |
---|
317 | |
---|