Core types for representing resolutions and regions in Whole Slide Images (WSIs).
This module defines lightweight, immutable classes for commonly used WSI attributes such as pyramid levels, physical pixel size (MPP), and objective magnification.
Classes:
Level: Pyramid level index (0 = highest resolution).
MPP: Microns per pixel (µm/pixel).
Magnification: Objective power (e.g., 20x).
WSIDims: Width and height dimensions.
WSIBounds: A bounding box for tissue regions.
MeasurementUnit: Enum for pixels or microns.
RegionLocation: X/Y coordinates of a region.
RegionShape: Dimensions and units of a region.
RegionSpec: Full specification (Location + Shape + Resolution).
Region: Container for PIL image data and its RegionSpec.
Level
Represents a pyramid level index in a WSI.
Level 0 is the highest resolution. Higher indices are lower resolutions.
The pyramid level index (0-based).
Raises:
ValueError - If index is negative.
Example:
Notes:
- Level 0 is the highest resolution.
- Higher indices correspond to lower resolution levels in the pyramid.
- This class stores the level index as a primitive integer and is immutable.
MPP
Physical size of a pixel in microns per pixel (µm/pixel) in a whole slide image (WSI).
MPP values indicate the real-world size of one image pixel and must be a finite number strictly greater than 0 (NaN and infinity are rejected).
The pixel size in microns to represent as MPP.
Raises:
ValueError - If value is not a finite positive number.
Example:
Magnification
Objective power of a microscope used to acquire a whole slide image (WSI).
Magnification indicates the target resolution (e.g., 20x, 40x). It is always stored as a finite positive float (NaN and infinity are rejected).
Typical magnifications:
- 10x: low-resolution overview
- 20x: standard diagnostic resolution
- 40x: high-resolution for detailed analysis
Raises:
ValueError - If value is not a finite positive number.
Example:
WSIDims
Dimensions of a slide or level at full resolution.
Attributes:
width int - The width of the image in pixels.
height int - The height of the image in pixels.
Example:
WSIBounds
The non-empty region (Bounding Box) of the slide in pixels.
This represents the “tissue-containing” area of a slide, allowing users to ignore large background areas.
Attributes:
x int - The X coordinate of the top-left corner.
y int - The Y coordinate of the top-left corner.
width int - The width of the bounding box.
height int - The height of the bounding box.
Example:
dims
Extracts width and height as a WSIDims object.
Returns:
WSIDims - The dimensions of the bounding box.
Example:
MeasurementUnit
Enumeration of measurement units for WSI tiles and regions.
has_value
Checks if a string is a valid MeasurementUnit.
Returns:
bool - True if valid, False otherwise.
Example:
RegionLocation
Defines the location of a region within a WSI.
Attributes:
top int - The top coordinate of the top-left corner of the region.
left int - The left coordinate of the top-left corner of the region.
width int - The width of the region in pixels.
height int - The height of the region in pixels.
Example:
Alias for left coordinate.
Alias for top coordinate.
RegionShape
Defines the shape of a region within a WSI.
Attributes:
width int - The width of the region in pixels.
height int - The height of the region in pixels.
unit MeasurementUnit - The unit of measurement for the shape dimensions (e.g., PIXELS, UM).
Example:
shape
Returns the width and height as a tuple.
Returns:
tuple[int, int]: A tuple containing (width, height).
to_um
Converts pixels to microns.
The pixel count to convert.
The microns-per-pixel scale.
Returns:
float - The length in microns.
Example:
to_pixels
Converts microns to pixels.
The length in microns to convert.
The microns-per-pixel scale.
Returns:
float - The length in pixels.
Example:
RegionMaskSpec
Defines the mask applied/corresponding to the region e.g., during extraction can be binary or multiclass mask.
Attributes:
ratio float - Pixels belonging to the mask relative to the total region area.
mask np.ndarray | None - The mask applied to the region, or None if mask storage was not requested.
RegionSpec
Defines the specifications of a region within a WSI.
Attributes:
location RegionLocation - The location of the region within the slide.
shape RegionShape - The shape of the region.
resolution Resolution - The resolution at which the region is defined (Level, MPP, or Magnification).
Example:
Region
Defines a region of interest within a WSI, including its image data and specifications. This class encapsulates both the pixel data of a region and its metadata, such as location, shape, and resolution.
Attributes:
image PIL.Image.Image - The pixel data of the region as a PIL Image.
spec RegionSpec - The specifications of the region, including location, shape, resolution, and mask.
Example:
location
Convenience property to access the region’s location directly.
shape
Convenience property to access the region’s shape directly.
resolution
Convenience property to access the region’s resolution directly.
measurement_unit
Convenience property to access the region’s measurement unit directly.
mask
Convenience property to access the region’s mask directly.
Returns:
np.ndarray | None: The mask array, or None if mask storage
was not requested during extraction.
mask_ratio
Convenience property to access the region’s mask ratio directly.