torch_em.data.datasets.medical.bhsd

The BHSD dataset contains annotations for multiclass intracranial hemorrhage segmentation in brain CT.

The Brain Hemorrhage Segmentation Dataset (BHSD) provides 192 non-contrast head CT volumes with pixel-level annotations of five intracranial hemorrhage (ICH) subtypes, see LABEL_IDS. The dataset also contains a much larger set of 1980 volumes with only slice-level (i.e. not pixel-level) annotations, which is not exposed by this module.

The data is hosted on Hugging Face at https://huggingface.co/datasets/Wendy-Fly/BHSD and is distributed under the MIT license.

This dataset is from the publication https://doi.org/10.1007/978-3-031-45673-2_15. Please cite it if you use this dataset for your research.

  1"""The BHSD dataset contains annotations for multiclass intracranial hemorrhage segmentation in
  2brain CT.
  3
  4The Brain Hemorrhage Segmentation Dataset (BHSD) provides 192 non-contrast head CT volumes with
  5pixel-level annotations of five intracranial hemorrhage (ICH) subtypes, see `LABEL_IDS`. The
  6dataset also contains a much larger set of 1980 volumes with only slice-level (i.e. not
  7pixel-level) annotations, which is not exposed by this module.
  8
  9The data is hosted on Hugging Face at https://huggingface.co/datasets/Wendy-Fly/BHSD and is
 10distributed under the MIT license.
 11
 12This dataset is from the publication https://doi.org/10.1007/978-3-031-45673-2_15.
 13Please cite it if you use this dataset for your research.
 14"""
 15
 16import os
 17from glob import glob
 18from natsort import natsorted
 19from typing import Union, Tuple, List
 20
 21from torch.utils.data import Dataset, DataLoader
 22
 23import torch_em
 24
 25from .. import util
 26
 27
 28URL = "https://huggingface.co/datasets/Wendy-Fly/BHSD/resolve/main/label_192.zip"
 29CHECKSUM = "582bf184af993541a4958a4d209a6a44e3bbe702a5daefaf9fb1733a4e7a6e39"
 30
 31LABEL_IDS = {
 32    0: "background", 1: "epidural", 2: "intraparenchymal", 3: "intraventricular", 4: "subarachnoid", 5: "subdural",
 33}
 34"""The label ids of the intracranial hemorrhage subtypes, as defined by the dataset authors."""
 35
 36
 37def get_bhsd_data(path: Union[os.PathLike, str], download: bool = False) -> str:
 38    """Download the BHSD dataset.
 39
 40    Args:
 41        path: Filepath to a folder where the data is downloaded for further processing.
 42        download: Whether to download the data if it is not present.
 43
 44    Returns:
 45        Filepath where the data is downloaded.
 46    """
 47    data_dir = os.path.join(path, "label_192")
 48    if os.path.exists(data_dir):
 49        return data_dir
 50
 51    os.makedirs(path, exist_ok=True)
 52
 53    zip_path = os.path.join(path, "label_192.zip")
 54    util.download_source(path=zip_path, url=URL, download=download, checksum=CHECKSUM)
 55    util.unzip(zip_path=zip_path, dst=path)
 56
 57    return data_dir
 58
 59
 60def get_bhsd_paths(path: Union[os.PathLike, str], download: bool = False) -> Tuple[List[str], List[str]]:
 61    """Get paths to the BHSD data.
 62
 63    Args:
 64        path: Filepath to a folder where the data is downloaded for further processing.
 65        download: Whether to download the data if it is not present.
 66
 67    Returns:
 68        List of filepaths for the image data.
 69        List of filepaths for the label data.
 70    """
 71    data_dir = get_bhsd_data(path, download)
 72
 73    image_paths = natsorted(glob(os.path.join(data_dir, "images", "*.nii.gz")))
 74    gt_paths = natsorted(glob(os.path.join(data_dir, "ground truths", "*.nii.gz")))
 75
 76    assert len(image_paths) == len(gt_paths) and len(image_paths) > 0
 77
 78    return image_paths, gt_paths
 79
 80
 81def get_bhsd_dataset(
 82    path: Union[os.PathLike, str],
 83    patch_shape: Tuple[int, ...],
 84    resize_inputs: bool = False,
 85    download: bool = False,
 86    **kwargs
 87) -> Dataset:
 88    """Get the BHSD dataset for multiclass intracranial hemorrhage segmentation.
 89
 90    Args:
 91        path: Filepath to a folder where the data is downloaded for further processing.
 92        patch_shape: The patch shape to use for training.
 93        resize_inputs: Whether to resize the inputs to the patch shape.
 94        download: Whether to download the data if it is not present.
 95        kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset`.
 96
 97    Returns:
 98        The segmentation dataset.
 99    """
