Changeset 845144e in sasview for src/sas/sascalc/dataloader/readers/cansas_reader_HDF5.py
- Timestamp:
- Oct 17, 2018 4:30:31 PM (6 years ago)
- Branches:
- master, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, unittest-saveload
- Children:
- 9fb4572
- Parents:
- b204004 (diff), 67ed543 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/readers/cansas_reader_HDF5.py
r61f329f0 rb204004 12 12 Data1D, Data2D, DataInfo, Process, Aperture, Collimation, \ 13 13 TransmissionSpectrum, Detector 14 from ..data_info import combine_data_info_with_plottable15 14 from ..loader_exceptions import FileContentsException, DefaultReaderException 16 15 from ..file_reader_base_class import FileReader, decode 17 16 17 18 18 def h5attr(node, key, default=None): 19 19 return decode(node.attrs.get(key, default)) 20 20 21 21 22 class Reader(FileReader): … … 38 39 # CanSAS version 39 40 cansas_version = 2.0 40 # Logged warnings or messages41 logging = None42 # List of errors for the current data set43 errors = None44 # Raw file contents to be processed45 raw_data = None46 # List of plottable1D objects that should be linked to the current_datainfo47 data1d = None48 # List of plottable2D objects that should be linked to the current_datainfo49 data2d = None50 41 # Data type name 51 type_name = " CanSAS 2.0"42 type_name = "NXcanSAS" 52 43 # Wildcards 53 type = [" CanSAS 2.0 HDF5 Files (*.h5)|*.h5"]44 type = ["NXcanSAS HDF5 Files (*.h5)|*.h5|"] 54 45 # List of allowed extensions 55 46 ext = ['.h5', '.H5'] … … 81 72 except Exception as e: 82 73 if extension not in self.ext: 83 msg = "CanSAS2.0 HDF5 Reader could not load file {}".format(basename + extension) 74 msg = "NXcanSAS Reader could not load file {}".format( 75 basename + extension) 84 76 raise DefaultReaderException(msg) 85 77 raise FileContentsException(e.message) … … 95 87 self.raw_data.close() 96 88 97 for dataset in self.output: 98 if isinstance(dataset, Data1D): 99 if dataset.x.size < 5: 100 self.output = [] 101 raise FileContentsException("Fewer than 5 data points found.") 89 for data_set in self.output: 90 if isinstance(data_set, Data1D): 91 if data_set.x.size < 5: 92 exception = FileContentsException( 93 "Fewer than 5 data points found.") 94 data_set.errors.append(exception) 102 95 103 96 def reset_state(self): … … 111 104 self.errors = set() 112 105 self.logging = [] 106 self.q_names = [] 107 self.mask_name = u'' 108 self.i_name = u'' 109 self.i_node = u'' 110 self.i_uncertainties_name = u'' 111 self.q_uncertainty_names = [] 112 self.q_resolution_names = [] 113 113 self.parent_class = u'' 114 114 self.detector = Detector() … … 131 131 value = data.get(key) 132 132 class_name = h5attr(value, u'canSAS_class') 133 if isinstance(class_name, (list, tuple, np.ndarray)): 134 class_name = class_name[0] 133 135 if class_name is None: 134 136 class_name = h5attr(value, u'NX_class') … … 140 142 if isinstance(value, h5py.Group): 141 143 # Set parent class before recursion 144 last_parent_class = self.parent_class 142 145 self.parent_class = class_name 143 146 parent_list.append(key) … … 147 150 self.add_data_set(key) 148 151 elif class_prog.match(u'SASdata'): 149 self._initialize_new_data_set(parent_list) 152 self._initialize_new_data_set(value) 153 self._find_data_attributes(value) 150 154 # Recursion step to access data within the group 151 155 self.read_children(value, parent_list) 156 self.add_intermediate() 152 157 # Reset parent class when returning from recursive method 153 self.parent_class = class_name 154 self.add_intermediate() 158 self.parent_class = last_parent_class 155 159 parent_list.remove(key) 156 160 157 161 elif isinstance(value, h5py.Dataset): 158 162 # If this is a dataset, store the data appropriately 159 data_set = data[key][:]163 data_set = value.value 160 164 unit = self._get_unit(value) 161 162 # I and Q Data163 if key == u'I':164 if isinstance(self.current_dataset, plottable_2D):165 self.current_dataset.data = data_set166 self.current_dataset.zaxis("Intensity", unit)167 else:168 self.current_dataset.y = data_set.flatten()169 self.current_dataset.yaxis("Intensity", unit)170 continue171 elif key == u'Idev':172 if isinstance(self.current_dataset, plottable_2D):173 self.current_dataset.err_data = data_set.flatten()174 else:175 self.current_dataset.dy = data_set.flatten()176 continue177 elif key == u'Q':178 self.current_dataset.xaxis("Q", unit)179 if isinstance(self.current_dataset, plottable_2D):180 self.current_dataset.q = data_set.flatten()181 else:182 self.current_dataset.x = data_set.flatten()183 continue184 elif key == u'Qdev':185 self.current_dataset.dx = data_set.flatten()186 continue187 elif key == u'dQw':188 self.current_dataset.dxw = data_set.flatten()189 continue190 elif key == u'dQl':191 self.current_dataset.dxl = data_set.flatten()192 continue193 elif key == u'Qy':194 self.current_dataset.yaxis("Q_y", unit)195 self.current_dataset.qy_data = data_set.flatten()196 continue197 elif key == u'Qydev':198 self.current_dataset.dqy_data = data_set.flatten()199 continue200 elif key == u'Qx':201 self.current_dataset.xaxis("Q_x", unit)202 self.current_dataset.qx_data = data_set.flatten()203 continue204 elif key == u'Qxdev':205 self.current_dataset.dqx_data = data_set.flatten()206 continue207 elif key == u'Mask':208 self.current_dataset.mask = data_set.flatten()209 continue210 # Transmission Spectrum211 elif (key == u'T'212 and self.parent_class == u'SAStransmission_spectrum'):213 self.trans_spectrum.transmission = data_set.flatten()214 continue215 elif (key == u'Tdev'216 and self.parent_class == u'SAStransmission_spectrum'):217 self.trans_spectrum.transmission_deviation = \218 data_set.flatten()219 continue220 elif (key == u'lambda'221 and self.parent_class == u'SAStransmission_spectrum'):222 self.trans_spectrum.wavelength = data_set.flatten()223 continue224 165 225 166 for data_point in data_set: … … 232 173 if key == u'definition': 233 174 self.current_datainfo.meta_data['reader'] = data_point 175 # Run 234 176 elif key == u'run': 235 177 self.current_datainfo.run.append(data_point) … … 240 182 except Exception: 241 183 pass 184 # Title 242 185 elif key == u'title': 243 186 self.current_datainfo.title = data_point 187 # Note 244 188 elif key == u'SASnote': 245 189 self.current_datainfo.notes.append(data_point) 246 247 190 # Sample Information 248 # CanSAS 2.0 format 249 elif key == u'Title' and self.parent_class == u'SASsample': 250 self.current_datainfo.sample.name = data_point 251 # NXcanSAS format 252 elif key == u'name' and self.parent_class == u'SASsample': 253 self.current_datainfo.sample.name = data_point 254 # NXcanSAS format 255 elif key == u'ID' and self.parent_class == u'SASsample': 256 self.current_datainfo.sample.name = data_point 257 elif (key == u'thickness' 258 and self.parent_class == u'SASsample'): 259 self.current_datainfo.sample.thickness = data_point 260 elif (key == u'temperature' 261 and self.parent_class == u'SASsample'): 262 self.current_datainfo.sample.temperature = data_point 263 elif (key == u'transmission' 264 and self.parent_class == u'SASsample'): 265 self.current_datainfo.sample.transmission = data_point 266 elif (key == u'x_position' 267 and self.parent_class == u'SASsample'): 268 self.current_datainfo.sample.position.x = data_point 269 elif (key == u'y_position' 270 and self.parent_class == u'SASsample'): 271 self.current_datainfo.sample.position.y = data_point 272 elif key == u'pitch' and self.parent_class == u'SASsample': 273 self.current_datainfo.sample.orientation.x = data_point 274 elif key == u'yaw' and self.parent_class == u'SASsample': 275 self.current_datainfo.sample.orientation.y = data_point 276 elif key == u'roll' and self.parent_class == u'SASsample': 277 self.current_datainfo.sample.orientation.z = data_point 278 elif (key == u'details' 279 and self.parent_class == u'SASsample'): 280 self.current_datainfo.sample.details.append(data_point) 281 191 elif self.parent_class == u'SASsample': 192 self.process_sample(data_point, key) 282 193 # Instrumental Information 283 194 elif (key == u'name' 284 195 and self.parent_class == u'SASinstrument'): 285 196 self.current_datainfo.instrument = data_point 286 elif key == u'name' and self.parent_class == u'SASdetector': 287 self.detector.name = data_point 288 elif key == u'SDD' and self.parent_class == u'SASdetector': 289 self.detector.distance = float(data_point) 290 self.detector.distance_unit = unit 291 elif (key == u'slit_length' 292 and self.parent_class == u'SASdetector'): 293 self.detector.slit_length = float(data_point) 294 self.detector.slit_length_unit = unit 295 elif (key == u'x_position' 296 and self.parent_class == u'SASdetector'): 297 self.detector.offset.x = float(data_point) 298 self.detector.offset_unit = unit 299 elif (key == u'y_position' 300 and self.parent_class == u'SASdetector'): 301 self.detector.offset.y = float(data_point) 302 self.detector.offset_unit = unit 303 elif (key == u'pitch' 304 and self.parent_class == u'SASdetector'): 305 self.detector.orientation.x = float(data_point) 306 self.detector.orientation_unit = unit 307 elif key == u'roll' and self.parent_class == u'SASdetector': 308 self.detector.orientation.z = float(data_point) 309 self.detector.orientation_unit = unit 310 elif key == u'yaw' and self.parent_class == u'SASdetector': 311 self.detector.orientation.y = float(data_point) 312 self.detector.orientation_unit = unit 313 elif (key == u'beam_center_x' 314 and self.parent_class == u'SASdetector'): 315 self.detector.beam_center.x = float(data_point) 316 self.detector.beam_center_unit = unit 317 elif (key == u'beam_center_y' 318 and self.parent_class == u'SASdetector'): 319 self.detector.beam_center.y = float(data_point) 320 self.detector.beam_center_unit = unit 321 elif (key == u'x_pixel_size' 322 and self.parent_class == u'SASdetector'): 323 self.detector.pixel_size.x = float(data_point) 324 self.detector.pixel_size_unit = unit 325 elif (key == u'y_pixel_size' 326 and self.parent_class == u'SASdetector'): 327 self.detector.pixel_size.y = float(data_point) 328 self.detector.pixel_size_unit = unit 329 elif (key == u'distance' 330 and self.parent_class == u'SAScollimation'): 331 self.collimation.length = data_point 332 self.collimation.length_unit = unit 333 elif (key == u'name' 334 and self.parent_class == u'SAScollimation'): 335 self.collimation.name = data_point 336 elif (key == u'shape' 337 and self.parent_class == u'SASaperture'): 338 self.aperture.shape = data_point 339 elif (key == u'x_gap' 340 and self.parent_class == u'SASaperture'): 341 self.aperture.size.x = data_point 342 elif (key == u'y_gap' 343 and self.parent_class == u'SASaperture'): 344 self.aperture.size.y = data_point 345 197 # Detector 198 elif self.parent_class == u'SASdetector': 199 self.process_detector(data_point, key, unit) 200 # Collimation 201 elif self.parent_class == u'SAScollimation': 202 self.process_collimation(data_point, key, unit) 203 # Aperture 204 elif self.parent_class == u'SASaperture': 205 self.process_aperture(data_point, key) 346 206 # Process Information 347 elif (key == u'Title' 348 and self.parent_class == u'SASprocess'): # CanSAS 2.0 349 self.process.name = data_point 350 elif (key == u'name' 351 and self.parent_class == u'SASprocess'): # NXcanSAS 352 self.process.name = data_point 353 elif (key == u'description' 354 and self.parent_class == u'SASprocess'): 355 self.process.description = data_point 356 elif key == u'date' and self.parent_class == u'SASprocess': 357 self.process.date = data_point 358 elif key == u'term' and self.parent_class == u'SASprocess': 359 self.process.term = data_point 360 elif self.parent_class == u'SASprocess': 361 self.process.notes.append(data_point) 362 207 elif self.parent_class == u'SASprocess': # CanSAS 2.0 208 self.process_process(data_point, key) 363 209 # Source 364 elif (key == u'wavelength' 365 and self.parent_class == u'SASdata'): 366 self.current_datainfo.source.wavelength = data_point 367 self.current_datainfo.source.wavelength_unit = unit 368 elif (key == u'incident_wavelength' 369 and self.parent_class == 'SASsource'): 370 self.current_datainfo.source.wavelength = data_point 371 self.current_datainfo.source.wavelength_unit = unit 372 elif (key == u'wavelength_max' 373 and self.parent_class == u'SASsource'): 374 self.current_datainfo.source.wavelength_max = data_point 375 self.current_datainfo.source.wavelength_max_unit = unit 376 elif (key == u'wavelength_min' 377 and self.parent_class == u'SASsource'): 378 self.current_datainfo.source.wavelength_min = data_point 379 self.current_datainfo.source.wavelength_min_unit = unit 380 elif (key == u'incident_wavelength_spread' 381 and self.parent_class == u'SASsource'): 382 self.current_datainfo.source.wavelength_spread = \ 383 data_point 384 self.current_datainfo.source.wavelength_spread_unit = \ 385 unit 386 elif (key == u'beam_size_x' 387 and self.parent_class == u'SASsource'): 388 self.current_datainfo.source.beam_size.x = data_point 389 self.current_datainfo.source.beam_size_unit = unit 390 elif (key == u'beam_size_y' 391 and self.parent_class == u'SASsource'): 392 self.current_datainfo.source.beam_size.y = data_point 393 self.current_datainfo.source.beam_size_unit = unit 394 elif (key == u'beam_shape' 395 and self.parent_class == u'SASsource'): 396 self.current_datainfo.source.beam_shape = data_point 397 elif (key == u'radiation' 398 and self.parent_class == u'SASsource'): 399 self.current_datainfo.source.radiation = data_point 400 elif (key == u'transmission' 401 and self.parent_class == u'SASdata'): 402 self.current_datainfo.sample.transmission = data_point 403 210 elif self.parent_class == u'SASsource': 211 self.process_source(data_point, key, unit) 404 212 # Everything else goes in meta_data 213 elif self.parent_class == u'SASdata': 214 if isinstance(self.current_dataset, plottable_2D): 215 self.process_2d_data_object(data_set, key, unit) 216 else: 217 self.process_1d_data_object(data_set, key, unit) 218 219 break 220 elif self.parent_class == u'SAStransmission_spectrum': 221 self.process_trans_spectrum(data_set, key) 222 break 405 223 else: 406 224 new_key = self._create_unique_key( … … 411 229 # I don't know if this reachable code 412 230 self.errors.add("ShouldNeverHappenException") 231 232 def process_1d_data_object(self, data_set, key, unit): 233 """ 234 SASdata processor method for 1d data items 235 :param data_set: data from HDF5 file 236 :param key: canSAS_class attribute 237 :param unit: unit attribute 238 """ 239 if key == self.i_name: 240 self.current_dataset.y = data_set.flatten() 241 self.current_dataset.yaxis("Intensity", unit) 242 elif key == self.i_uncertainties_name: 243 self.current_dataset.dy = data_set.flatten() 244 elif key in self.q_names: 245 self.current_dataset.xaxis("Q", unit) 246 self.current_dataset.x = data_set.flatten() 247 elif key in self.q_resolution_names: 248 if (len(self.q_resolution_names) > 1 249 and np.where(self.q_resolution_names == key)[0] == 0): 250 self.current_dataset.dxw = data_set.flatten() 251 elif (len(self.q_resolution_names) > 1 252 and np.where(self.q_resolution_names == key)[0] == 1): 253 self.current_dataset.dxl = data_set.flatten() 254 else: 255 self.current_dataset.dx = data_set.flatten() 256 elif key in self.q_uncertainty_names: 257 if (len(self.q_uncertainty_names) > 1 258 and np.where(self.q_uncertainty_names == key)[0] == 0): 259 self.current_dataset.dxw = data_set.flatten() 260 elif (len(self.q_uncertainty_names) > 1 261 and np.where(self.q_uncertainty_names == key)[0] == 1): 262 self.current_dataset.dxl = data_set.flatten() 263 else: 264 self.current_dataset.dx = data_set.flatten() 265 elif key == self.mask_name: 266 self.current_dataset.mask = data_set.flatten() 267 elif key == u'wavelength': 268 self.current_datainfo.source.wavelength = data_set[0] 269 self.current_datainfo.source.wavelength_unit = unit 270 271 def process_2d_data_object(self, data_set, key, unit): 272 if key == self.i_name: 273 self.current_dataset.data = data_set 274 self.current_dataset.zaxis("Intensity", unit) 275 elif key == self.i_uncertainties_name: 276 self.current_dataset.err_data = data_set.flatten() 277 elif key in self.q_names: 278 self.current_dataset.xaxis("Q_x", unit) 279 self.current_dataset.yaxis("Q_y", unit) 280 if self.q_names[0] == self.q_names[1]: 281 # All q data in a single array 282 self.current_dataset.qx_data = data_set[0] 283 self.current_dataset.qy_data = data_set[1] 284 elif self.q_names.index(key) == 0: 285 self.current_dataset.qx_data = data_set 286 elif self.q_names.index(key) == 1: 287 self.current_dataset.qy_data = data_set 288 elif key in self.q_uncertainty_names or key in self.q_resolution_names: 289 if ((self.q_uncertainty_names[0] == self.q_uncertainty_names[1]) or 290 (self.q_resolution_names[0] == self.q_resolution_names[1])): 291 # All q data in a single array 292 self.current_dataset.dqx_data = data_set[0].flatten() 293 self.current_dataset.dqy_data = data_set[1].flatten() 294 elif (self.q_uncertainty_names.index(key) == 0 or 295 self.q_resolution_names.index(key) == 0): 296 self.current_dataset.dqx_data = data_set.flatten() 297 elif (self.q_uncertainty_names.index(key) == 1 or 298 self.q_resolution_names.index(key) == 1): 299 self.current_dataset.dqy_data = data_set.flatten() 300 self.current_dataset.yaxis("Q_y", unit) 301 elif key == self.mask_name: 302 self.current_dataset.mask = data_set.flatten() 303 elif key == u'Qy': 304 self.current_dataset.yaxis("Q_y", unit) 305 self.current_dataset.qy_data = data_set.flatten() 306 elif key == u'Qydev': 307 self.current_dataset.dqy_data = data_set.flatten() 308 elif key == u'Qx': 309 self.current_dataset.xaxis("Q_x", unit) 310 self.current_dataset.qx_data = data_set.flatten() 311 elif key == u'Qxdev': 312 self.current_dataset.dqx_data = data_set.flatten() 313 314 def process_trans_spectrum(self, data_set, key): 315 """ 316 SAStransmission_spectrum processor 317 :param data_set: data from HDF5 file 318 :param key: canSAS_class attribute 319 """ 320 if key == u'T': 321 self.trans_spectrum.transmission = data_set.flatten() 322 elif key == u'Tdev': 323 self.trans_spectrum.transmission_deviation = data_set.flatten() 324 elif key == u'lambda': 325 self.trans_spectrum.wavelength = data_set.flatten() 326 327 def process_sample(self, data_point, key): 328 """ 329 SASsample processor 330 :param data_point: Single point from an HDF5 data file 331 :param key: class name data_point was taken from 332 """ 333 if key == u'Title': 334 self.current_datainfo.sample.name = data_point 335 elif key == u'name': 336 self.current_datainfo.sample.name = data_point 337 elif key == u'ID': 338 self.current_datainfo.sample.name = data_point 339 elif key == u'thickness': 340 self.current_datainfo.sample.thickness = data_point 341 elif key == u'temperature': 342 self.current_datainfo.sample.temperature = data_point 343 elif key == u'transmission': 344 self.current_datainfo.sample.transmission = data_point 345 elif key == u'x_position': 346 self.current_datainfo.sample.position.x = data_point 347 elif key == u'y_position': 348 self.current_datainfo.sample.position.y = data_point 349 elif key == u'pitch': 350 self.current_datainfo.sample.orientation.x = data_point 351 elif key == u'yaw': 352 self.current_datainfo.sample.orientation.y = data_point 353 elif key == u'roll': 354 self.current_datainfo.sample.orientation.z = data_point 355 elif key == u'details': 356 self.current_datainfo.sample.details.append(data_point) 357 358 def process_detector(self, data_point, key, unit): 359 """ 360 SASdetector processor 361 :param data_point: Single point from an HDF5 data file 362 :param key: class name data_point was taken from 363 :param unit: unit attribute from data set 364 """ 365 if key == u'name': 366 self.detector.name = data_point 367 elif key == u'SDD': 368 self.detector.distance = float(data_point) 369 self.detector.distance_unit = unit 370 elif key == u'slit_length': 371 self.detector.slit_length = float(data_point) 372 self.detector.slit_length_unit = unit 373 elif key == u'x_position': 374 self.detector.offset.x = float(data_point) 375 self.detector.offset_unit = unit 376 elif key == u'y_position': 377 self.detector.offset.y = float(data_point) 378 self.detector.offset_unit = unit 379 elif key == u'pitch': 380 self.detector.orientation.x = float(data_point) 381 self.detector.orientation_unit = unit 382 elif key == u'roll': 383 self.detector.orientation.z = float(data_point) 384 self.detector.orientation_unit = unit 385 elif key == u'yaw': 386 self.detector.orientation.y = float(data_point) 387 self.detector.orientation_unit = unit 388 elif key == u'beam_center_x': 389 self.detector.beam_center.x = float(data_point) 390 self.detector.beam_center_unit = unit 391 elif key == u'beam_center_y': 392 self.detector.beam_center.y = float(data_point) 393 self.detector.beam_center_unit = unit 394 elif key == u'x_pixel_size': 395 self.detector.pixel_size.x = float(data_point) 396 self.detector.pixel_size_unit = unit 397 elif key == u'y_pixel_size': 398 self.detector.pixel_size.y = float(data_point) 399 self.detector.pixel_size_unit = unit 400 401 def process_collimation(self, data_point, key, unit): 402 """ 403 SAScollimation processor 404 :param data_point: Single point from an HDF5 data file 405 :param key: class name data_point was taken from 406 :param unit: unit attribute from data set 407 """ 408 if key == u'distance': 409 self.collimation.length = data_point 410 self.collimation.length_unit = unit 411 elif key == u'name': 412 self.collimation.name = data_point 413 414 def process_aperture(self, data_point, key): 415 """ 416 SASaperture processor 417 :param data_point: Single point from an HDF5 data file 418 :param key: class name data_point was taken from 419 """ 420 if key == u'shape': 421 self.aperture.shape = data_point 422 elif key == u'x_gap': 423 self.aperture.size.x = data_point 424 elif key == u'y_gap': 425 self.aperture.size.y = data_point 426 427 def process_source(self, data_point, key, unit): 428 """ 429 SASsource processor 430 :param data_point: Single point from an HDF5 data file 431 :param key: class name data_point was taken from 432 :param unit: unit attribute from data set 433 """ 434 if key == u'incident_wavelength': 435 self.current_datainfo.source.wavelength = data_point 436 self.current_datainfo.source.wavelength_unit = unit 437 elif key == u'wavelength_max': 438 self.current_datainfo.source.wavelength_max = data_point 439 self.current_datainfo.source.wavelength_max_unit = unit 440 elif key == u'wavelength_min': 441 self.current_datainfo.source.wavelength_min = data_point 442 self.current_datainfo.source.wavelength_min_unit = unit 443 elif key == u'incident_wavelength_spread': 444 self.current_datainfo.source.wavelength_spread = data_point 445 self.current_datainfo.source.wavelength_spread_unit = unit 446 elif key == u'beam_size_x': 447 self.current_datainfo.source.beam_size.x = data_point 448 self.current_datainfo.source.beam_size_unit = unit 449 elif key == u'beam_size_y': 450 self.current_datainfo.source.beam_size.y = data_point 451 self.current_datainfo.source.beam_size_unit = unit 452 elif key == u'beam_shape': 453 self.current_datainfo.source.beam_shape = data_point 454 elif key == u'radiation': 455 self.current_datainfo.source.radiation = data_point 456 457 def process_process(self, data_point, key): 458 """ 459 SASprocess processor 460 :param data_point: Single point from an HDF5 data file 461 :param key: class name data_point was taken from 462 """ 463 term_match = re.compile(u'^term[0-9]+$') 464 if key == u'Title': # CanSAS 2.0 465 self.process.name = data_point 466 elif key == u'name': # NXcanSAS 467 self.process.name = data_point 468 elif key == u'description': 469 self.process.description = data_point 470 elif key == u'date': 471 self.process.date = data_point 472 elif term_match.match(key): 473 self.process.term.append(data_point) 474 else: 475 self.process.notes.append(data_point) 413 476 414 477 def add_intermediate(self): … … 452 515 spectrum_list = [] 453 516 for spectrum in self.current_datainfo.trans_spectrum: 454 spectrum.transmission = np.delete(spectrum.transmission, [0])455 517 spectrum.transmission = spectrum.transmission.astype(np.float64) 456 spectrum.transmission_deviation = np.delete(457 spectrum.transmission_deviation, [0])458 518 spectrum.transmission_deviation = \ 459 519 spectrum.transmission_deviation.astype(np.float64) 460 spectrum.wavelength = np.delete(spectrum.wavelength, [0])461 520 spectrum.wavelength = spectrum.wavelength.astype(np.float64) 462 521 if len(spectrum.transmission) > 0: … … 490 549 if dataset.data.ndim == 2: 491 550 (n_rows, n_cols) = dataset.data.shape 492 dataset.y_bins = dataset.qy_data[0::n_cols] 493 dataset.x_bins = dataset.qx_data[:n_cols] 551 flat_qy = dataset.qy_data[0::n_cols].flatten() 552 # For 2D arrays of Qx and Qy, the Q value should be constant 553 # along each row -OR- each column. The direction is not 554 # specified in the NXcanSAS standard. 555 if flat_qy[0] == flat_qy[1]: 556 flat_qy = np.transpose(dataset.qy_data)[0::n_cols].flatten() 557 dataset.y_bins = np.unique(flat_qy) 558 flat_qx = dataset.qx_data[0::n_rows].flatten() 559 # For 2D arrays of Qx and Qy, the Q value should be constant 560 # along each row -OR- each column. The direction is not 561 # specified in the NXcanSAS standard. 562 if flat_qx[0] == flat_qx[1]: 563 flat_qx = np.transpose(dataset.qx_data)[0::n_rows].flatten() 564 dataset.x_bins = np.unique(flat_qx) 494 565 dataset.data = dataset.data.flatten() 566 dataset.qx_data = dataset.qx_data.flatten() 567 dataset.qy_data = dataset.qy_data.flatten() 495 568 self.current_dataset = dataset 496 569 self.send_to_output() … … 515 588 self.current_datainfo = DataInfo() 516 589 517 518 def _initialize_new_data_set(self, parent_list=None): 590 def _initialize_new_data_set(self, value=None): 519 591 """ 520 592 A private class method to generate a new 1D or 2D data object based on … … 524 596 :param parent_list: List of names of parent elements 525 597 """ 526 527 if parent_list is None: 528 parent_list = [] 529 if self._find_intermediate(parent_list, "Qx"): 598 if self._is2d(value): 530 599 self.current_dataset = plottable_2D() 531 600 else: … … 534 603 self.current_dataset = plottable_1D(x, y) 535 604 self.current_datainfo.filename = self.raw_data.filename 536 537 def _find_intermediate(self, parent_list, basename=""): 538 """ 539 A private class used to find an entry by either using a direct key or 540 knowing the approximate basename. 605 self.mask_name = u'' 606 self.i_name = u'' 607 self.i_node = u'' 608 self.i_uncertainties_name = u'' 609 self.q_names = [] 610 self.q_uncertainty_names = [] 611 self.q_resolution_names = [] 612 613 @staticmethod 614 def check_is_list_or_array(iterable): 615 try: 616 iter(iterable) 617 if (not isinstance(iterable, np.ndarray) and not isinstance( 618 iterable, list)) or (isinstance(iterable, basestring)): 619 raise TypeError 620 except TypeError: 621 iterable = iterable.split(",") 622 return iterable 623 624 def _find_data_attributes(self, value): 625 """ 626 A class to find the indices for Q, the name of the Qdev and Idev, and 627 the name of the mask. 628 :param value: SASdata/NXdata HDF5 Group 629 """ 630 attrs = value.attrs 631 signal = attrs.get("signal", "I") 632 i_axes = attrs.get("I_axes", ["Q"]) 633 q_indices = attrs.get("Q_indices", [0]) 634 q_indices = map(int, self.check_is_list_or_array(q_indices)) 635 i_axes = self.check_is_list_or_array(i_axes) 636 keys = value.keys() 637 self.mask_name = attrs.get("mask") 638 for val in q_indices: 639 self.q_names.append(i_axes[val]) 640 self.i_name = signal 641 self.i_node = value.get(self.i_name) 642 for item in self.q_names: 643 if item in keys: 644 q_vals = value.get(item) 645 if q_vals.attrs.get("uncertainties") is not None: 646 self.q_uncertainty_names = q_vals.attrs.get("uncertainties") 647 elif q_vals.attrs.get("uncertainty") is not None: 648 self.q_uncertainty_names = q_vals.attrs.get("uncertainty") 649 if isinstance(self.q_uncertainty_names, basestring): 650 self.q_uncertainty_names = self.q_uncertainty_names.split(",") 651 if q_vals.attrs.get("resolutions") is not None: 652 self.q_resolution_names = q_vals.attrs.get("resolutions") 653 if isinstance(self.q_resolution_names, basestring): 654 self.q_resolution_names = self.q_resolution_names.split(",") 655 if self.i_name in keys: 656 i_vals = value.get(self.i_name) 657 self.i_uncertainties_name = i_vals.attrs.get("uncertainties") 658 if self.i_uncertainties_name is None: 659 self.i_uncertainties_name = i_vals.attrs.get("uncertainty") 660 661 def _is2d(self, value, basename="I"): 662 """ 663 A private class to determine if the data set is 1d or 2d. 541 664 542 665 :param parent_list: List of parents nodes in the HDF5 file 543 666 :param basename: Approximate name of an entry to search for 544 :return: 545 """ 546 547 entry = False 548 key_prog = re.compile(basename) 549 top = self.raw_data 550 for parent in parent_list: 551 top = top.get(parent) 552 for key in top.keys(): 553 if key_prog.match(key): 554 entry = True 555 break 556 return entry 667 :return: True if 2D, otherwise false 668 """ 669 670 vals = value.get(basename) 671 return (vals is not None and vals.shape is not None 672 and len(vals.shape) != 1) 557 673 558 674 def _create_unique_key(self, dictionary, name, numb=0): … … 583 699 if unit is None: 584 700 unit = h5attr(value, u'unit') 585 # Convert the unit formats586 if unit == "1/A":587 unit = "A^{-1}"588 elif unit == "1/cm":589 unit = "cm^{-1}"590 701 return unit
Note: See TracChangeset
for help on using the changeset viewer.