[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 |
---|
| 6 | from sans.models.DiamCylFunc import DiamCylFunc |
---|
| 7 | from sans.models.DiamEllipFunc import DiamEllipFunc |
---|
[c9636f7] | 8 | class MultiplicationModel(BaseComponent): |
---|
| 9 | """ |
---|
[8077fc4] | 10 | Use for P(Q)*S(Q); function call maut be in order of P(Q) and then S(Q). |
---|
[c9636f7] | 11 | Perform multplication of two models. |
---|
| 12 | Contains the models parameters combined. |
---|
| 13 | """ |
---|
| 14 | def __init__(self, model1, model2 ): |
---|
| 15 | BaseComponent.__init__(self) |
---|
| 16 | |
---|
[996fd35] | 17 | |
---|
[8cfdd5e] | 18 | ## Setting model name model description |
---|
[996fd35] | 19 | self.description="" |
---|
| 20 | if model1.name != "NoStructure"and model2.name != "NoStructure": |
---|
| 21 | self.name = model1.name +" * "+ model2.name |
---|
| 22 | self.description= self.name+"\n" |
---|
| 23 | self.description +="see %s description and %s description"%( model1.name, |
---|
| 24 | model2.name ) |
---|
| 25 | elif model2.name != "NoStructure": |
---|
| 26 | self.name = model2.name |
---|
| 27 | self.description= model2.description |
---|
| 28 | else : |
---|
| 29 | self.name = model1.name |
---|
| 30 | self.description= model1.description |
---|
[3740b11] | 31 | |
---|
| 32 | #For virial coefficients for only two P(Q) models,"CylinderModel","EllipsoidModel". SphereModel works w/o it. |
---|
| 33 | modelDiam = None |
---|
| 34 | if model1.__class__.__name__ == "CylinderModel": |
---|
| 35 | Model1DPlugin("DiamCylFunc") |
---|
| 36 | modelDiam = DiamCylFunc() |
---|
| 37 | para1 = 'radius' |
---|
| 38 | para2 = 'length' |
---|
| 39 | |
---|
| 40 | elif model1.__class__.__name__ == "EllipsoidModel": |
---|
| 41 | Model1DPlugin("DiamEllipFunc") |
---|
| 42 | modelDiam = DiamEllipFunc() |
---|
| 43 | para1 = 'radius_a' |
---|
| 44 | para2 = 'radius_b' |
---|
| 45 | |
---|
| 46 | self.modelD = modelDiam |
---|
[996fd35] | 47 | |
---|
[3740b11] | 48 | if model1.__class__.__name__ == "CylinderModel" \ |
---|
| 49 | or model1.__class__.__name__ == "EllipsoidModel": |
---|
| 50 | |
---|
| 51 | self.para1 = para1 |
---|
| 52 | self.para2 = para2 |
---|
| 53 | |
---|
[996fd35] | 54 | |
---|
[c9636f7] | 55 | self.model1= model1 |
---|
| 56 | self.model2= model2 |
---|
[3740b11] | 57 | |
---|
[5fc8e22] | 58 | |
---|
[c9636f7] | 59 | ## dispersion |
---|
| 60 | self._set_dispersion() |
---|
| 61 | ## Define parameters |
---|
| 62 | self._set_params() |
---|
| 63 | ## Parameter details [units, min, max] |
---|
| 64 | self._set_details() |
---|
| 65 | #list of parameter that can be fitted |
---|
| 66 | self._set_fixed_params() |
---|
[5fc8e22] | 67 | ## parameters with orientation |
---|
| 68 | for item in self.model1.orientation_params: |
---|
| 69 | self.orientation_params.append(item) |
---|
| 70 | |
---|
| 71 | for item in self.model2.orientation_params: |
---|
| 72 | if not item in self.orientation_params: |
---|
| 73 | self.orientation_params.append(item) |
---|
| 74 | |
---|
[c9636f7] | 75 | |
---|
[a68efd1] | 76 | def _clone(self, obj): |
---|
| 77 | """ |
---|
| 78 | Internal utility function to copy the internal |
---|
| 79 | data members to a fresh copy. |
---|
| 80 | """ |
---|
| 81 | obj.params = copy.deepcopy(self.params) |
---|
| 82 | obj.description = copy.deepcopy(self.description) |
---|
| 83 | obj.details = copy.deepcopy(self.details) |
---|
| 84 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
| 85 | obj.model1 = self.model1.clone() |
---|
| 86 | obj.model2 = self.model2.clone() |
---|
| 87 | |
---|
| 88 | return obj |
---|
| 89 | |
---|
| 90 | |
---|
[c9636f7] | 91 | def _set_dispersion(self): |
---|
| 92 | """ |
---|
| 93 | combined the two models dispersions |
---|
| 94 | """ |
---|
| 95 | for name , value in self.model1.dispersion.iteritems(): |
---|
| 96 | self.dispersion[name]= value |
---|
| 97 | |
---|
| 98 | for name , value in self.model2.dispersion.iteritems(): |
---|
[8077fc4] | 99 | # S(Q) has only 'radius' for dispersion. |
---|
| 100 | if not name in self.dispersion.keys()and name !='radius': |
---|
[c9636f7] | 101 | self.dispersion[name]= value |
---|
[3740b11] | 102 | |
---|
| 103 | |
---|
[c9636f7] | 104 | |
---|
| 105 | |
---|
| 106 | def _set_params(self): |
---|
| 107 | """ |
---|
| 108 | Concatenate the parameters of the two models to create |
---|
| 109 | this model parameters |
---|
| 110 | """ |
---|
[3740b11] | 111 | |
---|
[c9636f7] | 112 | for name , value in self.model1.params.iteritems(): |
---|
| 113 | self.params[name]= value |
---|
[3740b11] | 114 | |
---|
| 115 | if self.modelD !=None: |
---|
| 116 | if name == self.para1 or name == self.para2: |
---|
| 117 | self.modelD.params[name]= value |
---|
| 118 | elif name in self.model2.getParamList() and name !='radius': |
---|
| 119 | self.model2.setParam( name, value) |
---|
| 120 | |
---|
| 121 | elif name in self.model2.getParamList(): |
---|
[24415e9] | 122 | self.model2.setParam(name, value) |
---|
[3740b11] | 123 | if self.modelD !=None: |
---|
| 124 | self.model2.setParam('radius', self.modelD.run()) |
---|
[c9636f7] | 125 | |
---|
| 126 | for name , value in self.model2.params.iteritems(): |
---|
[3740b11] | 127 | if not name in self.params.keys()and name != 'radius': |
---|
[c9636f7] | 128 | self.params[name]= value |
---|
| 129 | |
---|
| 130 | def _set_details(self): |
---|
| 131 | """ |
---|
| 132 | Concatenate details of the two models to create |
---|
| 133 | this model details |
---|
| 134 | """ |
---|
| 135 | for name ,detail in self.model1.details.iteritems(): |
---|
| 136 | self.details[name]= detail |
---|
| 137 | |
---|
| 138 | for name , detail in self.model2.details.iteritems(): |
---|
[3740b11] | 139 | if not name in self.details.keys()and name != 'radius': |
---|
[c9636f7] | 140 | self.details[name]= detail |
---|
| 141 | |
---|
[8cfdd5e] | 142 | def setParam(self, name, value): |
---|
| 143 | """ |
---|
| 144 | Set the value of a model parameter |
---|
| 145 | |
---|
| 146 | @param name: name of the parameter |
---|
| 147 | @param value: value of the parameter |
---|
| 148 | """ |
---|
[3740b11] | 149 | |
---|
| 150 | self._setParamHelper( name, value) |
---|
| 151 | |
---|
[8cfdd5e] | 152 | if name in self.model1.getParamList(): |
---|
| 153 | self.model1.setParam( name, value) |
---|
[7c3aea3] | 154 | if self.modelD !=None: |
---|
| 155 | if name==self.para1 or name == self.para2: |
---|
| 156 | self.modelD.params[name]= value |
---|
| 157 | self.model2.setParam('radius', self.modelD.run()) |
---|
| 158 | elif name in self.model2.getParamList(): |
---|
| 159 | self.model2.setParam( name, value) |
---|
[6756712] | 160 | else: |
---|
| 161 | if name in self.model2.getParamList(): |
---|
[3740b11] | 162 | self.model2.setParam( name, value) |
---|
| 163 | |
---|
[6756712] | 164 | |
---|
| 165 | |
---|
[24415e9] | 166 | self._setParamHelper( name, value) |
---|
[8cfdd5e] | 167 | |
---|
| 168 | def _setParamHelper(self, name, value): |
---|
| 169 | """ |
---|
| 170 | Helper function to setparam |
---|
| 171 | """ |
---|
| 172 | # Look for dispersion parameters |
---|
| 173 | toks = name.split('.') |
---|
| 174 | if len(toks)==2: |
---|
| 175 | for item in self.dispersion.keys(): |
---|
| 176 | if item.lower()==toks[0].lower(): |
---|
| 177 | for par in self.dispersion[item]: |
---|
| 178 | if par.lower() == toks[1].lower(): |
---|
| 179 | self.dispersion[item][par] = value |
---|
| 180 | return |
---|
| 181 | else: |
---|
| 182 | # Look for standard parameter |
---|
| 183 | for item in self.params.keys(): |
---|
| 184 | if item.lower()==name.lower(): |
---|
| 185 | self.params[item] = value |
---|
| 186 | return |
---|
| 187 | |
---|
| 188 | raise ValueError, "Model does not contain parameter %s" % name |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | |
---|
[c9636f7] | 194 | def _set_fixed_params(self): |
---|
| 195 | """ |
---|
| 196 | fill the self.fixed list with the two models fixed list |
---|
| 197 | """ |
---|
| 198 | for item in self.model1.fixed: |
---|
| 199 | self.fixed.append(item) |
---|
| 200 | |
---|
| 201 | for item in self.model2.fixed: |
---|
| 202 | if not item in self.fixed: |
---|
| 203 | self.fixed.append(item) |
---|
| 204 | self.fixed.sort() |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | def run(self, x = 0.0): |
---|
| 208 | """ Evaluate the model |
---|
| 209 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
| 210 | @return: (DAB value) |
---|
| 211 | """ |
---|
[3740b11] | 212 | return self.model1.run(x)*self.model2.run(x) |
---|
[c9636f7] | 213 | |
---|
| 214 | def runXY(self, x = 0.0): |
---|
| 215 | """ Evaluate the model |
---|
| 216 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
| 217 | @return: DAB value |
---|
| 218 | """ |
---|
| 219 | return self.model1.runXY(x)* self.model2.runXY(x) |
---|
| 220 | |
---|
| 221 | def set_dispersion(self, parameter, dispersion): |
---|
| 222 | """ |
---|
| 223 | Set the dispersion object for a model parameter |
---|
| 224 | @param parameter: name of the parameter [string] |
---|
| 225 | @dispersion: dispersion object of type DispersionModel |
---|
| 226 | """ |
---|
[db39b2a] | 227 | value= None |
---|
| 228 | try: |
---|
[c3e4a7fa] | 229 | if parameter in self.model1.dispersion.keys(): |
---|
[db39b2a] | 230 | value= self.model1.set_dispersion(parameter, dispersion) |
---|
[8077fc4] | 231 | #There is no dispersion for the structure factors(S(Q)). |
---|
| 232 | #ToDo: need to decide whether or not the dispersion for S(Q) has to be considered for P*S. |
---|
| 233 | elif parameter in self.model2.dispersion.keys()and name !='radius': |
---|
[db39b2a] | 234 | value= self.model2.set_dispersion(parameter, dispersion) |
---|
[8077fc4] | 235 | self._set_dispersion() |
---|
[db39b2a] | 236 | return value |
---|
| 237 | except: |
---|
| 238 | raise |
---|
[c9636f7] | 239 | |
---|
| 240 | |
---|
| 241 | |
---|