100    image_paths, gt_paths = get_bhsd_paths(path, download)
101
102    if resize_inputs:
103        resize_kwargs = {"patch_shape": patch_shape, "is_rgb": False}
104        kwargs, patch_shape = util.update_kwargs_for_resize_trafo(
105            kwargs=kwargs, patch_shape=patch_shape, resize_inputs=resize_inputs, resize_kwargs=resize_kwargs
106        )
107
108    return torch_em.default_segmentation_dataset(
109        raw_paths=image_paths,
110        raw_key="data",
111        label_paths=gt_paths,
112        label_key="data",
113        patch_shape=patch_shape,
114        is_seg_dataset=True,
115        **kwargs
116    )
117
118
119def get_bhsd_loader(
120    path: Union[os.PathLike, str],
121    batch_size: int,
122    patch_shape: Tuple[int, ...],
123    resize_inputs: bool = False,
124    download: bool = False,
125    **kwargs
126) -> DataLoader:
127    """Get the BHSD dataloader for multiclass intracranial hemorrhage segmentation.
128
129    Args:
130        path: Filepath to a folder where the data is downloaded for further processing.
131        batch_size: The batch size for training.
132        patch_shape: The patch shape to use for training.
133        resize_inputs: Whether to resize the inputs to the patch shape.
134        download: Whether to download the data if it is not present.
135        kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset` or for the PyTorch DataLoader.
136
137    Returns:
138        The DataLoader.
139    """
140    ds_kwargs, loader_kwargs = util.split_kwargs(torch_em.default_segmentation_dataset, **kwargs)
141    dataset = get_bhsd_dataset(path, patch_shape, resize_inputs, download, **ds_kwargs)
142    return torch_em.get_data_loader(dataset, batch_size, **loader_kwargs)
URL = 'https://huggingface.co/datasets/Wendy-Fly/BHSD/resolve/main/label_192.zip'
CHECKSUM = '582bf184af993541a4958a4d209a6a44e3bbe702a5daefaf9fb1733a4e7a6e39'
LABEL_IDS = {0: 'background', 1: 'epidural', 2: 'intraparenchymal', 3: 'intraventricular', 4: 'subarachnoid', 5: 'subdural'}

The label ids of the intracranial hemorrhage subtypes, as defined by the dataset authors.

def get_bhsd_data(path: Union[os.PathLike, str], download: bool = False) -> str:
38def get_bhsd_data(path: Union[os.PathLike, str], download: bool = False) -> str:
39    """Download the BHSD dataset.
40
41    Args:
42        path: Filepath to a folder where the data is downloaded for further processing.
43        download: Whether to download the data if it is not present.
44
45    Returns:
46        Filepath where the data is downloaded.
47    """
48    data_dir = os.path.join(path, "label_192")
49    if os.path.exists(data_dir):
50        return data_dir
51
52    os.makedirs(path, exist_ok=True)
53
54    zip_path = os.path.join(path, "label_192.zip")
55    util.download_source(path=zip_path, url=URL, download=download, checksum=CHECKSUM)
56    util.unzip(zip_path=zip_path, dst=path)
57
58    return data_dir

Download the BHSD dataset.

Arguments:
  • path: Filepath to a folder where the data is downloaded for further processing.
  • download: Whether to download the data if it is not present.
Returns:

Filepath where the data is downloaded.

def get_bhsd_paths( path: Union[os.PathLike, str], download: bool = False) -> Tuple[List[str], List[str]]:
61def get_bhsd_paths(path: Union[os.PathLike, str], download: bool = False) -> Tuple[List[str], List[str]]:
62    """Get paths to the BHSD data.
63
64    Args:
65        path: Filepath to a folder where the data is downloaded for further processing.
66        download: Whether to download the data if it is not present.
67
68    Returns:
69        List of filepaths for the image data.
70        List of filepaths for the label data.
71    """
72    data_dir = get_bhsd_data(path, download)
73
74    image_paths = natsorted(glob(os.path.join(data_dir, "images", "*.nii.gz")))
75    gt_paths = natsorted(glob(os.path.join(data_dir, "ground truths", "*.nii.gz")))
76
77    assert len(image_paths) == len(gt_paths) and len(image_paths) > 0
78
79    return image_paths, gt_paths

