-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert.py
More file actions
70 lines (53 loc) · 2.07 KB
/
convert.py
File metadata and controls
70 lines (53 loc) · 2.07 KB
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
# Adapted from: https://github.com/KCL-BMEIS/VS_Seg/blob/master/preprocessing/TCIA_data_convert_into_convenient_folder_structure.py
#!/usr/bin/env python
# coding: utf-8
import os
from glob import glob
from natsort import natsorted
import pydicom
import SimpleITK as sitk
import re
import argparse
from tqdm import tqdm
parser = argparse.ArgumentParser(description='Convert the T2 scans from the TCIA dataset into the Nifti format')
parser.add_argument('--input', type=str, help='(string) path to TCIA dataset, in "Descriptive Directory Name" format, for example /home/user/.../manifest-T2/Vestibular-Schwannoma-SEG')
parser.add_argument('--output', type=str, help='(string) path to output folder')
args = parser.parse_args()
input_path = args.input
output_path = args.output
if not os.path.isdir(output_path):
os.makedirs(output_path, exist_ok=True)
cases = natsorted(glob(os.path.join(input_path, '*')))
for case in tqdm(cases):
folders = glob(case+'/*/*')
MRs = []
MRs_paths = []
# Test that the DICOM is a MRI
for folder in folders:
first_file = glob(folder+"/*")[0]
dd = pydicom.read_file(first_file)
if dd['Modality'].value == 'MR':
MRs.append(dd)
MRs_paths.append(first_file)
else:
raise Exception
file_paths = None
# Test that the DICOM is a T2 scan
for MR, path in zip(MRs, MRs_paths):
if "t2_" in MR['SeriesDescription'].value:
MR_T2 = MR
file_paths = path
else:
raise Exception
# write files into new folder structure
p = re.compile(r'VS-SEG-(\d+)')
case_idx = int(p.findall(case)[0])
old_T2_folder = os.path.dirname(file_paths)
# Output path
new_T2_path = os.path.join(output_path, 'vs_gk_' + str(case_idx) +'_T2.nii.gz')
# Conversion DICOM to NIFTI using SITK
reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(old_T2_folder)
reader.SetFileNames(dicom_names)
image = reader.Execute()
sitk.WriteImage(image, new_T2_path)