[fef38e8] | 1 | """ |
---|
| 2 | Adapters for fitting module |
---|
| 3 | """ |
---|
| 4 | import copy |
---|
| 5 | import numpy |
---|
| 6 | import math |
---|
| 7 | from sas.sascalc.data_util.uncertainty import Uncertainty |
---|
| 8 | |
---|
| 9 | from sas.qtgui.Plotting.Plottables import PlottableData1D |
---|
| 10 | from sas.qtgui.Plotting.Plottables import PlottableData2D |
---|
| 11 | |
---|
| 12 | from sas.sascalc.dataloader.data_info import Data1D as LoadData1D |
---|
| 13 | from sas.sascalc.dataloader.data_info import Data2D as LoadData2D |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | class Data1D(PlottableData1D, 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 | PlottableData1D.__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.x = copy.deepcopy(data1d.x) |
---|
| 43 | self.y = 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 |
---|
[749b715] | 56 | self.isSesans = data1d.isSesans |
---|
[fef38e8] | 57 | |
---|
| 58 | def __str__(self): |
---|
| 59 | """ |
---|
| 60 | print data |
---|
| 61 | """ |
---|
| 62 | _str = "%s\n" % LoadData1D.__str__(self) |
---|
| 63 | |
---|
| 64 | return _str |
---|
| 65 | |
---|
| 66 | def _perform_operation(self, other, operation): |
---|
| 67 | """ |
---|
| 68 | """ |
---|
| 69 | # First, check the data compatibility |
---|
| 70 | dy, dy_other = self._validity_check(other) |
---|
| 71 | result = Data1D(x=[], y=[], dx=None, dy=None) |
---|
| 72 | result.clone_without_data(length=len(self.x), clone=self) |
---|
| 73 | result.copy_from_datainfo(data1d=self) |
---|
[cee5c78] | 74 | if self.dxw is None: |
---|
[fef38e8] | 75 | result.dxw = None |
---|
| 76 | else: |
---|
| 77 | result.dxw = numpy.zeros(len(self.x)) |
---|
[cee5c78] | 78 | if self.dxl is None: |
---|
[fef38e8] | 79 | result.dxl = None |
---|
| 80 | else: |
---|
| 81 | result.dxl = numpy.zeros(len(self.x)) |
---|
| 82 | |
---|
| 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] |
---|
| 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] |
---|
| 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) |
---|
| 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]) |
---|
| 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 | |
---|
| 113 | def _perform_union(self, other): |
---|
| 114 | """ |
---|
| 115 | """ |
---|
| 116 | # First, check the data compatibility |
---|
| 117 | self._validity_check_union(other) |
---|
| 118 | result = Data1D(x=[], y=[], dx=None, dy=None) |
---|
| 119 | tot_length = len(self.x) + len(other.x) |
---|
| 120 | result = self.clone_without_data(length=tot_length, clone=result) |
---|
[cee5c78] | 121 | if self.dy is None or other.dy is None: |
---|
[fef38e8] | 122 | result.dy = None |
---|
| 123 | else: |
---|
| 124 | result.dy = numpy.zeros(tot_length) |
---|
[cee5c78] | 125 | if self.dx is None or other.dx is None: |
---|
[fef38e8] | 126 | result.dx = None |
---|
| 127 | else: |
---|
| 128 | result.dx = numpy.zeros(tot_length) |
---|
[cee5c78] | 129 | if self.dxw is None or other.dxw is None: |
---|
[fef38e8] | 130 | result.dxw = None |
---|
| 131 | else: |
---|
| 132 | result.dxw = numpy.zeros(tot_length) |
---|
[cee5c78] | 133 | if self.dxl is None or other.dxl is None: |
---|
[fef38e8] | 134 | result.dxl = None |
---|
| 135 | else: |
---|
| 136 | result.dxl = numpy.zeros(tot_length) |
---|
| 137 | |
---|
| 138 | result.x = numpy.append(self.x, other.x) |
---|
| 139 | #argsorting |
---|
| 140 | ind = numpy.argsort(result.x) |
---|
| 141 | result.x = result.x[ind] |
---|
| 142 | result.y = numpy.append(self.y, other.y) |
---|
| 143 | result.y = result.y[ind] |
---|
[cee5c78] | 144 | if result.dy is not None: |
---|
[fef38e8] | 145 | result.dy = numpy.append(self.dy, other.dy) |
---|
| 146 | result.dy = result.dy[ind] |
---|
| 147 | if result.dx is not None: |
---|
| 148 | result.dx = numpy.append(self.dx, other.dx) |
---|
| 149 | result.dx = result.dx[ind] |
---|
| 150 | if result.dxw is not None: |
---|
| 151 | result.dxw = numpy.append(self.dxw, other.dxw) |
---|
| 152 | result.dxw = result.dxw[ind] |
---|
| 153 | if result.dxl is not None: |
---|
| 154 | result.dxl = numpy.append(self.dxl, other.dxl) |
---|
| 155 | result.dxl = result.dxl[ind] |
---|
| 156 | return result |
---|
| 157 | |
---|
| 158 | class Data2D(PlottableData2D, LoadData2D): |
---|
| 159 | """ |
---|
| 160 | """ |
---|
| 161 | def __init__(self, image=None, err_image=None, |
---|
| 162 | qx_data=None, qy_data=None, q_data=None, |
---|
| 163 | mask=None, dqx_data=None, dqy_data=None, |
---|
| 164 | xmin=None, xmax=None, ymin=None, ymax=None, |
---|
| 165 | zmin=None, zmax=None): |
---|
| 166 | """ |
---|
| 167 | """ |
---|
| 168 | PlottableData2D.__init__(self, image=image, err_image=err_image, |
---|
| 169 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, |
---|
| 170 | zmin=zmin, zmax=zmax, qx_data=qx_data, |
---|
| 171 | qy_data=qy_data) |
---|
| 172 | |
---|
| 173 | LoadData2D.__init__(self, data=image, err_data=err_image, |
---|
| 174 | qx_data=qx_data, qy_data=qy_data, |
---|
| 175 | dqx_data=dqx_data, dqy_data=dqy_data, |
---|
| 176 | q_data=q_data, mask=mask) |
---|
| 177 | self.id = None |
---|
| 178 | self.list_group_id = [] |
---|
| 179 | self.group_id = None |
---|
| 180 | self.is_data = True |
---|
| 181 | self.path = None |
---|
| 182 | self.xtransform = None |
---|
| 183 | self.ytransform = None |
---|
| 184 | self.title = "" |
---|
| 185 | self.scale = None |
---|
| 186 | |
---|
| 187 | def copy_from_datainfo(self, data2d): |
---|
| 188 | """ |
---|
| 189 | copy value of Data2D of type DataLoader.data_info |
---|
| 190 | """ |
---|
| 191 | self.data = copy.deepcopy(data2d.data) |
---|
| 192 | self.qx_data = copy.deepcopy(data2d.qx_data) |
---|
| 193 | self.qy_data = copy.deepcopy(data2d.qy_data) |
---|
| 194 | self.q_data = copy.deepcopy(data2d.q_data) |
---|
| 195 | self.mask = copy.deepcopy(data2d.mask) |
---|
| 196 | self.err_data = copy.deepcopy(data2d.err_data) |
---|
| 197 | self.x_bins = copy.deepcopy(data2d.x_bins) |
---|
| 198 | self.y_bins = copy.deepcopy(data2d.y_bins) |
---|
| 199 | if data2d.dqx_data is not None: |
---|
| 200 | self.dqx_data = copy.deepcopy(data2d.dqx_data) |
---|
| 201 | if data2d.dqy_data is not None: |
---|
| 202 | self.dqy_data = copy.deepcopy(data2d.dqy_data) |
---|
| 203 | self.xmin = data2d.xmin |
---|
| 204 | self.xmax = data2d.xmax |
---|
| 205 | self.ymin = data2d.ymin |
---|
| 206 | self.ymax = data2d.ymax |
---|
| 207 | if hasattr(data2d, "zmin"): |
---|
| 208 | self.zmin = data2d.zmin |
---|
| 209 | if hasattr(data2d, "zmax"): |
---|
| 210 | self.zmax = data2d.zmax |
---|
| 211 | self.xaxis(data2d._xaxis, data2d._xunit) |
---|
| 212 | self.yaxis(data2d._yaxis, data2d._yunit) |
---|
| 213 | self.title = data2d.title |
---|
| 214 | |
---|
| 215 | def __str__(self): |
---|
| 216 | """ |
---|
| 217 | print data |
---|
| 218 | """ |
---|
| 219 | _str = "%s\n" % LoadData2D.__str__(self) |
---|
| 220 | return _str |
---|
| 221 | |
---|
| 222 | def _perform_operation(self, other, operation): |
---|
| 223 | """ |
---|
| 224 | Perform 2D operations between data sets |
---|
| 225 | |
---|
| 226 | :param other: other data set |
---|
| 227 | :param operation: function defining the operation |
---|
| 228 | |
---|
| 229 | """ |
---|
| 230 | # First, check the data compatibility |
---|
| 231 | dy, dy_other = self._validity_check(other) |
---|
| 232 | result = Data2D(image=None, qx_data=None, qy_data=None, |
---|
| 233 | q_data=None, err_image=None, xmin=None, xmax=None, |
---|
| 234 | ymin=None, ymax=None, zmin=None, zmax=None) |
---|
| 235 | result.clone_without_data(len(self.data)) |
---|
| 236 | result.copy_from_datainfo(data2d=self) |
---|
| 237 | result.xmin = self.xmin |
---|
| 238 | result.xmax = self.xmax |
---|
| 239 | result.ymin = self.ymin |
---|
| 240 | result.ymax = self.ymax |
---|
[cee5c78] | 241 | if self.dqx_data is None or self.dqy_data is None: |
---|
[fef38e8] | 242 | result.dqx_data = None |
---|
| 243 | result.dqy_data = None |
---|
| 244 | else: |
---|
| 245 | result.dqx_data = numpy.zeros(len(self.data)) |
---|
| 246 | result.dqy_data = numpy.zeros(len(self.data)) |
---|
| 247 | for i in range(numpy.size(self.data)): |
---|
| 248 | result.data[i] = self.data[i] |
---|
| 249 | if self.err_data is not None and \ |
---|
| 250 | numpy.size(self.data) == numpy.size(self.err_data): |
---|
| 251 | result.err_data[i] = self.err_data[i] |
---|
| 252 | if self.dqx_data is not None: |
---|
| 253 | result.dqx_data[i] = self.dqx_data[i] |
---|
| 254 | if self.dqy_data is not None: |
---|
| 255 | result.dqy_data[i] = self.dqy_data[i] |
---|
| 256 | result.qx_data[i] = self.qx_data[i] |
---|
| 257 | result.qy_data[i] = self.qy_data[i] |
---|
| 258 | result.q_data[i] = self.q_data[i] |
---|
| 259 | result.mask[i] = self.mask[i] |
---|
| 260 | |
---|
| 261 | a = Uncertainty(self.data[i], dy[i]**2) |
---|
| 262 | if isinstance(other, Data2D): |
---|
| 263 | b = Uncertainty(other.data[i], dy_other[i]**2) |
---|
| 264 | if other.dqx_data is not None and \ |
---|
| 265 | result.dqx_data is not None: |
---|
| 266 | result.dqx_data[i] *= self.dqx_data[i] |
---|
| 267 | result.dqx_data[i] += (other.dqx_data[i]**2) |
---|
| 268 | result.dqx_data[i] /= 2 |
---|
| 269 | result.dqx_data[i] = math.sqrt(result.dqx_data[i]) |
---|
| 270 | if other.dqy_data is not None and \ |
---|
| 271 | result.dqy_data is not None: |
---|
| 272 | result.dqy_data[i] *= self.dqy_data[i] |
---|
| 273 | result.dqy_data[i] += (other.dqy_data[i]**2) |
---|
| 274 | result.dqy_data[i] /= 2 |
---|
| 275 | result.dqy_data[i] = math.sqrt(result.dqy_data[i]) |
---|
| 276 | else: |
---|
| 277 | b = other |
---|
| 278 | |
---|
| 279 | output = operation(a, b) |
---|
| 280 | result.data[i] = output.x |
---|
| 281 | result.err_data[i] = math.sqrt(math.fabs(output.variance)) |
---|
| 282 | return result |
---|
| 283 | |
---|
| 284 | def _perform_union(self, other): |
---|
| 285 | """ |
---|
| 286 | Perform 2D operations between data sets |
---|
| 287 | |
---|
| 288 | :param other: other data set |
---|
| 289 | :param operation: function defining the operation |
---|
| 290 | |
---|
| 291 | """ |
---|
| 292 | # First, check the data compatibility |
---|
| 293 | self._validity_check_union(other) |
---|
| 294 | result = Data2D(image=None, qx_data=None, qy_data=None, |
---|
| 295 | q_data=None, err_image=None, xmin=None, xmax=None, |
---|
| 296 | ymin=None, ymax=None, zmin=None, zmax=None) |
---|
| 297 | length = len(self.data) |
---|
| 298 | tot_length = length + len(other.data) |
---|
| 299 | result.clone_without_data(tot_length) |
---|
| 300 | result.xmin = self.xmin |
---|
| 301 | result.xmax = self.xmax |
---|
| 302 | result.ymin = self.ymin |
---|
| 303 | result.ymax = self.ymax |
---|
[cee5c78] | 304 | if self.dqx_data is None or self.dqy_data is None or \ |
---|
| 305 | other.dqx_data is None or other.dqy_data is None : |
---|
[fef38e8] | 306 | result.dqx_data = None |
---|
| 307 | result.dqy_data = None |
---|
| 308 | else: |
---|
| 309 | result.dqx_data = numpy.zeros(len(self.data) + \ |
---|
| 310 | numpy.size(other.data)) |
---|
| 311 | result.dqy_data = numpy.zeros(len(self.data) + \ |
---|
| 312 | numpy.size(other.data)) |
---|
| 313 | |
---|
| 314 | result.data = numpy.append(self.data, other.data) |
---|
| 315 | result.qx_data = numpy.append(self.qx_data, other.qx_data) |
---|
| 316 | result.qy_data = numpy.append(self.qy_data, other.qy_data) |
---|
| 317 | result.q_data = numpy.append(self.q_data, other.q_data) |
---|
| 318 | result.mask = numpy.append(self.mask, other.mask) |
---|
| 319 | if result.err_data is not None: |
---|
| 320 | result.err_data = numpy.append(self.err_data, other.err_data) |
---|
| 321 | if self.dqx_data is not None: |
---|
| 322 | result.dqx_data = numpy.append(self.dqx_data, other.dqx_data) |
---|
| 323 | if self.dqy_data is not None: |
---|
| 324 | result.dqy_data = numpy.append(self.dqy_data, other.dqy_data) |
---|
| 325 | |
---|
| 326 | return result |
---|
| 327 | |
---|
| 328 | def check_data_validity(data): |
---|
| 329 | """ |
---|
| 330 | Return True is data is valid enough to compute chisqr, else False |
---|
| 331 | """ |
---|
| 332 | flag = True |
---|
| 333 | if data is not None: |
---|
| 334 | if issubclass(data.__class__, Data2D): |
---|
| 335 | if (data.data is None) or (len(data.data) == 0)\ |
---|
| 336 | or (len(data.err_data) == 0): |
---|
| 337 | flag = False |
---|
| 338 | else: |
---|
| 339 | if (data.y is None) or (len(data.y) == 0): |
---|
| 340 | flag = False |
---|
| 341 | if not data.is_data: |
---|
| 342 | flag = False |
---|
| 343 | else: |
---|
| 344 | flag = False |
---|
| 345 | return flag |
---|