This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
/
create_glaucoma_dataset_csv.py
54 lines (47 loc) · 2.29 KB
/
create_glaucoma_dataset_csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# ------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
# ------------------------------------------------------------------------------------------
"""
This script takes a folder with images and builds a dataset.csv with subject IDs, image file paths and labels
by parsing the image filenames. This script is meant to work with the glaucoma dataset in the
sample classification task. For details on creating classification datasets,
see https://github.com/microsoft/InnerEye-createdataset
"""
import sys
from pathlib import Path
from typing import Tuple
# each row in the dataset will contain 5 values: subject ID, channel, file path, label, and date
DatasetRow = Tuple[str, str, str, str, str]
def parse(file_name: Path) -> DatasetRow:
"""
Takes a filename and parses the name to find the subject ID, label and date. The filename has the form
<type>-<subjectID>-<date>-<eye>.npy.
- type is either Normal when no glaucoma is present, or POAG if glaucoma is present.
- subjectID is a 6 digit numeric ID that uniquely identifies each subject
- date, in the form yyyy-mm-dd
- eye is either OD or OS
:param file_name: File naem to be parsed.
:return: A tuple of 5 strings with the values of subject ID, channel, path, label and date for this file.
"""
parts = file_name.stem.split("-")
assert len(parts) == 6
if parts[0].startswith("POAG"):
label = "True"
elif parts[0].startswith("Normal"):
label = "False"
else:
raise ValueError(f"Invalid prefix: {parts[0]}")
eye = parts[5]
subject_id_and_eye = parts[1] + "-" + eye
date = "-".join(parts[2:5])
return subject_id_and_eye, "image", file_name.name, label, date
if __name__ == '__main__':
if not len(sys.argv) == 2:
raise ValueError(f"{sys.argv[0]} takes exactly one argument for dataset path")
dataset_dir = Path(sys.argv[1])
numpy_files = list(dataset_dir.glob("*.npy"))
header = ["subject,channel,filePath,label,date"]
lines = [",".join(parse(f)) for f in numpy_files]
out_file = dataset_dir / "dataset.csv"
out_file.write_text("\n".join(header + lines))