WSI Reader Base Module.
This module provides the WSIReader abstract base class, which standardizes access to Whole Slide Images (WSI). It handles coordinate translations, resolution scaling (Level, Magnification, MPP), and tissue-aware cropping.
Typical usage example:
with OpenSlideReader(“path/to/slide.svs”) as reader:
Get tissue-only dimensions
dims = reader.dimensions(bounded=True)
Read a region relative to the slide origin
region = reader.read_region((0, 0), (512, 512), Level(0), bounded=False)
WSIReader
Abstract Base Class for all WSI Backends.
Standardizes the interface for reading digital pathology slides across different vendors. It manages metadata, pyramid levels, and tissue bounding box transformations.
Attributes:
path - Path object pointing to the slide file.
props - Metadata properties extracted from the slide.
open_slide
Opens the file handle for the slide.
Raises:
NotImplementedError - If not implemented by subclass.
level_count
Number of pyramid levels available in the slide.
Returns:
int - Number of pyramid levels.
Raises:
NotImplementedError - If not implemented by subclass.
mpp
Native microns per pixel (MPP) value.
Returns:
Optional[MPP] - The MPP value if found in metadata, else None.
Example:
require_mpp
Returns the slide MPP, or raises a typed, actionable error when absent.
Centralizes the missing-MPP failure so every resolution path raises the same typed BioptimusValueError (BIO-WSI-004) instead of a bare ValueError that decays to BIO-GEN-001 at the inference boundary.
Returns:
MPP - The slide’s microns-per-pixel value.
Raises:
BioptimusValueError - Tagged WSI_MPP_MISSING when no MPP is set.
downsample_dimensions
Calculates slide dimensions at a specific downsample factor.
The ratio to scale down by.
Whether to return tissue bounds or full slide size.
Returns:
Union[WSIDims, WSIBounds]: Scaled slide dimensions or tissue bounds.
Example:
level_dimensions
Returns the dimensions of a specific pyramid level.
The target pyramid level (0 is highest resolution).
If True, returns tissue area at that level.
Returns:
Union[WSIDims, WSIBounds]: Dimensions or bounds at specified level.
Raises:
ValueError - If level is out of range.
Example:
magnification_dimensions
Returns slide dimensions for a specific optical magnification.
Target magnification (e.g., 40x, 20x, 10x).
If True, returns tissue area at that magnification.
Returns:
Union[WSIDims, WSIBounds]: Dimensions or bounds at specified magnification.
Example:
mpp_dimensions
Returns slide dimensions for a specific MPP (microns per pixel).
Target microns per pixel.
If True, returns tissue area at that MPP.
Returns:
Union[WSIDims, WSIBounds]: Dimensions or bounds at specified MPP.
Example:
dimensions
Returns full or bounded slide dimensions at Level 0.
Acts as the bounded toggle when a bool.
If True, returns tissue bounds.
dimensions
Returns slide dimensions at a pyramid level.
If True, returns tissue bounds.
dimensions
Returns slide dimensions at a magnification.
Target optical magnification.
If True, returns tissue bounds.
dimensions
Returns slide dimensions at a target MPP.
Target microns per pixel.
If True, returns tissue bounds.
dimensions
Returns slide dimensions for a specific resolution.
Desired resolution (Level, Magnification, or MPP).
If True, returns tissue area at that resolution.
Returns:
Union[WSIDims, WSIBounds]: Dimensions or bounds at specified resolution.
Example:
get_downsample_factor
Calculates the downsample factor for a given resolution.
The target resolution (Level, Magnification, or MPP).
Returns:
float - Downsample factor for the given resolution.
Example:
level_downsample
Returns downsample factor for a pyramid level.
Returns:
float - Downsample factor for the given level.
Raises:
NotImplementedError - If not implemented by subclass.
mpp_downsample
Calculates downsample needed for target MPP.
Target microns per pixel.
Returns:
float - Downsample factor for the target MPP.
Raises:
ValueError - If metadata is missing or target is too high-res.
Example:
magnification_downsample
Calculates downsample needed for target magnification.
Target magnification power.
Returns:
float - Downsample factor for the target magnification.
Raises:
ValueError - If metadata is missing or target is out of range.
Example:
props
Slide metadata properties.
Returns:
WSIProps - Metadata extracted from the slide.
Example:
get_best_level_for_downsample
Finds highest pyramid level smaller than target downsample factor.
Returns:
int - Best pyramid level index for the downsample.
Raises:
ValueError - If downsample is too small.
Example:
get_downsample_scaling_and_level
Determines the downsample factor, scaling factor, and best pyramid level for a given resolution.
Desired resolution of the slide. Can be one of:
- Level: integer pyramid level
- Magnification: desired optical magnification
- MPP: microns per pixel
Returns:
tuple - (downsample_factor (float), scaling_factor (float), best_level (int))
- downsample_factor: Factor by which the base level is downsampled.
- scaling_factor: Additional scaling needed at the chosen level.
- best_level: Pyramid level that best matches the resolution.
Raises:
ValueError - If required slide properties (objective power or MPP) are unavailable.
TypeError - If the resolution type is unsupported.
Example:
read_region
Reads a region from the slide.
(width, height) at target resolution.
Resolution level/mpp/magnification.
Unit for the region shape
If True, (0,0) is tissue top-left. Else slide top-left.
Returns:
Region - Object containing the image and metadata for the requested area.
Example:
is_supported_file
Checks if backend supports this file extension.
Returns:
bool - True if supported, False otherwise.
Raises:
NotImplementedError - If not implemented by subclass.
close
Closes slide file handle.
Returns:
None
Raises:
NotImplementedError - If not implemented by subclass.
__enter__
Context manager entry.
Returns:
WSIReader - The reader instance with slide file open.
Example:
__exit__
Context manager exit.
Returns:
None
Example:
get_thumbnail
Generates a slide thumbnail.
Max (width, height) for result.
If True, crop to tissue. Else show full slide (incl. white space).
Returns:
Image.Image - RGB thumbnail image.
Example: