[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 | """ |
---|
| 10 | Use for S(Q)*P(Q). |
---|
| 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(): |
---|
[3740b11] | 99 | if not name in self.dispersion.keys()and name !='radius': |
---|
[c9636f7] | 100 | self.dispersion[name]= value |
---|
[3740b11] | 101 | |
---|
| 102 | |
---|
[c9636f7] | 103 | |
---|
| 104 | |
---|
| 105 | def _set_params(self): |
---|
| 106 | """ |
---|
| 107 | Concatenate the parameters of the two models to create |
---|
| 108 | this model parameters |
---|
| 109 | """ |
---|
[3740b11] | 110 | |
---|
[c9636f7] | 111 | for name , value in self.model1.params.iteritems(): |
---|
| 112 | self.params[name]= value |
---|
[3740b11] | 113 | |
---|
| 114 | if self.modelD !=None: |
---|
| 115 | if name == self.para1 or name == self.para2: |
---|
| 116 | self.modelD.params[name]= value |
---|
| 117 | elif name in self.model2.getParamList() and name !='radius': |
---|
| 118 | self.model2.setParam( name, value) |
---|
| 119 | |
---|
| 120 | elif name in self.model2.getParamList(): |
---|
[24415e9] | 121 | self.model2.setParam(name, value) |
---|
[3740b11] | 122 | if self.modelD !=None: |
---|
| 123 | self.model2.setParam('radius', self.modelD.run()) |
---|
[c9636f7] | 124 | |
---|
| 125 | for name , value in self.model2.params.iteritems(): |
---|
[3740b11] | 126 | if not name in self.params.keys()and name != 'radius': |
---|
[c9636f7] | 127 | self.params[name]= value |
---|
| 128 | |
---|
| 129 | def _set_details(self): |
---|
| 130 | """ |
---|
| 131 | Concatenate details of the two models to create |
---|
| 132 | this model details |
---|
| 133 | """ |
---|
| 134 | for name ,detail in self.model1.details.iteritems(): |
---|
| 135 | self.details[name]= detail |
---|
| 136 | |
---|
| 137 | for name , detail in self.model2.details.iteritems(): |
---|
[3740b11] | 138 | if not name in self.details.keys()and name != 'radius': |
---|
[c9636f7] | 139 | self.details[name]= detail |
---|
| 140 | |
---|
[8cfdd5e] | 141 | def setParam(self, name, value): |
---|
| 142 | """ |
---|
| 143 | Set the value of a model parameter |
---|
| 144 | |
---|
| 145 | @param name: name of the parameter |
---|
| 146 | @param value: value of the parameter |
---|
| 147 | """ |
---|
[3740b11] | 148 | |
---|
| 149 | self._setParamHelper( name, value) |
---|
| 150 | |
---|
[8cfdd5e] | 151 | if name in self.model1.getParamList(): |
---|
| 152 | self.model1.setParam( name, value) |
---|
[3740b11] | 153 | |
---|
| 154 | |
---|
| 155 | if self.modelD !=None: |
---|
| 156 | if name == self.para1 or name == self.para2: |
---|
| 157 | self.modelD.params[name]= value |
---|
| 158 | elif name in self.model2.getParamList() and name != 'radius': |
---|
| 159 | self.model2.setParam( name, value) |
---|
| 160 | elif name in self.model2.getParamList()and name != 'radius': |
---|
[8cfdd5e] | 161 | self.model2.setParam( name, value) |
---|
[3740b11] | 162 | |
---|
[24415e9] | 163 | |
---|
| 164 | self._setParamHelper( name, value) |
---|
[8cfdd5e] | 165 | |
---|
| 166 | def _setParamHelper(self, name, value): |
---|
| 167 | """ |
---|
| 168 | Helper function to setparam |
---|
| 169 | """ |
---|
| 170 | # Look for dispersion parameters |
---|
| 171 | toks = name.split('.') |
---|
| 172 | if len(toks)==2: |
---|
| 173 | for item in self.dispersion.keys(): |
---|
| 174 | if item.lower()==toks[0].lower(): |
---|
| 175 | for par in self.dispersion[item]: |
---|
| 176 | if par.lower() == toks[1].lower(): |
---|
| 177 | self.dispersion[item][par] = value |
---|
| 178 | return |
---|
| 179 | else: |
---|
| 180 | # Look for standard parameter |
---|
| 181 | for item in self.params.keys(): |
---|
| 182 | if item.lower()==name.lower(): |
---|
| 183 | self.params[item] = value |
---|
| 184 | return |
---|
| 185 | |
---|
| 186 | raise ValueError, "Model does not contain parameter %s" % name |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | |
---|
[c9636f7] | 192 | def _set_fixed_params(self): |
---|
| 193 | """ |
---|
| 194 | fill the self.fixed list with the two models fixed list |
---|
| 195 | """ |
---|
| 196 | for item in self.model1.fixed: |
---|
| 197 | self.fixed.append(item) |
---|
| 198 | |
---|
| 199 | for item in self.model2.fixed: |
---|
| 200 | if not item in self.fixed: |
---|
| 201 | self.fixed.append(item) |
---|
| 202 | self.fixed.sort() |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | def run(self, x = 0.0): |
---|
| 206 | """ Evaluate the model |
---|
| 207 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
| 208 | @return: (DAB value) |
---|
| 209 | """ |
---|
[3740b11] | 210 | #MultiplicationModel(self.model1, self.model2 ) |
---|
| 211 | |
---|
| 212 | if self.modelD!=None: |
---|
| 213 | value = self.modelD.run(x) |
---|
| 214 | self.model2.setParam( "radius", value) |
---|
| 215 | |
---|
| 216 | print "self.model2.setParam( radius, value)",value |
---|
| 217 | return self.model1.run(x)*self.model2.run(x) |
---|
[c9636f7] | 218 | |
---|
| 219 | def runXY(self, x = 0.0): |
---|
| 220 | """ Evaluate the model |
---|
| 221 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
| 222 | @return: DAB value |
---|
| 223 | """ |
---|
| 224 | return self.model1.runXY(x)* self.model2.runXY(x) |
---|
| 225 | |
---|
| 226 | def set_dispersion(self, parameter, dispersion): |
---|
| 227 | """ |
---|
| 228 | Set the dispersion object for a model parameter |
---|
| 229 | @param parameter: name of the parameter [string] |
---|
| 230 | @dispersion: dispersion object of type DispersionModel |
---|
| 231 | """ |
---|
[db39b2a] | 232 | value= None |
---|
| 233 | try: |
---|
| 234 | if parameter in self.model1.getParamList(): |
---|
| 235 | value= self.model1.set_dispersion(parameter, dispersion) |
---|
| 236 | elif parameter in self.model2.ParamList(): |
---|
| 237 | value= self.model2.set_dispersion(parameter, dispersion) |
---|
| 238 | return value |
---|
| 239 | except: |
---|
| 240 | raise |
---|
[c9636f7] | 241 | |
---|
| 242 | |
---|
| 243 | |
---|