[12aa9b5] | 1 | """ |
---|
[d955bf19] | 2 | Adapters for fitting module |
---|
[12aa9b5] | 3 | """ |
---|
[8e87ece] | 4 | import copy |
---|
| 5 | import numpy |
---|
[901142f] | 6 | import math |
---|
[79492222] | 7 | from sas.data_util.uncertainty import Uncertainty |
---|
| 8 | from sas.plottools.plottables import Data1D as PlotData1D |
---|
| 9 | from sas.plottools.plottables import Data2D as PlotData2D |
---|
| 10 | from sas.plottools.plottables import Theory1D as PlotTheory1D |
---|
[81812d9] | 11 | |
---|
[79492222] | 12 | from sas.dataloader.data_info import Data1D as LoadData1D |
---|
| 13 | from sas.dataloader.data_info import Data2D as LoadData2D |
---|
[81812d9] | 14 | |
---|
[f444b20] | 15 | |
---|
[3562fbc] | 16 | class Data1D(PlotData1D, LoadData1D): |
---|
[d955bf19] | 17 | """ |
---|
| 18 | """ |
---|
[32c0841] | 19 | def __init__(self, x=None, y=None, dx=None, dy=None): |
---|
[d955bf19] | 20 | """ |
---|
| 21 | """ |
---|
[32c0841] | 22 | if x is None: |
---|
| 23 | x = [] |
---|
| 24 | if y is None: |
---|
| 25 | y = [] |
---|
[ff3f900b] | 26 | PlotData1D.__init__(self, x, y, dx, dy) |
---|
| 27 | LoadData1D.__init__(self, x, y, dx, dy) |
---|
[901142f] | 28 | self.id = None |
---|
[e88ebfd] | 29 | self.list_group_id = [] |
---|
| 30 | self.group_id = None |
---|
[ff3f900b] | 31 | self.is_data = True |
---|
[f444b20] | 32 | self.path = None |
---|
[8a7d922] | 33 | self.xtransform = None |
---|
| 34 | self.ytransform = None |
---|
[f444b20] | 35 | self.title = "" |
---|
[5c4b674] | 36 | self.scale = None |
---|
| 37 | |
---|
[ff3f900b] | 38 | def copy_from_datainfo(self, data1d): |
---|
| 39 | """ |
---|
[d955bf19] | 40 | copy values of Data1D of type DataLaoder.Data_info |
---|
[ff3f900b] | 41 | """ |
---|
| 42 | self.x = copy.deepcopy(data1d.x) |
---|
| 43 | self.y = copy.deepcopy(data1d.y) |
---|
| 44 | self.dy = copy.deepcopy(data1d.dy) |
---|
[8e87ece] | 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) |
---|
[ff3f900b] | 52 | |
---|
[901142f] | 53 | self.xaxis(data1d._xaxis, data1d._xunit) |
---|
| 54 | self.yaxis(data1d._yaxis, data1d._yunit) |
---|
[f444b20] | 55 | self.title = data1d.title |
---|
[3562fbc] | 56 | |
---|
| 57 | def __str__(self): |
---|
| 58 | """ |
---|
[d955bf19] | 59 | print data |
---|
[3562fbc] | 60 | """ |
---|
| 61 | _str = "%s\n" % LoadData1D.__str__(self) |
---|
| 62 | |
---|
| 63 | return _str |
---|
[901142f] | 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) |
---|
[a48842a2] | 71 | result.clone_without_data(length=len(self.x), clone=self) |
---|
[901142f] | 72 | result.copy_from_datainfo(data1d=self) |
---|
[a48842a2] | 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 | |
---|
[901142f] | 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] |
---|
[a48842a2] | 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] |
---|
[901142f] | 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) |
---|
[a48842a2] | 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]) |
---|
[901142f] | 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 | |
---|
[a48842a2] | 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 | |
---|
[f444b20] | 157 | |
---|
| 158 | |
---|
[32c0841] | 159 | class Theory1D(PlotTheory1D, LoadData1D): |
---|
[d955bf19] | 160 | """ |
---|
| 161 | """ |
---|
[32c0841] | 162 | def __init__(self, x=None, y=None, dy=None): |
---|
[d955bf19] | 163 | """ |
---|
| 164 | """ |
---|
[32c0841] | 165 | if x is None: |
---|
| 166 | x = [] |
---|
| 167 | if y is None: |
---|
| 168 | y = [] |
---|
[e5664f2] | 169 | PlotTheory1D.__init__(self, x, y, dy) |
---|
| 170 | LoadData1D.__init__(self, x, y, dy) |
---|
[901142f] | 171 | self.id = None |
---|
[e88ebfd] | 172 | self.list_group_id = [] |
---|
| 173 | self.group_id = None |
---|
[e5664f2] | 174 | self.is_data = True |
---|
[f444b20] | 175 | self.path = None |
---|
[8a7d922] | 176 | self.xtransform = None |
---|
| 177 | self.ytransform = None |
---|
[f444b20] | 178 | self.title = "" |
---|
[5c4b674] | 179 | self.scale = None |
---|
[e5664f2] | 180 | |
---|
| 181 | def copy_from_datainfo(self, data1d): |
---|
| 182 | """ |
---|
[d955bf19] | 183 | copy values of Data1D of type DataLaoder.Data_info |
---|
[e5664f2] | 184 | """ |
---|
| 185 | self.x = copy.deepcopy(data1d.x) |
---|
| 186 | self.y = copy.deepcopy(data1d.y) |
---|
| 187 | self.dy = copy.deepcopy(data1d.dy) |
---|
[8e87ece] | 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) |
---|
[901142f] | 194 | self.xaxis(data1d._xaxis, data1d._xunit) |
---|
| 195 | self.yaxis(data1d._yaxis, data1d._yunit) |
---|
[f444b20] | 196 | self.title = data1d.title |
---|
[8e87ece] | 197 | |
---|
[3562fbc] | 198 | def __str__(self): |
---|
| 199 | """ |
---|
[d955bf19] | 200 | print data |
---|
[3562fbc] | 201 | """ |
---|
| 202 | _str = "%s\n" % LoadData1D.__str__(self) |
---|
| 203 | |
---|
| 204 | return _str |
---|
[901142f] | 205 | |
---|
| 206 | def _perform_operation(self, other, operation): |
---|
| 207 | """ |
---|
| 208 | """ |
---|
| 209 | # First, check the data compatibility |
---|
| 210 | dy, dy_other = self._validity_check(other) |
---|
[a48842a2] | 211 | result = self.clone_without_data(len(self.x)) |
---|
[901142f] | 212 | result.copy_from_datainfo(data1d=self) |
---|
[a48842a2] | 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)): |
---|
[901142f] | 223 | result.x[i] = self.x[i] |
---|
[a48842a2] | 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 | |
---|
[901142f] | 231 | a = Uncertainty(self.y[i], dy[i]**2) |
---|
| 232 | if isinstance(other, Data1D): |
---|
| 233 | b = Uncertainty(other.y[i], dy_other[i]**2) |
---|
[a48842a2] | 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]) |
---|
[901142f] | 249 | else: |
---|
| 250 | b = other |
---|
[a48842a2] | 251 | |
---|
[901142f] | 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 | |
---|
[a48842a2] | 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 | |
---|
[ff3f900b] | 301 | |
---|
[32c0841] | 302 | class Data2D(PlotData2D, LoadData2D): |
---|
[d955bf19] | 303 | """ |
---|
| 304 | """ |
---|
[901142f] | 305 | def __init__(self, image=None, err_image=None, |
---|
[0008f54] | 306 | qx_data=None, qy_data=None, q_data=None, |
---|
| 307 | mask=None, dqx_data=None, dqy_data=None, |
---|
[901142f] | 308 | xmin=None, xmax=None, ymin=None, ymax=None, |
---|
[0008f54] | 309 | zmin=None, zmax=None): |
---|
[d955bf19] | 310 | """ |
---|
| 311 | """ |
---|
[901142f] | 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) |
---|
[ff3f900b] | 316 | |
---|
[901142f] | 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) |
---|
[f444b20] | 321 | self.id = None |
---|
[e88ebfd] | 322 | self.list_group_id = [] |
---|
| 323 | self.group_id = None |
---|
[5c4b674] | 324 | self.is_data = True |
---|
[f444b20] | 325 | self.path = None |
---|
[5c4b674] | 326 | self.xtransform = None |
---|
| 327 | self.ytransform = None |
---|
[f444b20] | 328 | self.title = "" |
---|
[8a7d922] | 329 | self.scale = None |
---|
[ff3f900b] | 330 | |
---|
| 331 | def copy_from_datainfo(self, data2d): |
---|
| 332 | """ |
---|
[d955bf19] | 333 | copy value of Data2D of type DataLoader.data_info |
---|
[ff3f900b] | 334 | """ |
---|
[32c0841] | 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 |
---|
[f7a5c7e] | 351 | if hasattr(data2d, "zmin"): |
---|
[32c0841] | 352 | self.zmin = data2d.zmin |
---|
[f7a5c7e] | 353 | if hasattr(data2d, "zmax"): |
---|
[32c0841] | 354 | self.zmax = data2d.zmax |
---|
[901142f] | 355 | self.xaxis(data2d._xaxis, data2d._xunit) |
---|
| 356 | self.yaxis(data2d._yaxis, data2d._yunit) |
---|
[f444b20] | 357 | self.title = data2d.title |
---|
[ff3f900b] | 358 | |
---|
[3562fbc] | 359 | def __str__(self): |
---|
| 360 | """ |
---|
[d955bf19] | 361 | print data |
---|
[3562fbc] | 362 | """ |
---|
| 363 | _str = "%s\n" % LoadData2D.__str__(self) |
---|
| 364 | return _str |
---|
[901142f] | 365 | |
---|
[fdef956] | 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 | |
---|
[901142f] | 434 | def _perform_operation(self, other, operation): |
---|
| 435 | """ |
---|
[d955bf19] | 436 | Perform 2D operations between data sets |
---|
| 437 | |
---|
| 438 | :param other: other data set |
---|
| 439 | :param operation: function defining the operation |
---|
| 440 | |
---|
[901142f] | 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, |
---|
[a48842a2] | 445 | q_data=None, err_image=None, xmin=None, xmax=None, |
---|
[901142f] | 446 | ymin=None, ymax=None, zmin=None, zmax=None) |
---|
[9053779] | 447 | result.clone_without_data(len(self.data)) |
---|
[901142f] | 448 | result.copy_from_datainfo(data2d=self) |
---|
[a48842a2] | 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: |
---|
[dcf73a4] | 457 | result.dqx_data = numpy.zeros(len(self.data)) |
---|
| 458 | result.dqy_data = numpy.zeros(len(self.data)) |
---|
[a48842a2] | 459 | for i in range(numpy.size(self.data)): |
---|
[dcf73a4] | 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 | |
---|
[a48842a2] | 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 |
---|
[901142f] | 499 | |
---|
[a48842a2] | 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) |
---|
[9053779] | 511 | result.clone_without_data(tot_length) |
---|
[a48842a2] | 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 | |
---|
[901142f] | 538 | return result |
---|
[1913820] | 539 | |
---|
| 540 | def check_data_validity(data): |
---|
[b21d32b] | 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 |
---|