I/O Module

I/O Functions

The IO module in the ExSeq Toolbox is designed to streamline the process of reading, converting, and managing image data files for expansion microscopy

exm.io.io.create_folder_structure(processed_dir, puncta_dir_name, fovs, codes)[source]

Creates a results folder for the specified codes.

Parameters:
  • processed_dir (str) – The directory where all results for the specified codes should be stored.

  • puncta_dir_name (str) – The directory where all results for puncta analysis.

  • fovs (List[int]) – The list of Fovs to create the folder structure for.

  • codes (List[int]) – The list of codes to create the folder structure for.

Return type:

None

exm.io.io.downsample(arr, block_size)[source]

Downsamples a single or multidimensional array using skimage.measure.block_reduce.

Parameters:
  • arr (np.ndarray) – Array to downsample.

  • block_size (int) – Integer factor for down-sampling along each axis.

Returns:

Downsampled array.

Return type:

np.ndarray

Raises:

ValueError – If block size does not match the array’s dimensions.

exm.io.io.imsToVol(filename, volume_index)[source]

Reads a specific volume from an IMS file based on the given volume index.

Parameters:
  • filename (str) – Path of the IMS file.

  • volume_index (int) – Index of the volume to retrieve, which corresponds to a specific channel in the dataset.

Returns:

The extracted volume as a NumPy array.

Return type:

np.ndarray

Raises:
  • ValueError – If volume_index is negative.

  • KeyError – If the specified path within the file does not exist.

  • FileNotFoundError – If the specified file does not exist.

  • OSError – If there is an issue opening the file, such as permissions or corruption.

exm.io.io.nd2ToChunk(filename, fov, z_min, z_max, channel_name='405 SD')[source]

Reads the specified Nd2 file and returns a chunk from it.

Parameters:
  • filename (str) – Path to the ND2 file.

  • fov (int) – The field of view to be returned.

  • z_min (int) – Starting z position of the chunk.

  • z_max (int) – Ending z position of the chunk.

  • channel_name (str) – The channel to be returned. Default is “405 SD”.

Returns:

A 3D numpy array representing the selected chunk of the ND2 file.

Return type:

np.ndarray

Raises:

ValueError – If the specified channel is not found or if there are multiple matches for the channel name.

exm.io.io.nd2ToSlice(filename, fov, z, channel_name='405 SD')[source]

Reads the specified Nd2 file and returns a single slice from it.

Parameters:
  • filename (str) – Path to the ND2 file.

  • fov (int) – The field of view to be returned.

  • z (int) – Index of the z slice to be returned.

  • channel_name (str) – The channel to be returned. Default is “405 SD”.

Returns:

A 2D numpy array representing the selected slice of the ND2 file.

Return type:

np.ndarray

Raises:

ValueError – If the specified channel is not found or if there are multiple matches for the channel name.

exm.io.io.nd2ToVol(filename, fov, channel_name='405 SD', ratio=1)[source]

Reads the specified Nd2 file and returns it as a numpy array.

Parameters:
  • filename (str) – Path of the ND2 file.

  • fov (int) – The field of view to be returned.

  • channel_name (str) – The channel to be returned. Default is “405 SD”.

  • ratio (int) – Downsampling factor. Default is 1.

Returns:

The ND2 file converted into a numpy array.

Return type:

np.ndarray

exm.io.io.parse_sitk_log(log_path)[source]

Parses a SimpleITK log file and returns the resulting metric and step size.

Parameters:

log_path (str) – Path to the SimpleITK log.

Returns:

A tuple containing arrays of metrics and step sizes.

Return type:

Tuple[np.ndarray, np.ndarray]

Raises:

FileNotFoundError – If the log file does not exist or cannot be opened.

exm.io.io.readNd2(nd2_file, do_info=True)[source]

Returns the image and metadata from the specified Nd2 file.

Parameters:
  • nd2_file (str) – File path to the Nd2 file.

  • do_info (bool) – Whether or not to extract metadata from the Nd2 file. Defaults to True.

Returns:

A tuple containing ND2Reader object for the image data and a dictionary for metadata if requested.

Return type:

Tuple[ND2Reader, Optional[Dict[str, Any]]]

Raises:
  • FileNotFoundError – If the Nd2 file cannot be found.

  • Exception – If an error occurs while reading the Nd2 file or extracting metadata.

exm.io.io.readXlsx(xlsx_file)[source]

Reads the experiment xlsx_file and returns it as a NumPy array.

The function expects the following columns to be present: ‘Point Name’, ‘Z Pos[µm]’, ‘Y Pos[µm]’, and ‘X Pos[µm]’. It processes the file to extract position data and returns this data, transforming ‘X Pos’ by negating its values.

Parameters:

xlsx_file (str) – Path to the xlsx file.

Returns:

A NumPy array containing the processed position data.

Return type:

np.ndarray

Raises:
exm.io.io.save_gif(img1, img2, filename)[source]

Creates a GIF by appending one image behind the other and loops between them.

Parameters:
  • img1 (np.ndarray) – The first image to be displayed.

  • img2 (np.ndarray) – The second image to be displayed.

  • filename (str) – The filename for saving the GIF.

Returns:

The resulting GIF image.

Return type:

PIL.Image

Raises:

IOError – If there is an error saving the GIF.

exm.io.io.tiff2H5(tiff_file, h5_file, chunk_size=(100, 1024, 1024), step=100, im_thres=None)[source]

Reads a TIFF file and re-saves it as an H5 file.

Parameters:
  • tiff_file (str) – Path to the existing TIFF file.

  • h5_file (str) – Path to the new H5 file.

  • chunk_size (Tuple[int, int, int]) – Chunk size to break the image into. Default: (100, 1024, 1024)

  • step (int) – Z step size. Default: 100

  • im_thres (Optional[int]) – Integer used for image thresholding, None to disable. Default: None

Raises:
  • FileNotFoundError – If the TIFF file is not found.

  • IOError – If there is an error reading the TIFF file or writing the H5 file.

  • ValueError – If there are issues with the image thresholding.

Return type:

None