[c9636f7] | 1 | |
---|
| 2 | from sans.models.BaseComponent import BaseComponent |
---|
| 3 | import numpy, math |
---|
[a68efd1] | 4 | import copy |
---|
[3740b11] | 5 | from sans.models.pluginmodel import Model1DPlugin |
---|
[c9636f7] | 6 | class MultiplicationModel(BaseComponent): |
---|
| 7 | """ |
---|
[1affe64] | 8 | Use for P(Q)*S(Q); function call must be in the order of P(Q) and then S(Q): |
---|
[c52f66f] | 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'. |
---|
[1affe64] | 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'. |
---|
[c9636f7] | 15 | """ |
---|
[1affe64] | 16 | def __init__(self, p_model, s_model ): |
---|
[c9636f7] | 17 | BaseComponent.__init__(self) |
---|
[1affe64] | 18 | """ |
---|
| 19 | @param p_model: form factor, P(Q) |
---|
| 20 | @param s_model: structure factor, S(Q) |
---|
| 21 | """ |
---|
[c9636f7] | 22 | |
---|
[8cfdd5e] | 23 | ## Setting model name model description |
---|
[996fd35] | 24 | self.description="" |
---|
[1affe64] | 25 | self.name = p_model.name +" * "+ s_model.name |
---|
| 26 | self.description= self.name+"\n" |
---|
| 27 | self.fill_description(p_model, s_model) |
---|
[c52f66f] | 28 | |
---|
| 29 | ## Define parameters |
---|
| 30 | self.params = {} |
---|
| 31 | |
---|
| 32 | ## Parameter details [units, min, max] |
---|
| 33 | self.details = {} |
---|
[35aface] | 34 | |
---|
[1affe64] | 35 | ##models |
---|
| 36 | self.p_model= p_model |
---|
[8960479] | 37 | self.s_model= s_model |
---|
[5eb9154] | 38 | |
---|
[c9636f7] | 39 | ## dispersion |
---|
| 40 | self._set_dispersion() |
---|
| 41 | ## Define parameters |
---|
| 42 | self._set_params() |
---|
[c52f66f] | 43 | ## New parameter:Scaling factor |
---|
| 44 | self.params['scale_factor'] = 1 |
---|
| 45 | |
---|
[c9636f7] | 46 | ## Parameter details [units, min, max] |
---|
| 47 | self._set_details() |
---|
[c52f66f] | 48 | self.details['scale_factor'] = ['', None, None] |
---|
| 49 | |
---|
[c9636f7] | 50 | #list of parameter that can be fitted |
---|
| 51 | self._set_fixed_params() |
---|
[5fc8e22] | 52 | ## parameters with orientation |
---|
[1affe64] | 53 | for item in self.p_model.orientation_params: |
---|
[5fc8e22] | 54 | self.orientation_params.append(item) |
---|
| 55 | |
---|
[1affe64] | 56 | for item in self.s_model.orientation_params: |
---|
[5fc8e22] | 57 | if not item in self.orientation_params: |
---|
[8b677ec] | 58 | self.orientation_params.append(item) |
---|
[35aface] | 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 |
---|
[8960479] | 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 | |
---|
[a68efd1] | 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) |
---|
[1affe64] | 89 | obj.p_model = self.p_model.clone() |
---|
| 90 | obj.s_model = self.s_model.clone() |
---|
[fe9c19b4] | 91 | #obj = copy.deepcopy(self) |
---|
[a68efd1] | 92 | return obj |
---|
| 93 | |
---|
| 94 | |
---|
[c9636f7] | 95 | def _set_dispersion(self): |
---|
| 96 | """ |
---|
| 97 | combined the two models dispersions |
---|
[1affe64] | 98 | Polydispersion should not be applied to s_model |
---|
[c9636f7] | 99 | """ |
---|
[1affe64] | 100 | ##set dispersion only from p_model |
---|
| 101 | for name , value in self.p_model.dispersion.iteritems(): |
---|
[a1b2471] | 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 | |
---|
[c9636f7] | 120 | def _set_params(self): |
---|
| 121 | """ |
---|
| 122 | Concatenate the parameters of the two models to create |
---|
| 123 | this model parameters |
---|
| 124 | """ |
---|
[1affe64] | 125 | |
---|
| 126 | for name , value in self.p_model.params.iteritems(): |
---|
[c52f66f] | 127 | if not name in self.params.keys() and name != 'scale': |
---|
| 128 | self.params[name]= value |
---|
[3740b11] | 129 | |
---|
[1affe64] | 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 |
---|
[c52f66f] | 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() |
---|
[c9636f7] | 139 | |
---|
| 140 | def _set_details(self): |
---|
| 141 | """ |
---|
| 142 | Concatenate details of the two models to create |
---|
| 143 | this model details |
---|
| 144 | """ |
---|
[1affe64] | 145 | for name ,detail in self.p_model.details.iteritems(): |
---|
[c52f66f] | 146 | if name != 'scale': |
---|
| 147 | self.details[name]= detail |
---|
[c9636f7] | 148 | |
---|
[1affe64] | 149 | for name , detail in self.s_model.details.iteritems(): |
---|
[c52f66f] | 150 | if not name in self.details.keys() or name != 'effect_radius': |
---|
[1affe64] | 151 | self.details[name]= detail |
---|
[c52f66f] | 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 | self.p_model.setParam( 'scale', value) |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | def _set_effect_radius(self): |
---|
| 163 | """ |
---|
| 164 | Set effective radius to S(Q) model |
---|
| 165 | """ |
---|
[ccb7363] | 166 | if not 'effect_radius' in self.s_model.params.keys(): |
---|
| 167 | return |
---|
[c52f66f] | 168 | effective_radius = self.p_model.calculate_ER() |
---|
| 169 | #Reset the effective_radius of s_model just before the run |
---|
| 170 | if effective_radius != None and effective_radius != NotImplemented: |
---|
| 171 | self.s_model.setParam('effect_radius',effective_radius) |
---|
[c9636f7] | 172 | |
---|
[8cfdd5e] | 173 | def setParam(self, name, value): |
---|
| 174 | """ |
---|
| 175 | Set the value of a model parameter |
---|
| 176 | |
---|
| 177 | @param name: name of the parameter |
---|
| 178 | @param value: value of the parameter |
---|
| 179 | """ |
---|
[c52f66f] | 180 | # set param to P*S model |
---|
[3740b11] | 181 | self._setParamHelper( name, value) |
---|
[c52f66f] | 182 | |
---|
| 183 | ## setParam to p model |
---|
| 184 | # set 'scale' in P(Q) equal to volfraction |
---|
| 185 | if name == 'volfraction': |
---|
| 186 | self._set_scale_factor() |
---|
| 187 | elif name in self.p_model.getParamList(): |
---|
[1affe64] | 188 | self.p_model.setParam( name, value) |
---|
[c52f66f] | 189 | |
---|
| 190 | ## setParam to s model |
---|
| 191 | # This is a little bit abundant: Todo: find better way |
---|
| 192 | self._set_effect_radius() |
---|
[1affe64] | 193 | if name in self.s_model.getParamList(): |
---|
| 194 | self.s_model.setParam( name, value) |
---|
[c52f66f] | 195 | |
---|
[5eb9154] | 196 | |
---|
[c52f66f] | 197 | #self._setParamHelper( name, value) |
---|
[8cfdd5e] | 198 | |
---|
| 199 | def _setParamHelper(self, name, value): |
---|
| 200 | """ |
---|
| 201 | Helper function to setparam |
---|
| 202 | """ |
---|
| 203 | # Look for dispersion parameters |
---|
| 204 | toks = name.split('.') |
---|
| 205 | if len(toks)==2: |
---|
| 206 | for item in self.dispersion.keys(): |
---|
| 207 | if item.lower()==toks[0].lower(): |
---|
| 208 | for par in self.dispersion[item]: |
---|
| 209 | if par.lower() == toks[1].lower(): |
---|
| 210 | self.dispersion[item][par] = value |
---|
| 211 | return |
---|
| 212 | else: |
---|
| 213 | # Look for standard parameter |
---|
| 214 | for item in self.params.keys(): |
---|
| 215 | if item.lower()==name.lower(): |
---|
| 216 | self.params[item] = value |
---|
| 217 | return |
---|
| 218 | |
---|
| 219 | raise ValueError, "Model does not contain parameter %s" % name |
---|
| 220 | |
---|
| 221 | |
---|
[c9636f7] | 222 | def _set_fixed_params(self): |
---|
| 223 | """ |
---|
[1affe64] | 224 | fill the self.fixed list with the p_model fixed list |
---|
[c9636f7] | 225 | """ |
---|
[1affe64] | 226 | for item in self.p_model.fixed: |
---|
[c9636f7] | 227 | self.fixed.append(item) |
---|
[8b677ec] | 228 | |
---|
[c9636f7] | 229 | self.fixed.sort() |
---|
[5eb9154] | 230 | |
---|
| 231 | |
---|
[c9636f7] | 232 | def run(self, x = 0.0): |
---|
| 233 | """ Evaluate the model |
---|
| 234 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
[35aface] | 235 | @return: (scattering function value) |
---|
[c9636f7] | 236 | """ |
---|
[c52f66f] | 237 | # set effective radius and scaling factor before run |
---|
| 238 | self._set_effect_radius() |
---|
| 239 | self._set_scale_factor() |
---|
| 240 | return self.params['scale_factor']*self.p_model.run(x)*self.s_model.run(x) |
---|
[1affe64] | 241 | |
---|
[c9636f7] | 242 | def runXY(self, x = 0.0): |
---|
| 243 | """ Evaluate the model |
---|
| 244 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
[35aface] | 245 | @return: scattering function value |
---|
[c52f66f] | 246 | """ |
---|
| 247 | # set effective radius and scaling factor before run |
---|
| 248 | self._set_effect_radius() |
---|
| 249 | self._set_scale_factor() |
---|
| 250 | return self.params['scale_factor']*self.p_model.runXY(x)* self.s_model.runXY(x) |
---|
[06c7fcc] | 251 | |
---|
| 252 | ## Now (May27,10) directly uses the model eval function |
---|
| 253 | ## instead of the for-loop in Base Component. |
---|
| 254 | def evalDistribution(self, x = []): |
---|
| 255 | """ Evaluate the model in cartesian coordinates |
---|
| 256 | @param x: input q[], or [qx[], qy[]] |
---|
| 257 | @return: scattering function P(q[]) |
---|
| 258 | """ |
---|
| 259 | # set effective radius and scaling factor before run |
---|
| 260 | self._set_effect_radius() |
---|
| 261 | self._set_scale_factor() |
---|
| 262 | return self.params['scale_factor']*self.p_model.evalDistribution(x)* self.s_model.evalDistribution(x) |
---|
[5eb9154] | 263 | |
---|
[c9636f7] | 264 | def set_dispersion(self, parameter, dispersion): |
---|
| 265 | """ |
---|
| 266 | Set the dispersion object for a model parameter |
---|
| 267 | @param parameter: name of the parameter [string] |
---|
| 268 | @dispersion: dispersion object of type DispersionModel |
---|
| 269 | """ |
---|
[db39b2a] | 270 | value= None |
---|
| 271 | try: |
---|
[1affe64] | 272 | if parameter in self.p_model.dispersion.keys(): |
---|
| 273 | value= self.p_model.set_dispersion(parameter, dispersion) |
---|
[8077fc4] | 274 | self._set_dispersion() |
---|
[db39b2a] | 275 | return value |
---|
| 276 | except: |
---|
| 277 | raise |
---|
[c9636f7] | 278 | |
---|
[1affe64] | 279 | def fill_description(self, p_model, s_model): |
---|
[8b677ec] | 280 | """ |
---|
| 281 | Fill the description for P(Q)*S(Q) |
---|
| 282 | """ |
---|
| 283 | description = "" |
---|
[1affe64] | 284 | description += "Note:1) The effect_radius (effective radius) of %s \n"% (s_model.name) |
---|
[8b677ec] | 285 | description +=" is automatically calculated from size parameters (radius...).\n" |
---|
| 286 | description += " 2) For non-spherical shape, this approximation is valid \n" |
---|
[1affe64] | 287 | description += " only for limited systems. Thus, use it at your own risk.\n" |
---|
| 288 | description +="See %s description and %s description \n"%( p_model.name, s_model.name ) |
---|
| 289 | description += " for details of individual models." |
---|
[8b677ec] | 290 | self.description += description |
---|
[c9636f7] | 291 | |
---|