torch_em.data.datasets.medical.jnuifm

  1import os
  2from glob import glob
  3from natsort import natsorted
  4from urllib.parse import urljoin
  5from typing import Union, Tuple, List
  6
  7from torch.utils.data import Dataset, DataLoader
  8
  9import torch_em
 10
 11from .. import util
 12
 13
 14BASE_URL = "https://zenodo.org/records/7851339/files/"
 15URL = urljoin(BASE_URL, "Pubic%20Symphysis-Fetal%20Head%20Segmentation%20and%20Angle%20of%20Progression.zip")
 16CHECKSUM = "2b14d1c78e11cfb799d74951b0b985b90777c195f7a456ccd00528bf02802e21"
 17
 18
 19def get_jnuifm_data(path: Union[os.PathLike, str], download: bool = False) -> str:
 20    """Download the JNUIFM dataset.
 21
 22    Args:
 23        path: Filepath to a folder where the data is downloaded for further processing.
 24        download: Whether to download the data if it is not present.
 25
 26    Returns:
 27        Filepath where the data is downloaded.
 28    """
 29    data_dir = os.path.join(path, r"Pubic Symphysis-Fetal Head Segmentation and Angle of Progression")
 30    if os.path.exists(data_dir):
 31        return data_dir
 32
 33    os.makedirs(path, exist_ok=True)
 34
 35    zip_path = os.path.join(path, "JNU-IFM.zip")
 36    util.download_source(path=zip_path, url=URL, download=download, checksum=CHECKSUM)
 37    util.unzip(zip_path=zip_path, dst=path)
 38
 39    return data_dir
 40
 41
 42def get_jnuifm_paths(path: Union[os.PathLike, str], download: bool = False) -> Tuple[List[str], List[str]]:
 43    """Get paths to the JNUIFM data.
 44
 45    Args:
 46        path: Filepath to a folder where the data is downloaded for further processing.
 47        download: Whether to download the data if it is not present.
 48
 49    Returns:
 50        List of filepaths for the image data.
 51        List of filepaths for the label data.
 52    """
 53    data_dir = get_jnuifm_data(path, download)
 54    image_paths = natsorted(glob(os.path.join(data_dir, "image_mha", "*.mha")))
 55    gt_paths = natsorted(glob(os.path.join(data_dir, "label_mha", "*.mha")))
 56    return image_paths, gt_paths
 57
 58
 59def get_jnuifm_dataset(
 60    path: Union[os.PathLike, str],
 61    patch_shape: Tuple[int, int],
 62    resize_inputs: bool = False,
 63    download: bool = False,
 64    **kwargs
 65) -> Dataset:
 66    """Get the JNUIFM dataset for segmentation of pubic symphysis and fetal head in ultrasound images.
 67
 68    Args:
 69        path: Filepath to a folder where the data is downloaded for further processing.
 70        patch_shape: The patch shape to use for training.
 71        resize_inputs: Whether to resize the inputs to the expected patch shape.
 72        download: Whether to download the data if it is not present.
 73        kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset`.
 74
 75    Returns:
 76        The segmentation dataset.
 77    """
 78    image_paths, gt_paths = get_jnuifm_paths(path, download)
 79
 80    if resize_inputs:
 81        resize_kwargs = {"patch_shape": patch_shape, "is_rgb": True}
 82        kwargs, patch_shape = util.update_kwargs_for_resize_trafo(
 83            kwargs=kwargs, patch_shape=patch_shape, resize_inputs=resize_inputs, resize_kwargs=resize_kwargs
 84        )
 85
 86    return torch_em.default_segmentation_dataset(
 87        raw_paths=image_paths,
 88        raw_key=None,
 89        label_paths=gt_paths,
 90        label_key=None,
 91        patch_shape=patch_shape,
 92        ndim=2,
 93        with_channels=True,
 94        is_seg_dataset=False,
 95        **kwargs
 96    )
 97
 98
 99def get_jnuifm_loader(
100    path: Union[os.PathLike, str],
101    batch_size: int,
102    patch_shape: Tuple[int, int],
103    resize_inputs: bool = False,
104    download: bool = False,
105    **kwargs
106) -> DataLoader:
107    """Get the JNUIFM dataloader for segmentation of pubic symphysis and fetal head in ultrasound images.
108
109    Args:
110        path: Filepath to a folder where the data is downloaded for further processing.
111        batch_size: The batch size for training.
112        patch_shape: The patch shape to use for training.
113        resize_inputs: Whether to resize the inputs to the expected patch shape.
114        download: Whether to download the data if it is not present.
115        kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset` or for the PyTorch DataLoader.
116
117    Returns:
118        The DataLoader.
119    """
120    ds_kwargs, loader_kwargs = util.split_kwargs(torch_em.default_segmentation_dataset, **kwargs)
121    dataset = get_jnuifm_dataset(path, patch_shape, resize_inputs, download, **ds_kwargs)
122    return torch_em.get_data_loader(dataset, batch_size, **loader_kwargs)
BASE_URL = 'https://zenodo.org/records/7851339/files/'
URL = 'https://zenodo.org/records/7851339/files/Pubic%20Symphysis-Fetal%20Head%20Segmentation%20and%20Angle%20of%20Progression.zip'
CHECKSUM = '2b14d1c78e11cfb799d74951b0b985b90777c195f7a456ccd00528bf02802e21'
def get_jnuifm_data(path: Union[os.PathLike, str], download: bool = False) -> str:
20def get_jnuifm_data(path: Union[os.PathLike, str], download: bool = False) -> str:
21    """Download the JNUIFM dataset.
22
23    Args:
24        path: Filepath to a folder where the data is downloaded for further processing.
25        download: Whether to download the data if it is not present.
26
27    Returns:
28        Filepath where the data is downloaded.
29    """
30    data_dir = os.path.join(path, r"Pubic Symphysis-Fetal Head Segmentation and Angle of Progression")
31    if os.path.exists(data_dir):
32        return data_dir
33
34    os.makedirs(path, exist_ok=True)
35
36    zip_path = os.path.join(path, "JNU-IFM.zip")
37    util.download_source(path=zip_path, url=URL, download=download, checksum=CHECKSUM)
38    util.unzip(zip_path=zip_path, dst=path)
39
40    return data_dir

