[a3084ada] | 1 | """ |
---|
| 2 | Module that contains classes to hold information read from |
---|
| 3 | reduced data files. |
---|
| 4 | |
---|
| 5 | A good description of the data members can be found in |
---|
| 6 | the CanSAS 1D XML data format: |
---|
| 7 | |
---|
| 8 | http://www.smallangles.net/wgwiki/index.php/cansas1d_documentation |
---|
| 9 | """ |
---|
| 10 | |
---|
| 11 | """ |
---|
| 12 | This software was developed by the University of Tennessee as part of the |
---|
| 13 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 14 | project funded by the US National Science Foundation. |
---|
| 15 | |
---|
[b99ac227] | 16 | If you use DANSE applications to do scientific research that leads to |
---|
| 17 | publication, we ask that you acknowledge the use of the software with the |
---|
| 18 | following sentence: |
---|
| 19 | |
---|
| 20 | "This work benefited from DANSE software developed under NSF award DMR-0520547." |
---|
[a3084ada] | 21 | |
---|
| 22 | copyright 2008, University of Tennessee |
---|
| 23 | """ |
---|
| 24 | |
---|
[b39c817] | 25 | #TODO: Keep track of data manipulation in the 'process' data structure. |
---|
[579ba85] | 26 | #TODO: This module should be independent of plottables. We should write |
---|
| 27 | # an adapter class for plottables when needed. |
---|
[b39c817] | 28 | |
---|
[579ba85] | 29 | #from sans.guitools.plottables import Data1D as plottable_1D |
---|
[9198b83] | 30 | from data_util.uncertainty import Uncertainty |
---|
| 31 | import numpy |
---|
| 32 | import math |
---|
[a3084ada] | 33 | |
---|
[579ba85] | 34 | class plottable_1D: |
---|
| 35 | """ |
---|
| 36 | Data1D is a place holder for 1D plottables. |
---|
| 37 | """ |
---|
[d00f8ff] | 38 | # The presence of these should be mutually exclusive with the presence of Qdev (dx) |
---|
[579ba85] | 39 | x = None |
---|
| 40 | y = None |
---|
| 41 | dx = None |
---|
| 42 | dy = None |
---|
[d00f8ff] | 43 | ## Slit smearing length |
---|
| 44 | dxl = None |
---|
| 45 | ## Slit smearing width |
---|
| 46 | dxw = None |
---|
[579ba85] | 47 | |
---|
| 48 | # Units |
---|
| 49 | _xaxis = '' |
---|
| 50 | _xunit = '' |
---|
| 51 | _yaxis = '' |
---|
| 52 | _yunit = '' |
---|
| 53 | |
---|
[d00f8ff] | 54 | def __init__(self,x,y,dx=None,dy=None,dxl=None,dxw=None): |
---|
[579ba85] | 55 | self.x = x |
---|
| 56 | self.y = y |
---|
| 57 | self.dx = dx |
---|
| 58 | self.dy = dy |
---|
[d00f8ff] | 59 | self.dxl = dxl |
---|
| 60 | self.dxw = dxw |
---|
[579ba85] | 61 | |
---|
| 62 | def xaxis(self, label, unit): |
---|
| 63 | self._xaxis = label |
---|
| 64 | self._xunit = unit |
---|
| 65 | |
---|
| 66 | def yaxis(self, label, unit): |
---|
| 67 | self._yaxis = label |
---|
| 68 | self._yunit = unit |
---|
| 69 | |
---|
[99d1af6] | 70 | class plottable_2D: |
---|
[8780e9a] | 71 | """ |
---|
[579ba85] | 72 | Data2D is a place holder for 2D plottables. |
---|
[8780e9a] | 73 | """ |
---|
| 74 | xmin = None |
---|
| 75 | xmax = None |
---|
| 76 | ymin = None |
---|
| 77 | ymax = None |
---|
[99d1af6] | 78 | data = None |
---|
| 79 | err_data = None |
---|
| 80 | |
---|
| 81 | # Units |
---|
| 82 | _xaxis = '' |
---|
| 83 | _xunit = '' |
---|
| 84 | _yaxis = '' |
---|
| 85 | _yunit = '' |
---|
| 86 | _zaxis = '' |
---|
| 87 | _zunit = '' |
---|
| 88 | |
---|
| 89 | def __init__(self, data=None, err_data=None): |
---|
[442f42f] | 90 | self.data = numpy.asarray(data) |
---|
| 91 | self.err_data = numpy.asarray(err_data) |
---|
[99d1af6] | 92 | |
---|
| 93 | def xaxis(self, label, unit): |
---|
| 94 | self._xaxis = label |
---|
| 95 | self._xunit = unit |
---|
| 96 | |
---|
| 97 | def yaxis(self, label, unit): |
---|
| 98 | self._yaxis = label |
---|
| 99 | self._yunit = unit |
---|
| 100 | |
---|
| 101 | def zaxis(self, label, unit): |
---|
| 102 | self._zaxis = label |
---|
| 103 | self._zunit = unit |
---|
[de5c813] | 104 | |
---|
[99d1af6] | 105 | |
---|
[a3084ada] | 106 | class Vector: |
---|
| 107 | """ |
---|
| 108 | Vector class to hold multi-dimensional objects |
---|
| 109 | """ |
---|
| 110 | ## x component |
---|
| 111 | x = None |
---|
| 112 | ## y component |
---|
| 113 | y = None |
---|
| 114 | ## z component |
---|
| 115 | z = None |
---|
| 116 | |
---|
| 117 | def __init__(self, x=None, y=None, z=None): |
---|
| 118 | """ |
---|
| 119 | Initialization. Components that are not |
---|
| 120 | set a set to None by default. |
---|
| 121 | |
---|
| 122 | @param x: x component |
---|
| 123 | @param y: y component |
---|
| 124 | @param z: z component |
---|
| 125 | """ |
---|
| 126 | self.x = x |
---|
| 127 | self.y = y |
---|
| 128 | self.z = z |
---|
| 129 | |
---|
| 130 | def __str__(self): |
---|
| 131 | return "x = %s\ty = %s\tz = %s" % (str(self.x), str(self.y), str(self.z)) |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | class Detector: |
---|
| 135 | """ |
---|
| 136 | Class to hold detector information |
---|
| 137 | """ |
---|
| 138 | ## Name of the instrument [string] |
---|
[fe78c7b] | 139 | name = None |
---|
[a3084ada] | 140 | ## Sample to detector distance [float] [mm] |
---|
| 141 | distance = None |
---|
[b39c817] | 142 | distance_unit = 'mm' |
---|
[a3084ada] | 143 | ## Offset of this detector position in X, Y, (and Z if necessary) [Vector] [mm] |
---|
[d6513cd] | 144 | offset = None |
---|
[b39c817] | 145 | offset_unit = 'm' |
---|
[a3084ada] | 146 | ## Orientation (rotation) of this detector in roll, pitch, and yaw [Vector] [degrees] |
---|
[d6513cd] | 147 | orientation = None |
---|
[8780e9a] | 148 | orientation_unit = 'degree' |
---|
[99d1af6] | 149 | ## Center of the beam on the detector in X and Y (and Z if necessary) [Vector] [mm] |
---|
[d6513cd] | 150 | beam_center = None |
---|
[8780e9a] | 151 | beam_center_unit = 'mm' |
---|
[a3084ada] | 152 | ## Pixel size in X, Y, (and Z if necessary) [Vector] [mm] |
---|
[d6513cd] | 153 | pixel_size = None |
---|
[8780e9a] | 154 | pixel_size_unit = 'mm' |
---|
[a3084ada] | 155 | ## Slit length of the instrument for this detector.[float] [mm] |
---|
| 156 | slit_length = None |
---|
[2e9b98c] | 157 | slit_length_unit = 'mm' |
---|
[8780e9a] | 158 | |
---|
[d6513cd] | 159 | def __init__(self): |
---|
| 160 | """ |
---|
| 161 | Initialize class attribute that are objects... |
---|
| 162 | """ |
---|
| 163 | self.offset = Vector() |
---|
| 164 | self.orientation = Vector() |
---|
| 165 | self.beam_center = Vector() |
---|
| 166 | self.pixel_size = Vector() |
---|
| 167 | |
---|
| 168 | |
---|
[8780e9a] | 169 | def __str__(self): |
---|
| 170 | _str = "Detector:\n" |
---|
| 171 | _str += " Name: %s\n" % self.name |
---|
| 172 | _str += " Distance: %s [%s]\n" % \ |
---|
| 173 | (str(self.distance), str(self.distance_unit)) |
---|
| 174 | _str += " Offset: %s [%s]\n" % \ |
---|
| 175 | (str(self.offset), str(self.offset_unit)) |
---|
| 176 | _str += " Orientation: %s [%s]\n" % \ |
---|
| 177 | (str(self.orientation), str(self.orientation_unit)) |
---|
| 178 | _str += " Beam center: %s [%s]\n" % \ |
---|
| 179 | (str(self.beam_center), str(self.beam_center_unit)) |
---|
| 180 | _str += " Pixel size: %s [%s]\n" % \ |
---|
| 181 | (str(self.pixel_size), str(self.pixel_size_unit)) |
---|
| 182 | _str += " Slit length: %s [%s]\n" % \ |
---|
| 183 | (str(self.slit_length), str(self.slit_length_unit)) |
---|
| 184 | return _str |
---|
[a3084ada] | 185 | |
---|
[d6513cd] | 186 | class Aperture: |
---|
[4c00964] | 187 | ## Name |
---|
[579ba85] | 188 | name = None |
---|
[4c00964] | 189 | ## Type |
---|
[579ba85] | 190 | type = None |
---|
| 191 | ## Size name |
---|
| 192 | size_name = None |
---|
[4c00964] | 193 | ## Aperture size [Vector] |
---|
[d6513cd] | 194 | size = None |
---|
| 195 | size_unit = 'mm' |
---|
[4c00964] | 196 | ## Aperture distance [float] |
---|
[d6513cd] | 197 | distance = None |
---|
| 198 | distance_unit = 'mm' |
---|
| 199 | |
---|
| 200 | def __init__(self): |
---|
| 201 | self.size = Vector() |
---|
| 202 | |
---|
[a3084ada] | 203 | class Collimation: |
---|
| 204 | """ |
---|
| 205 | Class to hold collimation information |
---|
| 206 | """ |
---|
[4c00964] | 207 | ## Name |
---|
[fe78c7b] | 208 | name = None |
---|
[a3084ada] | 209 | ## Length [float] [mm] |
---|
| 210 | length = None |
---|
[8780e9a] | 211 | length_unit = 'mm' |
---|
| 212 | ## Aperture |
---|
[d6513cd] | 213 | aperture = None |
---|
| 214 | |
---|
| 215 | def __init__(self): |
---|
| 216 | self.aperture = [] |
---|
[8780e9a] | 217 | |
---|
| 218 | def __str__(self): |
---|
| 219 | _str = "Collimation:\n" |
---|
| 220 | _str += " Length: %s [%s]\n" % \ |
---|
| 221 | (str(self.length), str(self.length_unit)) |
---|
| 222 | for item in self.aperture: |
---|
| 223 | _str += " Aperture size:%s [%s]\n" % \ |
---|
| 224 | (str(item.size), str(item.size_unit)) |
---|
| 225 | _str += " Aperture_dist:%s [%s]\n" % \ |
---|
| 226 | (str(item.distance), str(item.distance_unit)) |
---|
| 227 | return _str |
---|
[a3084ada] | 228 | |
---|
| 229 | class Source: |
---|
| 230 | """ |
---|
| 231 | Class to hold source information |
---|
| 232 | """ |
---|
[4c00964] | 233 | ## Name |
---|
[579ba85] | 234 | name = None |
---|
[a3084ada] | 235 | ## Radiation type [string] |
---|
[579ba85] | 236 | radiation = None |
---|
| 237 | ## Beam size name |
---|
| 238 | beam_size_name = None |
---|
[a3084ada] | 239 | ## Beam size [Vector] [mm] |
---|
[d6513cd] | 240 | beam_size = None |
---|
[8780e9a] | 241 | beam_size_unit = 'mm' |
---|
[a3084ada] | 242 | ## Beam shape [string] |
---|
[579ba85] | 243 | beam_shape = None |
---|
[a3084ada] | 244 | ## Wavelength [float] [Angstrom] |
---|
| 245 | wavelength = None |
---|
[8780e9a] | 246 | wavelength_unit = 'A' |
---|
[a3084ada] | 247 | ## Minimum wavelength [float] [Angstrom] |
---|
| 248 | wavelength_min = None |
---|
[8780e9a] | 249 | wavelength_min_unit = 'nm' |
---|
[a3084ada] | 250 | ## Maximum wavelength [float] [Angstrom] |
---|
| 251 | wavelength_max = None |
---|
[8780e9a] | 252 | wavelength_max_unit = 'nm' |
---|
[a3084ada] | 253 | ## Wavelength spread [float] [Angstrom] |
---|
| 254 | wavelength_spread = None |
---|
[8780e9a] | 255 | wavelength_spread_unit = 'percent' |
---|
| 256 | |
---|
[d6513cd] | 257 | def __init__(self): |
---|
| 258 | self.beam_size = Vector() |
---|
| 259 | |
---|
| 260 | |
---|
[8780e9a] | 261 | def __str__(self): |
---|
| 262 | _str = "Source:\n" |
---|
| 263 | _str += " Radiation: %s\n" % str(self.radiation) |
---|
| 264 | _str += " Shape: %s\n" % str(self.beam_shape) |
---|
| 265 | _str += " Wavelength: %s [%s]\n" % \ |
---|
| 266 | (str(self.wavelength), str(self.wavelength_unit)) |
---|
| 267 | _str += " Waveln_min: %s [%s]\n" % \ |
---|
| 268 | (str(self.wavelength_min), str(self.wavelength_min_unit)) |
---|
| 269 | _str += " Waveln_max: %s [%s]\n" % \ |
---|
| 270 | (str(self.wavelength_max), str(self.wavelength_max_unit)) |
---|
| 271 | _str += " Waveln_spread:%s [%s]\n" % \ |
---|
| 272 | (str(self.wavelength_spread), str(self.wavelength_spread_unit)) |
---|
| 273 | _str += " Beam_size: %s [%s]\n" % \ |
---|
| 274 | (str(self.beam_size), str(self.beam_size_unit)) |
---|
| 275 | return _str |
---|
| 276 | |
---|
[a3084ada] | 277 | |
---|
| 278 | """ |
---|
| 279 | Definitions of radiation types |
---|
| 280 | """ |
---|
| 281 | NEUTRON = 'neutron' |
---|
| 282 | XRAY = 'x-ray' |
---|
| 283 | MUON = 'muon' |
---|
| 284 | ELECTRON = 'electron' |
---|
| 285 | |
---|
| 286 | class Sample: |
---|
| 287 | """ |
---|
| 288 | Class to hold the sample description |
---|
| 289 | """ |
---|
[579ba85] | 290 | ## Short name for sample |
---|
| 291 | name = '' |
---|
[a3084ada] | 292 | ## ID |
---|
| 293 | ID = '' |
---|
| 294 | ## Thickness [float] [mm] |
---|
| 295 | thickness = None |
---|
[8780e9a] | 296 | thickness_unit = 'mm' |
---|
| 297 | ## Transmission [float] [fraction] |
---|
[a3084ada] | 298 | transmission = None |
---|
| 299 | ## Temperature [float] [C] |
---|
| 300 | temperature = None |
---|
[8780e9a] | 301 | temperature_unit = 'C' |
---|
[a3084ada] | 302 | ## Position [Vector] [mm] |
---|
[d6513cd] | 303 | position = None |
---|
[8780e9a] | 304 | position_unit = 'mm' |
---|
[a3084ada] | 305 | ## Orientation [Vector] [degrees] |
---|
[d6513cd] | 306 | orientation = None |
---|
[8780e9a] | 307 | orientation_unit = 'degree' |
---|
[a3084ada] | 308 | ## Details |
---|
[d6513cd] | 309 | details = None |
---|
| 310 | |
---|
| 311 | def __init__(self): |
---|
| 312 | self.position = Vector() |
---|
| 313 | self.orientation = Vector() |
---|
| 314 | self.details = [] |
---|
[8780e9a] | 315 | |
---|
| 316 | def __str__(self): |
---|
| 317 | _str = "Sample:\n" |
---|
| 318 | _str += " ID: %s\n" % str(self.ID) |
---|
| 319 | _str += " Transmission: %s\n" % str(self.transmission) |
---|
| 320 | _str += " Thickness: %s [%s]\n" % \ |
---|
| 321 | (str(self.thickness), str(self.thickness_unit)) |
---|
| 322 | _str += " Temperature: %s [%s]\n" % \ |
---|
| 323 | (str(self.temperature), str(self.temperature_unit)) |
---|
| 324 | _str += " Position: %s [%s]\n" % \ |
---|
| 325 | (str(self.position), str(self.position_unit)) |
---|
| 326 | _str += " Orientation: %s [%s]\n" % \ |
---|
| 327 | (str(self.orientation), str(self.orientation_unit)) |
---|
| 328 | |
---|
| 329 | _str += " Details:\n" |
---|
| 330 | for item in self.details: |
---|
| 331 | _str += " %s\n" % item |
---|
| 332 | |
---|
| 333 | return _str |
---|
| 334 | |
---|
| 335 | class Process: |
---|
| 336 | """ |
---|
| 337 | Class that holds information about the processes |
---|
| 338 | performed on the data. |
---|
| 339 | """ |
---|
| 340 | name = '' |
---|
| 341 | date = '' |
---|
| 342 | description= '' |
---|
[d6513cd] | 343 | term = None |
---|
| 344 | notes = None |
---|
| 345 | |
---|
| 346 | def __init__(self): |
---|
| 347 | self.term = [] |
---|
| 348 | self.notes = [] |
---|
[8780e9a] | 349 | |
---|
| 350 | def __str__(self): |
---|
| 351 | _str = "Process:\n" |
---|
| 352 | _str += " Name: %s\n" % self.name |
---|
| 353 | _str += " Date: %s\n" % self.date |
---|
| 354 | _str += " Description: %s\n" % self.description |
---|
| 355 | for item in self.term: |
---|
| 356 | _str += " Term: %s\n" % item |
---|
| 357 | for item in self.notes: |
---|
| 358 | _str += " Note: %s\n" % item |
---|
| 359 | return _str |
---|
[a3084ada] | 360 | |
---|
| 361 | |
---|
| 362 | class DataInfo: |
---|
| 363 | """ |
---|
| 364 | Class to hold the data read from a file. |
---|
| 365 | It includes four blocks of data for the |
---|
| 366 | instrument description, the sample description, |
---|
| 367 | the data itself and any other meta data. |
---|
| 368 | """ |
---|
[8780e9a] | 369 | ## Title |
---|
| 370 | title = '' |
---|
[a3084ada] | 371 | ## Run number |
---|
| 372 | run = None |
---|
[579ba85] | 373 | ## Run name |
---|
| 374 | run_name = None |
---|
[a3084ada] | 375 | ## File name |
---|
| 376 | filename = '' |
---|
| 377 | ## Notes |
---|
[d6513cd] | 378 | notes = None |
---|
[a3084ada] | 379 | ## Processes (Action on the data) |
---|
[d6513cd] | 380 | process = None |
---|
[8780e9a] | 381 | ## Instrument name |
---|
| 382 | instrument = '' |
---|
[a3084ada] | 383 | ## Detector information |
---|
[d6513cd] | 384 | detector = None |
---|
[a3084ada] | 385 | ## Sample information |
---|
[d6513cd] | 386 | sample = None |
---|
[a3084ada] | 387 | ## Source information |
---|
[d6513cd] | 388 | source = None |
---|
[8780e9a] | 389 | ## Collimation information |
---|
[d6513cd] | 390 | collimation = None |
---|
[a3084ada] | 391 | ## Additional meta-data |
---|
[d6513cd] | 392 | meta_data = None |
---|
[8780e9a] | 393 | ## Loading errors |
---|
[d6513cd] | 394 | errors = None |
---|
[a3084ada] | 395 | |
---|
[b99ac227] | 396 | def __init__(self): |
---|
| 397 | """ |
---|
| 398 | Initialization |
---|
| 399 | """ |
---|
| 400 | ## Title |
---|
| 401 | self.title = '' |
---|
| 402 | ## Run number |
---|
[579ba85] | 403 | self.run = [] |
---|
| 404 | self.run_name = {} |
---|
[b99ac227] | 405 | ## File name |
---|
| 406 | self.filename = '' |
---|
| 407 | ## Notes |
---|
| 408 | self.notes = [] |
---|
| 409 | ## Processes (Action on the data) |
---|
| 410 | self.process = [] |
---|
| 411 | ## Instrument name |
---|
| 412 | self.instrument = '' |
---|
| 413 | ## Detector information |
---|
| 414 | self.detector = [] |
---|
| 415 | ## Sample information |
---|
| 416 | self.sample = Sample() |
---|
| 417 | ## Source information |
---|
| 418 | self.source = Source() |
---|
| 419 | ## Collimation information |
---|
| 420 | self.collimation = [] |
---|
| 421 | ## Additional meta-data |
---|
| 422 | self.meta_data = {} |
---|
| 423 | ## Loading errors |
---|
| 424 | self.errors = [] |
---|
| 425 | |
---|
[99d1af6] | 426 | def __str__(self): |
---|
| 427 | """ |
---|
| 428 | Nice printout |
---|
| 429 | """ |
---|
| 430 | _str = "File: %s\n" % self.filename |
---|
| 431 | _str += "Title: %s\n" % self.title |
---|
| 432 | _str += "Run: %s\n" % str(self.run) |
---|
| 433 | _str += "Instrument: %s\n" % str(self.instrument) |
---|
| 434 | _str += "%s\n" % str(self.sample) |
---|
| 435 | _str += "%s\n" % str(self.source) |
---|
| 436 | for item in self.detector: |
---|
| 437 | _str += "%s\n" % str(item) |
---|
| 438 | for item in self.collimation: |
---|
| 439 | _str += "%s\n" % str(item) |
---|
| 440 | for item in self.process: |
---|
| 441 | _str += "%s\n" % str(item) |
---|
| 442 | for item in self.notes: |
---|
| 443 | _str += "%s\n" % str(item) |
---|
| 444 | |
---|
| 445 | return _str |
---|
| 446 | |
---|
[b39c817] | 447 | # Private method to perform operation. Not implemented for DataInfo, |
---|
| 448 | # but should be implemented for each data class inherited from DataInfo |
---|
| 449 | # that holds actual data (ex.: Data1D) |
---|
| 450 | def _perform_operation(self, other, operation): return NotImplemented |
---|
| 451 | |
---|
| 452 | def __add__(self, other): |
---|
| 453 | """ |
---|
| 454 | Add two data sets |
---|
| 455 | |
---|
| 456 | @param other: data set to add to the current one |
---|
| 457 | @return: new data set |
---|
| 458 | @raise ValueError: raised when two data sets are incompatible |
---|
| 459 | """ |
---|
| 460 | def operation(a, b): return a+b |
---|
| 461 | return self._perform_operation(other, operation) |
---|
| 462 | |
---|
| 463 | def __radd__(self, other): |
---|
| 464 | """ |
---|
| 465 | Add two data sets |
---|
| 466 | |
---|
| 467 | @param other: data set to add to the current one |
---|
| 468 | @return: new data set |
---|
| 469 | @raise ValueError: raised when two data sets are incompatible |
---|
| 470 | """ |
---|
| 471 | def operation(a, b): return b+a |
---|
| 472 | return self._perform_operation(other, operation) |
---|
| 473 | |
---|
| 474 | def __sub__(self, other): |
---|
| 475 | """ |
---|
| 476 | Subtract two data sets |
---|
| 477 | |
---|
| 478 | @param other: data set to subtract from the current one |
---|
| 479 | @return: new data set |
---|
| 480 | @raise ValueError: raised when two data sets are incompatible |
---|
| 481 | """ |
---|
| 482 | def operation(a, b): return a-b |
---|
| 483 | return self._perform_operation(other, operation) |
---|
| 484 | |
---|
| 485 | def __rsub__(self, other): |
---|
| 486 | """ |
---|
| 487 | Subtract two data sets |
---|
| 488 | |
---|
| 489 | @param other: data set to subtract from the current one |
---|
| 490 | @return: new data set |
---|
| 491 | @raise ValueError: raised when two data sets are incompatible |
---|
| 492 | """ |
---|
| 493 | def operation(a, b): return b-a |
---|
| 494 | return self._perform_operation(other, operation) |
---|
| 495 | |
---|
| 496 | def __mul__(self, other): |
---|
| 497 | """ |
---|
| 498 | Multiply two data sets |
---|
| 499 | |
---|
| 500 | @param other: data set to subtract from the current one |
---|
| 501 | @return: new data set |
---|
| 502 | @raise ValueError: raised when two data sets are incompatible |
---|
| 503 | """ |
---|
| 504 | def operation(a, b): return a*b |
---|
| 505 | return self._perform_operation(other, operation) |
---|
| 506 | |
---|
| 507 | def __rmul__(self, other): |
---|
| 508 | """ |
---|
| 509 | Multiply two data sets |
---|
| 510 | |
---|
| 511 | @param other: data set to subtract from the current one |
---|
| 512 | @return: new data set |
---|
| 513 | @raise ValueError: raised when two data sets are incompatible |
---|
| 514 | """ |
---|
| 515 | def operation(a, b): return b*a |
---|
| 516 | return self._perform_operation(other, operation) |
---|
| 517 | |
---|
| 518 | def __div__(self, other): |
---|
| 519 | """ |
---|
| 520 | Divided a data set by another |
---|
| 521 | |
---|
| 522 | @param other: data set that the current one is divided by |
---|
| 523 | @return: new data set |
---|
| 524 | @raise ValueError: raised when two data sets are incompatible |
---|
| 525 | """ |
---|
| 526 | def operation(a, b): return a/b |
---|
| 527 | return self._perform_operation(other, operation) |
---|
| 528 | |
---|
| 529 | def __rdiv__(self, other): |
---|
| 530 | """ |
---|
| 531 | Divided a data set by another |
---|
| 532 | |
---|
| 533 | @param other: data set that the current one is divided by |
---|
| 534 | @return: new data set |
---|
| 535 | @raise ValueError: raised when two data sets are incompatible |
---|
| 536 | """ |
---|
| 537 | def operation(a, b): return b/a |
---|
| 538 | return self._perform_operation(other, operation) |
---|
| 539 | |
---|
[a3084ada] | 540 | class Data1D(plottable_1D, DataInfo): |
---|
| 541 | """ |
---|
| 542 | 1D data class |
---|
| 543 | """ |
---|
[ca10d8e] | 544 | x_unit = '1/A' |
---|
| 545 | y_unit = '1/cm' |
---|
[8780e9a] | 546 | |
---|
[a3084ada] | 547 | def __init__(self, x, y, dx=None, dy=None): |
---|
[b99ac227] | 548 | DataInfo.__init__(self) |
---|
[a3084ada] | 549 | plottable_1D.__init__(self, x, y, dx, dy) |
---|
[b99ac227] | 550 | |
---|
[a3084ada] | 551 | |
---|
| 552 | def __str__(self): |
---|
| 553 | """ |
---|
| 554 | Nice printout |
---|
| 555 | """ |
---|
[99d1af6] | 556 | _str = "%s\n" % DataInfo.__str__(self) |
---|
| 557 | |
---|
[a3084ada] | 558 | _str += "Data:\n" |
---|
| 559 | _str += " Type: %s\n" % self.__class__.__name__ |
---|
| 560 | _str += " X-axis: %s\t[%s]\n" % (self._xaxis, self._xunit) |
---|
| 561 | _str += " Y-axis: %s\t[%s]\n" % (self._yaxis, self._yunit) |
---|
| 562 | _str += " Length: %g\n" % len(self.x) |
---|
| 563 | |
---|
| 564 | return _str |
---|
| 565 | |
---|
[7d8094b] | 566 | def clone_without_data(self, length=0, clone=None): |
---|
[b39c817] | 567 | """ |
---|
| 568 | Clone the current object, without copying the data (which |
---|
| 569 | will be filled out by a subsequent operation). |
---|
| 570 | The data arrays will be initialized to zero. |
---|
| 571 | |
---|
| 572 | @param length: length of the data array to be initialized |
---|
[7d8094b] | 573 | @param clone: if provided, the data will be copied to clone |
---|
[b39c817] | 574 | """ |
---|
[9198b83] | 575 | from copy import deepcopy |
---|
| 576 | |
---|
[7d8094b] | 577 | if clone is None or not issubclass(clone.__class__, Data1D): |
---|
| 578 | x = numpy.zeros(length) |
---|
| 579 | dx = numpy.zeros(length) |
---|
| 580 | y = numpy.zeros(length) |
---|
| 581 | dy = numpy.zeros(length) |
---|
| 582 | clone = Data1D(x, y, dx=dx, dy=dy) |
---|
[9198b83] | 583 | |
---|
| 584 | clone.title = self.title |
---|
| 585 | clone.run = self.run |
---|
| 586 | clone.filename = self.filename |
---|
| 587 | clone.notes = deepcopy(self.notes) |
---|
| 588 | clone.process = deepcopy(self.process) |
---|
| 589 | clone.detector = deepcopy(self.detector) |
---|
| 590 | clone.sample = deepcopy(self.sample) |
---|
| 591 | clone.source = deepcopy(self.source) |
---|
| 592 | clone.collimation = deepcopy(self.collimation) |
---|
| 593 | clone.meta_data = deepcopy(self.meta_data) |
---|
| 594 | clone.errors = deepcopy(self.errors) |
---|
| 595 | |
---|
| 596 | return clone |
---|
| 597 | |
---|
| 598 | def _validity_check(self, other): |
---|
| 599 | """ |
---|
| 600 | Checks that the data lengths are compatible. |
---|
| 601 | Checks that the x vectors are compatible. |
---|
| 602 | Returns errors vectors equal to original |
---|
| 603 | errors vectors if they were present or vectors |
---|
| 604 | of zeros when none was found. |
---|
| 605 | |
---|
| 606 | @param other: other data set for operation |
---|
| 607 | @return: dy for self, dy for other [numpy arrays] |
---|
| 608 | @raise ValueError: when lengths are not compatible |
---|
| 609 | """ |
---|
| 610 | dy_other = None |
---|
| 611 | if isinstance(other, Data1D): |
---|
| 612 | # Check that data lengths are the same |
---|
| 613 | if len(self.x) != len(other.x) or \ |
---|
| 614 | len(self.y) != len(other.y): |
---|
| 615 | raise ValueError, "Unable to perform operation: data length are not equal" |
---|
| 616 | |
---|
| 617 | # Here we could also extrapolate between data points |
---|
| 618 | for i in range(len(self.x)): |
---|
| 619 | if self.x[i] != other.x[i]: |
---|
| 620 | raise ValueError, "Incompatible data sets: x-values do not match" |
---|
| 621 | |
---|
| 622 | # Check that the other data set has errors, otherwise |
---|
| 623 | # create zero vector |
---|
| 624 | dy_other = other.dy |
---|
| 625 | if other.dy==None or (len(other.dy) != len(other.y)): |
---|
| 626 | dy_other = numpy.zeros(len(other.y)) |
---|
| 627 | |
---|
| 628 | # Check that we have errors, otherwise create zero vector |
---|
| 629 | dy = self.dy |
---|
| 630 | if self.dy==None or (len(self.dy) != len(self.y)): |
---|
| 631 | dy = numpy.zeros(len(self.y)) |
---|
| 632 | |
---|
| 633 | return dy, dy_other |
---|
[a3084ada] | 634 | |
---|
[9198b83] | 635 | def _perform_operation(self, other, operation): |
---|
| 636 | """ |
---|
| 637 | """ |
---|
| 638 | # First, check the data compatibility |
---|
| 639 | dy, dy_other = self._validity_check(other) |
---|
| 640 | result = self.clone_without_data(len(self.x)) |
---|
| 641 | |
---|
| 642 | for i in range(len(self.x)): |
---|
| 643 | result.x[i] = self.x[i] |
---|
| 644 | if self.dx is not None and len(self.x)==len(self.dx): |
---|
| 645 | result.dx[i] = self.dx[i] |
---|
| 646 | |
---|
| 647 | a = Uncertainty(self.y[i], dy[i]**2) |
---|
| 648 | if isinstance(other, Data1D): |
---|
| 649 | b = Uncertainty(other.y[i], dy_other[i]**2) |
---|
| 650 | else: |
---|
| 651 | b = other |
---|
| 652 | |
---|
| 653 | output = operation(a, b) |
---|
| 654 | result.y[i] = output.x |
---|
| 655 | result.dy[i] = math.sqrt(math.fabs(output.variance)) |
---|
| 656 | return result |
---|
| 657 | |
---|
[99d1af6] | 658 | class Data2D(plottable_2D, DataInfo): |
---|
| 659 | """ |
---|
| 660 | 2D data class |
---|
| 661 | """ |
---|
| 662 | ## Units for Q-values |
---|
[ca10d8e] | 663 | Q_unit = '1/A' |
---|
[99d1af6] | 664 | |
---|
| 665 | ## Units for I(Q) values |
---|
[ca10d8e] | 666 | I_unit = '1/cm' |
---|
[99d1af6] | 667 | |
---|
| 668 | ## Vector of Q-values at the center of each bin in x |
---|
[d6513cd] | 669 | x_bins = None |
---|
[99d1af6] | 670 | |
---|
| 671 | ## Vector of Q-values at the center of each bin in y |
---|
[d6513cd] | 672 | y_bins = None |
---|
[99d1af6] | 673 | |
---|
| 674 | |
---|
| 675 | def __init__(self, data=None, err_data=None): |
---|
[d6513cd] | 676 | self.y_bins = [] |
---|
| 677 | self.x_bins = [] |
---|
[b99ac227] | 678 | DataInfo.__init__(self) |
---|
[99d1af6] | 679 | plottable_2D.__init__(self, data, err_data) |
---|
[b99ac227] | 680 | if len(self.detector)>0: |
---|
| 681 | raise RuntimeError, "Data2D: Detector bank already filled at init" |
---|
[99d1af6] | 682 | |
---|
| 683 | def __str__(self): |
---|
| 684 | _str = "%s\n" % DataInfo.__str__(self) |
---|
| 685 | |
---|
| 686 | _str += "Data:\n" |
---|
| 687 | _str += " Type: %s\n" % self.__class__.__name__ |
---|
| 688 | _str += " X- & Y-axis: %s\t[%s]\n" % (self._yaxis, self._yunit) |
---|
| 689 | _str += " Z-axis: %s\t[%s]\n" % (self._zaxis, self._zunit) |
---|
| 690 | leny = 0 |
---|
| 691 | if len(self.data)>0: |
---|
| 692 | leny = len(self.data[0]) |
---|
| 693 | _str += " Length: %g x %g\n" % (len(self.data), leny) |
---|
| 694 | |
---|
| 695 | return _str |
---|
| 696 | |
---|
[7d8094b] | 697 | def clone_without_data(self, length=0, clone=None): |
---|
[442f42f] | 698 | """ |
---|
| 699 | Clone the current object, without copying the data (which |
---|
| 700 | will be filled out by a subsequent operation). |
---|
| 701 | The data arrays will be initialized to zero. |
---|
| 702 | |
---|
| 703 | @param length: length of the data array to be initialized |
---|
[7d8094b] | 704 | @param clone: if provided, the data will be copied to clone |
---|
[442f42f] | 705 | """ |
---|
| 706 | from copy import deepcopy |
---|
| 707 | |
---|
[7d8094b] | 708 | if clone is None or not issubclass(clone.__class__, Data2D): |
---|
| 709 | data = numpy.zeros(length) |
---|
| 710 | err_data = numpy.zeros(length) |
---|
| 711 | clone = Data2D(data, err_data) |
---|
| 712 | |
---|
[442f42f] | 713 | clone.title = self.title |
---|
| 714 | clone.run = self.run |
---|
| 715 | clone.filename = self.filename |
---|
| 716 | clone.notes = deepcopy(self.notes) |
---|
| 717 | clone.process = deepcopy(self.process) |
---|
| 718 | clone.detector = deepcopy(self.detector) |
---|
| 719 | clone.sample = deepcopy(self.sample) |
---|
| 720 | clone.source = deepcopy(self.source) |
---|
| 721 | clone.collimation = deepcopy(self.collimation) |
---|
| 722 | clone.meta_data = deepcopy(self.meta_data) |
---|
| 723 | clone.errors = deepcopy(self.errors) |
---|
| 724 | |
---|
| 725 | return clone |
---|
| 726 | |
---|
| 727 | |
---|
| 728 | def _validity_check(self, other): |
---|
| 729 | """ |
---|
| 730 | Checks that the data lengths are compatible. |
---|
| 731 | Checks that the x vectors are compatible. |
---|
| 732 | Returns errors vectors equal to original |
---|
| 733 | errors vectors if they were present or vectors |
---|
| 734 | of zeros when none was found. |
---|
| 735 | |
---|
| 736 | @param other: other data set for operation |
---|
| 737 | @return: dy for self, dy for other [numpy arrays] |
---|
| 738 | @raise ValueError: when lengths are not compatible |
---|
| 739 | """ |
---|
| 740 | err_other = None |
---|
| 741 | if isinstance(other, Data2D): |
---|
| 742 | # Check that data lengths are the same |
---|
| 743 | if numpy.size(self.data) != numpy.size(other.data): |
---|
| 744 | raise ValueError, "Unable to perform operation: data length are not equal" |
---|
| 745 | |
---|
| 746 | # Check that the scales match |
---|
| 747 | #TODO: matching scales? |
---|
| 748 | |
---|
| 749 | # Check that the other data set has errors, otherwise |
---|
| 750 | # create zero vector |
---|
| 751 | #TODO: test this |
---|
| 752 | err_other = other.err_data |
---|
| 753 | if other.err_data==None or (numpy.size(other.err_data) != numpy.size(other.data)): |
---|
| 754 | err_other = numpy.zeros([numpy.size(other.data,0), numpy.size(other.data,1)]) |
---|
| 755 | |
---|
| 756 | # Check that we have errors, otherwise create zero vector |
---|
| 757 | err = self.err_data |
---|
| 758 | if self.err_data==None or (numpy.size(self.err_data) != numpy.size(self.data)): |
---|
| 759 | err = numpy.zeros([numpy.size(self.data,0), numpy.size(self.data,1)]) |
---|
| 760 | |
---|
| 761 | return err, err_other |
---|
| 762 | |
---|
| 763 | |
---|
| 764 | def _perform_operation(self, other, operation): |
---|
| 765 | """ |
---|
| 766 | Perform 2D operations between data sets |
---|
| 767 | |
---|
| 768 | @param other: other data set |
---|
| 769 | @param operation: function defining the operation |
---|
| 770 | """ |
---|
| 771 | # First, check the data compatibility |
---|
| 772 | dy, dy_other = self._validity_check(other) |
---|
| 773 | |
---|
| 774 | result = self.clone_without_data([numpy.size(self.data,0), numpy.size(self.data,1)]) |
---|
| 775 | |
---|
| 776 | for i in range(numpy.size(self.data,0)): |
---|
| 777 | for j in range(numpy.size(self.data,1)): |
---|
| 778 | result.data[i][j] = self.data[i][j] |
---|
| 779 | if self.err_data is not None and numpy.size(self.data)==numpy.size(self.err_data): |
---|
| 780 | result.err_data[i][j] = self.err_data[i][j] |
---|
| 781 | |
---|
| 782 | a = Uncertainty(self.data[i][j], dy[i][j]**2) |
---|
| 783 | if isinstance(other, Data2D): |
---|
| 784 | b = Uncertainty(other.data[i][j], dy_other[i][j]**2) |
---|
| 785 | else: |
---|
| 786 | b = other |
---|
| 787 | |
---|
| 788 | output = operation(a, b) |
---|
| 789 | result.data[i][j] = output.x |
---|
| 790 | result.err_data[i][j] = math.sqrt(math.fabs(output.variance)) |
---|
| 791 | return result |
---|
[2c0f2a5] | 792 | |
---|
| 793 | |
---|
| 794 | |
---|