[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 = {} |
---|
| 34 | |
---|
[1affe64] | 35 | ##models |
---|
| 36 | self.p_model= p_model |
---|
| 37 | self.s_model= s_model |
---|
[5eb9154] | 38 | |
---|
| 39 | |
---|
[c9636f7] | 40 | ## dispersion |
---|
| 41 | self._set_dispersion() |
---|
| 42 | ## Define parameters |
---|
| 43 | self._set_params() |
---|
[c52f66f] | 44 | ## New parameter:Scaling factor |
---|
| 45 | self.params['scale_factor'] = 1 |
---|
| 46 | |
---|
[c9636f7] | 47 | ## Parameter details [units, min, max] |
---|
| 48 | self._set_details() |
---|
[c52f66f] | 49 | self.details['scale_factor'] = ['', None, None] |
---|
| 50 | |
---|
[c9636f7] | 51 | #list of parameter that can be fitted |
---|
| 52 | self._set_fixed_params() |
---|
[5fc8e22] | 53 | ## parameters with orientation |
---|
[1affe64] | 54 | for item in self.p_model.orientation_params: |
---|
[5fc8e22] | 55 | self.orientation_params.append(item) |
---|
| 56 | |
---|
[1affe64] | 57 | for item in self.s_model.orientation_params: |
---|
[5fc8e22] | 58 | if not item in self.orientation_params: |
---|
[8b677ec] | 59 | self.orientation_params.append(item) |
---|
[5fc8e22] | 60 | |
---|
[5eb9154] | 61 | |
---|
[a68efd1] | 62 | def _clone(self, obj): |
---|
| 63 | """ |
---|
| 64 | Internal utility function to copy the internal |
---|
| 65 | data members to a fresh copy. |
---|
| 66 | """ |
---|
| 67 | obj.params = copy.deepcopy(self.params) |
---|
| 68 | obj.description = copy.deepcopy(self.description) |
---|
| 69 | obj.details = copy.deepcopy(self.details) |
---|
| 70 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
[1affe64] | 71 | obj.p_model = self.p_model.clone() |
---|
| 72 | obj.s_model = self.s_model.clone() |
---|
[fe9c19b4] | 73 | #obj = copy.deepcopy(self) |
---|
[a68efd1] | 74 | return obj |
---|
| 75 | |
---|
| 76 | |
---|
[c9636f7] | 77 | def _set_dispersion(self): |
---|
| 78 | """ |
---|
| 79 | combined the two models dispersions |
---|
[1affe64] | 80 | Polydispersion should not be applied to s_model |
---|
[c9636f7] | 81 | """ |
---|
[1affe64] | 82 | ##set dispersion only from p_model |
---|
| 83 | for name , value in self.p_model.dispersion.iteritems(): |
---|
| 84 | self.dispersion[name]= value |
---|
[3740b11] | 85 | |
---|
[c9636f7] | 86 | def _set_params(self): |
---|
| 87 | """ |
---|
| 88 | Concatenate the parameters of the two models to create |
---|
| 89 | this model parameters |
---|
| 90 | """ |
---|
[1affe64] | 91 | |
---|
| 92 | for name , value in self.p_model.params.iteritems(): |
---|
[c52f66f] | 93 | if not name in self.params.keys() and name != 'scale': |
---|
| 94 | self.params[name]= value |
---|
[3740b11] | 95 | |
---|
[1affe64] | 96 | for name , value in self.s_model.params.iteritems(): |
---|
| 97 | #Remove the effect_radius from the (P*S) model parameters. |
---|
| 98 | if not name in self.params.keys() and name != 'effect_radius': |
---|
| 99 | self.params[name]= value |
---|
[c52f66f] | 100 | |
---|
| 101 | # Set "scale and effec_radius to P and S model as initializing |
---|
| 102 | # since run P*S comes from P and S separately. |
---|
| 103 | self._set_scale_factor() |
---|
| 104 | self._set_effect_radius() |
---|
[c9636f7] | 105 | |
---|
| 106 | def _set_details(self): |
---|
| 107 | """ |
---|
| 108 | Concatenate details of the two models to create |
---|
| 109 | this model details |
---|
| 110 | """ |
---|
[1affe64] | 111 | for name ,detail in self.p_model.details.iteritems(): |
---|
[c52f66f] | 112 | if name != 'scale': |
---|
| 113 | self.details[name]= detail |
---|
[c9636f7] | 114 | |
---|
[1affe64] | 115 | for name , detail in self.s_model.details.iteritems(): |
---|
[c52f66f] | 116 | if not name in self.details.keys() or name != 'effect_radius': |
---|
[1affe64] | 117 | self.details[name]= detail |
---|
[c52f66f] | 118 | |
---|
| 119 | def _set_scale_factor(self): |
---|
| 120 | """ |
---|
| 121 | Set scale=volfraction to P model |
---|
| 122 | """ |
---|
| 123 | value = self.params['volfraction'] |
---|
| 124 | if value != None: |
---|
| 125 | self.p_model.setParam( 'scale', value) |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | def _set_effect_radius(self): |
---|
| 129 | """ |
---|
| 130 | Set effective radius to S(Q) model |
---|
| 131 | """ |
---|
| 132 | effective_radius = self.p_model.calculate_ER() |
---|
| 133 | #Reset the effective_radius of s_model just before the run |
---|
| 134 | if effective_radius != None and effective_radius != NotImplemented: |
---|
| 135 | self.s_model.setParam('effect_radius',effective_radius) |
---|
[c9636f7] | 136 | |
---|
[8cfdd5e] | 137 | def setParam(self, name, value): |
---|
| 138 | """ |
---|
| 139 | Set the value of a model parameter |
---|
| 140 | |
---|
| 141 | @param name: name of the parameter |
---|
| 142 | @param value: value of the parameter |
---|
| 143 | """ |
---|
[c52f66f] | 144 | # set param to P*S model |
---|
[3740b11] | 145 | self._setParamHelper( name, value) |
---|
[c52f66f] | 146 | |
---|
| 147 | ## setParam to p model |
---|
| 148 | # set 'scale' in P(Q) equal to volfraction |
---|
| 149 | if name == 'volfraction': |
---|
| 150 | self._set_scale_factor() |
---|
| 151 | elif name in self.p_model.getParamList(): |
---|
[1affe64] | 152 | self.p_model.setParam( name, value) |
---|
[c52f66f] | 153 | |
---|
| 154 | ## setParam to s model |
---|
| 155 | # This is a little bit abundant: Todo: find better way |
---|
| 156 | self._set_effect_radius() |
---|
[1affe64] | 157 | if name in self.s_model.getParamList(): |
---|
| 158 | self.s_model.setParam( name, value) |
---|
[c52f66f] | 159 | |
---|
[5eb9154] | 160 | |
---|
[c52f66f] | 161 | #self._setParamHelper( name, value) |
---|
[8cfdd5e] | 162 | |
---|
| 163 | def _setParamHelper(self, name, value): |
---|
| 164 | """ |
---|
| 165 | Helper function to setparam |
---|
| 166 | """ |
---|
| 167 | # Look for dispersion parameters |
---|
| 168 | toks = name.split('.') |
---|
| 169 | if len(toks)==2: |
---|
| 170 | for item in self.dispersion.keys(): |
---|
| 171 | if item.lower()==toks[0].lower(): |
---|
| 172 | for par in self.dispersion[item]: |
---|
| 173 | if par.lower() == toks[1].lower(): |
---|
| 174 | self.dispersion[item][par] = value |
---|
| 175 | return |
---|
| 176 | else: |
---|
| 177 | # Look for standard parameter |
---|
| 178 | for item in self.params.keys(): |
---|
| 179 | if item.lower()==name.lower(): |
---|
| 180 | self.params[item] = value |
---|
| 181 | return |
---|
| 182 | |
---|
| 183 | raise ValueError, "Model does not contain parameter %s" % name |
---|
| 184 | |
---|
| 185 | |
---|
[c9636f7] | 186 | def _set_fixed_params(self): |
---|
| 187 | """ |
---|
[1affe64] | 188 | fill the self.fixed list with the p_model fixed list |
---|
[c9636f7] | 189 | """ |
---|
[1affe64] | 190 | for item in self.p_model.fixed: |
---|
[c9636f7] | 191 | self.fixed.append(item) |
---|
[8b677ec] | 192 | |
---|
[c9636f7] | 193 | self.fixed.sort() |
---|
[5eb9154] | 194 | |
---|
| 195 | |
---|
[c9636f7] | 196 | def run(self, x = 0.0): |
---|
| 197 | """ Evaluate the model |
---|
| 198 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
| 199 | @return: (DAB value) |
---|
| 200 | """ |
---|
[c52f66f] | 201 | # set effective radius and scaling factor before run |
---|
| 202 | self._set_effect_radius() |
---|
| 203 | self._set_scale_factor() |
---|
| 204 | return self.params['scale_factor']*self.p_model.run(x)*self.s_model.run(x) |
---|
[1affe64] | 205 | |
---|
[c9636f7] | 206 | def runXY(self, x = 0.0): |
---|
| 207 | """ Evaluate the model |
---|
| 208 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
| 209 | @return: DAB value |
---|
[c52f66f] | 210 | """ |
---|
| 211 | # set effective radius and scaling factor before run |
---|
| 212 | self._set_effect_radius() |
---|
| 213 | self._set_scale_factor() |
---|
| 214 | return self.params['scale_factor']*self.p_model.runXY(x)* self.s_model.runXY(x) |
---|
[5eb9154] | 215 | |
---|
[c9636f7] | 216 | def set_dispersion(self, parameter, dispersion): |
---|
| 217 | """ |
---|
| 218 | Set the dispersion object for a model parameter |
---|
| 219 | @param parameter: name of the parameter [string] |
---|
| 220 | @dispersion: dispersion object of type DispersionModel |
---|
| 221 | """ |
---|
[db39b2a] | 222 | value= None |
---|
| 223 | try: |
---|
[1affe64] | 224 | if parameter in self.p_model.dispersion.keys(): |
---|
| 225 | value= self.p_model.set_dispersion(parameter, dispersion) |
---|
[8077fc4] | 226 | self._set_dispersion() |
---|
[db39b2a] | 227 | return value |
---|
| 228 | except: |
---|
| 229 | raise |
---|
[c9636f7] | 230 | |
---|
[1affe64] | 231 | def fill_description(self, p_model, s_model): |
---|
[8b677ec] | 232 | """ |
---|
| 233 | Fill the description for P(Q)*S(Q) |
---|
| 234 | """ |
---|
| 235 | description = "" |
---|
[1affe64] | 236 | description += "Note:1) The effect_radius (effective radius) of %s \n"% (s_model.name) |
---|
[8b677ec] | 237 | description +=" is automatically calculated from size parameters (radius...).\n" |
---|
| 238 | description += " 2) For non-spherical shape, this approximation is valid \n" |
---|
[1affe64] | 239 | description += " only for limited systems. Thus, use it at your own risk.\n" |
---|
| 240 | description +="See %s description and %s description \n"%( p_model.name, s_model.name ) |
---|
| 241 | description += " for details of individual models." |
---|
[8b677ec] | 242 | self.description += description |
---|
[c9636f7] | 243 | |
---|