source: sasview/src/sas/sasgui/guiframe/dataFitting.py @ 51a4d78

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 51a4d78 was 51a4d78, checked in by jhbakker, 8 years ago

Merge branch 'ajj_sesans' into Jurrian1D

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