[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): |
---|
| 9 | The model parameters are combined from both models, P(Q) and S(Q), except 'effective_radius' of S(Q) |
---|
| 10 | which will be calculated from P(Q) via calculate_ER(). |
---|
| 11 | The polydispersion is applicable only to P(Q), not to S(Q). |
---|
| 12 | Note: P(Q) refers to 'form factor' model while S(Q) does to 'structure factor'. |
---|
[c9636f7] | 13 | """ |
---|
[1affe64] | 14 | def __init__(self, p_model, s_model ): |
---|
[c9636f7] | 15 | BaseComponent.__init__(self) |
---|
[1affe64] | 16 | """ |
---|
| 17 | @param p_model: form factor, P(Q) |
---|
| 18 | @param s_model: structure factor, S(Q) |
---|
| 19 | """ |
---|
[c9636f7] | 20 | |
---|
[8cfdd5e] | 21 | ## Setting model name model description |
---|
[996fd35] | 22 | self.description="" |
---|
[1affe64] | 23 | self.name = p_model.name +" * "+ s_model.name |
---|
| 24 | self.description= self.name+"\n" |
---|
| 25 | self.fill_description(p_model, s_model) |
---|
[3740b11] | 26 | |
---|
[1affe64] | 27 | ##models |
---|
| 28 | self.p_model= p_model |
---|
| 29 | self.s_model= s_model |
---|
[5eb9154] | 30 | |
---|
| 31 | |
---|
[c9636f7] | 32 | ## dispersion |
---|
| 33 | self._set_dispersion() |
---|
| 34 | ## Define parameters |
---|
| 35 | self._set_params() |
---|
| 36 | ## Parameter details [units, min, max] |
---|
| 37 | self._set_details() |
---|
| 38 | #list of parameter that can be fitted |
---|
| 39 | self._set_fixed_params() |
---|
[5fc8e22] | 40 | ## parameters with orientation |
---|
[1affe64] | 41 | for item in self.p_model.orientation_params: |
---|
[5fc8e22] | 42 | self.orientation_params.append(item) |
---|
| 43 | |
---|
[1affe64] | 44 | for item in self.s_model.orientation_params: |
---|
[5fc8e22] | 45 | if not item in self.orientation_params: |
---|
[8b677ec] | 46 | self.orientation_params.append(item) |
---|
[5fc8e22] | 47 | |
---|
[5eb9154] | 48 | |
---|
[a68efd1] | 49 | def _clone(self, obj): |
---|
| 50 | """ |
---|
| 51 | Internal utility function to copy the internal |
---|
| 52 | data members to a fresh copy. |
---|
| 53 | """ |
---|
| 54 | obj.params = copy.deepcopy(self.params) |
---|
| 55 | obj.description = copy.deepcopy(self.description) |
---|
| 56 | obj.details = copy.deepcopy(self.details) |
---|
| 57 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
[1affe64] | 58 | obj.p_model = self.p_model.clone() |
---|
| 59 | obj.s_model = self.s_model.clone() |
---|
[fe9c19b4] | 60 | #obj = copy.deepcopy(self) |
---|
[a68efd1] | 61 | return obj |
---|
| 62 | |
---|
| 63 | |
---|
[c9636f7] | 64 | def _set_dispersion(self): |
---|
| 65 | """ |
---|
| 66 | combined the two models dispersions |
---|
[1affe64] | 67 | Polydispersion should not be applied to s_model |
---|
[c9636f7] | 68 | """ |
---|
[1affe64] | 69 | ##set dispersion only from p_model |
---|
| 70 | for name , value in self.p_model.dispersion.iteritems(): |
---|
| 71 | self.dispersion[name]= value |
---|
[3740b11] | 72 | |
---|
[c9636f7] | 73 | def _set_params(self): |
---|
| 74 | """ |
---|
| 75 | Concatenate the parameters of the two models to create |
---|
| 76 | this model parameters |
---|
| 77 | """ |
---|
[1affe64] | 78 | |
---|
| 79 | for name , value in self.p_model.params.iteritems(): |
---|
[c9636f7] | 80 | self.params[name]= value |
---|
[3740b11] | 81 | |
---|
[1affe64] | 82 | for name , value in self.s_model.params.iteritems(): |
---|
| 83 | #Remove the effect_radius from the (P*S) model parameters. |
---|
| 84 | if not name in self.params.keys() and name != 'effect_radius': |
---|
| 85 | self.params[name]= value |
---|
[c9636f7] | 86 | |
---|
| 87 | def _set_details(self): |
---|
| 88 | """ |
---|
| 89 | Concatenate details of the two models to create |
---|
| 90 | this model details |
---|
| 91 | """ |
---|
[1affe64] | 92 | for name ,detail in self.p_model.details.iteritems(): |
---|
[c9636f7] | 93 | self.details[name]= detail |
---|
| 94 | |
---|
[1affe64] | 95 | for name , detail in self.s_model.details.iteritems(): |
---|
| 96 | if not name in self.details.keys(): |
---|
| 97 | self.details[name]= detail |
---|
[c9636f7] | 98 | |
---|
[8cfdd5e] | 99 | def setParam(self, name, value): |
---|
| 100 | """ |
---|
| 101 | Set the value of a model parameter |
---|
| 102 | |
---|
| 103 | @param name: name of the parameter |
---|
| 104 | @param value: value of the parameter |
---|
| 105 | """ |
---|
[3740b11] | 106 | |
---|
| 107 | self._setParamHelper( name, value) |
---|
| 108 | |
---|
[1affe64] | 109 | if name in self.p_model.getParamList(): |
---|
| 110 | self.p_model.setParam( name, value) |
---|
[ab62adb] | 111 | |
---|
[1affe64] | 112 | if name in self.s_model.getParamList(): |
---|
| 113 | self.s_model.setParam( name, value) |
---|
[5eb9154] | 114 | |
---|
[24415e9] | 115 | self._setParamHelper( name, value) |
---|
[8cfdd5e] | 116 | |
---|
| 117 | def _setParamHelper(self, name, value): |
---|
| 118 | """ |
---|
| 119 | Helper function to setparam |
---|
| 120 | """ |
---|
| 121 | # Look for dispersion parameters |
---|
| 122 | toks = name.split('.') |
---|
| 123 | if len(toks)==2: |
---|
| 124 | for item in self.dispersion.keys(): |
---|
| 125 | if item.lower()==toks[0].lower(): |
---|
| 126 | for par in self.dispersion[item]: |
---|
| 127 | if par.lower() == toks[1].lower(): |
---|
| 128 | self.dispersion[item][par] = value |
---|
| 129 | return |
---|
| 130 | else: |
---|
| 131 | # Look for standard parameter |
---|
| 132 | for item in self.params.keys(): |
---|
| 133 | if item.lower()==name.lower(): |
---|
| 134 | self.params[item] = value |
---|
| 135 | return |
---|
| 136 | |
---|
| 137 | raise ValueError, "Model does not contain parameter %s" % name |
---|
| 138 | |
---|
| 139 | |
---|
[c9636f7] | 140 | def _set_fixed_params(self): |
---|
| 141 | """ |
---|
[1affe64] | 142 | fill the self.fixed list with the p_model fixed list |
---|
[c9636f7] | 143 | """ |
---|
[1affe64] | 144 | for item in self.p_model.fixed: |
---|
[c9636f7] | 145 | self.fixed.append(item) |
---|
[8b677ec] | 146 | |
---|
[c9636f7] | 147 | self.fixed.sort() |
---|
[5eb9154] | 148 | |
---|
| 149 | |
---|
[c9636f7] | 150 | def run(self, x = 0.0): |
---|
| 151 | """ Evaluate the model |
---|
| 152 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
| 153 | @return: (DAB value) |
---|
| 154 | """ |
---|
[1affe64] | 155 | |
---|
| 156 | effective_radius = self.p_model.calculate_ER() |
---|
| 157 | #Reset the effective_radius of s_model just before the run |
---|
| 158 | if effective_radius != None and effective_radius != NotImplemented: |
---|
| 159 | self.s_model.setParam('effect_radius',effective_radius) |
---|
| 160 | return self.p_model.run(x)*self.s_model.run(x) |
---|
| 161 | |
---|
[c9636f7] | 162 | def runXY(self, x = 0.0): |
---|
| 163 | """ Evaluate the model |
---|
| 164 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
| 165 | @return: DAB value |
---|
| 166 | """ |
---|
[8b677ec] | 167 | |
---|
[1affe64] | 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) |
---|
| 172 | return self.p_model.runXY(x)* self.s_model.runXY(x) |
---|
[5eb9154] | 173 | |
---|
[c9636f7] | 174 | def set_dispersion(self, parameter, dispersion): |
---|
| 175 | """ |
---|
| 176 | Set the dispersion object for a model parameter |
---|
| 177 | @param parameter: name of the parameter [string] |
---|
| 178 | @dispersion: dispersion object of type DispersionModel |
---|
| 179 | """ |
---|
[db39b2a] | 180 | value= None |
---|
| 181 | try: |
---|
[1affe64] | 182 | if parameter in self.p_model.dispersion.keys(): |
---|
| 183 | value= self.p_model.set_dispersion(parameter, dispersion) |
---|
[8077fc4] | 184 | self._set_dispersion() |
---|
[db39b2a] | 185 | return value |
---|
| 186 | except: |
---|
| 187 | raise |
---|
[c9636f7] | 188 | |
---|
[1affe64] | 189 | def fill_description(self, p_model, s_model): |
---|
[8b677ec] | 190 | """ |
---|
| 191 | Fill the description for P(Q)*S(Q) |
---|
| 192 | """ |
---|
| 193 | description = "" |
---|
[1affe64] | 194 | description += "Note:1) The effect_radius (effective radius) of %s \n"% (s_model.name) |
---|
[8b677ec] | 195 | description +=" is automatically calculated from size parameters (radius...).\n" |
---|
| 196 | description += " 2) For non-spherical shape, this approximation is valid \n" |
---|
[1affe64] | 197 | description += " only for limited systems. Thus, use it at your own risk.\n" |
---|
| 198 | description +="See %s description and %s description \n"%( p_model.name, s_model.name ) |
---|
| 199 | description += " for details of individual models." |
---|
[8b677ec] | 200 | self.description += description |
---|
[c9636f7] | 201 | |
---|