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

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 f2724b6 was 1fac6c0, checked in by jhbakker, 8 years ago

SESANS is almost working, but this is NOT a stable version!

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