Update LyNoS.py
#2
by
dbouget
- opened
LyNoS.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
"""LyNoS: Mediastinal lymph nodes segmentation using 3D convolutional neural network ensembles and anatomical priors guiding."""
|
| 2 |
|
| 3 |
|
| 4 |
-
import
|
|
|
|
|
|
|
| 5 |
|
| 6 |
_DESCRIPTION = """\
|
| 7 |
LyNoS: Mediastinal lymph nodes segmentation using 3D convolutional neural network ensembles and anatomical priors guiding.
|
|
@@ -25,51 +27,113 @@ _CITATION = """\
|
|
| 25 |
|
| 26 |
"""
|
| 27 |
|
| 28 |
-
_URLS = [
|
| 29 |
-
{
|
| 30 |
-
"ct": f"data/Pat{i}/Pat{i}_data.nii.gz",
|
| 31 |
-
"azygos": f"data/Pat{i}/Pat{i}_labels_Azygos.nii.gz",
|
| 32 |
-
"brachiocephalicveins": f"data/Pat{i}/Pat{i}_labels_BrachiocephalicVeins.nii.gz",
|
| 33 |
-
"esophagus": f"data/Pat{i}/Pat{i}_labels_Esophagus.nii.gz",
|
| 34 |
-
"lymphnodes": f"data/Pat{i}/Pat{i}_labels_LymphNodes.nii.gz",
|
| 35 |
-
"subclaviancarotidarteries": f"data/Pat{i}/Pat{i}_labels_SubCarArt.nii.gz",
|
| 36 |
-
}
|
| 37 |
-
for i in range(1, 15)
|
| 38 |
-
]
|
| 39 |
-
|
| 40 |
|
| 41 |
class LyNoS(datasets.GeneratorBasedBuilder):
|
| 42 |
"""A segmentation benchmark dataset for enlarged lymph nodes in patients with primary lung cancer."""
|
| 43 |
|
| 44 |
VERSION = datasets.Version("1.0.0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
def _info(self):
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
description=_DESCRIPTION,
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
homepage=_HOMEPAGE,
|
|
|
|
| 57 |
license=_LICENSE,
|
|
|
|
| 58 |
citation=_CITATION,
|
| 59 |
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
| 61 |
def _split_generators(self, dl_manager):
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
return [
|
| 64 |
datasets.SplitGenerator(
|
| 65 |
name=datasets.Split.TEST,
|
| 66 |
# These kwargs will be passed to _generate_examples
|
| 67 |
gen_kwargs={
|
| 68 |
-
"
|
| 69 |
},
|
| 70 |
),
|
| 71 |
]
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""LyNoS: Mediastinal lymph nodes segmentation using 3D convolutional neural network ensembles and anatomical priors guiding."""
|
| 2 |
|
| 3 |
|
| 4 |
+
import os
|
| 5 |
+
import csv
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
_DESCRIPTION = """\
|
| 9 |
LyNoS: Mediastinal lymph nodes segmentation using 3D convolutional neural network ensembles and anatomical priors guiding.
|
|
|
|
| 27 |
|
| 28 |
"""
|
| 29 |
|
| 30 |
+
#_URLS = [
|
| 31 |
+
# {
|
| 32 |
+
# "ct": f"data/Pat{i}/Pat{i}_data.nii.gz",
|
| 33 |
+
# "azygos": f"data/Pat{i}/Pat{i}_labels_Azygos.nii.gz",
|
| 34 |
+
# "brachiocephalicveins": f"data/Pat{i}/Pat{i}_labels_BrachiocephalicVeins.nii.gz",
|
| 35 |
+
# "esophagus": f"data/Pat{i}/Pat{i}_labels_Esophagus.nii.gz",
|
| 36 |
+
# "lymphnodes": f"data/Pat{i}/Pat{i}_labels_LymphNodes.nii.gz",
|
| 37 |
+
# "subclaviancarotidarteries": f"data/Pat{i}/Pat{i}_labels_SubCarArt.nii.gz",
|
| 38 |
+
# }
|
| 39 |
+
# for i in range(1, 15)
|
| 40 |
+
#]
|
| 41 |
+
_URLS = {"zenodo": "https://zenodo.org/records/10102261/files/LyNoS.zip?download=1"}
|
| 42 |
|
| 43 |
class LyNoS(datasets.GeneratorBasedBuilder):
|
| 44 |
"""A segmentation benchmark dataset for enlarged lymph nodes in patients with primary lung cancer."""
|
| 45 |
|
| 46 |
VERSION = datasets.Version("1.0.0")
|
| 47 |
+
DEFAULT_CONFIG_NAME = "zenodo"
|
| 48 |
+
BUILDER_CONFIGS = [
|
| 49 |
+
#datasets.BuilderConfig(name="first_domain", version=VERSION, description="This part of my dataset covers a first domain"),
|
| 50 |
+
#datasets.BuilderConfig(name="second_domain", version=VERSION, description="This part of my dataset covers a second domain"),
|
| 51 |
+
datasets.BuilderConfig(name="zenodo", version=VERSION, description="This includes all 15 CTs stored as a single zip on Zenodo"),
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
DEFAULT_CONFIG_NAME = "zenodo" # It's not mandatory to have a default configuration. Just use one if it make sense.
|
| 55 |
+
|
| 56 |
+
def __init__(self, **kwargs):
|
| 57 |
+
super().__init__(**kwargs)
|
| 58 |
+
self.DATA_DIR = None
|
| 59 |
+
|
| 60 |
+
def get_patient(self, patient_id):
|
| 61 |
+
if (patient_id < 1) or (patiend_id > 15):
|
| 62 |
+
raise ValueError("patient_id should be an integer in range [1, 15].")
|
| 63 |
|
| 64 |
def _info(self):
|
| 65 |
+
# TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
|
| 66 |
+
if self.config.name == "zenodo": # This is the name of the configuration selected in BUILDER_CONFIGS above
|
| 67 |
+
features = datasets.Features(
|
| 68 |
+
{
|
| 69 |
+
"ct": datasets.Value("string"),
|
| 70 |
+
"lymphnodes": datasets.Value("string"),
|
| 71 |
+
"azygos": datasets.Value("string"),
|
| 72 |
+
"brachiocephalicveins": datasets.Value("string"),
|
| 73 |
+
"esophagus": datasets.Value("string"),
|
| 74 |
+
"subclaviancarotidarteries": datasets.Value("string")
|
| 75 |
+
}
|
| 76 |
+
)
|
| 77 |
+
else:
|
| 78 |
+
raise ValueError("Only 'zenodo' is supported.")# This is an example to show how to have different features for "first_domain" and "second_domain"
|
| 79 |
+
|
| 80 |
+
return datasets.DatasetInfo(
|
| 81 |
+
# This is the description that will appear on the datasets page.
|
| 82 |
description=_DESCRIPTION,
|
| 83 |
+
# This defines the different columns of the dataset and their types
|
| 84 |
+
features=features, # Here we define them above because they are different between the two configurations
|
| 85 |
+
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
|
| 86 |
+
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
|
| 87 |
+
# supervised_keys=("sentence", "label"),
|
| 88 |
+
# Homepage of the dataset for documentation
|
| 89 |
homepage=_HOMEPAGE,
|
| 90 |
+
# License for the dataset if available
|
| 91 |
license=_LICENSE,
|
| 92 |
+
# Citation for the dataset
|
| 93 |
citation=_CITATION,
|
| 94 |
)
|
| 95 |
|
| 96 |
+
def get_data_dir(self):
|
| 97 |
+
return self.DATA_DIR
|
| 98 |
+
|
| 99 |
def _split_generators(self, dl_manager):
|
| 100 |
+
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
| 101 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
| 102 |
+
|
| 103 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
| 104 |
+
# 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.
|
| 105 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 106 |
+
urls = _URLS[self.config.name]
|
| 107 |
+
self.DATA_DIR = dl_manager.download_and_extract(urls)
|
| 108 |
+
|
| 109 |
+
# append AeroPath
|
| 110 |
+
self.DATA_DIR = os.path.join(self.DATA_DIR, "LyNoS")
|
| 111 |
+
|
| 112 |
+
print("data is downloaded to:", self.DATA_DIR)
|
| 113 |
+
|
| 114 |
return [
|
| 115 |
datasets.SplitGenerator(
|
| 116 |
name=datasets.Split.TEST,
|
| 117 |
# These kwargs will be passed to _generate_examples
|
| 118 |
gen_kwargs={
|
| 119 |
+
"split": "test",
|
| 120 |
},
|
| 121 |
),
|
| 122 |
]
|
| 123 |
|
| 124 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
| 125 |
+
def _generate_examples(self, split):
|
| 126 |
+
# TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
| 127 |
+
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
|
| 128 |
+
for patient_id in os.listdir(self.DATA_DIR):
|
| 129 |
+
curr_path = os.path.join(self.DATA_DIR, patient_id)
|
| 130 |
+
if patient_id in ["README.md", "license.md", "stations_sto.csv"]:
|
| 131 |
+
continue
|
| 132 |
+
yield patient_id, {
|
| 133 |
+
"ct": os.path.join(curr_path, patient_id.lower() + "_data.nii.gz"),
|
| 134 |
+
"lymphnodes": os.path.join(curr_path, patient_id.lower() + "_labels_LymphNodes.nii.gz"),
|
| 135 |
+
"azygos": os.path.join(curr_path, patient_id.lower() + "_labels_Azygos.nii.gz"),
|
| 136 |
+
"brachiocephalicveins": os.path.join(curr_path, patient_id.lower() + "_labels_BrachiocephalicVeins.nii.gz"),
|
| 137 |
+
"esophagus": os.path.join(curr_path, patient_id.lower() + "_labels_Esophagus.nii.gz"),
|
| 138 |
+
"subclaviancarotidarteries": os.path.join(curr_path, patient_id.lower() + "_labels_SubCarArt.nii.gz")
|
| 139 |
+
}
|