1 | |
---|
2 | from sans.models.BaseComponent import BaseComponent |
---|
3 | import numpy, math |
---|
4 | import copy |
---|
5 | from sans.models.pluginmodel import Model1DPlugin |
---|
6 | from sans.models.DiamCylFunc import DiamCylFunc |
---|
7 | from sans.models.DiamEllipFunc import DiamEllipFunc |
---|
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 | |
---|
17 | |
---|
18 | ## Setting model name model description |
---|
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 |
---|
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 |
---|
47 | |
---|
48 | if model1.__class__.__name__ == "CylinderModel" \ |
---|
49 | or model1.__class__.__name__ == "EllipsoidModel": |
---|
50 | |
---|
51 | self.para1 = para1 |
---|
52 | self.para2 = para2 |
---|
53 | |
---|
54 | |
---|
55 | self.model1= model1 |
---|
56 | self.model2= model2 |
---|
57 | |
---|
58 | #if modelDiam != None: |
---|
59 | #self.model2.params["radius"]= DiamEllipFunc() |
---|
60 | |
---|
61 | #print "self.modelD",self.modelD.params,self.model1.params,self.model2.params |
---|
62 | |
---|
63 | ## dispersion |
---|
64 | self._set_dispersion() |
---|
65 | ## Define parameters |
---|
66 | self._set_params() |
---|
67 | ## Parameter details [units, min, max] |
---|
68 | self._set_details() |
---|
69 | #list of parameter that can be fitted |
---|
70 | self._set_fixed_params() |
---|
71 | |
---|
72 | def _clone(self, obj): |
---|
73 | """ |
---|
74 | Internal utility function to copy the internal |
---|
75 | data members to a fresh copy. |
---|
76 | """ |
---|
77 | obj.params = copy.deepcopy(self.params) |
---|
78 | obj.description = copy.deepcopy(self.description) |
---|
79 | obj.details = copy.deepcopy(self.details) |
---|
80 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
81 | obj.model1 = self.model1.clone() |
---|
82 | obj.model2 = self.model2.clone() |
---|
83 | |
---|
84 | return obj |
---|
85 | |
---|
86 | |
---|
87 | def _set_dispersion(self): |
---|
88 | """ |
---|
89 | combined the two models dispersions |
---|
90 | """ |
---|
91 | for name , value in self.model1.dispersion.iteritems(): |
---|
92 | self.dispersion[name]= value |
---|
93 | |
---|
94 | for name , value in self.model2.dispersion.iteritems(): |
---|
95 | if not name in self.dispersion.keys()and name !='radius': |
---|
96 | self.dispersion[name]= value |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | def _set_params(self): |
---|
102 | """ |
---|
103 | Concatenate the parameters of the two models to create |
---|
104 | this model parameters |
---|
105 | """ |
---|
106 | |
---|
107 | for name , value in self.model1.params.iteritems(): |
---|
108 | self.params[name]= value |
---|
109 | |
---|
110 | if self.modelD !=None: |
---|
111 | if name == self.para1 or name == self.para2: |
---|
112 | self.modelD.params[name]= value |
---|
113 | elif name in self.model2.getParamList() and name !='radius': |
---|
114 | self.model2.setParam( name, value) |
---|
115 | |
---|
116 | elif name in self.model2.getParamList(): |
---|
117 | self.model2.setParam(name, value) |
---|
118 | if self.modelD !=None: |
---|
119 | self.model2.setParam('radius', self.modelD.run()) |
---|
120 | |
---|
121 | for name , value in self.model2.params.iteritems(): |
---|
122 | if not name in self.params.keys()and name != 'radius': |
---|
123 | self.params[name]= value |
---|
124 | |
---|
125 | def _set_details(self): |
---|
126 | """ |
---|
127 | Concatenate details of the two models to create |
---|
128 | this model details |
---|
129 | """ |
---|
130 | for name ,detail in self.model1.details.iteritems(): |
---|
131 | self.details[name]= detail |
---|
132 | |
---|
133 | for name , detail in self.model2.details.iteritems(): |
---|
134 | if not name in self.details.keys()and name != 'radius': |
---|
135 | self.details[name]= detail |
---|
136 | |
---|
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 | """ |
---|
144 | |
---|
145 | self._setParamHelper( name, value) |
---|
146 | |
---|
147 | if name in self.model1.getParamList(): |
---|
148 | self.model1.setParam( name, value) |
---|
149 | |
---|
150 | |
---|
151 | if self.modelD !=None: |
---|
152 | if name == self.para1 or name == self.para2: |
---|
153 | self.modelD.params[name]= value |
---|
154 | elif name in self.model2.getParamList() and name != 'radius': |
---|
155 | self.model2.setParam( name, value) |
---|
156 | elif name in self.model2.getParamList()and name != 'radius': |
---|
157 | self.model2.setParam( name, value) |
---|
158 | |
---|
159 | |
---|
160 | self._setParamHelper( name, value) |
---|
161 | |
---|
162 | def _setParamHelper(self, name, value): |
---|
163 | """ |
---|
164 | Helper function to setparam |
---|
165 | """ |
---|
166 | # Look for dispersion parameters |
---|
167 | toks = name.split('.') |
---|
168 | if len(toks)==2: |
---|
169 | for item in self.dispersion.keys(): |
---|
170 | if item.lower()==toks[0].lower(): |
---|
171 | for par in self.dispersion[item]: |
---|
172 | if par.lower() == toks[1].lower(): |
---|
173 | self.dispersion[item][par] = value |
---|
174 | return |
---|
175 | else: |
---|
176 | # Look for standard parameter |
---|
177 | for item in self.params.keys(): |
---|
178 | if item.lower()==name.lower(): |
---|
179 | self.params[item] = value |
---|
180 | return |
---|
181 | |
---|
182 | raise ValueError, "Model does not contain parameter %s" % name |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | def _set_fixed_params(self): |
---|
189 | """ |
---|
190 | fill the self.fixed list with the two models fixed list |
---|
191 | """ |
---|
192 | for item in self.model1.fixed: |
---|
193 | self.fixed.append(item) |
---|
194 | |
---|
195 | for item in self.model2.fixed: |
---|
196 | if not item in self.fixed: |
---|
197 | self.fixed.append(item) |
---|
198 | self.fixed.sort() |
---|
199 | |
---|
200 | |
---|
201 | def run(self, x = 0.0): |
---|
202 | """ Evaluate the model |
---|
203 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
204 | @return: (DAB value) |
---|
205 | """ |
---|
206 | #MultiplicationModel(self.model1, self.model2 ) |
---|
207 | |
---|
208 | if self.modelD!=None: |
---|
209 | value = self.modelD.run(x) |
---|
210 | self.model2.setParam( "radius", value) |
---|
211 | |
---|
212 | print "self.model2.setParam( radius, value)",value |
---|
213 | return self.model1.run(x)*self.model2.run(x) |
---|
214 | |
---|
215 | def runXY(self, x = 0.0): |
---|
216 | """ Evaluate the model |
---|
217 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
218 | @return: DAB value |
---|
219 | """ |
---|
220 | return self.model1.runXY(x)* self.model2.runXY(x) |
---|
221 | |
---|
222 | def set_dispersion(self, parameter, dispersion): |
---|
223 | """ |
---|
224 | Set the dispersion object for a model parameter |
---|
225 | @param parameter: name of the parameter [string] |
---|
226 | @dispersion: dispersion object of type DispersionModel |
---|
227 | """ |
---|
228 | value= None |
---|
229 | try: |
---|
230 | if parameter in self.model1.getParamList(): |
---|
231 | value= self.model1.set_dispersion(parameter, dispersion) |
---|
232 | elif parameter in self.model2.ParamList(): |
---|
233 | value= self.model2.set_dispersion(parameter, dispersion) |
---|
234 | return value |
---|
235 | except: |
---|
236 | raise |
---|
237 | |
---|
238 | |
---|
239 | |
---|