4.1.1. discorpy.losa.loadersaver

Module for I/O tasks:

  • Load data from an image file (tif, png, jpg) or a hdf file.

  • Save a 2D array as a tif/png/jpg image or a 2D, 3D array to a hdf file.

  • Save a plot of data points to an image.

  • Save/load metadata to/from a text file.

Functions:

load_image(file_path[, average])

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(file_path[, key_path, index, axis])

Load data from a hdf5/nxs file.

load_hdf_object(file_path, key_path)

Load a hdf/nexus dataset as an object.

save_image(file_path, mat[, overwrite])

Save 2D data to an image.

save_plot_image(file_path, list_lines, ...)

Save the plot of dot-centroids to an image.

save_residual_plot(file_path, list_data, ...)

Save the plot of residual against radius to an image.

save_hdf_file(file_path, idata[, key_path, ...])

Write data to a hdf5 file.

open_hdf_stream(file_path, data_shape[, ...])

Open stream to write data to a hdf/nxs file with options to add metadata.

save_metadata_txt(file_path, xcenter, ...[, ...])

Write metadata to a text file.

load_metadata_txt(file_path)

Load distortion coefficients from a text file.

save_plot_points(file_path, list_points, ...)

Save the plot of dot-centroids to an image.

discorpy.losa.loadersaver.load_image(file_path, average=True)[source]

Load data from an image.

Parameters:
  • file_path (str) – Path to a file.

  • average (bool, optional) – Average a multichannel image if True.

Returns:

array_like

discorpy.losa.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.

discorpy.losa.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.

discorpy.losa.loadersaver.load_hdf_file(file_path, key_path=None, index=None, axis=0)[source]

Load data from a hdf5/nxs file.

Parameters:
  • file_path (str) – Path to a hdf/nxs file.

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

  • index (int or tuple of int) – Values for slicing data. Can be integer, tuple or list, e.g index=(start,stop,step) or index=(slice1, slice2, slice3,slice4).

  • axis (int) – Slice direction

Returns:

array_like – 2D array or 3D array.

discorpy.losa.loadersaver.load_hdf_object(file_path, key_path)[source]

Load a hdf/nexus dataset as an object.

Parameters:
  • file_path (str) – Path to a hdf/nxs file.

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

Returns:

object – hdf/nxs object.

discorpy.losa.loadersaver.save_image(file_path, mat, overwrite=True)[source]

Save 2D data to an image.

Parameters:
  • file_path (str) – Output file path.

  • mat (array_like) – 2D array.

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

Returns:

str – Updated file path.

discorpy.losa.loadersaver.save_plot_image(file_path, list_lines, height, width, overwrite=True, dpi=100)[source]

Save the plot of dot-centroids to an image. Useful to check if the dots are arranged properly where dots on the same line having the same color.

Parameters:
  • file_path (str) – Output file path.

  • list_lines (list of array_like) – List of 2D arrays. Each list is the coordinates of dots on a line.

  • height (int) – Height of the image.

  • width (int) – Width of the image.

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

  • dpi (int, optional) – The resolution in dots per inch.

Returns:

str – Updated file path.

discorpy.losa.loadersaver.save_residual_plot(file_path, list_data, height, width, overwrite=True, dpi=100)[source]

Save the plot of residual against radius to an image. Useful to check the accuracy of unwarping results.

Parameters:
  • file_path (str) – Output file path.

  • list_data (array_like) – 2D array. List of [residual, radius] of each dot.

  • height (int) – Height of the output image.

  • width (int) – Width of the output image.

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

  • dpi (int, optional) – The resolution in dots per inch.

Returns:

str – Updated file path.

discorpy.losa.loadersaver.save_hdf_file(file_path, idata, key_path='entry', overwrite=True)[source]

Write data to a hdf5 file.

Parameters:
  • file_path (str) – Output file path.

  • idata (array_like) – Data to be saved.

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

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

Returns:

str – Updated file path.

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

Open stream to write data 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. Example: options={“entry/angles”: angles, “entry/energy”: 53}.

Returns:

object – hdf object.

discorpy.losa.loadersaver.save_metadata_txt(file_path, xcenter, ycenter, list_fact, overwrite=True)[source]

Write metadata to a text file.

Parameters:
  • file_path (str) – Output file path.

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

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

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

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

Returns:

str – Updated file path.

discorpy.losa.loadersaver.load_metadata_txt(file_path)[source]

Load distortion coefficients from a text file.

Parameters:

file_path (str) – Path to a file.

Returns:

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

discorpy.losa.loadersaver.save_plot_points(file_path, list_points, height, width, overwrite=True, dpi=100, marker='o', color='blue')[source]

Save the plot of dot-centroids to an image. Useful to check if the dots are arranged properly where dots on the same line having the same color.

Parameters:
Returns:

str – Updated file path.