io

This module contains:

Parsing functions used to extract information from experimental files.

Classes used to help make parsing more modular. These include the NexusBase class and its children.

Module Contents

Classes

NexusBase

This class contains mostly beamline agnostic nexus parsing convenience stuff. It’s worth noting that this class still makes a series of assumptions about how data is laid out in a nexus file that can be broken. Instead of striving for some impossible perfection, this class is practical in its assumptions of how data is laid out in a .nxs file, and will raise if an assumption is violated. All instrument-specific assumptions that one must inevitably make to extract truly meaningful information from a nexus file are made in children of this class.

I07Nexus

This class extends NexusBase with methods useful for scraping information from nexus files produced at the I07 beamline at Diamond.

Functions

i07_dat_to_dict_dataframe

Parses a .dat file recorded by I07, returning a [now mostly obsolete] tuple containing a metadata dictionary and a pandas dataframe of the data.

load_images_from_h5

Loads images from a .h5 file.

i07_nxs_parser

Parses a .nxs file acquired from the I07 beamline at diamond, returning an instance of Scan2D. This process involves loading the images contained in the .h5 file pointed at by the .nxs file, as well as retrieving the metadata from the .nxs file that is relevant for XRR reduction.

_try_to_find_files

Check that data files exist if the file parsed by parser pointed to a separate file containing intensity information. If the intensity data file could not be found in its original location, check a series of probable locations for the data file. If the data file is found in one of these locations, update file’s entry in self.data.

API

class io.NexusBase(local_path: str)

Bases: islatu.metadata.Metadata

This class contains mostly beamline agnostic nexus parsing convenience stuff. It’s worth noting that this class still makes a series of assumptions about how data is laid out in a nexus file that can be broken. Instead of striving for some impossible perfection, this class is practical in its assumptions of how data is laid out in a .nxs file, and will raise if an assumption is violated. All instrument-specific assumptions that one must inevitably make to extract truly meaningful information from a nexus file are made in children of this class.

Attrs:
file_path:

The local path to the file on the local filesystem.

nxfile:

The object produced by loading the file at file_path with nxload.

Initialization

property src_path

The name of this nexus file, as it was recorded when the nexus file was written.

property detector

Returns the NXdetector instance stored in this NexusFile.

Raises:

ValueError if more than one NXdetector is found.

property instrument

Returns the NXinstrument instanced stored in this NexusFile.

Raises:

ValueError if more than one NXinstrument is found.

property entry: nexusformat.nexus.tree.NXentry

Returns this nexusfile’s entry.

Raises:

ValueError if more than one entry is found.

property default_signal: numpy.ndarray

The numpy array of intensities pointed to by the signal attribute in the nexus file.

property default_axis: numpy.ndarray

Returns the nxdata associated with the default axis.

property default_signal_name

Returns the name of the default signal.

property default_axis_name: str

Returns the name of the default axis.

property default_nxdata_name

Returns the name of the default nxdata.

property default_nxdata: numpy.ndarray

Returns the default NXdata.

abstract property default_axis_type: str
class io.I07Nexus(local_path: str)

Bases: io.NexusBase

This class extends NexusBase with methods useful for scraping information from nexus files produced at the I07 beamline at Diamond.

Initialization

excalibur_detector_2021 = 'excroi'
excalibur_04_2022 = 'exr'
pilatus_02_2024 = 'PILATUS'
excalibur_08_2024 = 'EXCALIBUR'
property local_data_path: str

The local path to the data (.h5) file. Note that this isn’t in the NexusBase class because it need not be reasonably expected to point at a .h5 file.

Raises:

FileNotFoundError if the data file cant be found.

property detector_name: str

Returns the name of the detector that we’re using. Because life sucks, this is a function of time.

property default_axis_name: str

Returns the name of the default axis.

property default_axis_type: str

Returns the type of our default axis, either being ‘q’, ‘th’ or ‘tth’.

_get_ith_region(i: int)

Returns the ith region of interest found in the .nxs file.

Args:
i:

The region of interest number to return. This number should match the ROI name as found in the .nxs file (generally not 0 indexed).

Returns:

The ith region of interest found in the .nxs file.

get_string_for_json(instrument_string)
region_from_json(json_str)
property signal_regions: List[islatu.region.Region]

Returns a list of region objects that define the location of the signal. Currently there is nothing better to do than assume that this is a list of length 1.

property background_regions: List[islatu.region.Region]

Returns a list of region objects that define the location of background. Currently we just ignore the zeroth region and call the rest of them background regions.

property probe_energy

Returns the energy of the probe particle parsed from this NexusFile.

property transmission

Proportional to the fraction of probe particles allowed by an attenuator to strike the sample.

property detector_distance

Returns the distance between sample and detector.

property _src_data_path

Returns the raw path to the data file. This is useless if you aren’t on site, but used by islatu to guess where you’ve stored the data file locally.

property _region_keys: List[str]

Parses all of the detector’s dictionary keys and returns all keys relating to regions of interest.

property _number_of_regions: int

Returns the number of regions of interest described by this nexus file. This assumes that the region keys take the form f’region_{an_int}’.

_get_region_bounds_key(region_no: int, kind: str) List[str]

Returns the detector key relating to the bounds of the region of interest corresponding to region_no.

Args:
region_no:

An integer corresponding the the particular region of interest we’re interested in generating a key for.

kind:
The kind of region bounds keys we’re interested in. This can take the values:

‘x_1’, ‘width’, ‘y_1’, ‘height’

where ‘1’ can be replaced with ‘start’ and with/without caps on first letter of width/height.

Raises:

ValueError if ‘kind’ argument is not one of the above.

Returns:

A list of region bounds keys that is ordered by region number.

io.i07_dat_to_dict_dataframe(file_path)

Parses a .dat file recorded by I07, returning a [now mostly obsolete] tuple containing a metadata dictionary and a pandas dataframe of the data.

Though outdated, this is still a handy way to parse the DCD normalization .dat file.

Args:

(str): The .dat file to be read.

Returns:
tuple: Containing:
  • dict: The metadata from the .dat file.

  • pandas.DataFrame: The data from the .dat file.

io.load_images_from_h5(h5_file_path, datanxfilepath, transpose=False)

Loads images from a .h5 file.

Args:
h5_file_path:

Path to the h5 file from which we’re loading images.

transpose:

Should we take the transpose of these images? Defaults to True.

io.i07_nxs_parser(file_path: str, remove_indices=None, adjustments=None)

Parses a .nxs file acquired from the I07 beamline at diamond, returning an instance of Scan2D. This process involves loading the images contained in the .h5 file pointed at by the .nxs file, as well as retrieving the metadata from the .nxs file that is relevant for XRR reduction.

Args:
file_path:

Path to the .nxs file.

Returns:

An initialized Scan2D object containing all loaded detector frames, as well as the relevant metadata from the .nxs file.

io._try_to_find_files(filenames: List[str], additional_search_paths: List[str])

Check that data files exist if the file parsed by parser pointed to a separate file containing intensity information. If the intensity data file could not be found in its original location, check a series of probable locations for the data file. If the data file is found in one of these locations, update file’s entry in self.data.

Returns:
list of str:

List of the corrected, actual paths to the files.