Download the JNUIFM 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_jnuifm_paths( path: Union[os.PathLike, str], download: bool = False) -> Tuple[List[str], List[str]]:
43def get_jnuifm_paths(path: Union[os.PathLike, str], download: bool = False) -> Tuple[List[str], List[str]]:
44    """Get paths to the JNUIFM data.
45
46    Args:
47        path: Filepath to a folder where the data is downloaded for further processing.
48        download: Whether to download the data if it is not present.
49
50    Returns:
51        List of filepaths for the image data.
52        List of filepaths for the label data.
53    """
54    data_dir = get_jnuifm_data(path, download)
55    image_paths = natsorted(glob(os.path.join(data_dir, "image_mha", "*.mha")))
56    gt_paths = natsorted(glob(os.path.join(data_dir, "label_mha", "*.mha")))
57    return image_paths, gt_paths

Get paths to the JNUIFM 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_jnuifm_dataset( path: Union[os.PathLike, str], patch_shape: Tuple[int, int], resize_inputs: bool = False, download: bool = False, **kwargs) -> torch.utils.data.dataset.Dataset:
60def get_jnuifm_dataset(
61    path: Union[os.PathLike, str],
62    patch_shape: Tuple[int, int],
63    resize_inputs: bool = False,
64    download: bool = False,
65    **kwargs
66) -> Dataset:
67    """Get the JNUIFM dataset for segmentation of pubic symphysis and fetal head in ultrasound images.
68
69    Args:
70        path: Filepath to a folder where the data is downloaded for further processing.
71        patch_shape: The patch shape to use for training.
72        resize_inputs: Whether to resize the inputs to the expected patch shape.
73        download: Whether to download the data if it is not present.
74        kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset`.
75
76    Returns:
77        The segmentation dataset.
78    """
79    image_paths, gt_paths = get_jnuifm_paths(path, download)
80
81    if resize_inputs:
82        resize_kwargs = {"patch_shape": patch_shape, "is_rgb": True}
83        kwargs, patch_shape = util.update_kwargs_for_resize_trafo(
84            kwargs=kwargs, patch_shape=patch_shape, resize_inputs=resize_inputs, resize_kwargs=resize_kwargs
85        )
86
87    return torch_em.default_segmentation_dataset(
88        raw_paths=image_paths,
89        raw_key=None,
90        label_paths=gt_paths,
91        label_key=None,
92        patch_shape=patch_shape,
93        ndim=2,
94        with_channels=True,
95        is_seg_dataset=False,
96        **kwargs
97    )

Get the JNUIFM dataset for segmentation of pubic symphysis and fetal head in ultrasound images.

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 expected 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_jnuifm_loader( path: Union[os.PathLike, str], batch_size: int, patch_shape: Tuple[int, int], resize_inputs: bool = False, download: bool = False, **kwargs) -> torch.utils.data.dataloader.DataLoader:
100def get_jnuifm_loader(
101    path: Union[os.PathLike, str],
102    batch_size: int,
103    patch_shape: Tuple[int, int],
104    resize_inputs: bool = False,
105    download: bool = False,
106    **kwargs
107) -> DataLoader:
108    """Get the JNUIFM dataloader for segmentation of pubic symphysis and fetal head in ultrasound images.
109
110    Args:
111        path: Filepath to a folder where the data is downloaded for further processing.
112        batch_size: The batch size for training.
113        patch_shape: The patch shape to use for training.
114        resize_inputs: Whether to resize the inputs to the expected patch shape.
115        download: Whether to download the data if it is not present.
116        kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset` or for the PyTorch DataLoader.
117
118    Returns:
119        The DataLoader.
120    """
121    ds_kwargs, loader_kwargs = util.split_kwargs(torch_em.default_segmentation_dataset, **kwargs)
122    dataset = get_jnuifm_dataset(path, patch_shape, resize_inputs, download, **ds_kwargs)
123    return torch_em.get_data_loader(dataset, batch_size, **loader_kwargs)

Get the JNUIFM dataloader for segmentation of pubic symphysis and fetal head in ultrasound images.

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 expected 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.