File size: 1,313 Bytes
ee594f0 | 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 | import pandas as pd
import os
import torch
from torch.utils.data import Dataset
class SurvivalDataset(Dataset):
def __init__(self, csv_file, root_dir):
# Using the correct filename you just provided
self.metadata = pd.read_csv(csv_file)
self.root_dir = root_dir
def __len__(self):
return len(self.metadata)
def __getitem__(self, idx):
# 1. Get the patient ID
patient_id = self.metadata.iloc[idx]['ID']
# 2. IMPORTANT: Check your folder structure on the D: drive.
# If your folders are named just "UCSF-PDGM-004" instead of "UCSF-PDGM-004_nifti",
# remove the "_nifti" from the line below.
img_folder = os.path.join(self.root_dir, patient_id)
# 3. Load your T1c path
t1c_path = os.path.join(img_folder, f"{patient_id}_T1c.nii.gz")
# 4. Get your Survival Targets
survival_time = self.metadata.iloc[idx]['OS']
event_status = self.metadata.iloc[idx]['1-dead 0-alive']
return {
't1c_path': t1c_path,
'time': torch.tensor(float(survival_time)),
'event': torch.tensor(float(event_status))
}
# Quick Test
dataset = SurvivalDataset("clinicaldata.csv", r"D:\jainam\UCSF-PDGM-v5")
print(dataset[0]) |