microcorrelate.utils#

This module is a placeholder for the utils module of the microcorrelate package.

Functions#

deep_items() → Iterator[tuple[str, Any]])

Recursively yield (key.path, value) for all non-dict values

extract_integers(→ list[int])

Extract integers matching the group in a regex pattern from filenames matching

find_common_vals(→ dict[str, Any])

Returns a dictionary with the keys share the same value between the two input

flatten_dict(→ dict[str, Any])

Flatten a nested dictionary. Key hierarchy is preserved by assigning new

get_crop_idx(→ tuple[slice, Ellipsis])

Compute array slices to crop a numpy array to the bounding box of non-zero mask regions.

resample_to_spacing(→ numpy.ndarray)

Resample an array to a target pixel spacing.

round_up_multiple(→ int)

Round up a number to a multiple of base

vprint(→ None)

Helper function for cleanly handling print statements with a verbose option

Module Contents#

microcorrelate.utils.deep_items(d: dict[str, Any], prefix: tuple[str, Ellipsis] = ()) Iterator[tuple[str, Any]]#

Recursively yield (key.path, value) for all non-dict values in a nested dictionary.

microcorrelate.utils.extract_integers(base_path: pathlib.Path, regex_pattern: str, glob_pattern: str) list[int]#

Extract integers matching the group in a regex pattern from filenames matching the glob pattern

microcorrelate.utils.find_common_vals(d1: dict[str, Any], d2: dict[str, Any]) dict[str, Any]#

Returns a dictionary with the keys share the same value between the two input dictionaries

microcorrelate.utils.flatten_dict(d: dict[str, Any]) dict[str, Any]#

Flatten a nested dictionary. Key hierarchy is preserved by assigning new keys as ‘a.b.c’.

microcorrelate.utils.get_crop_idx(mask: numpy.ndarray, margin: int = 0) tuple[slice, Ellipsis]#

Compute array slices to crop a numpy array to the bounding box of non-zero mask regions.

Useful for removing empty borders (e.g. black frames from image stitching).

Parameters:
  • mask (np.ndarray) – 2D binary mask where non-zero pixels define the region of interest.

  • margin (int, optional) – Number of pixels to expand the bounding box on each side, by default 0. Clamped to array boundaries.

Returns:

A tuple of slices (row_slice, col_slice) that can be used to index a numpy array directly, e.g. image[get_crop_idx(mask)].

Return type:

tuple[slice, …]

Examples

cropped = image[get_crop_idx(mask, margin=10)]
microcorrelate.utils.resample_to_spacing(array: numpy.ndarray, input_spacing: float | tuple[float, float], output_spacing: float | tuple[float, float], channel_axis: int | None = None) numpy.ndarray#

Resample an array to a target pixel spacing.

Parameters:
  • array (np.ndarray) – Input image array.

  • input_spacing (float or tuple of float) – Current pixel spacing. A scalar is treated as isotropic.

  • output_spacing (float or tuple of float) – Target pixel spacing. A scalar is treated as isotropic.

  • channel_axis (int or None, optional) – Axis containing channels, excluded from resampling. Default is None.

Returns:

Resampled array with the same dtype as the input.

Return type:

np.ndarray

microcorrelate.utils.round_up_multiple(num: int, base: int) int#

Round up a number to a multiple of base

microcorrelate.utils.vprint(text: str, verbose: bool) None#

Helper function for cleanly handling print statements with a verbose option