Chris Oswald
commited on
Commit
•
5fa2de2
1
Parent(s):
f7a1cfb
generated examples
Browse files
SPIDER.py
CHANGED
@@ -14,17 +14,28 @@
|
|
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 |
-
from typing import Dict, List, Optional, Set
|
22 |
|
23 |
import numpy as np
|
24 |
|
25 |
import datasets
|
26 |
import SimpleITK as sitk
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
# TODO: Add BibTeX citation
|
29 |
# Find for instance the citation on arxiv or on the dataset repo/website
|
30 |
_CITATION = """\
|
@@ -156,11 +167,36 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
156 |
]
|
157 |
|
158 |
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
159 |
-
def _generate_examples(
|
160 |
-
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
|
163 |
-
|
|
|
|
|
|
|
164 |
#TODO: make hardcoded values dynamic
|
165 |
np.random.seed(9999)
|
166 |
N_PATIENTS = 257
|
@@ -188,6 +224,21 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
188 |
overview_data = import_csv_data(paths_dict['overview'])
|
189 |
grades_data = import_csv_data(paths_dict['gradings'])
|
190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
# Import image and mask data
|
192 |
image_files = [
|
193 |
file for file in os.listdir(os.path.join(paths_dict['images'], 'images'))
|
@@ -221,8 +272,10 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
221 |
subset_ids = test_ids
|
222 |
else:
|
223 |
subset_ids = None
|
224 |
-
raise ValueError(
|
225 |
-
|
|
|
|
|
226 |
|
227 |
image_files = [
|
228 |
file for file in image_files
|
@@ -258,40 +311,25 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
258 |
image_array = sitk.GetArrayFromImage(image)
|
259 |
|
260 |
# Extract overview data corresponding to image
|
261 |
-
|
262 |
-
# Extract patient radiological gradings corresponding to image
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
for line in reader:
|
277 |
-
results.append(line)
|
278 |
-
return results
|
279 |
-
|
280 |
-
|
281 |
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
"option2": data["option2"],
|
296 |
-
"second_domain_answer": "" if split == "test" else data["second_domain_answer"],
|
297 |
-
}
|
|
|
14 |
# TODO: Address all TODOs and remove all explanatory comments
|
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, Optional, Set, Tuple
|
22 |
|
23 |
import numpy as np
|
24 |
|
25 |
import datasets
|
26 |
import SimpleITK as sitk
|
27 |
|
28 |
+
# Define functions
|
29 |
+
def import_csv_data(filepath: str) -> List[Dict[str, str]]:
|
30 |
+
"""Import all rows of CSV file."""
|
31 |
+
results = []
|
32 |
+
with open(filepath, encoding='utf-8') as f:
|
33 |
+
reader = csv.DictReader(f)
|
34 |
+
for line in reader:
|
35 |
+
results.append(line)
|
36 |
+
return results
|
37 |
+
|
38 |
+
|
39 |
# TODO: Add BibTeX citation
|
40 |
# Find for instance the citation on arxiv or on the dataset repo/website
|
41 |
_CITATION = """\
|
|
|
167 |
]
|
168 |
|
169 |
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
170 |
+
def _generate_examples(
|
171 |
+
self,
|
172 |
+
paths_dict: Dict[str, str],
|
173 |
+
split: str = 'train', # ['train', 'validate', 'test']
|
174 |
+
validate_share: float = 0.3,
|
175 |
+
test_share: float = 0.2,
|
176 |
+
raw_image: bool = True,
|
177 |
+
numeric_array: bool = True,
|
178 |
+
metadata: bool = True,
|
179 |
+
rad_gradings: bool = True,
|
180 |
+
) -> Tuple[str, Dict]:
|
181 |
+
"""
|
182 |
+
This method handles input defined in _split_generators to yield
|
183 |
+
(key, example) tuples from the dataset. The `key` is for legacy reasons
|
184 |
+
(tfds) and is not important in itself, but must be unique for each example.
|
185 |
+
|
186 |
+
Args
|
187 |
+
paths_dict
|
188 |
+
split:
|
189 |
+
validate_share
|
190 |
+
test_share
|
191 |
+
raw_image
|
192 |
+
numeric_array
|
193 |
+
metadata
|
194 |
+
rad_gradings
|
195 |
|
196 |
+
Yields
|
197 |
+
|
198 |
+
"""
|
199 |
+
# Configure params
|
200 |
#TODO: make hardcoded values dynamic
|
201 |
np.random.seed(9999)
|
202 |
N_PATIENTS = 257
|
|
|
224 |
overview_data = import_csv_data(paths_dict['overview'])
|
225 |
grades_data = import_csv_data(paths_dict['gradings'])
|
226 |
|
227 |
+
# Convert overview data list of dicts to dict of dicts
|
228 |
+
overview_dict = {}
|
229 |
+
for item in overview_data:
|
230 |
+
key = item['new_file_name']
|
231 |
+
overview_dict[key] = item
|
232 |
+
|
233 |
+
# Merge patient records for radiological gradings data
|
234 |
+
grades_dict = {}
|
235 |
+
for patient_id in patient_ids:
|
236 |
+
patient_grades = [
|
237 |
+
x for x in grades_data if x['Patient'] == str(patient_id)
|
238 |
+
]
|
239 |
+
if patient_grades:
|
240 |
+
grades_dict[str(patient_id)] = patient_grades
|
241 |
+
|
242 |
# Import image and mask data
|
243 |
image_files = [
|
244 |
file for file in os.listdir(os.path.join(paths_dict['images'], 'images'))
|
|
|
272 |
subset_ids = test_ids
|
273 |
else:
|
274 |
subset_ids = None
|
275 |
+
raise ValueError( #TODO: move all parameter checking to beginning
|
276 |
+
f'Split argument "{split}" is not recognized. \
|
277 |
+
Please enter one of ["train", "validate", "test"]'
|
278 |
+
)
|
279 |
|
280 |
image_files = [
|
281 |
file for file in image_files
|
|
|
311 |
image_array = sitk.GetArrayFromImage(image)
|
312 |
|
313 |
# Extract overview data corresponding to image
|
314 |
+
image_overview = overview_dict[scan_id]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
315 |
|
316 |
+
# Extract patient radiological gradings corresponding to image
|
317 |
+
patient_grades_dict = {}
|
318 |
+
for item in grades_dict[patient_id]:
|
319 |
+
key = f'IVD{item["IVD label"]}'
|
320 |
+
value = {k:v for k,v in item.items() if k not in ['Patient', 'IVD label']}
|
321 |
+
patient_grades_dict[key] = value
|
|
|
|
|
|
|
|
|
|
|
322 |
|
323 |
+
# Prepare example return dict
|
324 |
+
return_dict = {'patient_id':patient_id, 'scan_type':scan_type}
|
325 |
+
if raw_image:
|
326 |
+
return_dict['raw_image'] = image
|
327 |
+
if numeric_array:
|
328 |
+
return_dict['numeric_array'] = image_array
|
329 |
+
if metadata:
|
330 |
+
return_dict['metadata'] = image_overview
|
331 |
+
if rad_gradings:
|
332 |
+
return_dict['rad_gradings'] = patient_grades_dict
|
333 |
+
|
334 |
+
# Yield example
|
335 |
+
yield (scan_id, return_dict)
|
|
|
|
|
|