-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·173 lines (159 loc) · 9 KB
/
main.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import sys
from utils import get_list_of_folders, mkdir_when_not_existent, make_list_of_dirs_in_path, \
check_if_text_is_a_date_and_output_in_isofromat, get_path_of_a_file_in_a_folder
import os
def main():
"""
kuriert -> dicom(ids(ios_datum(DICOM))) , Niff(ids(iso_daum(Alle relvanten niffty)
Main function for the data curation skript.
At a spesifite path it creates tow sub folders Dicom and nif
in both folders it creates the same folder with names from an origin folder
Then for every created folder it locks in the origin if ther a sub folders with dates as names
it this is the case it copies the sub folder to the new renamed with iso formateded date as file name
if ther is a dicom folder in the date folder from the orign folder it copies the dicom folder to the new folder
same proces is repeated fo the niff folder, but only select secenses get copied
:return:
"""
# get the path from the args
origin_path = sys.argv[1]
target_path = sys.argv[2]
if len(sys.argv) > 3:
if sys.argv[3] == "V" or sys.argv[3] == "v":
verbose = True
else:
verbose = False
if os.name == "nt":
if not "\\\\" in origin_path:
origin_path = origin_path.replace("\\","\\\\")
if not "\\\\" in target_path:
target_path = target_path.replace("\\","\\\\")
# create the target path if they do not exist
mkdir_when_not_existent(target_path)
# create the dicom and nifti folder in the target folder if they do not exist
mkdir_when_not_existent(target_path + r"\\dicom")
mkdir_when_not_existent(target_path + r"\\nifti")
patient_folders = get_list_of_folders(origin_path)
# create the same folders in the target folder if they do not exist
make_list_of_dirs_in_path(target_path + r"\\dicom\\", patient_folders)
make_list_of_dirs_in_path(target_path + r"\\nifti\\", patient_folders)
if verbose:
print(f"patient folders created {patient_folders}")
else:
# create the target path if they do not exist
mkdir_when_not_existent(target_path)
# create the dicom and nifti folder in the target folder if they do not exist
mkdir_when_not_existent(target_path + "/dicom")
mkdir_when_not_existent(target_path + "/nifti")
patient_folders = get_list_of_folders(origin_path)
# create the same folders in the target folder if they do not exist
make_list_of_dirs_in_path(target_path + "/dicom/", patient_folders)
make_list_of_dirs_in_path(target_path + "/nifti/", patient_folders)
if verbose:
print(f"patient folders created {patient_folders}")
number_dicoms = 0
# for every patient folder in the origin folder get the list of sub folder names, create a list of iso formatted dates
for patient_folder in patient_folders:
if os.name == "nt":
record_dates = get_list_of_folders(origin_path + r"\\" + patient_folder)
else:
record_dates = get_list_of_folders(origin_path + "/" + patient_folder)
for record_date in record_dates:
# check if the recorde date containes a dicom study
dicom_folder_name = ""
dicom_folder_name_options = ["dicom", "DICOM", "Dicom"]
if os.name == "nt":
for dicom_folder_name_option in dicom_folder_name_options:
if os.path.isdir(
origin_path + r"\\" + patient_folder + r"\\" + record_date + r"\\" + dicom_folder_name_option):
dicom_folder_name = dicom_folder_name_option
break
if dicom_folder_name == "":
continue
else:
for dicom_folder_name_option in dicom_folder_name_options:
if os.path.isdir(
origin_path + "/" + patient_folder + "/" + record_date + "/" + dicom_folder_name_option):
dicom_folder_name = dicom_folder_name_option
break
if dicom_folder_name == "":
continue
if os.name == "nt":
record_date = record_date.split(r"\\")[-1]
else:
record_date = record_date.split("/")[-1]
# check if text is a date and output in isofromat
record_date_iso = check_if_text_is_a_date_and_output_in_isofromat(record_date)
if record_date_iso == "":
continue
# create a new folder with the iso formatted date as name in the patient folder of the target folder
if os.name == "nt":
mkdir_when_not_existent(target_path + r"\\dicom\\" + patient_folder + r"\\" + record_date_iso)
else:
mkdir_when_not_existent(target_path + "/dicom/" + patient_folder + "/" + record_date_iso)
# check if the recorde date containes a dicom study folder either dicom or Dicom or DICOM, list folder name
# copy the dicom folder to the new folder
# check the os system and copy the dicom folder to the new folder
if os.name == "nt":
os.system(
r"xcopy " + origin_path + r"\\" + patient_folder + r"\\" + record_date + r"\\" + dicom_folder_name + " " + target_path + r"\\dicom\\" + patient_folder + r"\\" + record_date_iso + r"\\dicom\ /e /y")
if verbose:
print(
r"xcopy " + origin_path + r"\\" + patient_folder + r"\\" + record_date + r"\\" + dicom_folder_name + " " + target_path + r"\\dicom\\" + patient_folder + r"\\" + record_date_iso + r"\\dicom\ /e /y")
else:
os.system(
f"cp -r {origin_path}/{patient_folder}/{record_date}/{dicom_folder_name}/ {target_path}/dicom/{patient_folder}/{record_date_iso}/dicom")
if verbose:
print(
f"cp -r {origin_path}/{patient_folder}/{record_date}/{dicom_folder_name}/ {target_path}/dicom/{patient_folder}/{record_date_iso}/dicom")
number_dicoms += 1
list_niftis = ["T2ax.nii", "t1_3d.nii", "flair_ax.nii"]
niffis_count = {"T2ax.nii": 0, "t1_3d.nii": 0, "flair_ax.nii": 0}
for patient_folder in patient_folders:
if os.name == "nt":
record_dates = get_list_of_folders(origin_path + r"\\" + patient_folder)
else:
record_dates = get_list_of_folders(origin_path + "/" + patient_folder)
for record_date in record_dates:
for nifti in list_niftis:
# check if the recorde date contains a nifti
if os.name == "nt":
# check if nifti is in the folder or in a subfolder
nifti_path = get_path_of_a_file_in_a_folder(origin_path + r"\\" + patient_folder + r"\\" + record_date, nifti)
if nifti_path is None:
continue
else:
# check if nifti is in the folder or recursively in subfolders
nifti_path = get_path_of_a_file_in_a_folder(origin_path + "/" + patient_folder + "/" + record_date, nifti)
if nifti_path is None:
continue
if os.name == "nt":
record_date = record_date.split(r"\\")[-1]
else:
record_date = record_date.split("/")[-1]
record_date_iso = check_if_text_is_a_date_and_output_in_isofromat(record_date)
if record_date_iso == "":
continue
# create a new folder with the iso formatted date as name in the patient folder of the target folder
if os.name == "nt":
mkdir_when_not_existent(target_path + r"\nifti\\" + patient_folder + r"\\" + record_date_iso)
else:
mkdir_when_not_existent(target_path + "/nifti/" + patient_folder + "/" + record_date_iso)
# check the os system and copy the dicom folder to the new folder
if os.name == "nt":
os.system(
r"xcopy " + nifti_path + r" " + target_path + r"\\nifti\\" + patient_folder + r"\\" + record_date_iso + r"\\" + nifti + r"* /y")
if verbose:
print(
r"xcopy " + nifti_path + r" " + target_path + r"\\nifti\\" + patient_folder + r"\\" + record_date_iso + r"\\" + nifti + r"* /y")
else:
os.system(
f"cp {nifti_path} {target_path}/nifti/{patient_folder}/{record_date_iso}")
if verbose:
print(
f"cp {nifti_path} {target_path}/nifti/{patient_folder}/{record_date_iso}")
niffis_count[nifti] += 1
print(f"{number_dicoms} dicoms copied")
print(f"{niffis_count} nifti copied")
# for nifti and dicom remove the patient folders without dicom or niftis
if __name__ == '__main__':
main()