1 | |
---|
2 | from sans.models.BaseComponent import BaseComponent |
---|
3 | from sans.models.CoreFourShellModel import CoreFourShellModel |
---|
4 | import copy |
---|
5 | max_nshells = 4 |
---|
6 | class CoreMultiShellModel(BaseComponent): |
---|
7 | """ |
---|
8 | This multi-model is based on CoreFourShellModel and provides the capability |
---|
9 | of changing the number of shells between 1 and 4. |
---|
10 | """ |
---|
11 | def __init__(self, n_shells=4): |
---|
12 | BaseComponent.__init__(self) |
---|
13 | """ |
---|
14 | :param n_shells: number of shells in the model, assumes 1<= n_shells <=4. |
---|
15 | """ |
---|
16 | |
---|
17 | ## Setting model name model description |
---|
18 | self.description="" |
---|
19 | model = CoreFourShellModel() |
---|
20 | self.model = model |
---|
21 | self.name = "CoreMultiShellModel" |
---|
22 | self.description="" |
---|
23 | self.n_shells = n_shells |
---|
24 | ## Define parameters |
---|
25 | self.params = {} |
---|
26 | |
---|
27 | ## Parameter details [units, min, max] |
---|
28 | self.details = {} |
---|
29 | |
---|
30 | ## dispersion |
---|
31 | self._set_dispersion() |
---|
32 | ## Define parameters |
---|
33 | self._set_params() |
---|
34 | |
---|
35 | ## Parameter details [units, min, max] |
---|
36 | self._set_details() |
---|
37 | |
---|
38 | #list of parameter that can be fitted |
---|
39 | self._set_fixed_params() |
---|
40 | |
---|
41 | ## parameters with orientation: can be removed since there is no orientational params |
---|
42 | for item in self.model.orientation_params: |
---|
43 | self.orientation_params.append(item) |
---|
44 | |
---|
45 | |
---|
46 | def _clone(self, obj): |
---|
47 | """ |
---|
48 | Internal utility function to copy the internal |
---|
49 | data members to a fresh copy. |
---|
50 | """ |
---|
51 | obj.params = copy.deepcopy(self.params) |
---|
52 | obj.description = copy.deepcopy(self.description) |
---|
53 | obj.details = copy.deepcopy(self.details) |
---|
54 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
55 | obj.model = self.model.clone() |
---|
56 | |
---|
57 | return obj |
---|
58 | |
---|
59 | |
---|
60 | def _set_dispersion(self): |
---|
61 | """ |
---|
62 | model dispersions |
---|
63 | Polydispersion should not be applied to s_model |
---|
64 | """ |
---|
65 | ##set dispersion from model |
---|
66 | for name , value in self.model.dispersion.iteritems(): |
---|
67 | nshell = 0 |
---|
68 | if name.split('_')[0] == 'thick': |
---|
69 | while nshell<self.n_shells: |
---|
70 | nshell += 1 |
---|
71 | if name.split('_')[1] == 'shell%s' % str(nshell): |
---|
72 | self.dispersion[name]= value |
---|
73 | else: |
---|
74 | continue |
---|
75 | else: |
---|
76 | self.dispersion[name]= value |
---|
77 | |
---|
78 | |
---|
79 | def _set_params(self): |
---|
80 | """ |
---|
81 | Concatenate the parameters of the model to create |
---|
82 | this model parameters |
---|
83 | """ |
---|
84 | # rearrange the parameters for the given # of shells |
---|
85 | for name , value in self.model.params.iteritems(): |
---|
86 | nshell = 0 |
---|
87 | if name.split('_')[0] == 'thick' or name.split('_')[0] == 'sld': |
---|
88 | if name.split('_')[1] == 'solv' or name.split('_')[1] == 'core': |
---|
89 | self.params[name]= value |
---|
90 | continue |
---|
91 | while nshell<self.n_shells: |
---|
92 | nshell += 1 |
---|
93 | if name.split('_')[1] == 'shell%s' % str(nshell): |
---|
94 | self.params[name]= value |
---|
95 | continue |
---|
96 | else: |
---|
97 | self.params[name]= value |
---|
98 | |
---|
99 | |
---|
100 | # set constrained values for the original model params |
---|
101 | self._set_xtra_model_param() |
---|
102 | |
---|
103 | def _set_details(self): |
---|
104 | """ |
---|
105 | Concatenate details of the original model to create |
---|
106 | this model details |
---|
107 | """ |
---|
108 | for name ,detail in self.model.details.iteritems(): |
---|
109 | if name in self.params.iterkeys(): |
---|
110 | self.details[name]= detail |
---|
111 | |
---|
112 | |
---|
113 | def _set_xtra_model_param(self): |
---|
114 | """ |
---|
115 | Set params of original model that are hidden from this model |
---|
116 | """ |
---|
117 | # look for the model parameters that are not in param list |
---|
118 | for key in self.model.params.iterkeys(): |
---|
119 | if key not in self.params.keys(): |
---|
120 | if key.split('_')[0] == 'thick': |
---|
121 | self.model.setParam(key, 0) |
---|
122 | continue |
---|
123 | |
---|
124 | for nshell in range(self.n_shells,max_nshells): |
---|
125 | if key.split('_')[0] == 'sld' and key.split('_')[1] == 'shell%s' % str(nshell+1): |
---|
126 | try: |
---|
127 | value = self.model.params['sld_solv'] |
---|
128 | self.model.setParam(key, value) |
---|
129 | except: pass |
---|
130 | |
---|
131 | |
---|
132 | def setParam(self, name, value): |
---|
133 | """ |
---|
134 | Set the value of a model parameter |
---|
135 | |
---|
136 | : param name: name of the parameter |
---|
137 | : param value: value of the parameter |
---|
138 | """ |
---|
139 | # set param to new model |
---|
140 | self._setParamHelper( name, value) |
---|
141 | ## setParam to model |
---|
142 | if name=='sld_solv': |
---|
143 | # the sld_*** model.params not in params must set to value of sld_solv |
---|
144 | for key in self.model.params.iterkeys(): |
---|
145 | if key not in self.params.keys()and key.split('_')[0] == 'sld': |
---|
146 | self.model.setParam(key, value) |
---|
147 | self.model.setParam( name, value) |
---|
148 | |
---|
149 | def _setParamHelper(self, name, value): |
---|
150 | """ |
---|
151 | Helper function to setParam |
---|
152 | """ |
---|
153 | #look for dispersion parameters |
---|
154 | toks = name.split('.') |
---|
155 | if len(toks)==2: |
---|
156 | for item in self.dispersion.keys(): |
---|
157 | if item.lower()==toks[0].lower(): |
---|
158 | for par in self.dispersion[item]: |
---|
159 | if par.lower() == toks[1].lower(): |
---|
160 | self.dispersion[item][par] = value |
---|
161 | return |
---|
162 | # Look for standard parameter |
---|
163 | for item in self.params.keys(): |
---|
164 | if item.lower()==name.lower(): |
---|
165 | self.params[item] = value |
---|
166 | return |
---|
167 | |
---|
168 | raise ValueError, "Model does not contain parameter %s" % name |
---|
169 | |
---|
170 | |
---|
171 | def _set_fixed_params(self): |
---|
172 | """ |
---|
173 | Fill the self.fixed list with the model fixed list |
---|
174 | """ |
---|
175 | for item in self.model.fixed: |
---|
176 | if item.split('.')[0] in self.params.keys(): |
---|
177 | self.fixed.append(item) |
---|
178 | |
---|
179 | self.fixed.sort() |
---|
180 | pass |
---|
181 | |
---|
182 | def run(self, x = 0.0): |
---|
183 | """ |
---|
184 | Evaluate the model |
---|
185 | |
---|
186 | : param x: input q-value (float or [float, float] as [r, theta]) |
---|
187 | : return: (DAB value) |
---|
188 | """ |
---|
189 | # set effective radius and scaling factor before run |
---|
190 | |
---|
191 | return self.model.run(x) |
---|
192 | |
---|
193 | def runXY(self, x = 0.0): |
---|
194 | """ |
---|
195 | Evaluate the model |
---|
196 | |
---|
197 | : param x: input q-value (float or [float, float] as [qx, qy]) |
---|
198 | : return: DAB value |
---|
199 | """ |
---|
200 | # set effective radius and scaling factor before run |
---|
201 | |
---|
202 | return self.model.runXY(x) |
---|
203 | |
---|
204 | ## Now (May27,10) directly uses the model eval function |
---|
205 | ## instead of the for-loop in Base Component. |
---|
206 | def evalDistribution(self, x = []): |
---|
207 | """ |
---|
208 | Evaluate the model in cartesian coordinates |
---|
209 | |
---|
210 | : param x: input q[], or [qx[], qy[]] |
---|
211 | : return: scattering function P(q[]) |
---|
212 | """ |
---|
213 | # set effective radius and scaling factor before run |
---|
214 | return self.model.evalDistribution(x) |
---|
215 | |
---|
216 | def set_dispersion(self, parameter, dispersion): |
---|
217 | """ |
---|
218 | Set the dispersion object for a model parameter |
---|
219 | |
---|
220 | : param parameter: name of the parameter [string] |
---|
221 | :dispersion: dispersion object of type DispersionModel |
---|
222 | """ |
---|
223 | value= None |
---|
224 | try: |
---|
225 | if parameter in self.model.dispersion.keys(): |
---|
226 | value= self.model.set_dispersion(parameter, dispersion) |
---|
227 | self._set_dispersion() |
---|
228 | return value |
---|
229 | except: |
---|
230 | raise |
---|