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