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

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 a9f579c was a9f579c, checked in by jhbakker, 7 years ago

Manually added in all the SESANS modifications from Jurtest

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