microcorrelate.io#

This module contains various functions to read image and create image objects.

Functions#

create_itk_image(, origin, float] =, channel_axis)

Convert a NumPy array to a SimpleITK Image.

extract_tpef_spacing(→ tuple[float, float])

Extract YX spacing values for a TPEF experiment.

read_laicp_hdf5(→ tuple[numpy.ndarray, tuple[float, ...)

Read LA-ICP-MS data from a TOFWERK HDF5 file.

read_ome_zarr(→ tuple[numpy.ndarray, tuple[float, float]])

Read highest resolution level from OME-Zarr with spacing.

read_tofsims(→ tuple[numpy.ndarray, tuple[float, float]])

Read ToF-SIMS measurement and returns image data and spacing.

read_tpef(→ tuple[numpy.ndarray, tuple[float, float]])

Read TPEF image data and discover pixel spacing from metadata file.

select_laicp_channels(→ list[int])

Select relevant LA-ICP-MS channel indices from a list of channel labels.

Module Contents#

microcorrelate.io.create_itk_image(image: numpy.ndarray, spacing: float | tuple[float, float] = (1.0, 1.0), origin: tuple[float, float] = (0.0, 0.0), channel_axis: int | None = None) SimpleITK.Image#

Convert a NumPy array to a SimpleITK Image.

This is a simple conversion function for creating ITK images from in-memory arrays (e.g., from HDF5 files, zarr arrays, or other sources).

Both single- and multi-channel images are supported. Pixel spacing can be isotropic (scalar) or anisotropic (tuple). For a reference of the SimpleITK Image origin and spacing, see: https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch4.html

Parameters:
  • image (np.ndarray) – The input image as a NumPy array.

  • spacing (float | tuple of float, optional) – The pixel spacing in physical units. For isotropic images, this is the distance between pixel centers. Default is (1.0, 1.0).

  • origin (tuple of float, optional) – The physical coordinates of the origin (0,0) pixel. Default is (0.0, 0.0).

  • channel_axis (int | None, optional) – The axis in image that contains channel information, if the image is multichannel. If None, the image is treated as single channel. Default is None.

Returns:

image_itk – SimpleITK Image object with specified spacing and origin.

Return type:

SimpleITK.Image

Examples

import numpy as np
from microcorrelate.utils import create_itk_image

# Create from HDF5 array
import h5py
with h5py.File('data.h5', 'r') as f:
    array = f['images/layer1'][:]
    image = create_itk_image(array, spacing=0.5)

# Create from zarr array
import zarr
z = zarr.open('data.zarr', mode='r')
image = create_itk_image(z['images/0'][:], spacing=1.2)
microcorrelate.io.extract_tpef_spacing(filepath: pathlib.Path | str) tuple[float, float]#

Extract YX spacing values for a TPEF experiment.

Input should be an exp_prop.txt file, which contains acquisition properties for the experiment. This function currently ignores Z spacing.

Parameters:

filepath (Path | str) – Path to exp_prop.txt file. The file should contain spacing in the format “ X 512 1.381068 µm”

Returns:

spacing – Spacing in micrometers (Y, X).

Return type:

tuple[float, float]

Raises:

ValueError – If X or Y spacing was not detected in the file.

microcorrelate.io.read_laicp_hdf5(filepath: pathlib.Path | str, elements: list[str] | str | None = None, quant_suffix: str = '\n') tuple[numpy.ndarray, tuple[float, float], list[str]]#

Read LA-ICP-MS data from a TOFWERK HDF5 file.

Reads the reconstructed image data, infers pixel spacing from the position grid, and only keep user selected channels and total ion count.

Parameters:
  • filepath (Path | str) – Path to the HDF5 file.

  • elements (list[str] | str | None, optional) – Element names to search for in channel labels (e.g. ['Cu', 'Zn']). Channels whose label contains any of these names are included in addition to TIC and user-selected channels. If None, no element-based filtering is applied.

  • quant_suffix (str) – Suffix identifying user-defined quantification channels. Default is "\n".

Returns:

  • data (np.ndarray) – Image array of shape (n_channels, rows, cols). Total ion count, user-selected channels (ending with '\n'), and any element-matched channels are kept.

  • spacing (tuple[float, float]) – Pixel spacing in micrometers (row, col).

  • labels (list[str]) – Channel labels corresponding to axis 0 of the preserved data.

microcorrelate.io.read_ome_zarr(zarr_path: pathlib.Path | str) tuple[numpy.ndarray, tuple[float, float]]#

Read highest resolution level from OME-Zarr with spacing.

Parameters:

zarr_path (Path or str) – Path to the zarr store.

Returns:

  • data (np.ndarray) – Image array at highest resolution.

  • spacing (tuple[float, float]) – Pixel spacing in micrometers (Y, X).

Raises:

ValueError – If unit is not nanometer or micrometer.

microcorrelate.io.read_tofsims(filepath: pathlib.Path) tuple[numpy.ndarray, tuple[float, float]]#

Read ToF-SIMS measurement and returns image data and spacing.

Parameters:

filepath (Path) – Path to the txt file containing the ToF-SIMS data.

Returns:

  • data (np.ndarray) – Single channel image array.

  • spacing (tuple[float, float]) – Pixel spacing in micrometers (Y, X).

microcorrelate.io.read_tpef(image_path: pathlib.Path, max_level: int = 3) tuple[numpy.ndarray, tuple[float, float]]#

Read TPEF image data and discover pixel spacing from metadata file.

Searches for exp_prop.txt in the image directory and up to max_level parent directories to extract pixel spacing information.

Parameters:
  • image_path (Path) – Path to the TPEF image file.

  • max_level (int, optional) – Maximum number of parent directory levels to search for exp_prop.txt. Default is 3.

Returns:

  • image (np.ndarray) – Image array with shape (Z, channels, Y, X) or (channels, Y, X).

  • spacing (tuple[float, float]) – Pixel spacing in micrometers (Y, X).

Raises:

FileNotFoundError – If exp_prop.txt is not found within the search scope.

See also

extract_tpef_spacing

Extract spacing from exp_prop.txt file.

microcorrelate.io.select_laicp_channels(channels: list[str], elements: list[str] | str | None = None, quant_suffix: str = '\n') list[int]#

Select relevant LA-ICP-MS channel indices from a list of channel labels.

This function assumes that the first channel represents the total ion count, and that user-defined quantification channels have a suffix that identifies them.

Parameters:
  • channels (list[str]) – All channel names from the measurement.

  • elements (list[str] | None) – Element symbols to retain (e.g. ["Y", "N", "Ca"]). If None, keep only quantification channels. Default is None.

  • quant_suffix (str) – Suffix identifying user-defined quantification channels. Default is "\n".

Returns:

Ordered, deduplicated indices of selected channels.

Return type:

list[int]