survivalanalysis_checkpointing / code /survival_dataset.py
unknownBrain's picture
Upload all remaining preprocessing, standalone, test and utility files
ee594f0 verified
Raw History Blame Contribute Delete
1.31 kB
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])