Update convert.py
Browse files- convert.py +15 -19
convert.py
CHANGED
|
@@ -3,12 +3,15 @@ import os
|
|
| 3 |
import pydicom
|
| 4 |
import pydicom_seg
|
| 5 |
|
|
|
|
|
|
|
| 6 |
import pandas as pd
|
| 7 |
import SimpleITK as sitk
|
| 8 |
|
| 9 |
from tqdm import tqdm
|
| 10 |
|
| 11 |
|
|
|
|
| 12 |
patient_ids = data['Subject ID'].unique()
|
| 13 |
|
| 14 |
for pid in tqdm(patient_ids):
|
|
@@ -17,23 +20,16 @@ for pid in tqdm(patient_ids):
|
|
| 17 |
out_fn = f'NSCLC-Radiomics-NIFTI/{pid}'
|
| 18 |
os.makedirs(out_fn)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
dcm = pydicom.dcmread(inp_fn_seg)
|
| 30 |
-
reader = pydicom_seg.SegmentReader()
|
| 31 |
-
result = reader.read(dcm)
|
| 32 |
-
|
| 33 |
-
for segment_number in result.available_segments:
|
| 34 |
-
image = result.segment_image(segment_number) # lazy construction
|
| 35 |
-
sitk.WriteImage(image, os.path.join(out_fn, f'seg-{result.segment_infos[segment_number].SegmentDescription}.nii.gz'), True)
|
| 36 |
-
except:
|
| 37 |
-
print(f'Segmentation {inp_fn_seg} failed to convert')
|
| 38 |
-
pass
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import pydicom
|
| 4 |
import pydicom_seg
|
| 5 |
|
| 6 |
+
import dicom2nifti
|
| 7 |
+
|
| 8 |
import pandas as pd
|
| 9 |
import SimpleITK as sitk
|
| 10 |
|
| 11 |
from tqdm import tqdm
|
| 12 |
|
| 13 |
|
| 14 |
+
data = pd.read_csv('NSCLC-Radiomics/metadata.csv')
|
| 15 |
patient_ids = data['Subject ID'].unique()
|
| 16 |
|
| 17 |
for pid in tqdm(patient_ids):
|
|
|
|
| 20 |
out_fn = f'NSCLC-Radiomics-NIFTI/{pid}'
|
| 21 |
os.makedirs(out_fn)
|
| 22 |
|
| 23 |
+
inp_fn_img = row[row['SOP Class Name'] == 'CT Image Storage']['File Location'].values[0]
|
| 24 |
+
dicom2nifti.convert_directory(inp_fn_img, out_fn)
|
| 25 |
+
|
| 26 |
+
if pid == 'LUNG1-128': continue # LUNG1-128 missing segmentation
|
| 27 |
+
|
| 28 |
+
inp_fn_seg = row[row['SOP Class Name'] == 'Segmentation Storage']['File Location'].values[0] + '/1-1.dcm'
|
| 29 |
+
dcm = pydicom.dcmread(inp_fn_seg)
|
| 30 |
+
reader = pydicom_seg.SegmentReader()
|
| 31 |
+
result = reader.read(dcm)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
for segment_number in result.available_segments:
|
| 34 |
+
image = result.segment_image(segment_number) # lazy construction
|
| 35 |
+
sitk.WriteImage(image, os.path.join(out_fn, f'seg-{result.segment_infos[segment_number].SegmentDescription}.nii.gz'), True)
|