Get paths to the BHSD data.

Arguments:
  • path: Filepath to a folder where the data is downloaded for further processing.
  • download: Whether to download the data if it is not present.
Returns:

List of filepaths for the image data. List of filepaths for the label data.

def get_bhsd_dataset( path: Union[os.PathLike, str], patch_shape: Tuple[int, ...], resize_inputs: bool = False, download: bool = False, **kwargs) -> torch.utils.data.dataset.Dataset:
 82def get_bhsd_dataset(
 83    path: Union[os.PathLike, str],
 84    patch_shape: Tuple[int, ...],
 85    resize_inputs: bool = False,
 86    download: bool = False,
 87    **kwargs
 88) -> Dataset:
 89    """Get the BHSD dataset for multiclass intracranial hemorrhage segmentation.
 90
 91    Args:
 92        path: Filepath to a folder where the data is downloaded for further processing.
 93        patch_shape: The patch shape to use for training.
 94        resize_inputs: Whether to resize the inputs to the patch shape.
 95        download: Whether to download the data if it is not present.
 96        kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset`.
 97
 98    Returns:
 99        The segmentation dataset.
100    """
101    image_paths, gt_paths = get_bhsd_paths(path, download)
102
103    if resize_inputs:
104        resize_kwargs = {"patch_shape": patch_shape, "is_rgb": False}
105        kwargs, patch_shape = util.update_kwargs_for_resize_trafo(
106            kwargs=kwargs, patch_shape=patch_shape, resize_inputs=resize_inputs, resize_kwargs=resize_kwargs
107        )
108
109    return torch_em.default_segmentation_dataset(
110        raw_paths=image_paths,
111        raw_key="data",
112        label_paths=gt_paths,
113        label_key="data",
114        patch_shape=patch_shape,
115        is_seg_dataset=True,
116        **kwargs
117    )

Get the BHSD dataset for multiclass intracranial hemorrhage segmentation.

Arguments:
  • path: Filepath to a folder where the data is downloaded for further processing.
  • patch_shape: The patch shape to use for training.
  • resize_inputs: Whether to resize the inputs to the patch shape.
  • download: Whether to download the data if it is not present.
  • kwargs: Additional keyword arguments for torch_em.default_segmentation_dataset.
Returns:

The segmentation dataset.

def get_bhsd_loader( path: Union[os.PathLike, str], batch_size: int, patch_shape: Tuple[int, ...], resize_inputs: bool = False, download: bool = False, **kwargs) -> torch.utils.data.dataloader.DataLoader:
120def get_bhsd_loader(
121    path: Union[os.PathLike, str],
122    batch_size: int,
123    patch_shape: Tuple[int, ...],
124    resize_inputs: bool = False,
125    download: bool = False,
126    **kwargs
127) -> DataLoader:
128    """Get the BHSD dataloader for multiclass intracranial hemorrhage segmentation.
129
130    Args:
131        path: Filepath to a folder where the data is downloaded for further processing.
132        batch_size: The batch size for training.
133        patch_shape: The patch shape to use for training.
134        resize_inputs: Whether to resize the inputs to the patch shape.
135        download: Whether to download the data if it is not present.
136        kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset` or for the PyTorch DataLoader.
137
138    Returns:
139        The DataLoader.
140    """
141    ds_kwargs, loader_kwargs = util.split_kwargs(torch_em.default_segmentation_dataset, **kwargs)
142    dataset = get_bhsd_dataset(path, patch_shape, resize_inputs, download, **ds_kwargs)
143    return torch_em.get_data_loader(dataset, batch_size, **loader_kwargs)

Get the BHSD dataloader for multiclass intracranial hemorrhage segmentation.

Arguments:
  • path: Filepath to a folder where the data is downloaded for further processing.
  • batch_size: The batch size for training.
  • patch_shape: The patch shape to use for training.
  • resize_inputs: Whether to resize the inputs to the patch shape.
  • download: Whether to download the data if it is not present.
  • kwargs: Additional keyword arguments for torch_em.default_segmentation_dataset or for the PyTorch DataLoader.
Returns:

The DataLoader.