loki2.data.dataclass.wsi
WSI Model for Loki2.
This module defines data structures for working with Whole Slide Images (WSI), including metadata handling, patch processing, and dataset classes.
Module Contents
- class loki2.data.dataclass.wsi.WSIMetadata
Metadata container for Whole Slide Image information.
- Parameters:
name – Name identifier for the WSI.
slide_path – Full path to the WSI file.
metadata – Dictionary containing additional metadata about the WSI.
- name: str
- slide_path: str | pathlib.Path
- metadata: Dict[str, Any]
- class loki2.data.dataclass.wsi.WSI
WSI object
- Parameters:
name (str) – WSI name
patient (str) – Patient name
slide_path (Union[str, Path]) – Full path to the WSI file.
patched_slide_path (Union[str, Path], optional) – Full path to preprocessed WSI files (patches). Defaults to None.
embedding_name (Union[str, Path], optional) – Defaults to None.
label (Union[str, int, float, np.ndarray], optional) – Label of the WSI. Defaults to None.
logger (logging.logger, optional) – Logger module for logging information. Defaults to None.
- name: str
- patient: str
- slide_path: str | pathlib.Path
- metadata: Dict[str, Any]
- all_patch_metadata: List[Dict[str, Any]]
- patches_list: List[str]
- load_patch_metadata(patch_name: str) Dict[str, Any]
Return the metadata of a patch with given name.
This function assumes that metadata path is a subpath of the patches dataset path. The patch name should include the patch suffix (e.g., wsi_1_1.png).
- Parameters:
patch_name – Name of patch including suffix.
- Returns:
Patch metadata dictionary.
- Return type:
Dict[str, Any]
- set_patch_transform(transform: Callable) None
Set the transformation function to process a patch.
- Parameters:
transform – Transformation function to apply to patches.
- process_patch_image(patch_name: str, transform: Callable | None = None) Tuple[torch.Tensor, Dict[str, Any]]
Process one patch: Load from disk, apply transformation if needed.
ToTensor is applied automatically. The patch name should include the patch suffix (e.g., wsi_1_1.png).
- Parameters:
patch_name – Name of patch to load, including patch suffix.
transform – Optional patch transformation function.
- Returns:
torch.Tensor: Patch as torch tensor with shape (3, H, W)
Dict[str, Any]: Patch metadata as dictionary
- Return type:
Tuple[torch.Tensor, Dict[str, Any]]
- get_number_patches() int
Return the number of patches for this WSI
- Returns:
number of patches
- Return type:
int
- get_patches(transform: Callable | None = None) Tuple[torch.Tensor, List[Dict[str, Any]]]
Get all patches for one image.
- Parameters:
transform – Optional patch transformation function.
- Returns:
torch.Tensor: Patched images with shape (num_patches, 3, height, width)
List[Dict[str, Any]]: List of metadata dictionaries for each patch
- Return type:
Tuple[torch.Tensor, List[Dict[str, Any]]]
- load_embedding() torch.Tensor
Load embedding from subfolder patched_slide_path/embedding/
- Raises:
FileNotFoundError – If embedding is not given
- Returns:
WSI embedding
- Return type:
torch.Tensor
- class loki2.data.dataclass.wsi.PatchedWSIInference(wsi_object: WSI, transform: Callable)
Bases:
torch.utils.data.DatasetInference Dataset for calculating embeddings of a single WSI.
This dataset is wrapped around a WSI object and provides access to patches for inference processing.
- Parameters:
wsi_object – WSI object containing patch information.
transform – Inference transformations to apply to patches.
- transform
- wsi_object
- static collate_batch(batch: List[Tuple[torch.Tensor, Dict[str, Any]]]) Tuple[torch.Tensor, List[Dict[str, Any]]]
Create a custom batch from a list of patch tuples.
Needed to unpack list of tuples with dictionaries and tensors.
- Parameters:
batch – Input batch consisting of a list of tuples (patch, patch-metadata).
- Returns:
torch.Tensor: Stacked patches with shape (batch_size, 3, patch_size, patch_size)
List[Dict[str, Any]]: List of metadata dictionaries
- Return type:
Tuple[torch.Tensor, List[Dict[str, Any]]]