liom_toolkit.utils.io module

class liom_toolkit.utils.io.CustomScaler(order: int = 1, anti_aliasing: bool = True, downscale: int = 2, method: str = 'nearest', input_layer: int = 0, max_layer: int = 4, original_image: str | None = None)

Bases: Scaler

A custom scaler that can down-sample 3D images for OME-Zarr Conversion.

Parameters:
  • order (int) – The order of the transformation.

  • anti_aliasing (bool) – Whether to use anti-aliasing

  • downscale (int) – The amount to downscale by

  • method (str) – The method to use for downscaling. Disclaimer: Only “nearest” is supported.

  • input_layer (int) – The input layer to use for the transformation.

  • max_layer (int) – The maximum layer to use for the transformation.

  • original_image (str | None) – The original image to use for the transformation.

anti_aliasing: bool
current_scale: int = None
do_upscale: bool = True
input_layer: int
nearest(base: ndarray) list[ndarray]

Down-sample using skimage.transform.resize().

Parameters:

base (np.ndarray) – The base image to down-sample.

Returns:

The down-sampled image.

Return type:

list[np.ndarray]

order: int
original_image: str | None
to_down_scale: ndarray
to_up_scale: ndarray
liom_toolkit.utils.io.convert_dask_to_ants(dask_array: Array, node: Node, resolution_level: int = 2, volume_direction: tuple = ([1.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, -1.0, 0.0])) ANTsImage

Convert a dask array to an ANTs image.

Parameters:
  • dask_array (da.Array) – The dask array to convert.

  • node (Node) – The zarr node corresponding to the image.

  • resolution_level (int) – The resolution level to load.

  • volume_direction (tuple) – The direction of the volume.

Returns:

The converted ANTs image.

Return type:

ants.ANTsImage

liom_toolkit.utils.io.create_and_write_mask(zarr_file: str, scales: tuple = (6.5, 6.5, 6.5), chunks: tuple = (128, 128, 128), resolution_level: int = 0) None

Create a mask for a zarr file and write it to disk inside the labels group.

Parameters:
  • zarr_file (str) – The zarr file to create a mask for.

  • scales (tuple.) – The scales to use for the mask.

  • chunks (tuple) – The chunks to use for the mask

  • resolution_level (int) – The resolution level of the mask.

liom_toolkit.utils.io.create_mask_from_zarr(zarr_file: str, resolution_level: int = 0) ndarray

Create a brain mask from a zarr file.

Parameters:
  • zarr_file (str) – The zarr file to create a mask for.

  • resolution_level (int) – The resolution level of the mask.

Returns:

The mask

Return type:

np.ndarray

liom_toolkit.utils.io.create_transformation_dict(scales: tuple, levels: int, dimensions: int) list

Create a dictionary with the transformation information for 3D images.

Parameters:
  • scales (tuple) – The scale of the image, in z y x order.

  • levels (int) – The number of levels in the pyramid.

  • dimensions (int) – The number of dimensions in the image.

Returns:

The transformation dictionary.

Return type:

list

liom_toolkit.utils.io.extract_zarr_to_png(zarr_file: str, target_dir: str) None

Extract a zarr file to a directory of PNG images.

Parameters:
  • zarr_file (str) – The zarr file to extract.

  • target_dir (str) – The directory to save the PNG images to.

Returns:

None

liom_toolkit.utils.io.generate_axes_dict(dimensions: int) list

Generate the axes dictionary for the zarr file.

Parameters:

dimensions (int) – The number of dimensions in the image.

Returns:

The axes dictionary.

Return type:

list

liom_toolkit.utils.io.generate_label_color_dict_allen() list[dict]

Generate a label color dictionary for the allen atlas.

Returns:

The label color dictionary.

Return type:

list[dict]

liom_toolkit.utils.io.generate_label_color_dict_mask() list[dict]

Generate a label color dictionary for the mask. Black is background, white is foreground.

Returns:

The label color dictionary.

Return type:

list[dict]

liom_toolkit.utils.io.load_allen_template(atlas_file: str, resolution: int, padding: bool) ANTsImage

Load the allen template and set the resolution and direction (PIR).

Parameters:
  • atlas_file (str) – The file to load.

  • resolution (int) – The resolution to set.

  • padding (bool) – Whether to pad the atlas or not.

Returns:

The loaded template.

Return type:

ants.ANTsImage

liom_toolkit.utils.io.load_ants_image_from_node(node: Node, resolution_level: int = 2, channel=0) ANTsImage

Load an ANTs image from a zarr node.

Parameters:
  • node (Node) – The zarr node to load.

  • resolution_level (int) – The resolution level to load.

  • channel (int) – The channel to load.

Returns:

The loaded ANTs image.

Return type:

ants.ANTsImage

liom_toolkit.utils.io.load_node_by_name(nodes: list[Node], name: str) Node | None

Load a node by name from a zarr file. Returns None if the node is not found.

Parameters:
  • nodes (list[Node]) – The nodes to search through.

  • name (str) – The name of the node to load.

Returns:

The loaded node.

Return type:

Node | None

liom_toolkit.utils.io.load_zarr(zarr_file: str) list[Node]

Load a zarr file to an ANTs image.

Parameters:

zarr_file (str) – The zarr file to load.

Returns:

The loaded zarr file.

Return type:

list[Node]

liom_toolkit.utils.io.load_zarr_image_from_node(node: Node, resolution_level: int = 1) array

Load a zarr file to an ANTs image. Loads one channel at a time.

Parameters:
  • node (Node) – The zarr node to load.

  • resolution_level (int) – The resolution level to load.

Returns:

The image.

Return type:

da.array

liom_toolkit.utils.io.load_zarr_transform_from_node(node: Node, resolution_level: int = 1) dict

Load a zarr file to an ANTs image.

Parameters:
  • node (Node) – The zarr node to load.

  • resolution_level (int) – The resolution level to load.

Returns:

The coordinate transform matching the resolution level.

Return type:

ants.ANTsImage

liom_toolkit.utils.io.save_atlas_to_zarr(zarr_file: str, atlas: Array | ndarray, scales: tuple = (6.5, 6.5, 6.5), chunks: tuple = (128, 128, 128), resolution_level: int = 0) None

Save an atlas to a zarr file inside the labels group.

Parameters:
  • zarr_file (str) – The zarr file to save the atlas to.

  • atlas (ArrayLike) – The atlas to save.

  • scales (tuple) – The scales to use for the atlas.

  • chunks (tuple) – The chunks to use for the atlas.

  • resolution_level (int) – The resolution level of the atlas.

liom_toolkit.utils.io.save_label_to_zarr(label: Array | ndarray, zarr_file: str, color_dict: list[dict], name: str, scales: tuple = (6.5, 6.5, 6.5), chunks: tuple = (128, 128, 128), resolution_level: int = 0) None

Save a mask to a zarr file inside the labels group.

Parameters:
  • label (np.ndarray) – The mask to save.

  • zarr_file (str) – The zarr file to save the mask to.

  • color_dict (list[dict]) – The color dictionary to use for the mask.

  • scales (tuple) – The scales to use for the mask.

  • chunks (tuple) – The chunks to use for the mask.

  • resolution_level (int) – The resolution level of the mask.

  • name (str) – The name of the mask.

liom_toolkit.utils.io.set_physical_shape(image: ANTsImage) None

Set the physical shape of an ANTs image by multiplying the shape with the spacing.

Parameters:

image (ants.ANTsImage) – The image to set the physical shape for.