-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhecktor_dataset.py
156 lines (129 loc) · 5.71 KB
/
hecktor_dataset.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
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
153
154
155
import os
import torch
from torch.utils.data import Dataset
import pandas as pd
import numpy as np
class HecktorDataset(Dataset):
def __init__(
self,
clinical_data,
transform,
args,
):
self.clinical_data = clinical_data
self.transform = transform
self.data_path = args['data_path']
def __len__(self):
"""Return the length of the dataset."""
return len(self.clinical_data)
def get_targets(self):
return self.clinical_data['event'].values
def __getitem__(self, idx: int):
"""Get an input-target pair from the dataset.
The images are assumed to be preprocessed and cached.
Parameters
----------
idx : int
The index to retrieve (note: this is not the subject ID).
Returns
-------
tuple
((ctpt, x_ehr), y) where:
- ctpt is the preprocessed CT/PT image tensor
- x_ehr is the clinical data as a numpy array
- y is the target data as a numpy array
"""
row_data = self.clinical_data.iloc[idx]
patient_id = row_data['PatientID']
# Load preprocessed CT/PT image
ctpt = torch.load(os.path.join(self.data_path, 'processed', 'ctpt', f'{patient_id}_ctpt.pt'))
if self.transform:
# Apply transformation if specified
data_dict = {'ctpt': ctpt}
data_dict = self.transform(data_dict)
ctpt = data_dict['ctpt']
# Prepare target data
y = np.array([row_data['y_bin'], row_data['event'], row_data['duration']])
# Prepare clinical data
# Note: Center ID is excluded as we're doing leave-one-center-out validation
x_ehr = np.array([
row_data['Age'], row_data['Weight'],
row_data['Chemotherapy'], row_data['Gender_M'],
row_data['Performance_0.0'], row_data['Performance_1.0'], row_data['Performance_2.0'],
row_data['Performance_3.0'], row_data['Performance_4.0'],
row_data['HPV_0.0'], row_data['HPV_1.0'],
row_data['Surgery_0.0'], row_data['Surgery_1.0'],
row_data['Tobacco_0.0'], row_data['Tobacco_1.0'],
row_data['Alcohol_0.0'], row_data['Alcohol_1.0'],
]).astype(np.float32)
return (ctpt, x_ehr), y
class HecktorDataset2Images(HecktorDataset):
def __getitem__(self, idx: int):
"""
Get an input-target pair from the dataset with two augmented versions of the same image.
Parameters
----------
idx : int
The index to retrieve (note: this is not the subject ID).
Returns
-------
tuple
((ctpt1, ctpt2, x_ehr), y) where:
- ctpt1, ctpt2 are two augmented versions of the CT/PT image tensor
- x_ehr is the clinical data as a numpy array
- y is the target data as a numpy array
"""
row_data = self.clinical_data.iloc[idx]
patient_id = row_data['PatientID']
ctpt = torch.load(os.path.join(self.data_path, 'processed', 'ctpt', f'{patient_id}_ctpt.pt'))
if self.transform:
ctpt1 = self.transform({'ctpt': ctpt})['ctpt']
ctpt2 = self.transform({'ctpt': ctpt})['ctpt']
else:
raise AttributeError("self.transform must be defined for augmentation!")
# Prepare target and clinical data (same as in HecktorDataset)
y = np.array([row_data['y_bin'], row_data['event'], row_data['duration']])
x_ehr = np.array([
row_data['Age'], row_data['Weight'],
row_data['Chemotherapy'], row_data['Gender_M'],
row_data['Performance_0.0'], row_data['Performance_1.0'], row_data['Performance_2.0'],
row_data['Performance_3.0'], row_data['Performance_4.0'],
row_data['HPV_0.0'], row_data['HPV_1.0'],
row_data['Surgery_0.0'], row_data['Surgery_1.0'],
row_data['Tobacco_0.0'], row_data['Tobacco_1.0'],
row_data['Alcohol_0.0'], row_data['Alcohol_1.0'],
]).astype(np.float32)
return (ctpt1, ctpt2, x_ehr), y
class HecktorTestDataset(HecktorDataset):
def __getitem__(self, idx: int):
"""
Get an input pair and patient ID from the test dataset.
Parameters
----------
idx : int
The index to retrieve (note: this is not the subject ID).
Returns
-------
tuple
((ctpt, x_ehr), patient_id) where:
- ctpt is the preprocessed CT/PT image tensor
- x_ehr is the clinical data as a numpy array
- patient_id is the ID of the patient
"""
row_data = self.clinical_data.iloc[idx]
patient_id = row_data['PatientID']
ctpt = torch.load(os.path.join(self.data_path, 'ctpt', f'{patient_id}_ctpt.pt'))
if self.transform:
ctpt = self.transform(ctpt)
# Prepare clinical data (same as in HecktorDataset)
x_ehr = np.array([
row_data['Age'], row_data['Weight'],
row_data['Chemotherapy'], row_data['Gender_M'],
row_data['Performance_0.0'], row_data['Performance_1.0'], row_data['Performance_2.0'],
row_data['Performance_3.0'], row_data['Performance_4.0'],
row_data['HPV_0.0'], row_data['HPV_1.0'],
row_data['Surgery_0'], row_data['Surgery_1'],
row_data['Tobacco_0.0'], row_data['Tobacco_1.0'],
row_data['Alcohol_0.0'], row_data['Alcohol_1.0'],
]).astype(np.float32)
return (ctpt, x_ehr), patient_id