NSCLC-Radiomics-NIFTI / convert.py
farrell236's picture
r2
7c85a48
import os
import pydicom
import pydicom_seg
import pandas as pd
import SimpleITK as sitk
from glob import glob
from tqdm import tqdm
data = pd.read_csv('metadata.csv') # This file should be created by NBIA Data Retriever
patient_ids = data['PatientID'].unique()
img_reader = sitk.ImageSeriesReader()
seg_reader = pydicom_seg.SegmentReader()
for pid in tqdm(patient_ids):
row = data[data['PatientID'] == pid]
out_fn = f'NSCLC-Radiomics-NIFTI/{pid}'
os.makedirs(out_fn)
dicom_names = img_reader.GetGDCMSeriesFileNames(row[row['Modality'] == 'CT'].SeriesInstanceUID.values[0])
img_reader.SetFileNames(dicom_names)
image = img_reader.Execute()
sitk.WriteImage(image, os.path.join(out_fn, f'image.nii.gz'), True)
if pid == 'LUNG1-128': continue # LUNG1-128 missing segmentation
dicom_names = img_reader.GetGDCMSeriesFileNames(row[row['Modality'] == 'SEG'].SeriesInstanceUID.values[0])
dcm = pydicom.dcmread(dicom_names[0])
result = seg_reader.read(dcm)
for segment_number in result.available_segments:
image = result.segment_image(segment_number) # lazy construction
sitk.WriteImage(image, os.path.join(out_fn, f'seg-{result.segment_infos[segment_number].SegmentDescription}.nii.gz'), True)