loki2.utils.tools
Utility functions for Loki2.
This module provides various helper functions for image processing, dictionary manipulation, file handling, and other common operations used throughout the Loki2 framework.
Module Contents
- loki2.utils.tools.get_bounding_box(img: numpy.ndarray) List[int]
Get bounding box coordinate information.
- Parameters:
img – Binary or labeled image array.
- Returns:
Bounding box coordinates as [rmin, rmax, cmin, cmax].
- Return type:
List[int]
- loki2.utils.tools.remove_small_objects_cp(pred: cupy.ndarray, min_size: int = 64, connectivity: int = 1) cupy.ndarray
Remove connected components smaller than the specified size.
This function is taken from skimage.morphology.remove_small_objects, but the warning is removed when a single label is provided. Uses CuPy for GPU acceleration.
- Parameters:
pred – Input labelled array (CuPy array).
min_size – Minimum size of instance in output array. Defaults to 64.
connectivity – The connectivity defining the neighborhood of a pixel. Defaults to 1.
- Returns:
Output array with instances removed under min_size.
- Return type:
cp.ndarray
- Raises:
ValueError – If negative value labels are found in the input.
- loki2.utils.tools.remove_small_objects(pred: numpy.ndarray, min_size: int = 64, connectivity: int = 1) numpy.ndarray
Remove connected components smaller than the specified size.
This function is taken from skimage.morphology.remove_small_objects, but the warning is removed when a single label is provided. Uses NumPy/ SciPy for CPU processing.
- Parameters:
pred – Input labelled array (NumPy array).
min_size – Minimum size of instance in output array. Defaults to 64.
connectivity – The connectivity defining the neighborhood of a pixel. Defaults to 1.
- Returns:
Output array with instances removed under min_size.
- Return type:
np.ndarray
- Raises:
ValueError – If negative value labels are found in the input.
- loki2.utils.tools.unflatten_dict(d: dict, sep: str = '.') dict
Unflatten a flattened dictionary (create a nested dictionary).
- Parameters:
d – Dictionary to be nested (with keys separated by separator).
sep – Separator used in flattened keys. Defaults to ‘.’.
- Returns:
Nested dictionary reconstructed from flattened keys.
- Return type:
dict
Example
>>> d = {'a.b': 1, 'a.c': 2} >>> unflatten_dict(d) {'a': {'b': 1, 'c': 2}}
- loki2.utils.tools.remap_label(pred: numpy.ndarray, by_size: bool = False) numpy.ndarray
Rename all instance IDs so that IDs are contiguous (e.g., [0, 1, 2, 3]).
The ordering of instances (which one comes first) is preserved unless by_size=True, in which case instances will be reordered so that larger nuclei have smaller IDs.
- Parameters:
pred – The 2D array containing instances where each instance is marked by a non-zero integer.
by_size – If True, reorder instances so that larger nuclei have smaller IDs. Defaults to False.
- Returns:
Array with remapped contiguous instance IDs.
- Return type:
np.ndarray