1 | """ |
---|
2 | Adapters for fitting module |
---|
3 | """ |
---|
4 | import copy |
---|
5 | import numpy |
---|
6 | import math |
---|
7 | from sas.sascalc.data_util.uncertainty import Uncertainty |
---|
8 | |
---|
9 | from sas.qtgui.Plotting.Plottables import PlottableData1D |
---|
10 | from sas.qtgui.Plotting.Plottables import PlottableData2D |
---|
11 | |
---|
12 | from sas.sascalc.dataloader.data_info import Data1D as LoadData1D |
---|
13 | from sas.sascalc.dataloader.data_info import Data2D as LoadData2D |
---|
14 | |
---|
15 | |
---|
16 | class Data1D(PlottableData1D, LoadData1D): |
---|
17 | """ |
---|
18 | """ |
---|
19 | def __init__(self, x=None, y=None, dx=None, dy=None): |
---|
20 | """ |
---|
21 | """ |
---|
22 | if x is None: |
---|
23 | x = [] |
---|
24 | if y is None: |
---|
25 | y = [] |
---|
26 | PlottableData1D.__init__(self, x, y, dx, dy) |
---|
27 | LoadData1D.__init__(self, x, y, dx, dy) |
---|
28 | self.id = None |
---|
29 | self.list_group_id = [] |
---|
30 | self.group_id = None |
---|
31 | self.is_data = True |
---|
32 | self.path = None |
---|
33 | self.xtransform = None |
---|
34 | self.ytransform = None |
---|
35 | self.title = "" |
---|
36 | self.scale = None |
---|
37 | |
---|
38 | def copy_from_datainfo(self, data1d): |
---|
39 | """ |
---|
40 | copy values of Data1D of type DataLaoder.Data_info |
---|
41 | """ |
---|
42 | self.x = copy.deepcopy(data1d.x) |
---|
43 | self.y = copy.deepcopy(data1d.y) |
---|
44 | self.dy = copy.deepcopy(data1d.dy) |
---|
45 | |
---|
46 | if hasattr(data1d, "dx"): |
---|
47 | self.dx = copy.deepcopy(data1d.dx) |
---|
48 | if hasattr(data1d, "dxl"): |
---|
49 | self.dxl = copy.deepcopy(data1d.dxl) |
---|
50 | if hasattr(data1d, "dxw"): |
---|
51 | self.dxw = copy.deepcopy(data1d.dxw) |
---|
52 | |
---|
53 | self.xaxis(data1d._xaxis, data1d._xunit) |
---|
54 | self.yaxis(data1d._yaxis, data1d._yunit) |
---|
55 | self.title = data1d.title |
---|
56 | |
---|
57 | def __str__(self): |
---|
58 | """ |
---|
59 | print data |
---|
60 | """ |
---|
61 | _str = "%s\n" % LoadData1D.__str__(self) |
---|
62 | |
---|
63 | return _str |
---|
64 | |
---|
65 | def _perform_operation(self, other, operation): |
---|
66 | """ |
---|
67 | """ |
---|
68 | # First, check the data compatibility |
---|
69 | dy, dy_other = self._validity_check(other) |
---|
70 | result = Data1D(x=[], y=[], dx=None, dy=None) |
---|
71 | result.clone_without_data(length=len(self.x), clone=self) |
---|
72 | result.copy_from_datainfo(data1d=self) |
---|
73 | if self.dxw == None: |
---|
74 | result.dxw = None |
---|
75 | else: |
---|
76 | result.dxw = numpy.zeros(len(self.x)) |
---|
77 | if self.dxl == None: |
---|
78 | result.dxl = None |
---|
79 | else: |
---|
80 | result.dxl = numpy.zeros(len(self.x)) |
---|
81 | |
---|
82 | for i in range(len(self.x)): |
---|
83 | result.x[i] = self.x[i] |
---|
84 | if self.dx is not None and len(self.x) == len(self.dx): |
---|
85 | result.dx[i] = self.dx[i] |
---|
86 | if self.dxw is not None and len(self.x) == len(self.dxw): |
---|
87 | result.dxw[i] = self.dxw[i] |
---|
88 | if self.dxl is not None and len(self.x) == len(self.dxl): |
---|
89 | result.dxl[i] = self.dxl[i] |
---|
90 | |
---|
91 | a = Uncertainty(self.y[i], dy[i]**2) |
---|
92 | if isinstance(other, Data1D): |
---|
93 | b = Uncertainty(other.y[i], dy_other[i]**2) |
---|
94 | if other.dx is not None: |
---|
95 | result.dx[i] *= self.dx[i] |
---|
96 | result.dx[i] += (other.dx[i]**2) |
---|
97 | result.dx[i] /= 2 |
---|
98 | result.dx[i] = math.sqrt(result.dx[i]) |
---|
99 | if result.dxl is not None and other.dxl is not None: |
---|
100 | result.dxl[i] *= self.dxl[i] |
---|
101 | result.dxl[i] += (other.dxl[i]**2) |
---|
102 | result.dxl[i] /= 2 |
---|
103 | result.dxl[i] = math.sqrt(result.dxl[i]) |
---|
104 | else: |
---|
105 | b = other |
---|
106 | |
---|
107 | output = operation(a, b) |
---|
108 | result.y[i] = output.x |
---|
109 | result.dy[i] = math.sqrt(math.fabs(output.variance)) |
---|
110 | return result |
---|
111 | |
---|
112 | def _perform_union(self, other): |
---|
113 | """ |
---|
114 | """ |
---|
115 | # First, check the data compatibility |
---|
116 | self._validity_check_union(other) |
---|
117 | result = Data1D(x=[], y=[], dx=None, dy=None) |
---|
118 | tot_length = len(self.x) + len(other.x) |
---|
119 | result = self.clone_without_data(length=tot_length, clone=result) |
---|
120 | if self.dy == None or other.dy is None: |
---|
121 | result.dy = None |
---|
122 | else: |
---|
123 | result.dy = numpy.zeros(tot_length) |
---|
124 | if self.dx == None or other.dx is None: |
---|
125 | result.dx = None |
---|
126 | else: |
---|
127 | result.dx = numpy.zeros(tot_length) |
---|
128 | if self.dxw == None or other.dxw is None: |
---|
129 | result.dxw = None |
---|
130 | else: |
---|
131 | result.dxw = numpy.zeros(tot_length) |
---|
132 | if self.dxl == None or other.dxl is None: |
---|
133 | result.dxl = None |
---|
134 | else: |
---|
135 | result.dxl = numpy.zeros(tot_length) |
---|
136 | |
---|
137 | result.x = numpy.append(self.x, other.x) |
---|
138 | #argsorting |
---|
139 | ind = numpy.argsort(result.x) |
---|
140 | result.x = result.x[ind] |
---|
141 | result.y = numpy.append(self.y, other.y) |
---|
142 | result.y = result.y[ind] |
---|
143 | if result.dy != None: |
---|
144 | result.dy = numpy.append(self.dy, other.dy) |
---|
145 | result.dy = result.dy[ind] |
---|
146 | if result.dx is not None: |
---|
147 | result.dx = numpy.append(self.dx, other.dx) |
---|
148 | result.dx = result.dx[ind] |
---|
149 | if result.dxw is not None: |
---|
150 | result.dxw = numpy.append(self.dxw, other.dxw) |
---|
151 | result.dxw = result.dxw[ind] |
---|
152 | if result.dxl is not None: |
---|
153 | result.dxl = numpy.append(self.dxl, other.dxl) |
---|
154 | result.dxl = result.dxl[ind] |
---|
155 | return result |
---|
156 | |
---|
157 | class Data2D(PlottableData2D, LoadData2D): |
---|
158 | """ |
---|
159 | """ |
---|
160 | def __init__(self, image=None, err_image=None, |
---|
161 | qx_data=None, qy_data=None, q_data=None, |
---|
162 | mask=None, dqx_data=None, dqy_data=None, |
---|
163 | xmin=None, xmax=None, ymin=None, ymax=None, |
---|
164 | zmin=None, zmax=None): |
---|
165 | """ |
---|
166 | """ |
---|
167 | PlottableData2D.__init__(self, image=image, err_image=err_image, |
---|
168 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, |
---|
169 | zmin=zmin, zmax=zmax, qx_data=qx_data, |
---|
170 | qy_data=qy_data) |
---|
171 | |
---|
172 | LoadData2D.__init__(self, data=image, err_data=err_image, |
---|
173 | qx_data=qx_data, qy_data=qy_data, |
---|
174 | dqx_data=dqx_data, dqy_data=dqy_data, |
---|
175 | q_data=q_data, mask=mask) |
---|
176 | self.id = None |
---|
177 | self.list_group_id = [] |
---|
178 | self.group_id = None |
---|
179 | self.is_data = True |
---|
180 | self.path = None |
---|
181 | self.xtransform = None |
---|
182 | self.ytransform = None |
---|
183 | self.title = "" |
---|
184 | self.scale = None |
---|
185 | |
---|
186 | def copy_from_datainfo(self, data2d): |
---|
187 | """ |
---|
188 | copy value of Data2D of type DataLoader.data_info |
---|
189 | """ |
---|
190 | self.data = copy.deepcopy(data2d.data) |
---|
191 | self.qx_data = copy.deepcopy(data2d.qx_data) |
---|
192 | self.qy_data = copy.deepcopy(data2d.qy_data) |
---|
193 | self.q_data = copy.deepcopy(data2d.q_data) |
---|
194 | self.mask = copy.deepcopy(data2d.mask) |
---|
195 | self.err_data = copy.deepcopy(data2d.err_data) |
---|
196 | self.x_bins = copy.deepcopy(data2d.x_bins) |
---|
197 | self.y_bins = copy.deepcopy(data2d.y_bins) |
---|
198 | if data2d.dqx_data is not None: |
---|
199 | self.dqx_data = copy.deepcopy(data2d.dqx_data) |
---|
200 | if data2d.dqy_data is not None: |
---|
201 | self.dqy_data = copy.deepcopy(data2d.dqy_data) |
---|
202 | self.xmin = data2d.xmin |
---|
203 | self.xmax = data2d.xmax |
---|
204 | self.ymin = data2d.ymin |
---|
205 | self.ymax = data2d.ymax |
---|
206 | if hasattr(data2d, "zmin"): |
---|
207 | self.zmin = data2d.zmin |
---|
208 | if hasattr(data2d, "zmax"): |
---|
209 | self.zmax = data2d.zmax |
---|
210 | self.xaxis(data2d._xaxis, data2d._xunit) |
---|
211 | self.yaxis(data2d._yaxis, data2d._yunit) |
---|
212 | self.title = data2d.title |
---|
213 | |
---|
214 | def __str__(self): |
---|
215 | """ |
---|
216 | print data |
---|
217 | """ |
---|
218 | _str = "%s\n" % LoadData2D.__str__(self) |
---|
219 | return _str |
---|
220 | |
---|
221 | def _perform_operation(self, other, operation): |
---|
222 | """ |
---|
223 | Perform 2D operations between data sets |
---|
224 | |
---|
225 | :param other: other data set |
---|
226 | :param operation: function defining the operation |
---|
227 | |
---|
228 | """ |
---|
229 | # First, check the data compatibility |
---|
230 | dy, dy_other = self._validity_check(other) |
---|
231 | result = Data2D(image=None, qx_data=None, qy_data=None, |
---|
232 | q_data=None, err_image=None, xmin=None, xmax=None, |
---|
233 | ymin=None, ymax=None, zmin=None, zmax=None) |
---|
234 | result.clone_without_data(len(self.data)) |
---|
235 | result.copy_from_datainfo(data2d=self) |
---|
236 | result.xmin = self.xmin |
---|
237 | result.xmax = self.xmax |
---|
238 | result.ymin = self.ymin |
---|
239 | result.ymax = self.ymax |
---|
240 | if self.dqx_data == None or self.dqy_data == None: |
---|
241 | result.dqx_data = None |
---|
242 | result.dqy_data = None |
---|
243 | else: |
---|
244 | result.dqx_data = numpy.zeros(len(self.data)) |
---|
245 | result.dqy_data = numpy.zeros(len(self.data)) |
---|
246 | for i in range(numpy.size(self.data)): |
---|
247 | result.data[i] = self.data[i] |
---|
248 | if self.err_data is not None and \ |
---|
249 | numpy.size(self.data) == numpy.size(self.err_data): |
---|
250 | result.err_data[i] = self.err_data[i] |
---|
251 | if self.dqx_data is not None: |
---|
252 | result.dqx_data[i] = self.dqx_data[i] |
---|
253 | if self.dqy_data is not None: |
---|
254 | result.dqy_data[i] = self.dqy_data[i] |
---|
255 | result.qx_data[i] = self.qx_data[i] |
---|
256 | result.qy_data[i] = self.qy_data[i] |
---|
257 | result.q_data[i] = self.q_data[i] |
---|
258 | result.mask[i] = self.mask[i] |
---|
259 | |
---|
260 | a = Uncertainty(self.data[i], dy[i]**2) |
---|
261 | if isinstance(other, Data2D): |
---|
262 | b = Uncertainty(other.data[i], dy_other[i]**2) |
---|
263 | if other.dqx_data is not None and \ |
---|
264 | result.dqx_data is not None: |
---|
265 | result.dqx_data[i] *= self.dqx_data[i] |
---|
266 | result.dqx_data[i] += (other.dqx_data[i]**2) |
---|
267 | result.dqx_data[i] /= 2 |
---|
268 | result.dqx_data[i] = math.sqrt(result.dqx_data[i]) |
---|
269 | if other.dqy_data is not None and \ |
---|
270 | result.dqy_data is not None: |
---|
271 | result.dqy_data[i] *= self.dqy_data[i] |
---|
272 | result.dqy_data[i] += (other.dqy_data[i]**2) |
---|
273 | result.dqy_data[i] /= 2 |
---|
274 | result.dqy_data[i] = math.sqrt(result.dqy_data[i]) |
---|
275 | else: |
---|
276 | b = other |
---|
277 | |
---|
278 | output = operation(a, b) |
---|
279 | result.data[i] = output.x |
---|
280 | result.err_data[i] = math.sqrt(math.fabs(output.variance)) |
---|
281 | return result |
---|
282 | |
---|
283 | def _perform_union(self, other): |
---|
284 | """ |
---|
285 | Perform 2D operations between data sets |
---|
286 | |
---|
287 | :param other: other data set |
---|
288 | :param operation: function defining the operation |
---|
289 | |
---|
290 | """ |
---|
291 | # First, check the data compatibility |
---|
292 | self._validity_check_union(other) |
---|
293 | result = Data2D(image=None, qx_data=None, qy_data=None, |
---|
294 | q_data=None, err_image=None, xmin=None, xmax=None, |
---|
295 | ymin=None, ymax=None, zmin=None, zmax=None) |
---|
296 | length = len(self.data) |
---|
297 | tot_length = length + len(other.data) |
---|
298 | result.clone_without_data(tot_length) |
---|
299 | result.xmin = self.xmin |
---|
300 | result.xmax = self.xmax |
---|
301 | result.ymin = self.ymin |
---|
302 | result.ymax = self.ymax |
---|
303 | if self.dqx_data == None or self.dqy_data == None or \ |
---|
304 | other.dqx_data == None or other.dqy_data == None : |
---|
305 | result.dqx_data = None |
---|
306 | result.dqy_data = None |
---|
307 | else: |
---|
308 | result.dqx_data = numpy.zeros(len(self.data) + \ |
---|
309 | numpy.size(other.data)) |
---|
310 | result.dqy_data = numpy.zeros(len(self.data) + \ |
---|
311 | numpy.size(other.data)) |
---|
312 | |
---|
313 | result.data = numpy.append(self.data, other.data) |
---|
314 | result.qx_data = numpy.append(self.qx_data, other.qx_data) |
---|
315 | result.qy_data = numpy.append(self.qy_data, other.qy_data) |
---|
316 | result.q_data = numpy.append(self.q_data, other.q_data) |
---|
317 | result.mask = numpy.append(self.mask, other.mask) |
---|
318 | if result.err_data is not None: |
---|
319 | result.err_data = numpy.append(self.err_data, other.err_data) |
---|
320 | if self.dqx_data is not None: |
---|
321 | result.dqx_data = numpy.append(self.dqx_data, other.dqx_data) |
---|
322 | if self.dqy_data is not None: |
---|
323 | result.dqy_data = numpy.append(self.dqy_data, other.dqy_data) |
---|
324 | |
---|
325 | return result |
---|
326 | |
---|
327 | def check_data_validity(data): |
---|
328 | """ |
---|
329 | Return True is data is valid enough to compute chisqr, else False |
---|
330 | """ |
---|
331 | flag = True |
---|
332 | if data is not None: |
---|
333 | if issubclass(data.__class__, Data2D): |
---|
334 | if (data.data is None) or (len(data.data) == 0)\ |
---|
335 | or (len(data.err_data) == 0): |
---|
336 | flag = False |
---|
337 | else: |
---|
338 | if (data.y is None) or (len(data.y) == 0): |
---|
339 | flag = False |
---|
340 | if not data.is_data: |
---|
341 | flag = False |
---|
342 | else: |
---|
343 | flag = False |
---|
344 | return flag |
---|