4. io functions (specsanalyzer.io)

io functions for the specsanalyzer package

This module contains file input/output functions for the specsanalyzer module

specsanalyzer.io.recursive_write_metadata(h5group, node)

Recurses through a python dictionary and writes it into an hdf5 file.

Parameters:
  • h5group (h5py.Group) – hdf5 group element where to store the current dict node to.

  • node (dict) – dictionary node to store

Raises:
  • Warning – warns if elements have been converted into strings for saving.

  • ValueError – Rises when elements cannot be saved even as strings.

specsanalyzer.io.recursive_parse_metadata(node)

Recurses through an hdf5 file, and parse it into a dictionary.

Parameters:

node (h5py.Group | h5py.Dataset) – hdf5 group or dataset to parse into dictionary.

Returns:

Dictionary of elements in the hdf5 path contained in node

Return type:

dict

specsanalyzer.io.to_h5(data, faddr, mode='w')

Save xarray formatted data to hdf5

Parameters:
  • data (xr.DataArray) – input data

  • faddr (str) – complete file name (including path)

  • mode (str) – hdf5 read/write mode

Raises:

Warning – subfunction warns if elements have been converted into strings for saving.

specsanalyzer.io.load_h5(faddr, mode='r')

Read xarray data from formatted hdf5 file

Parameters:
  • faddr (str) – complete file name (including path)

  • mode (str, optional) – hdf5 read/write mode. Defaults to “r”

Returns:

output xarray data

Return type:

xr.DataArray

specsanalyzer.io.to_tiff(data, faddr, alias_dict=None)

Save an array as a .tiff stack compatible with ImageJ

Parameters:
  • data (xr.DataArray | np.ndarray) – data to be saved. If a np.ndarray, the order is retained. If it is an xarray.DataArray, the order is inferred from axis_dict instead. ImageJ likes tiff files with axis order as TZCYXS. Therefore, best axis order in input should be: Time, Energy, posY, posX. The channels ‘C’ and ‘S’ are automatically added and can be ignored.

  • faddr (Path | str) – full path and name of file to save.

  • alias_dict (dict, optional) – name pairs for correct axis ordering. Keys should be any of T,Z,C,Y,X,S. The Corresponding value should be a dimension of the xarray or the dimension number if a numpy array. This is used to sort the data in the correct order for imagej standards. If None it tries to guess the order from the name of the axes or assumes T,Z,C,Y,X,S order for numpy arrays. Defaults to None.

Raises:
  • AttributeError – if more than one axis corresponds to a single dimension

  • NotImplementedError – if data is not 2,3 or 4 dimensional

  • TypeError – if data is not a np.ndarray or an xarray.DataArray

specsanalyzer.io._sort_dims_for_imagej(dims, alias_dict=None)

Guess the order of the dimensions from the alias dictionary

Parameters:
  • dims (list) – the list of dimensions to sort

  • alias_dict (dict, optional) – name pairs for correct axis ordering. Keys should be any of T,Z,C,Y,X,S. The Corresponding value should be a dimension of the xarray or the dimension number if a numpy array. This is used to sort the data in the correct order for imagej standards. If None it tries to guess the order from the name of the axes or assumes T,Z,C,Y,X,S order for numpy arrays. Defaults to None.

Raises:
  • ValueError – for duplicate entries for a single imagej dimension

  • NameError – when a dimension cannot be found in the alias dictionary

Returns:

List of sorted dimensions

Return type:

list

specsanalyzer.io._fill_missing_dims(dims, alias_dict=None)

Guess the order of the dimensions from the alias dictionary

Parameters:
  • dims (list) – the list of dimensions to sort

  • alias_dict (dict, optional) – name pairs for correct axis ordering. Keys should be any of T,Z,C,Y,X,S. The Corresponding value should be a dimension of the xarray or the dimension number if a numpy array. This is used to sort the data in the correct order for imagej standards. If None it tries to guess the order from the name of the axes or assumes T,Z,C,Y,X,S order for numpy arrays. Defaults to None.

Raises:
  • ValueError – for duplicate entries for a single imagej dimension

  • NameError – when a dimension cannot be found in the alias dictionary

Returns:

List of extended dimensions

Return type:

list

specsanalyzer.io.load_tiff(faddr, coords=None, dims=None, attrs=None)

Loads a tiff stack to an xarray.

The .tiff format does not retain information on the axes, so these need to be manually added with the axes argument. Otherwise, this returns the data only as np.ndarray

Parameters:
  • faddr (str | Path) – Path to file to load.

  • coords (dict, optional) – The axes describing the data, following the tiff stack order:

  • dims (Sequence, optional) – the order of the coordinates provided, considering the data is ordered as TZCYXS. If None (default) it infers the order from the order of the coords dictionary.

  • attrs (dict, optional) – dictionary to add as attributes to the xarray.DataArray

Returns:

an xarray representing the data loaded from the .tiff file

Return type:

xr.DataArray

specsanalyzer.io.to_nexus(data, faddr, reader, definition, input_files, **kwds)

Saves the x-array provided to a NeXus file at faddr, using the provided reader, NeXus definition and configuration file.

Parameters:
  • data (xr.DataArray) – The data to save, containing metadata definitions in data._attrs[“metadata”].

  • faddr (str) – The file path to save to.

  • reader (str) – The name of the NeXus reader to use.

  • definition (str) – The NeXus definition to use.

  • input_files (str | Sequence[str]) – The file path to the configuration file to use.

  • **kwds – Keyword arguments for pynxtools.dataconverter.convert.

specsanalyzer.io.get_pair_from_list(list_line)

Returns key value pair for the read function where a line in the file contains ‘=’ character.

Parameters:

list_line (list) – list of splitted line from the file.

Returns:

List of a tuple containing key value pair.

Return type:

list

specsanalyzer.io.read_calib2d(filepath)

Reads the calib2d file into a convenient list for the parser function containing useful and cleaned data.

Parameters:

filepath (str) – Path to file to load.

Returns:

List containing dictionary, string and float objects.

Return type:

list

specsanalyzer.io.parse_calib2d_to_dict(filepath)

Parses the given calib2d file into a nested dictionary structure to provide parameters for image conversion.

Parameters:

filepath (str) – Path to file to load.

Returns:

Populated nested dictionary parsed from the provided calib2d file.

Return type:

dict

specsanalyzer.io.get_modes_from_calib_dict(calib_dict)

create a list of supported modes, divided in spatial and angular modes

Parameters:
  • calib_dict (dict) – the calibration dictionary, created with the io

  • parse_calib2d_to_dict

Returns:

lists of supported angular and spatial lens modes

Return type:

tuple[list, list]