[4d457df] | 1 | import numpy |
---|
| 2 | |
---|
| 3 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
| 4 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
| 5 | from sas.sascalc.dataloader.data_info import Detector |
---|
| 6 | from sas.sascalc.dataloader.data_info import Source |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | class 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 |
---|
[7248d75d] | 16 | self.data_is_loaded = False |
---|
| 17 | if data is not None: |
---|
| 18 | self.data_is_loaded = True |
---|
[4d457df] | 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 | |
---|
[7d077d1] | 105 | def new1DPlot(self, return_data, tab_id): |
---|
[4d457df] | 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 | |
---|
| 122 | new_plot.group_id = data.group_id |
---|
[7d077d1] | 123 | new_plot.id = str(tab_id) + " " + data.name |
---|
[4d457df] | 124 | new_plot.name = model.name + " [" + data.name + "]" |
---|
[0268aed] | 125 | new_plot.title = new_plot.name |
---|
[4d457df] | 126 | new_plot.xaxis(_xaxis, _xunit) |
---|
| 127 | new_plot.yaxis(_yaxis, _yunit) |
---|
| 128 | |
---|
[6fd4e36] | 129 | return new_plot |
---|
[4d457df] | 130 | |
---|
| 131 | def new2DPlot(self, return_data): |
---|
| 132 | """ |
---|
| 133 | Create a new 2D data instance based on fitting results |
---|
| 134 | """ |
---|
| 135 | image, data, page_id, model, state, toggle_mode_on,\ |
---|
| 136 | elapsed, index, fid, qmin, qmax, weight, \ |
---|
| 137 | update_chisqr, source = return_data |
---|
| 138 | |
---|
| 139 | numpy.nan_to_num(image) |
---|
| 140 | new_plot = Data2D(image=image, err_image=data.err_data) |
---|
| 141 | new_plot.name = model.name + '2d' |
---|
| 142 | new_plot.title = "Analytical model 2D " |
---|
| 143 | new_plot.id = str(page_id) + " " + data.name |
---|
| 144 | new_plot.group_id = str(page_id) + " Model2D" |
---|
| 145 | new_plot.detector = data.detector |
---|
| 146 | new_plot.source = data.source |
---|
| 147 | new_plot.is_data = False |
---|
| 148 | new_plot.qx_data = data.qx_data |
---|
| 149 | new_plot.qy_data = data.qy_data |
---|
| 150 | new_plot.q_data = data.q_data |
---|
| 151 | new_plot.mask = data.mask |
---|
| 152 | ## plot boundaries |
---|
| 153 | new_plot.ymin = data.ymin |
---|
| 154 | new_plot.ymax = data.ymax |
---|
| 155 | new_plot.xmin = data.xmin |
---|
| 156 | new_plot.xmax = data.xmax |
---|
| 157 | |
---|
| 158 | title = data.title |
---|
| 159 | |
---|
| 160 | new_plot.is_data = False |
---|
| 161 | if data.is_data: |
---|
| 162 | data_name = str(data.name) |
---|
| 163 | else: |
---|
| 164 | data_name = str(model.__class__.__name__) + '2d' |
---|
| 165 | |
---|
| 166 | if len(title) > 1: |
---|
| 167 | new_plot.title = "Model2D for %s " % model.name + data_name |
---|
| 168 | new_plot.name = model.name + " [" + \ |
---|
| 169 | data_name + "]" |
---|
| 170 | |
---|
[6fd4e36] | 171 | return new_plot |
---|
[4d457df] | 172 | |
---|
| 173 | def computeDataRange(self): |
---|
| 174 | """ |
---|
| 175 | Compute the minimum and the maximum range of the data |
---|
| 176 | return the npts contains in data |
---|
| 177 | """ |
---|
| 178 | qmin, qmax, npts = None, None, None |
---|
| 179 | if isinstance(self.data, Data1D): |
---|
| 180 | try: |
---|
| 181 | qmin = min(self.data.x) |
---|
| 182 | qmax = max(self.data.x) |
---|
| 183 | npts = len(self.data.x) |
---|
| 184 | except (ValueError, TypeError): |
---|
| 185 | msg = "Unable to find min/max/length of \n data named %s" % \ |
---|
| 186 | self.data.filename |
---|
| 187 | raise ValueError, msg |
---|
| 188 | |
---|
| 189 | else: |
---|
| 190 | qmin = 0 |
---|
| 191 | try: |
---|
| 192 | x = max(numpy.fabs(self.data.xmin), numpy.fabs(self.data.xmax)) |
---|
| 193 | y = max(numpy.fabs(self.data.ymin), numpy.fabs(self.data.ymax)) |
---|
| 194 | except (ValueError, TypeError): |
---|
| 195 | msg = "Unable to find min/max of \n data named %s" % \ |
---|
| 196 | self.data.filename |
---|
| 197 | raise ValueError, msg |
---|
| 198 | qmax = numpy.sqrt(x * x + y * y) |
---|
| 199 | npts = len(self.data.data) |
---|
| 200 | return qmin, qmax, npts |
---|