Started making dataset script
Browse files- my_dataset.py +170 -0
my_dataset.py
ADDED
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
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: Address all TODOs and remove all explanatory comments
|
15 |
+
"""TODO: Add a description here."""
|
16 |
+
|
17 |
+
|
18 |
+
import csv
|
19 |
+
import json
|
20 |
+
import os
|
21 |
+
|
22 |
+
import datasets
|
23 |
+
|
24 |
+
|
25 |
+
# TODO: Add BibTeX citation
|
26 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
27 |
+
_CITATION = """\
|
28 |
+
@misc{støverud2023aeropath,
|
29 |
+
title={AeroPath: An airway segmentation benchmark dataset with challenging pathology},
|
30 |
+
author={Karen-Helene Støverud and David Bouget and Andre Pedersen and Håkon Olav Leira and Thomas Langø and Erlend Fagertun Hofstad},
|
31 |
+
year={2023},
|
32 |
+
eprint={2311.01138},
|
33 |
+
archivePrefix={arXiv},
|
34 |
+
primaryClass={cs.CV}
|
35 |
+
}
|
36 |
+
"""
|
37 |
+
|
38 |
+
# TODO: Add description of the dataset here
|
39 |
+
# You can copy an official description
|
40 |
+
_DESCRIPTION = """\
|
41 |
+
AeroPath: An airway segmentation benchmark dataset with challenging pathology.
|
42 |
+
"""
|
43 |
+
|
44 |
+
# TODO: Add a link to an official homepage for the dataset here
|
45 |
+
_HOMEPAGE = "https://github.com/raidionics/AeroPath"
|
46 |
+
|
47 |
+
# TODO: Add the licence for the dataset here if you can find it
|
48 |
+
_LICENSE = "MIT"
|
49 |
+
|
50 |
+
# TODO: Add link to the official dataset URLs here
|
51 |
+
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
52 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
53 |
+
_URLS = {
|
54 |
+
#"first_domain": "https://huggingface.co/great-new-dataset-first_domain.zip",
|
55 |
+
#"second_domain": "https://huggingface.co/great-new-dataset-second_domain.zip",
|
56 |
+
"zenodo": "https://zenodo.org/records/10069289/files/AeroPath.zip?download=1"
|
57 |
+
}
|
58 |
+
|
59 |
+
|
60 |
+
# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
|
61 |
+
class AeroPath(datasets.GeneratorBasedBuilder):
|
62 |
+
"""An airway segmentation benchmark dataset with challenging pathology."""
|
63 |
+
|
64 |
+
VERSION = datasets.Version("1.0.0")
|
65 |
+
|
66 |
+
# This is an example of a dataset with multiple configurations.
|
67 |
+
# If you don't want/need to define several sub-sets in your dataset,
|
68 |
+
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
|
69 |
+
|
70 |
+
# If you need to make complex sub-parts in the datasets with configurable options
|
71 |
+
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
|
72 |
+
# BUILDER_CONFIG_CLASS = MyBuilderConfig
|
73 |
+
|
74 |
+
# You will be able to load one or the other configurations in the following list with
|
75 |
+
# data = datasets.load_dataset('my_dataset', 'first_domain')
|
76 |
+
# data = datasets.load_dataset('my_dataset', 'second_domain')
|
77 |
+
BUILDER_CONFIGS = [
|
78 |
+
#datasets.BuilderConfig(name="first_domain", version=VERSION, description="This part of my dataset covers a first domain"),
|
79 |
+
#datasets.BuilderConfig(name="second_domain", version=VERSION, description="This part of my dataset covers a second domain"),
|
80 |
+
datasets.BuilderConfig(name="zenodo", version=VERSION, description="This includes all 27 CTs stored as a single zip on Zenodo"),
|
81 |
+
]
|
82 |
+
|
83 |
+
DEFAULT_CONFIG_NAME = "zenodo" # It's not mandatory to have a default configuration. Just use one if it make sense.
|
84 |
+
|
85 |
+
def _info(self):
|
86 |
+
# TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
|
87 |
+
if self.config.name == "zenodo": # This is the name of the configuration selected in BUILDER_CONFIGS above
|
88 |
+
features = datasets.Features(
|
89 |
+
{
|
90 |
+
"sentence": datasets.Value("string"),
|
91 |
+
"option1": datasets.Value("string"),
|
92 |
+
"answer": datasets.Value("string")
|
93 |
+
# These are the features of your dataset like images, labels ...
|
94 |
+
}
|
95 |
+
)
|
96 |
+
else:
|
97 |
+
raise ValueError("Only 'Zenodo' is supported.")# This is an example to show how to have different features for "first_domain" and "second_domain"
|
98 |
+
|
99 |
+
return datasets.DatasetInfo(
|
100 |
+
# This is the description that will appear on the datasets page.
|
101 |
+
description=_DESCRIPTION,
|
102 |
+
# This defines the different columns of the dataset and their types
|
103 |
+
features=features, # Here we define them above because they are different between the two configurations
|
104 |
+
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
|
105 |
+
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
|
106 |
+
# supervised_keys=("sentence", "label"),
|
107 |
+
# Homepage of the dataset for documentation
|
108 |
+
homepage=_HOMEPAGE,
|
109 |
+
# License for the dataset if available
|
110 |
+
license=_LICENSE,
|
111 |
+
# Citation for the dataset
|
112 |
+
citation=_CITATION,
|
113 |
+
)
|
114 |
+
|
115 |
+
def _split_generators(self, dl_manager):
|
116 |
+
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
117 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
118 |
+
|
119 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
120 |
+
# 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.
|
121 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
122 |
+
urls = _URLS[self.config.name]
|
123 |
+
data_dir = dl_manager.download_and_extract(urls)
|
124 |
+
return [
|
125 |
+
datasets.SplitGenerator(
|
126 |
+
name=datasets.Split.TRAIN,
|
127 |
+
# These kwargs will be passed to _generate_examples
|
128 |
+
gen_kwargs={
|
129 |
+
"filepath": os.path.join(data_dir, "train.jsonl"),
|
130 |
+
"split": "train",
|
131 |
+
},
|
132 |
+
),
|
133 |
+
datasets.SplitGenerator(
|
134 |
+
name=datasets.Split.VALIDATION,
|
135 |
+
# These kwargs will be passed to _generate_examples
|
136 |
+
gen_kwargs={
|
137 |
+
"filepath": os.path.join(data_dir, "dev.jsonl"),
|
138 |
+
"split": "dev",
|
139 |
+
},
|
140 |
+
),
|
141 |
+
datasets.SplitGenerator(
|
142 |
+
name=datasets.Split.TEST,
|
143 |
+
# These kwargs will be passed to _generate_examples
|
144 |
+
gen_kwargs={
|
145 |
+
"filepath": os.path.join(data_dir, "test.jsonl"),
|
146 |
+
"split": "test"
|
147 |
+
},
|
148 |
+
),
|
149 |
+
]
|
150 |
+
|
151 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
152 |
+
def _generate_examples(self, filepath, split):
|
153 |
+
# TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
154 |
+
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
|
155 |
+
with open(filepath, encoding="utf-8") as f:
|
156 |
+
for key, row in enumerate(f):
|
157 |
+
data = json.loads(row)
|
158 |
+
if self.config.name == "first_domain":
|
159 |
+
# Yields examples as (key, example) tuples
|
160 |
+
yield key, {
|
161 |
+
"sentence": data["sentence"],
|
162 |
+
"option1": data["option1"],
|
163 |
+
"answer": "" if split == "test" else data["answer"],
|
164 |
+
}
|
165 |
+
else:
|
166 |
+
yield key, {
|
167 |
+
"sentence": data["sentence"],
|
168 |
+
"option2": data["option2"],
|
169 |
+
"second_domain_answer": "" if split == "test" else data["second_domain_answer"],
|
170 |
+
}
|