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 | from sas.sasgui.plottools.plottables import Data1D as PlotData1D |
---|
9 | from sas.sasgui.plottools.plottables import Data2D as PlotData2D |
---|
10 | from sas.sasgui.plottools.plottables import Theory1D as PlotTheory1D |
---|
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(PlotData1D, 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 | PlotData1D.__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 | |
---|
158 | |
---|
159 | class Theory1D(PlotTheory1D, LoadData1D): |
---|
160 | """ |
---|
161 | """ |
---|
162 | def __init__(self, x=None, y=None, dy=None): |
---|
163 | """ |
---|
164 | """ |
---|
165 | if x is None: |
---|
166 | x = [] |
---|
167 | if y is None: |
---|
168 | y = [] |
---|
169 | PlotTheory1D.__init__(self, x, y, dy) |
---|
170 | LoadData1D.__init__(self, x, y, dy) |
---|
171 | self.id = None |
---|
172 | self.list_group_id = [] |
---|
173 | self.group_id = None |
---|
174 | self.is_data = True |
---|
175 | self.path = None |
---|
176 | self.xtransform = None |
---|
177 | self.ytransform = None |
---|
178 | self.title = "" |
---|
179 | self.scale = None |
---|
180 | |
---|
181 | def copy_from_datainfo(self, data1d): |
---|
182 | """ |
---|
183 | copy values of Data1D of type DataLaoder.Data_info |
---|
184 | """ |
---|
185 | self.x = copy.deepcopy(data1d.x) |
---|
186 | self.y = copy.deepcopy(data1d.y) |
---|
187 | self.dy = copy.deepcopy(data1d.dy) |
---|
188 | if hasattr(data1d, "dx"): |
---|
189 | self.dx = copy.deepcopy(data1d.dx) |
---|
190 | if hasattr(data1d, "dxl"): |
---|
191 | self.dxl = copy.deepcopy(data1d.dxl) |
---|
192 | if hasattr(data1d, "dxw"): |
---|
193 | self.dxw = copy.deepcopy(data1d.dxw) |
---|
194 | self.xaxis(data1d._xaxis, data1d._xunit) |
---|
195 | self.yaxis(data1d._yaxis, data1d._yunit) |
---|
196 | self.title = data1d.title |
---|
197 | |
---|
198 | def __str__(self): |
---|
199 | """ |
---|
200 | print data |
---|
201 | """ |
---|
202 | _str = "%s\n" % LoadData1D.__str__(self) |
---|
203 | |
---|
204 | return _str |
---|
205 | |
---|
206 | def _perform_operation(self, other, operation): |
---|
207 | """ |
---|
208 | """ |
---|
209 | # First, check the data compatibility |
---|
210 | dy, dy_other = self._validity_check(other) |
---|
211 | result = self.clone_without_data(len(self.x)) |
---|
212 | result.copy_from_datainfo(data1d=self) |
---|
213 | if self.dxw == None: |
---|
214 | result.dxw = None |
---|
215 | else: |
---|
216 | result.dxw = numpy.zeros(len(self.x)) |
---|
217 | if self.dxl == None: |
---|
218 | result.dxl = None |
---|
219 | else: |
---|
220 | result.dxl = numpy.zeros(len(self.x)) |
---|
221 | |
---|
222 | for i in range(numpy.size(self.x)): |
---|
223 | result.x[i] = self.x[i] |
---|
224 | if self.dx is not None and len(self.x) == len(self.dx): |
---|
225 | result.dx[i] = self.dx[i] |
---|
226 | if self.dxw is not None and len(self.x) == len(self.dxw): |
---|
227 | result.dxw[i] = self.dxw[i] |
---|
228 | if self.dxl is not None and len(self.x) == len(self.dxl): |
---|
229 | result.dxl[i] = self.dxl[i] |
---|
230 | |
---|
231 | a = Uncertainty(self.y[i], dy[i]**2) |
---|
232 | if isinstance(other, Data1D): |
---|
233 | b = Uncertainty(other.y[i], dy_other[i]**2) |
---|
234 | if other.dx is not None: |
---|
235 | result.dx[i] *= self.dx[i] |
---|
236 | result.dx[i] += (other.dx[i]**2) |
---|
237 | result.dx[i] /= 2 |
---|
238 | result.dx[i] = math.sqrt(result.dx[i]) |
---|
239 | if result.dxl is not None and other.dxl is not None: |
---|
240 | result.dxl[i] *= self.dxl[i] |
---|
241 | other.dxl[i] += (other.dxl[i]**2) |
---|
242 | result.dxl[i] /= 2 |
---|
243 | result.dxl[i] = math.sqrt(result.dxl[i]) |
---|
244 | if result.dxw is not None and self.dxw is not None: |
---|
245 | result.dxw[i] *= self.dxw[i] |
---|
246 | other.dxw[i] += (other.dxw[i]**2) |
---|
247 | result.dxw[i] /= 2 |
---|
248 | result.dxw[i] = math.sqrt(result.dxw[i]) |
---|
249 | else: |
---|
250 | b = other |
---|
251 | |
---|
252 | output = operation(a, b) |
---|
253 | result.y[i] = output.x |
---|
254 | result.dy[i] = math.sqrt(math.fabs(output.variance)) |
---|
255 | return result |
---|
256 | |
---|
257 | def _perform_union(self, other): |
---|
258 | """ |
---|
259 | """ |
---|
260 | # First, check the data compatibility |
---|
261 | self._validity_check_union(other) |
---|
262 | result = Data1D(x=[], y=[], dx=None, dy=None) |
---|
263 | tot_length = len(self.x)+len(other.x) |
---|
264 | result.clone_without_data(length=tot_length, clone=self) |
---|
265 | if self.dy == None or other.dy is None: |
---|
266 | result.dy = None |
---|
267 | else: |
---|
268 | result.dy = numpy.zeros(tot_length) |
---|
269 | if self.dx == None or other.dx is None: |
---|
270 | result.dx = None |
---|
271 | else: |
---|
272 | result.dx = numpy.zeros(tot_length) |
---|
273 | if self.dxw == None or other.dxw is None: |
---|
274 | result.dxw = None |
---|
275 | else: |
---|
276 | result.dxw = numpy.zeros(tot_length) |
---|
277 | if self.dxl == None or other.dxl is None: |
---|
278 | result.dxl = None |
---|
279 | else: |
---|
280 | result.dxl = numpy.zeros(tot_length) |
---|
281 | result.x = numpy.append(self.x, other.x) |
---|
282 | #argsorting |
---|
283 | ind = numpy.argsort(result.x) |
---|
284 | result.x = result.x[ind] |
---|
285 | result.y = numpy.append(self.y, other.y) |
---|
286 | result.y = result.y[ind] |
---|
287 | if result.dy != None: |
---|
288 | result.dy = numpy.append(self.dy, other.dy) |
---|
289 | result.dy = result.dy[ind] |
---|
290 | if result.dx is not None: |
---|
291 | result.dx = numpy.append(self.dx, other.dx) |
---|
292 | result.dx = result.dx[ind] |
---|
293 | if result.dxw is not None: |
---|
294 | result.dxw = numpy.append(self.dxw, other.dxw) |
---|
295 | result.dxw = result.dxw[ind] |
---|
296 | if result.dxl is not None: |
---|
297 | result.dxl = numpy.append(self.dxl, other.dxl) |
---|
298 | result.dxl = result.dxl[ind] |
---|
299 | return result |
---|
300 | |
---|
301 | |
---|
302 | class Data2D(PlotData2D, LoadData2D): |
---|
303 | """ |
---|
304 | """ |
---|
305 | def __init__(self, image=None, err_image=None, |
---|
306 | qx_data=None, qy_data=None, q_data=None, |
---|
307 | mask=None, dqx_data=None, dqy_data=None, |
---|
308 | xmin=None, xmax=None, ymin=None, ymax=None, |
---|
309 | zmin=None, zmax=None): |
---|
310 | """ |
---|
311 | """ |
---|
312 | PlotData2D.__init__(self, image=image, err_image=err_image, |
---|
313 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, |
---|
314 | zmin=zmin, zmax=zmax, qx_data=qx_data, |
---|
315 | qy_data=qy_data) |
---|
316 | |
---|
317 | LoadData2D.__init__(self, data=image, err_data=err_image, |
---|
318 | qx_data=qx_data, qy_data=qy_data, |
---|
319 | dqx_data=dqx_data, dqy_data=dqy_data, |
---|
320 | q_data=q_data, mask=mask) |
---|
321 | self.id = None |
---|
322 | self.list_group_id = [] |
---|
323 | self.group_id = None |
---|
324 | self.is_data = True |
---|
325 | self.path = None |
---|
326 | self.xtransform = None |
---|
327 | self.ytransform = None |
---|
328 | self.title = "" |
---|
329 | self.scale = None |
---|
330 | |
---|
331 | def copy_from_datainfo(self, data2d): |
---|
332 | """ |
---|
333 | copy value of Data2D of type DataLoader.data_info |
---|
334 | """ |
---|
335 | self.data = copy.deepcopy(data2d.data) |
---|
336 | self.qx_data = copy.deepcopy(data2d.qx_data) |
---|
337 | self.qy_data = copy.deepcopy(data2d.qy_data) |
---|
338 | self.q_data = copy.deepcopy(data2d.q_data) |
---|
339 | self.mask = copy.deepcopy(data2d.mask) |
---|
340 | self.err_data = copy.deepcopy(data2d.err_data) |
---|
341 | self.x_bins = copy.deepcopy(data2d.x_bins) |
---|
342 | self.y_bins = copy.deepcopy(data2d.y_bins) |
---|
343 | if data2d.dqx_data is not None: |
---|
344 | self.dqx_data = copy.deepcopy(data2d.dqx_data) |
---|
345 | if data2d.dqy_data is not None: |
---|
346 | self.dqy_data = copy.deepcopy(data2d.dqy_data) |
---|
347 | self.xmin = data2d.xmin |
---|
348 | self.xmax = data2d.xmax |
---|
349 | self.ymin = data2d.ymin |
---|
350 | self.ymax = data2d.ymax |
---|
351 | if hasattr(data2d, "zmin"): |
---|
352 | self.zmin = data2d.zmin |
---|
353 | if hasattr(data2d, "zmax"): |
---|
354 | self.zmax = data2d.zmax |
---|
355 | self.xaxis(data2d._xaxis, data2d._xunit) |
---|
356 | self.yaxis(data2d._yaxis, data2d._yunit) |
---|
357 | self.title = data2d.title |
---|
358 | |
---|
359 | def __str__(self): |
---|
360 | """ |
---|
361 | print data |
---|
362 | """ |
---|
363 | _str = "%s\n" % LoadData2D.__str__(self) |
---|
364 | return _str |
---|
365 | |
---|
366 | def _perform_operation(self, other, operation): |
---|
367 | """ |
---|
368 | Perform 2D operations between data sets |
---|
369 | |
---|
370 | :param other: other data set |
---|
371 | :param operation: function defining the operation |
---|
372 | |
---|
373 | """ |
---|
374 | # First, check the data compatibility |
---|
375 | dy, dy_other = self._validity_check(other) |
---|
376 | result = Data2D(image=None, qx_data=None, qy_data=None, |
---|
377 | q_data=None, err_image=None, xmin=None, xmax=None, |
---|
378 | ymin=None, ymax=None, zmin=None, zmax=None) |
---|
379 | result.clone_without_data(len(self.data)) |
---|
380 | result.copy_from_datainfo(data2d=self) |
---|
381 | result.xmin = self.xmin |
---|
382 | result.xmax = self.xmax |
---|
383 | result.ymin = self.ymin |
---|
384 | result.ymax = self.ymax |
---|
385 | if self.dqx_data == None or self.dqy_data == None: |
---|
386 | result.dqx_data = None |
---|
387 | result.dqy_data = None |
---|
388 | else: |
---|
389 | result.dqx_data = numpy.zeros(len(self.data)) |
---|
390 | result.dqy_data = numpy.zeros(len(self.data)) |
---|
391 | for i in range(numpy.size(self.data)): |
---|
392 | result.data[i] = self.data[i] |
---|
393 | if self.err_data is not None and \ |
---|
394 | numpy.size(self.data) == numpy.size(self.err_data): |
---|
395 | result.err_data[i] = self.err_data[i] |
---|
396 | if self.dqx_data is not None: |
---|
397 | result.dqx_data[i] = self.dqx_data[i] |
---|
398 | if self.dqy_data is not None: |
---|
399 | result.dqy_data[i] = self.dqy_data[i] |
---|
400 | result.qx_data[i] = self.qx_data[i] |
---|
401 | result.qy_data[i] = self.qy_data[i] |
---|
402 | result.q_data[i] = self.q_data[i] |
---|
403 | result.mask[i] = self.mask[i] |
---|
404 | |
---|
405 | a = Uncertainty(self.data[i], dy[i]**2) |
---|
406 | if isinstance(other, Data2D): |
---|
407 | b = Uncertainty(other.data[i], dy_other[i]**2) |
---|
408 | if other.dqx_data is not None and \ |
---|
409 | result.dqx_data is not None: |
---|
410 | result.dqx_data[i] *= self.dqx_data[i] |
---|
411 | result.dqx_data[i] += (other.dqx_data[i]**2) |
---|
412 | result.dqx_data[i] /= 2 |
---|
413 | result.dqx_data[i] = math.sqrt(result.dqx_data[i]) |
---|
414 | if other.dqy_data is not None and \ |
---|
415 | result.dqy_data is not None: |
---|
416 | result.dqy_data[i] *= self.dqy_data[i] |
---|
417 | result.dqy_data[i] += (other.dqy_data[i]**2) |
---|
418 | result.dqy_data[i] /= 2 |
---|
419 | result.dqy_data[i] = math.sqrt(result.dqy_data[i]) |
---|
420 | else: |
---|
421 | b = other |
---|
422 | |
---|
423 | output = operation(a, b) |
---|
424 | result.data[i] = output.x |
---|
425 | result.err_data[i] = math.sqrt(math.fabs(output.variance)) |
---|
426 | return result |
---|
427 | |
---|
428 | def _perform_union(self, other): |
---|
429 | """ |
---|
430 | Perform 2D operations between data sets |
---|
431 | |
---|
432 | :param other: other data set |
---|
433 | :param operation: function defining the operation |
---|
434 | |
---|
435 | """ |
---|
436 | # First, check the data compatibility |
---|
437 | self._validity_check_union(other) |
---|
438 | result = Data2D(image=None, qx_data=None, qy_data=None, |
---|
439 | q_data=None, err_image=None, xmin=None, xmax=None, |
---|
440 | ymin=None, ymax=None, zmin=None, zmax=None) |
---|
441 | length = len(self.data) |
---|
442 | tot_length = length + len(other.data) |
---|
443 | result.clone_without_data(tot_length) |
---|
444 | result.xmin = self.xmin |
---|
445 | result.xmax = self.xmax |
---|
446 | result.ymin = self.ymin |
---|
447 | result.ymax = self.ymax |
---|
448 | if self.dqx_data == None or self.dqy_data == None or \ |
---|
449 | other.dqx_data == None or other.dqy_data == None : |
---|
450 | result.dqx_data = None |
---|
451 | result.dqy_data = None |
---|
452 | else: |
---|
453 | result.dqx_data = numpy.zeros(len(self.data) + \ |
---|
454 | numpy.size(other.data)) |
---|
455 | result.dqy_data = numpy.zeros(len(self.data) + \ |
---|
456 | numpy.size(other.data)) |
---|
457 | |
---|
458 | result.data = numpy.append(self.data, other.data) |
---|
459 | result.qx_data = numpy.append(self.qx_data, other.qx_data) |
---|
460 | result.qy_data = numpy.append(self.qy_data, other.qy_data) |
---|
461 | result.q_data = numpy.append(self.q_data, other.q_data) |
---|
462 | result.mask = numpy.append(self.mask, other.mask) |
---|
463 | if result.err_data is not None: |
---|
464 | result.err_data = numpy.append(self.err_data, other.err_data) |
---|
465 | if self.dqx_data is not None: |
---|
466 | result.dqx_data = numpy.append(self.dqx_data, other.dqx_data) |
---|
467 | if self.dqy_data is not None: |
---|
468 | result.dqy_data = numpy.append(self.dqy_data, other.dqy_data) |
---|
469 | |
---|
470 | return result |
---|
471 | |
---|
472 | def check_data_validity(data): |
---|
473 | """ |
---|
474 | Return True is data is valid enough to compute chisqr, else False |
---|
475 | """ |
---|
476 | flag = True |
---|
477 | if data is not None: |
---|
478 | if issubclass(data.__class__, Data2D): |
---|
479 | if (data.data is None) or (len(data.data) == 0)\ |
---|
480 | or (len(data.err_data) == 0): |
---|
481 | flag = False |
---|
482 | else: |
---|
483 | if (data.y is None) or (len(data.y) == 0): |
---|
484 | flag = False |
---|
485 | if not data.is_data: |
---|
486 | flag = False |
---|
487 | else: |
---|
488 | flag = False |
---|
489 | return flag |
---|