-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore_dataset.py
More file actions
152 lines (126 loc) · 4.92 KB
/
explore_dataset.py
File metadata and controls
152 lines (126 loc) · 4.92 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
Dataset Exploration Module for Medical Imaging Data
This module provides functions to explore, organize, and extract information from
a medical imaging dataset containing CT scans and segmentation data from different
institutions (MSKCC and TCGA).
The module includes functions for:
- Scanning the dataset directory structure
- Extracting patient IDs
- Generating summary reports of available data
Functions:
scan_dataset(): Scan the dataset directory structure and return a dictionary of files
get_patient_ids(): Extract unique patient IDs from the dataset
generate_dataset_report(): Generate a summary report of dataset contents
Usage:
import explore_dataset
# Get dictionary of patient IDs by institution
patient_ids = explore_dataset.get_patient_ids()
# Generate and print a dataset summary report
report = explore_dataset.generate_dataset_report()
"""
import os
import nibabel as nib
import pandas as pd
import numpy as np
from pathlib import Path
def scan_dataset(root_dir="DatasetChallenge"):
"""
Scan the dataset directory and organize file information.
This function traverses the dataset directory structure and collects
information about CT scans and segmentation files organized by institution.
Args:
root_dir (str): Path to the root directory of the dataset. Defaults to "DatasetChallenge".
Returns:
dict: A nested dictionary with the following structure:
{
"CT": {
"MSKCC": [list of files],
"TCGA": [list of files]
},
"Segmentation": {
"MSKCC": [list of files],
"TCGA": [list of files]
}
}
"""
dataset_info = {
"CT": {"MSKCC": [], "TCGA": []},
"Segmentation": {"MSKCC": [], "TCGA": []}
}
# Scan CT directory
ct_dir = os.path.join(root_dir, "CT")
for institution in ["MSKCC", "TCGA"]:
inst_dir = os.path.join(ct_dir, institution)
if os.path.exists(inst_dir):
dataset_info["CT"][institution] = sorted(os.listdir(inst_dir))
# Scan Segmentation directory
seg_dir = os.path.join(root_dir, "Segmentation")
for institution in ["MSKCC", "TCGA"]:
inst_dir = os.path.join(seg_dir, institution)
if os.path.exists(inst_dir):
dataset_info["Segmentation"][institution] = sorted(os.listdir(inst_dir))
return dataset_info
def get_patient_ids():
"""
Extract unique patient IDs from the dataset.
This function identifies all unique patient IDs by processing filenames
from CT scans in the dataset, organized by institution.
Returns:
dict: A dictionary containing patient IDs by institution:
{
"MSKCC": [list of patient IDs],
"TCGA": [list of patient IDs]
}
"""
dataset_info = scan_dataset()
patient_ids = {}
for institution in ["MSKCC", "TCGA"]:
ct_files = dataset_info["CT"][institution]
# Extract patient IDs by removing file extension
ids = sorted(list(set([f.split('.')[0] for f in ct_files])))
patient_ids[institution] = ids
return patient_ids
def generate_dataset_report():
"""
Generate a summary report of the dataset.
This function analyzes the dataset structure and produces a summary report
containing counts of CT images, segmentation files, and unique patient IDs
for each institution.
Returns:
dict: A dictionary containing the report data with the following structure:
{
"CT Images Count": {"MSKCC": count, "TCGA": count},
"Segmentation Files Count": {"MSKCC": count, "TCGA": count},
"Unique Patient IDs": {"MSKCC": count, "TCGA": count}
}
"""
dataset_info = scan_dataset()
patient_ids = get_patient_ids()
report = {
"CT Images Count": {
"MSKCC": len(dataset_info["CT"]["MSKCC"]),
"TCGA": len(dataset_info["CT"]["TCGA"])
},
"Segmentation Files Count": {
"MSKCC": len(dataset_info["Segmentation"]["MSKCC"]),
"TCGA": len(dataset_info["Segmentation"]["TCGA"])
},
"Unique Patient IDs": {
"MSKCC": len(patient_ids["MSKCC"]),
"TCGA": len(patient_ids["TCGA"])
}
}
return report
if __name__ == "__main__":
report = generate_dataset_report()
print("Dataset Report:")
print("===============")
for category, values in report.items():
print(f"\n{category}:")
for institution, count in values.items():
print(f" {institution}: {count}")
patient_ids = get_patient_ids()
for institution in ["MSKCC", "TCGA"]:
print(f"\nFirst 5 patient IDs from {institution}:")
for pid in patient_ids[institution][:5]:
print(f" {pid}")