Skip to main content

Dataset information

Inspect dataset metadata, suggest preprocessing parameters and build a reusable preprocessing cache.

source

MedDataset

def MedDataset(
    dataframe:NoneType=None, image_col:str=None, mask_col:str='mask_path', path:NoneType=None,
    img_list:NoneType=None, postfix:str='', apply_reorder:bool=True,
    dtype:(<class 'fastMONAI.vision_core.MedImage'>, <class 'fastMONAI.vision_core.MedMask'>)=MedImage,
    max_workers:int=1, use_cache:bool=True, cache_path:NoneType=None
):

A class to extract and present information about the dataset.


source

suggest_patch_size

def suggest_patch_size(
    dataset:MedDataset, target_spacing:list=None, min_patch_size:list=None, max_patch_size:list=None, divisor:int=16
)->list:

Suggest optimal patch size based on dataset dimensions.

Uses median shape as the starting point but clamps to the minimum volume size per axis, ensuring the suggested patch fits ALL volumes without requiring padding during training.

Algorithm: 1. Use min(median, min_volume) per axis for safety 2. Round down to nearest multiple of divisor (16 for UNet compatibility) 3. Clamp to [min_patch_size, max_patch_size] bounds 4. Validate: error if min_patch_size or divisor exceeds the smallest volume

Args: dataset: MedDataset instance with analyzed images. target_spacing: Target voxel spacing [x, y, z]. If None, uses dataset.get_suggestion()[‘target_spacing’]. min_patch_size: Minimum per dimension. Default [32, 32, 32]. max_patch_size: Maximum per dimension. Default [256, 256, 256]. divisor: Ensure divisibility (default 16 for UNet compatibility).

Returns: list: [patch_dim_0, patch_dim_1, patch_dim_2]

Example: >>> from fastMONAI.dataset_info import MedDataset >>> dataset = MedDataset(dataframe=df, mask_col=‘mask_path’, dtype=MedMask) >>> >>> # Use recommended spacing >>> patch_size = suggest_patch_size(dataset) >>> >>> # Use custom spacing >>> patch_size = suggest_patch_size(dataset, target_spacing=[1.0, 1.0, 2.0])


source

preprocess_dataset

def preprocess_dataset(
    df, img_col, mask_col:NoneType=None, output_dir:str='preprocessed', target_spacing:NoneType=None,
    apply_reorder:bool=True, transforms:NoneType=None, max_workers:int=4, skip_existing:bool=True,
    dataset_version:NoneType=None
):

Preprocess a dataset into a manifest-validated, fingerprinted disk cache.

Cache identity covers the optional mask-based dataset_version, preprocessing contract, and cheap source signatures (resolved path, size, and nanosecond mtime). A complete cache is published only after every case succeeds and the manifest is written. The DataFrame receives output paths only after publication.

Transform pipeline order: CopyAffine (if masks) -> ToCanonical (if apply_reorder) -> Resample (if target_spacing) -> user transforms

Args: df: DataFrame with file paths. img_col: Column name for image paths. mask_col: Optional column name for mask paths. output_dir: Cache root. A versioned child directory is created below it. target_spacing: Target voxel spacing for resampling. apply_reorder: Whether to reorder to RAS+ canonical orientation. transforms: Additional TorchIO or serializable fastMONAI transforms. max_workers: Number of parallel volume workers. skip_existing: Reuse a valid matching cache. False explicitly rebuilds it. dataset_version: Optional existing dataset/annotation fingerprint to bind to this cache. Raw image contents are not read to compute the cache version.

Returns: PreprocessingResult describing the published or reused cache.


source

PreprocessingResult

def PreprocessingResult(
    cache_version:str, manifest_path:Path, output_dir:Path, processed:int, reused:bool
)->None:

Published preprocessing-cache identity and location.


source

get_class_weights

def get_class_weights(
    labels:(<built-in function array>, <class 'list'>), class_weight:str='balanced'
)->Tensor:

Calculates and returns the class weights.

Args: labels: An array or list of class labels for each instance in the dataset. class_weight: Defaults to ‘balanced’.

Returns: A tensor of class weights.