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