algotom.io.loadersaver

Module for I/O tasks: - Load data from an image file (tif, png, jpeg) or a hdf/nxs file. - Get dataset information in a hdf/nxs file. - Search for datasets in a hdf/nxs file. - Save a 2D array as a tif image or 2D, 3D array to a hdf/nxs file. - Search file names, make a file/folder name. - Load distortion coefficients from a txt file.

Functions:

load_image(file_path)

Load data from an image.

get_hdf_information(file_path)

Get information of datasets in a hdf/nxs file.

find_hdf_key(file_path, pattern)

Find datasets matching the pattern in a hdf/nxs file.

load_hdf(file_path, key_path)

Load a hdf/nexus dataset as an object.

make_folder(file_path)

Create a folder if not exist.

make_file_name(file_path)

Create a new file name to avoid overwriting.

make_folder_name(folder_path[, name_prefix])

Create a new folder name to avoid overwriting.

find_file(path)

Search file

save_image(file_path, mat[, overwrite])

Save a 2D array to an image.

open_hdf_stream(file_path, data_shape[, …])

Write an array to a hdf/nxs file with options to add metadata.

load_distortion_coefficient(file_path)

Load distortion coefficients from a text file.

save_distortion_coefficient(file_path, …)

Write distortion coefficients to a text file.

algotom.io.loadersaver.find_file(path)[source]

Search file

Parameters

path (str) – Path and pattern to find files.

Returns

str or list of str – List of files.

algotom.io.loadersaver.find_hdf_key(file_path, pattern)[source]

Find datasets matching the pattern in a hdf/nxs file.

Parameters
  • file_path (str) – Path to the file.

  • pattern (str) – Pattern to find the full names of the datasets.

Returns

  • list_key (str) – Keys to the datasets.

  • list_shape (tuple of int) – Shapes of the datasets.

  • list_type (str) – Types of the datasets.

algotom.io.loadersaver.get_hdf_information(file_path)[source]

Get information of datasets in a hdf/nxs file.

Parameters

file_path (str) – Path to the file.

Returns

  • list_key (str) – Keys to the datasets.

  • list_shape (tuple of int) – Shapes of the datasets.

  • list_type (str) – Types of the datasets.

algotom.io.loadersaver.load_distortion_coefficient(file_path)[source]

Load distortion coefficients from a text file.

Parameters

file_path (str) – Path to the file

Returns

tuple of float and list – Tuple of (xcenter, ycenter, list_fact).

algotom.io.loadersaver.load_hdf(file_path, key_path)[source]

Load a hdf/nexus dataset as an object.

Parameters
  • file_path (str) – Path to the file.

  • key_path (str) – Key path to the dataset.

Returns

object – hdf/nxs object.

algotom.io.loadersaver.load_image(file_path)[source]

Load data from an image.

Parameters

file_path (str) – Path to the file.

Returns

float – 2D array.

algotom.io.loadersaver.make_file_name(file_path)[source]

Create a new file name to avoid overwriting.

Parameters

file_path (str)

Returns

str – Updated file path.

algotom.io.loadersaver.make_folder(file_path)[source]

Create a folder if not exist.

Parameters

file_path (str)

algotom.io.loadersaver.make_folder_name(folder_path, name_prefix='Output')[source]

Create a new folder name to avoid overwriting. E.g: Output_00001, Output_00002…

Parameters
  • folder_path (str) – Path to the parent folder.

  • name_prefix (str) – Name prefix

Returns

str – Name of the folder.

algotom.io.loadersaver.open_hdf_stream(file_path, data_shape, key_path='entry/data', data_type='float32', overwrite=True, **options)[source]

Write an array to a hdf/nxs file with options to add metadata.

Parameters
  • file_path (str) – Path to the file.

  • data_shape (tuple of int) – Shape of the data.

  • key_path (str) – Key path to the dataset.

  • data_type (str) – Type of data.

  • overwrite (bool) – Overwrite the existing file if True.

  • options (dict, optional) – Add metadata. E.g. options={“entry/angles”: angles, “entry/energy”: 53}.

Returns

object – hdf object.

algotom.io.loadersaver.save_distortion_coefficient(file_path, xcenter, ycenter, list_fact, overwrite=True)[source]

Write distortion coefficients to a text file.

Parameters
  • file_path (str) – Path to the file.

  • xcenter (float) – Center of distortion in x-direction.

  • ycenter (float) – Center of distortion in y-direction.

  • list_fact (float) – 1D array. Coefficients of the polynomial fit.

  • overwrite (bool) – Overwrite an existing file if True.

Returns

str – Updated file path.

algotom.io.loadersaver.save_image(file_path, mat, overwrite=True)[source]

Save a 2D array to an image.

Parameters
  • file_path (str) – Path to the file.

  • mat (int or float) – 2D array.

  • overwrite (bool) – Overwrite an existing file if True.

Returns

str – Updated file path.