1 | |
---|
2 | from sans.models.BaseComponent import BaseComponent |
---|
3 | import numpy, math |
---|
4 | import copy |
---|
5 | class MultiplicationModel(BaseComponent): |
---|
6 | """ |
---|
7 | Use for S(Q)*P(Q). |
---|
8 | Perform multplication of two models. |
---|
9 | Contains the models parameters combined. |
---|
10 | """ |
---|
11 | def __init__(self, model1, model2 ): |
---|
12 | BaseComponent.__init__(self) |
---|
13 | |
---|
14 | |
---|
15 | ## Setting model name model description |
---|
16 | self.description="" |
---|
17 | if model1.name != "NoStructure"and model2.name != "NoStructure": |
---|
18 | self.name = model1.name +" * "+ model2.name |
---|
19 | self.description= self.name+"\n" |
---|
20 | self.description +="see %s description and %s description"%( model1.name, |
---|
21 | model2.name ) |
---|
22 | elif model2.name != "NoStructure": |
---|
23 | self.name = model2.name |
---|
24 | self.description= model2.description |
---|
25 | else : |
---|
26 | self.name = model1.name |
---|
27 | self.description= model1.description |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | self.model1= model1 |
---|
32 | self.model2= model2 |
---|
33 | ## dispersion |
---|
34 | self._set_dispersion() |
---|
35 | ## Define parameters |
---|
36 | self._set_params() |
---|
37 | ## Parameter details [units, min, max] |
---|
38 | self._set_details() |
---|
39 | #list of parameter that can be fitted |
---|
40 | self._set_fixed_params() |
---|
41 | |
---|
42 | def _clone(self, obj): |
---|
43 | """ |
---|
44 | Internal utility function to copy the internal |
---|
45 | data members to a fresh copy. |
---|
46 | """ |
---|
47 | obj.params = copy.deepcopy(self.params) |
---|
48 | obj.description = copy.deepcopy(self.description) |
---|
49 | obj.details = copy.deepcopy(self.details) |
---|
50 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
51 | obj.model1 = self.model1.clone() |
---|
52 | obj.model2 = self.model2.clone() |
---|
53 | |
---|
54 | return obj |
---|
55 | |
---|
56 | |
---|
57 | def _set_dispersion(self): |
---|
58 | """ |
---|
59 | combined the two models dispersions |
---|
60 | """ |
---|
61 | for name , value in self.model1.dispersion.iteritems(): |
---|
62 | self.dispersion[name]= value |
---|
63 | |
---|
64 | for name , value in self.model2.dispersion.iteritems(): |
---|
65 | if not name in self.dispersion.keys(): |
---|
66 | self.dispersion[name]= value |
---|
67 | |
---|
68 | |
---|
69 | def _set_params(self): |
---|
70 | """ |
---|
71 | Concatenate the parameters of the two models to create |
---|
72 | this model parameters |
---|
73 | """ |
---|
74 | for name , value in self.model1.params.iteritems(): |
---|
75 | self.params[name]= value |
---|
76 | |
---|
77 | for name , value in self.model2.params.iteritems(): |
---|
78 | if not name in self.params.keys(): |
---|
79 | self.params[name]= value |
---|
80 | |
---|
81 | def _set_details(self): |
---|
82 | """ |
---|
83 | Concatenate details of the two models to create |
---|
84 | this model details |
---|
85 | """ |
---|
86 | for name ,detail in self.model1.details.iteritems(): |
---|
87 | self.details[name]= detail |
---|
88 | |
---|
89 | for name , detail in self.model2.details.iteritems(): |
---|
90 | if not name in self.details.keys(): |
---|
91 | self.details[name]= detail |
---|
92 | |
---|
93 | def setParam(self, name, value): |
---|
94 | """ |
---|
95 | Set the value of a model parameter |
---|
96 | |
---|
97 | @param name: name of the parameter |
---|
98 | @param value: value of the parameter |
---|
99 | """ |
---|
100 | self._setParamHelper( name, value) |
---|
101 | if name in self.model1.getParamList(): |
---|
102 | self.model1.setParam( name, value) |
---|
103 | |
---|
104 | if name in self.model2.getParamList(): |
---|
105 | self.model2.setParam( name, value) |
---|
106 | |
---|
107 | |
---|
108 | def _setParamHelper(self, name, value): |
---|
109 | """ |
---|
110 | Helper function to setparam |
---|
111 | """ |
---|
112 | # Look for dispersion parameters |
---|
113 | toks = name.split('.') |
---|
114 | if len(toks)==2: |
---|
115 | for item in self.dispersion.keys(): |
---|
116 | if item.lower()==toks[0].lower(): |
---|
117 | for par in self.dispersion[item]: |
---|
118 | if par.lower() == toks[1].lower(): |
---|
119 | self.dispersion[item][par] = value |
---|
120 | return |
---|
121 | else: |
---|
122 | # Look for standard parameter |
---|
123 | for item in self.params.keys(): |
---|
124 | if item.lower()==name.lower(): |
---|
125 | self.params[item] = value |
---|
126 | return |
---|
127 | |
---|
128 | raise ValueError, "Model does not contain parameter %s" % name |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | def _set_fixed_params(self): |
---|
135 | """ |
---|
136 | fill the self.fixed list with the two models fixed list |
---|
137 | """ |
---|
138 | for item in self.model1.fixed: |
---|
139 | self.fixed.append(item) |
---|
140 | |
---|
141 | for item in self.model2.fixed: |
---|
142 | if not item in self.fixed: |
---|
143 | self.fixed.append(item) |
---|
144 | self.fixed.sort() |
---|
145 | |
---|
146 | |
---|
147 | def run(self, x = 0.0): |
---|
148 | """ Evaluate the model |
---|
149 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
150 | @return: (DAB value) |
---|
151 | """ |
---|
152 | return self.model1.run(x)* self.model2.run(x) |
---|
153 | |
---|
154 | def runXY(self, x = 0.0): |
---|
155 | """ Evaluate the model |
---|
156 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
157 | @return: DAB value |
---|
158 | """ |
---|
159 | return self.model1.runXY(x)* self.model2.runXY(x) |
---|
160 | |
---|
161 | def set_dispersion(self, parameter, dispersion): |
---|
162 | """ |
---|
163 | Set the dispersion object for a model parameter |
---|
164 | @param parameter: name of the parameter [string] |
---|
165 | @dispersion: dispersion object of type DispersionModel |
---|
166 | """ |
---|
167 | if parameter in self.model1.getParamList(): |
---|
168 | return self.model1.set_dispersion(parameter, dispersion) |
---|
169 | else: |
---|
170 | return self.model2.set_dispersion(parameter, dispersion) |
---|
171 | |
---|
172 | |
---|
173 | |
---|
174 | |
---|