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

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.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since e68c9bf was d85c194, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 8 years ago

Remaining modules refactored

  • Property mode set to 100644
File size: 20.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):
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.= 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=[], 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   
159class 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.= copy.deepcopy(data1d.x)
186        self.= 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     
302class 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 _validity_check(self, other):
367        """
368        Checks that the data lengths are compatible.
369        Checks that the x vectors are compatible.
370        Returns errors vectors equal to original
371        errors vectors if they were present or vectors
372        of zeros when none was found.
373       
374        :param other: other data set for operation
375       
376        :return: dy for self, dy for other [numpy arrays]
377       
378        :raise ValueError: when lengths are not compatible
379       
380        """
381        err_other = None
382        if isinstance(other, Data2D):
383            # Check that data lengths are the same
384            if len(self.data) != len(other.data) or \
385                len(self.qx_data) != len(other.qx_data) or \
386                len(self.qy_data) != len(other.qy_data):
387                msg = "Unable to perform operation: data length are not equal"
388                raise ValueError, msg
389            #if len(self.data) < 1:
390            #    msg = "Incompatible data sets: I-values do not match"
391            #    raise ValueError, msg
392            for ind in range(len(self.data)):
393                if self.qx_data[ind] != other.qx_data[ind]:
394                    msg = "Incompatible data sets: qx-values do not match"
395                    raise ValueError, msg
396                if self.qy_data[ind] != other.qy_data[ind]:
397                    msg = "Incompatible data sets: qy-values do not match"
398                    raise ValueError, msg
399                   
400            # Check that the scales match
401            err_other = other.err_data
402            if other.err_data == None or \
403                (len(other.err_data) != len(other.data)):
404                err_other = numpy.zeros(len(other.data))
405           
406        # Check that we have errors, otherwise create zero vector
407        err = self.err_data
408        if self.err_data == None or \
409            (len(self.err_data) != len(self.data)):
410            err = numpy.zeros(len(other.data))
411           
412        return err, err_other
413
414    def _validity_check_union(self, other):
415        """
416        Checks that the data lengths are compatible.
417        Checks that the x vectors are compatible.
418        Returns errors vectors equal to original
419        errors vectors if they were present or vectors
420        of zeros when none was found.
421       
422        :param other: other data set for operation
423       
424        :return: bool
425       
426        :raise ValueError: when data types are not compatible
427       
428        """
429        if not isinstance(other, Data2D):
430            msg = "Unable to perform operation: different types of data set"
431            raise ValueError, msg   
432        return True
433   
434    def _perform_operation(self, other, operation):
435        """
436        Perform 2D operations between data sets
437       
438        :param other: other data set
439        :param operation: function defining the operation
440       
441        """
442        # First, check the data compatibility
443        dy, dy_other = self._validity_check(other)
444        result = Data2D(image=None, qx_data=None, qy_data=None,
445                         q_data=None, err_image=None, xmin=None, xmax=None,
446                         ymin=None, ymax=None, zmin=None, zmax=None)
447        result.clone_without_data(len(self.data))
448        result.copy_from_datainfo(data2d=self)
449        result.xmin = self.xmin
450        result.xmax = self.xmax
451        result.ymin = self.ymin
452        result.ymax = self.ymax
453        if self.dqx_data == None or self.dqy_data == None:
454            result.dqx_data = None
455            result.dqy_data = None
456        else:
457            result.dqx_data = numpy.zeros(len(self.data))
458            result.dqy_data = numpy.zeros(len(self.data))
459        for i in range(numpy.size(self.data)):
460            result.data[i] = self.data[i]
461            if self.err_data is not None and \
462                numpy.size(self.data) == numpy.size(self.err_data):
463                result.err_data[i] = self.err_data[i]   
464            if self.dqx_data is not None:
465                result.dqx_data[i] = self.dqx_data[i]
466            if self.dqy_data is not None:
467                result.dqy_data[i] = self.dqy_data[i]
468            result.qx_data[i] = self.qx_data[i]
469            result.qy_data[i] = self.qy_data[i]
470            result.q_data[i] = self.q_data[i]
471            result.mask[i] = self.mask[i]
472           
473            a = Uncertainty(self.data[i], dy[i]**2)
474            if isinstance(other, Data2D):
475                b = Uncertainty(other.data[i], dy_other[i]**2)
476                if other.dqx_data is not None and \
477                        result.dqx_data is not None:
478                    result.dqx_data[i] *= self.dqx_data[i]
479                    result.dqx_data[i] += (other.dqx_data[i]**2)
480                    result.dqx_data[i] /= 2
481                    result.dqx_data[i] = math.sqrt(result.dqx_data[i])     
482                if other.dqy_data is not None and \
483                        result.dqy_data is not None:
484                    result.dqy_data[i] *= self.dqy_data[i]
485                    result.dqy_data[i] += (other.dqy_data[i]**2)
486                    result.dqy_data[i] /= 2
487                    result.dqy_data[i] = math.sqrt(result.dqy_data[i])
488            else:
489                b = other
490           
491            output = operation(a, b)
492            result.data[i] = output.x
493            result.err_data[i] = math.sqrt(math.fabs(output.variance))
494        return result
495   
496    def _perform_union(self, other):
497        """
498        Perform 2D operations between data sets
499       
500        :param other: other data set
501        :param operation: function defining the operation
502       
503        """
504        # First, check the data compatibility
505        self._validity_check_union(other)
506        result = Data2D(image=None, qx_data=None, qy_data=None,
507                         q_data=None, err_image=None, xmin=None, xmax=None,
508                         ymin=None, ymax=None, zmin=None, zmax=None)
509        length = len(self.data)
510        tot_length = length + len(other.data)
511        result.clone_without_data(tot_length)
512        result.xmin = self.xmin
513        result.xmax = self.xmax
514        result.ymin = self.ymin
515        result.ymax = self.ymax
516        if self.dqx_data == None or self.dqy_data == None or \
517                other.dqx_data == None or other.dqy_data == None :
518            result.dqx_data = None
519            result.dqy_data = None
520        else:
521            result.dqx_data = numpy.zeros(len(self.data) + \
522                                         numpy.size(other.data))
523            result.dqy_data = numpy.zeros(len(self.data) + \
524                                         numpy.size(other.data))
525       
526        result.data = numpy.append(self.data, other.data)
527        result.qx_data = numpy.append(self.qx_data, other.qx_data)
528        result.qy_data = numpy.append(self.qy_data, other.qy_data)
529        result.q_data = numpy.append(self.q_data, other.q_data)
530        result.mask = numpy.append(self.mask, other.mask)
531        if result.err_data is not None:
532            result.err_data = numpy.append(self.err_data, other.err_data) 
533        if self.dqx_data is not None:
534            result.dqx_data = numpy.append(self.dqx_data, other.dqx_data)
535        if self.dqy_data is not None:
536            result.dqy_data = numpy.append(self.dqy_data, other.dqy_data)
537
538        return result
539       
540def check_data_validity(data):
541    """
542    Return True is data is valid enough to compute chisqr, else False
543    """
544    flag = True
545    if data is not None:
546        if issubclass(data.__class__, Data2D):
547            if (data.data is None) or (len(data.data) == 0)\
548            or (len(data.err_data) == 0):
549                flag = False
550        else:
551            if (data.y is None) or (len(data.y) == 0): 
552                flag = False
553        if not data.is_data:
554            flag = False
555    else:
556        flag = False
557    return flag
Note: See TracBrowser for help on using the repository browser.