Chris Oswald
commited on
Commit
·
81b3fb4
1
Parent(s):
76cb606
cleaned up comments
Browse files
SPIDER.py
CHANGED
|
@@ -11,20 +11,17 @@
|
|
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
# See the License for the specific language governing permissions and
|
| 13 |
# limitations under the License.
|
| 14 |
-
|
| 15 |
-
"""TODO: Add a description here."""
|
| 16 |
|
| 17 |
# Import packages
|
| 18 |
import csv
|
| 19 |
-
import json
|
| 20 |
import os
|
| 21 |
-
from typing import Dict, List, Mapping, Optional,
|
| 22 |
|
| 23 |
import numpy as np
|
| 24 |
import pandas as pd
|
| 25 |
|
| 26 |
import datasets
|
| 27 |
-
import PIL
|
| 28 |
import skimage
|
| 29 |
import SimpleITK as sitk
|
| 30 |
|
|
@@ -59,14 +56,17 @@ MAX_IVD = 9
|
|
| 59 |
DEFAULT_SCAN_TYPES = ['t1', 't2', 't2_SPACE']
|
| 60 |
DEFAULT_RESIZE = (512, 512, 30)
|
| 61 |
|
| 62 |
-
# TODO: Add BibTeX citation
|
| 63 |
-
# Find for instance the citation on arxiv or on the dataset repo/website
|
| 64 |
_CITATION = """\
|
| 65 |
-
@
|
| 66 |
-
title
|
| 67 |
-
author={
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
}
|
| 71 |
"""
|
| 72 |
|
|
@@ -81,9 +81,6 @@ _HOMEPAGE = "https://zenodo.org/records/10159290"
|
|
| 81 |
_LICENSE = """Creative Commons Attribution 4.0 International License \
|
| 82 |
(https://creativecommons.org/licenses/by/4.0/legalcode)"""
|
| 83 |
|
| 84 |
-
# TODO: Add link to the official dataset URLs here
|
| 85 |
-
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
| 86 |
-
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
| 87 |
_URLS = {
|
| 88 |
"images":"https://zenodo.org/records/10159290/files/images.zip",
|
| 89 |
"masks":"https://zenodo.org/records/10159290/files/masks.zip",
|
|
@@ -111,45 +108,11 @@ class CustomBuilderConfig(datasets.BuilderConfig):
|
|
| 111 |
class SPIDER(datasets.GeneratorBasedBuilder):
|
| 112 |
"""TODO: Short description of my dataset."""
|
| 113 |
|
|
|
|
| 114 |
DEFAULT_WRITER_BATCH_SIZE = 16 # PyArrow default is too large for image data
|
| 115 |
-
|
| 116 |
VERSION = datasets.Version("1.1.0")
|
| 117 |
-
|
| 118 |
BUILDER_CONFIG_CLASS = CustomBuilderConfig
|
| 119 |
|
| 120 |
-
# BUILDER_CONFIGS = [
|
| 121 |
-
# CustomBuilderConfig(
|
| 122 |
-
# name="all_scan_types",
|
| 123 |
-
# version=VERSION,
|
| 124 |
-
# description="Use images of all scan types (t1, t2, t2 SPACE)",
|
| 125 |
-
# scan_types=['t1', 't2', 't2_SPACE'],
|
| 126 |
-
# resize_shape=DEFAULT_RESIZE,
|
| 127 |
-
# ),
|
| 128 |
-
# CustomBuilderConfig(
|
| 129 |
-
# name="t1_scan_types",
|
| 130 |
-
# version=VERSION,
|
| 131 |
-
# description="Use images of t1 scan types only",
|
| 132 |
-
# scan_types=['t1'],
|
| 133 |
-
# resize_shape=DEFAULT_RESIZE,
|
| 134 |
-
# ),
|
| 135 |
-
# CustomBuilderConfig(
|
| 136 |
-
# name="t2_scan_types",
|
| 137 |
-
# version=VERSION,
|
| 138 |
-
# description="Use images of t2 scan types only",
|
| 139 |
-
# scan_types=['t2'],
|
| 140 |
-
# resize_shape=DEFAULT_RESIZE,
|
| 141 |
-
# ),
|
| 142 |
-
# CustomBuilderConfig(
|
| 143 |
-
# name="t2_SPACE_scan_types",
|
| 144 |
-
# version=VERSION,
|
| 145 |
-
# description="Use images of t2 SPACE scan types only",
|
| 146 |
-
# scan_types=['t2_SPACE'],
|
| 147 |
-
# resize_shape=DEFAULT_RESIZE,
|
| 148 |
-
# ),
|
| 149 |
-
# ]
|
| 150 |
-
|
| 151 |
-
# DEFAULT_CONFIG_NAME = "all_scan_types"
|
| 152 |
-
|
| 153 |
def __init__(
|
| 154 |
self,
|
| 155 |
*args,
|
|
@@ -162,10 +125,9 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 162 |
self.resize_shape = resize_shape
|
| 163 |
|
| 164 |
def _info(self):
|
| 165 |
-
"""
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
"""
|
| 169 |
image_size = self.config.resize_shape
|
| 170 |
features = datasets.Features({
|
| 171 |
"patient_id": datasets.Value("string"),
|
|
@@ -227,32 +189,16 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 227 |
})
|
| 228 |
|
| 229 |
return datasets.DatasetInfo(
|
| 230 |
-
# This is the description that will appear on the datasets page.
|
| 231 |
description=_DESCRIPTION,
|
| 232 |
-
|
| 233 |
-
features=features, # Here we define them above because they are different between the two configurations
|
| 234 |
-
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
|
| 235 |
-
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
|
| 236 |
-
# supervised_keys=("sentence", "label"),
|
| 237 |
-
# Homepage of the dataset for documentation
|
| 238 |
homepage=_HOMEPAGE,
|
| 239 |
-
# License for the dataset if available
|
| 240 |
license=_LICENSE,
|
| 241 |
-
# Citation for the dataset
|
| 242 |
citation=_CITATION,
|
| 243 |
)
|
| 244 |
|
| 245 |
def _split_generators(self, dl_manager):
|
| 246 |
-
"""
|
| 247 |
-
This method is tasked with downloading/extracting the data
|
| 248 |
-
and defining the splits depending on the configuration
|
| 249 |
-
If several configurations are possible (listed in BUILDER_CONFIGS),
|
| 250 |
-
the configuration selected by the user is in self.config.name
|
| 251 |
-
"""
|
| 252 |
|
| 253 |
-
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
| 254 |
-
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
|
| 255 |
-
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 256 |
paths_dict = dl_manager.download_and_extract(_URLS)
|
| 257 |
return [
|
| 258 |
datasets.SplitGenerator(
|
|
@@ -373,13 +319,6 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 373 |
overview_dict[key] = {
|
| 374 |
k:v for k,v in item.items() if k not in exclude_vars
|
| 375 |
}
|
| 376 |
-
|
| 377 |
-
# # Determine maximum number of radiological gradings per patient
|
| 378 |
-
# max_ivd = 0
|
| 379 |
-
# for temp_dict_1 in grades_dict.values():
|
| 380 |
-
# for temp_dict_2 in temp_dict_1:
|
| 381 |
-
# if int(temp_dict_2['IVD label']) > max_ivd:
|
| 382 |
-
# max_ivd = int(temp_dict_2['IVD label'])
|
| 383 |
|
| 384 |
# Merge patient records for radiological gradings data
|
| 385 |
grades_dict = {}
|
|
@@ -478,10 +417,7 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 478 |
# Load .mha image file
|
| 479 |
image_path = os.path.join(paths_dict['images'], 'images', example)
|
| 480 |
image = sitk.ReadImage(image_path)
|
| 481 |
-
|
| 482 |
-
# # Rescale image intensities to [0, 255] and cast as UInt8 type
|
| 483 |
-
# image = sitk.Cast(sitk.RescaleIntensity(image), sitk.sitkUInt8)
|
| 484 |
-
|
| 485 |
# Convert .mha image to original size numeric array
|
| 486 |
image_array_original = sitk.GetArrayFromImage(image)
|
| 487 |
|
|
@@ -491,14 +427,9 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 491 |
resize_shape,
|
| 492 |
)
|
| 493 |
|
| 494 |
-
# NOTE: since the original array shape is not standardized, cannot return in dataset
|
| 495 |
-
|
| 496 |
# Load .mha mask file
|
| 497 |
mask_path = os.path.join(paths_dict['masks'], 'masks', example)
|
| 498 |
mask = sitk.ReadImage(mask_path)
|
| 499 |
-
|
| 500 |
-
# # Rescale mask intensities to [0, 255] and cast as UInt8 type
|
| 501 |
-
# mask = sitk.Cast(sitk.RescaleIntensity(mask), sitk.sitkUInt8)
|
| 502 |
|
| 503 |
# Convert .mha mask to original size numeric array
|
| 504 |
mask_array_original = sitk.GetArrayFromImage(mask)
|
|
|
|
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
# See the License for the specific language governing permissions and
|
| 13 |
# limitations under the License.
|
| 14 |
+
"""TODO: Add a description here.""" #TODO
|
|
|
|
| 15 |
|
| 16 |
# Import packages
|
| 17 |
import csv
|
|
|
|
| 18 |
import os
|
| 19 |
+
from typing import Dict, List, Mapping, Optional, Sequence, Tuple, Union
|
| 20 |
|
| 21 |
import numpy as np
|
| 22 |
import pandas as pd
|
| 23 |
|
| 24 |
import datasets
|
|
|
|
| 25 |
import skimage
|
| 26 |
import SimpleITK as sitk
|
| 27 |
|
|
|
|
| 56 |
DEFAULT_SCAN_TYPES = ['t1', 't2', 't2_SPACE']
|
| 57 |
DEFAULT_RESIZE = (512, 512, 30)
|
| 58 |
|
|
|
|
|
|
|
| 59 |
_CITATION = """\
|
| 60 |
+
@misc{vandergraaf2023lumbar,
|
| 61 |
+
title={Lumbar spine segmentation in MR images: a dataset and a public benchmark},
|
| 62 |
+
author={Jasper W. van der Graaf and Miranda L. van Hooff and \
|
| 63 |
+
Constantinus F. M. Buckens and Matthieu Rutten and \
|
| 64 |
+
Job L. C. van Susante and Robert Jan Kroeze and \
|
| 65 |
+
Marinus de Kleuver and Bram van Ginneken and Nikolas Lessmann},
|
| 66 |
+
year={2023},
|
| 67 |
+
eprint={2306.12217},
|
| 68 |
+
archivePrefix={arXiv},
|
| 69 |
+
primaryClass={eess.IV}
|
| 70 |
}
|
| 71 |
"""
|
| 72 |
|
|
|
|
| 81 |
_LICENSE = """Creative Commons Attribution 4.0 International License \
|
| 82 |
(https://creativecommons.org/licenses/by/4.0/legalcode)"""
|
| 83 |
|
|
|
|
|
|
|
|
|
|
| 84 |
_URLS = {
|
| 85 |
"images":"https://zenodo.org/records/10159290/files/images.zip",
|
| 86 |
"masks":"https://zenodo.org/records/10159290/files/masks.zip",
|
|
|
|
| 108 |
class SPIDER(datasets.GeneratorBasedBuilder):
|
| 109 |
"""TODO: Short description of my dataset."""
|
| 110 |
|
| 111 |
+
# Class attributes
|
| 112 |
DEFAULT_WRITER_BATCH_SIZE = 16 # PyArrow default is too large for image data
|
|
|
|
| 113 |
VERSION = datasets.Version("1.1.0")
|
|
|
|
| 114 |
BUILDER_CONFIG_CLASS = CustomBuilderConfig
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
def __init__(
|
| 117 |
self,
|
| 118 |
*args,
|
|
|
|
| 125 |
self.resize_shape = resize_shape
|
| 126 |
|
| 127 |
def _info(self):
|
| 128 |
+
"""Specify datasets.DatasetInfo object containing information and typing
|
| 129 |
+
for the dataset."""
|
| 130 |
+
|
|
|
|
| 131 |
image_size = self.config.resize_shape
|
| 132 |
features = datasets.Features({
|
| 133 |
"patient_id": datasets.Value("string"),
|
|
|
|
| 189 |
})
|
| 190 |
|
| 191 |
return datasets.DatasetInfo(
|
|
|
|
| 192 |
description=_DESCRIPTION,
|
| 193 |
+
features=features,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
homepage=_HOMEPAGE,
|
|
|
|
| 195 |
license=_LICENSE,
|
|
|
|
| 196 |
citation=_CITATION,
|
| 197 |
)
|
| 198 |
|
| 199 |
def _split_generators(self, dl_manager):
|
| 200 |
+
"""Download and extract data and define splits based on configuration."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
|
|
|
|
|
|
|
|
|
| 202 |
paths_dict = dl_manager.download_and_extract(_URLS)
|
| 203 |
return [
|
| 204 |
datasets.SplitGenerator(
|
|
|
|
| 319 |
overview_dict[key] = {
|
| 320 |
k:v for k,v in item.items() if k not in exclude_vars
|
| 321 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
|
| 323 |
# Merge patient records for radiological gradings data
|
| 324 |
grades_dict = {}
|
|
|
|
| 417 |
# Load .mha image file
|
| 418 |
image_path = os.path.join(paths_dict['images'], 'images', example)
|
| 419 |
image = sitk.ReadImage(image_path)
|
| 420 |
+
|
|
|
|
|
|
|
|
|
|
| 421 |
# Convert .mha image to original size numeric array
|
| 422 |
image_array_original = sitk.GetArrayFromImage(image)
|
| 423 |
|
|
|
|
| 427 |
resize_shape,
|
| 428 |
)
|
| 429 |
|
|
|
|
|
|
|
| 430 |
# Load .mha mask file
|
| 431 |
mask_path = os.path.join(paths_dict['masks'], 'masks', example)
|
| 432 |
mask = sitk.ReadImage(mask_path)
|
|
|
|
|
|
|
|
|
|
| 433 |
|
| 434 |
# Convert .mha mask to original size numeric array
|
| 435 |
mask_array_original = sitk.GetArrayFromImage(mask)
|