source: sasview/src/sas/qtgui/Perspectives/Fitting/FittingLogic.py @ 7248d75d

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 7248d75d was 7248d75d, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

More unit tests for fitting perspective components - SASVIEW-499

  • Property mode set to 100755
File size: 6.9 KB
Line 
1import numpy
2
3from sas.sasgui.guiframe.dataFitting import Data1D
4from sas.sasgui.guiframe.dataFitting import Data2D
5from sas.sascalc.dataloader.data_info import Detector
6from sas.sascalc.dataloader.data_info import Source
7
8
9class FittingLogic(object):
10    """
11    All the data-related logic. This class deals exclusively with Data1D/2D
12    No QStandardModelIndex here.
13    """
14    def __init__(self, data=None):
15        self._data = data
16        self.data_is_loaded = False
17        if data is not None:
18            self.data_is_loaded = True
19
20    @property
21    def data(self):
22        return self._data
23
24    @data.setter
25    def data(self, value):
26        """ data setter """
27        self._data = value
28        self.data_is_loaded = True
29
30    def createDefault1dData(self, interval, tab_id=0):
31        """
32        Create default data for fitting perspective
33        Only when the page is on theory mode.
34        """
35        self._data = Data1D(x=interval)
36        self._data.xaxis('\\rm{Q}', "A^{-1}")
37        self._data.yaxis('\\rm{Intensity}', "cm^{-1}")
38        self._data.is_data = False
39        self._data.id = str(tab_id) + " data"
40        self._data.group_id = str(tab_id) + " Model1D"
41
42    def createDefault2dData(self, qmax, qstep, tab_id=0):
43        """
44        Create 2D data by default
45        Only when the page is on theory mode.
46        """
47        self._data = Data2D()
48        self._data.xaxis('\\rm{Q_{x}}', 'A^{-1}')
49        self._data.yaxis('\\rm{Q_{y}}', 'A^{-1}')
50        self._data.is_data = False
51        self._data.id = str(tab_id) + " data"
52        self._data.group_id = str(tab_id) + " Model2D"
53
54        # Default detector
55        self._data.detector.append(Detector())
56        index = len(self._data.detector) - 1
57        self._data.detector[index].distance = 8000   # mm
58        self._data.source.wavelength = 6             # A
59        self._data.detector[index].pixel_size.x = 5  # mm
60        self._data.detector[index].pixel_size.y = 5  # mm
61        self._data.detector[index].beam_center.x = qmax
62        self._data.detector[index].beam_center.y = qmax
63        # theory default: assume the beam
64        #center is located at the center of sqr detector
65        xmax = qmax
66        xmin = -qmax
67        ymax = qmax
68        ymin = -qmax
69
70        x = numpy.linspace(start=xmin, stop=xmax, num=qstep, endpoint=True)
71        y = numpy.linspace(start=ymin, stop=ymax, num=qstep, endpoint=True)
72        # Use data info instead
73        new_x = numpy.tile(x, (len(y), 1))
74        new_y = numpy.tile(y, (len(x), 1))
75        new_y = new_y.swapaxes(0, 1)
76
77        # all data required in 1d array
78        qx_data = new_x.flatten()
79        qy_data = new_y.flatten()
80        q_data = numpy.sqrt(qx_data * qx_data + qy_data * qy_data)
81
82        # set all True (standing for unmasked) as default
83        mask = numpy.ones(len(qx_data), dtype=bool)
84        # calculate the range of qx and qy: this way,
85        # it is a little more independent
86        # store x and y bin centers in q space
87        x_bins = x
88        y_bins = y
89
90        self._data.source = Source()
91        self._data.data = numpy.ones(len(mask))
92        self._data.err_data = numpy.ones(len(mask))
93        self._data.qx_data = qx_data
94        self._data.qy_data = qy_data
95        self._data.q_data = q_data
96        self._data.mask = mask
97        self._data.x_bins = x_bins
98        self._data.y_bins = y_bins
99        # max and min taking account of the bin sizes
100        self._data.xmin = xmin
101        self._data.xmax = xmax
102        self._data.ymin = ymin
103        self._data.ymax = ymax
104
105    def new1DPlot(self, return_data):
106        """
107        Create a new 1D data instance based on fitting results
108        """
109        # Unpack return data from Calc1D
110        x, y, page_id, state, weight,\
111        fid, toggle_mode_on, \
112        elapsed, index, model,\
113        data, update_chisqr, source = return_data
114
115        # Create the new plot
116        new_plot = Data1D(x=x, y=y)
117        new_plot.is_data = False
118        new_plot.dy = numpy.zeros(len(y))
119        _yaxis, _yunit = data.get_yaxis()
120        _xaxis, _xunit = data.get_xaxis()
121        new_plot.title = data.name
122
123        new_plot.group_id = data.group_id
124        #new_plot.id = str(self.tab_id) + " " + data.name
125        new_plot.name = model.name + " [" + data.name + "]"
126        new_plot.xaxis(_xaxis, _xunit)
127        new_plot.yaxis(_yaxis, _yunit)
128
129        # Assign the new Data1D object-wide
130        self._data = new_plot
131
132    def new2DPlot(self, return_data):
133        """
134        Create a new 2D data instance based on fitting results
135        """
136
137        image, data, page_id, model, state, toggle_mode_on,\
138        elapsed, index, fid, qmin, qmax, weight, \
139        update_chisqr, source = return_data
140
141        numpy.nan_to_num(image)
142        new_plot = Data2D(image=image, err_image=data.err_data)
143        new_plot.name = model.name + '2d'
144        new_plot.title = "Analytical model 2D "
145        new_plot.id = str(page_id) + " " + data.name
146        new_plot.group_id = str(page_id) + " Model2D"
147        new_plot.detector = data.detector
148        new_plot.source = data.source
149        new_plot.is_data = False
150        new_plot.qx_data = data.qx_data
151        new_plot.qy_data = data.qy_data
152        new_plot.q_data = data.q_data
153        new_plot.mask = data.mask
154        ## plot boundaries
155        new_plot.ymin = data.ymin
156        new_plot.ymax = data.ymax
157        new_plot.xmin = data.xmin
158        new_plot.xmax = data.xmax
159
160        title = data.title
161
162        new_plot.is_data = False
163        if data.is_data:
164            data_name = str(data.name)
165        else:
166            data_name = str(model.__class__.__name__) + '2d'
167
168        if len(title) > 1:
169            new_plot.title = "Model2D for %s " % model.name + data_name
170        new_plot.name = model.name + " [" + \
171                                    data_name + "]"
172
173        # Assign the new Data2D object-wide
174        self._data = new_plot
175
176    def computeDataRange(self):
177        """
178        Compute the minimum and the maximum range of the data
179        return the npts contains in data
180        """
181        qmin, qmax, npts = None, None, None
182        if isinstance(self.data, Data1D):
183            try:
184                qmin = min(self.data.x)
185                qmax = max(self.data.x)
186                npts = len(self.data.x)
187            except (ValueError, TypeError):
188                msg = "Unable to find min/max/length of \n data named %s" % \
189                            self.data.filename
190                raise ValueError, msg
191
192        else:
193            qmin = 0
194            try:
195                x = max(numpy.fabs(self.data.xmin), numpy.fabs(self.data.xmax))
196                y = max(numpy.fabs(self.data.ymin), numpy.fabs(self.data.ymax))
197            except (ValueError, TypeError):
198                msg = "Unable to find min/max of \n data named %s" % \
199                            self.data.filename
200                raise ValueError, msg
201            qmax = numpy.sqrt(x * x + y * y)
202            npts = len(self.data.data)
203        return qmin, qmax, npts
Note: See TracBrowser for help on using the repository browser.