Skip to content

Commit bd7e0f4

Browse files
committed
update argparse with direct paths to the DICOM files
1 parent f9b5610 commit bd7e0f4

File tree

1 file changed

+87
-29
lines changed

1 file changed

+87
-29
lines changed

A_read_files_info.py

Lines changed: 87 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def create_paths(rootdir):
7575
return list_all_ct_series
7676

7777

78-
def preprocess_call_main_metrics(df_paths_mapping, plots_dir):
78+
def get_paths_from_metatags(df_paths_mapping):
7979
"""
8080
8181
:param df_paths_mapping:
@@ -111,31 +111,51 @@ def preprocess_call_main_metrics(df_paths_mapping, plots_dir):
111111
tumor_path, file = os.path.split(df_paths_mapping.iloc[idx_tumor_path].PathSeries)
112112
source_ct_ablation_path, file = os.path.split(df_paths_mapping.iloc[idx_source_ablation].PathSeries)
113113
source_ct_tumor_path, file = os.path.split(df_paths_mapping.iloc[idx_source_tumor].PathSeries)
114-
# READ THE SEGMENTATION MASKS AS SIMPLEITK OBJ
115-
tumor_segmentation_sitk, tumor_sitk_reader = Reader.read_dcm_series(tumor_path, True)
116-
ablation_segmentation_sitk, ablation_sitk_reader = Reader.read_dcm_series(ablation_path, True)
117-
# READ THE CT SOURCE IMAGES AS SIMPLEITK DICOM IMGS
118-
source_ct_tumor_sitk, reader = Reader.read_dcm_series(source_ct_tumor_path, True)
119-
source_ct_ablation_sitk, reader = Reader.read_dcm_series(source_ct_ablation_path, True)
120-
# GET THE GENERAL INFO FROM THE METATAGS
121114
lesion_number = df_paths_mapping.iloc[idx_tumor_path].LesionNumber
122115
ablation_date = df_paths_mapping.iloc[idx_tumor_path].AblationDate
123116
patient_id = df_paths_mapping.iloc[idx_tumor_path].PatientID
124-
# %% RESAMPLE THE TUMOR MASK TO THE SIZE OF THE ABLATION MASK
125-
# so that we can extract metrics from the two combined
126-
resizer = ResizeSegmentation(ablation_segmentation_sitk,
127-
tumor_segmentation_sitk,
128-
source_ct_ablation_sitk)
129-
130-
tumor_segmentation_resampled, ablation_segmentation_resampled = resizer.resample_segmentation()
131-
# %% EXTRACT DISTANCE AND VOLUME METRICS
132-
main_distance_volume_metrics(patient_id,
133-
source_ct_ablation_sitk, source_ct_tumor_sitk,
134-
ablation_segmentation_resampled, tumor_segmentation_resampled,
135-
lesion_number,
136-
ablation_date,
137-
plots_dir)
138-
sys.stdout.flush()
117+
return tumor_path, source_ct_tumor_path, ablation_path, source_ct_ablation_path, \
118+
lesion_number, ablation_date, patient_id
119+
else:
120+
print("No metatags mapping found in ReferencedSequence for Tumor and Ablation!!!!")
121+
122+
123+
def read_dcm_imgs(tumor_path, source_ct_tumor_path, ablation_path, source_ct_ablation_path):
124+
"""
125+
reads DICOM images as SimpeleITK objects given their file paths as input
126+
:param tumor_path:
127+
:param source_ct_tumor_path:
128+
:param ablation_path:
129+
:param source_ct_ablation_path:
130+
:return: all DICOM files as SimpleITK objects
131+
"""
132+
133+
# READ THE SEGMENTATION MASKS AS SIMPLEITK OBJ
134+
tumor_segmentation_sitk, tumor_sitk_reader = Reader.read_dcm_series(tumor_path, True)
135+
ablation_segmentation_sitk, ablation_sitk_reader = Reader.read_dcm_series(ablation_path, True)
136+
# READ THE CT SOURCE IMAGES AS SIMPLEITK DICOM IMGS
137+
source_ct_tumor_sitk, reader = Reader.read_dcm_series(source_ct_tumor_path, True)
138+
source_ct_ablation_sitk, reader = Reader.read_dcm_series(source_ct_ablation_path, True)
139+
return tumor_segmentation_sitk, source_ct_tumor_sitk, ablation_segmentation_sitk, source_ct_ablation_sitk
140+
141+
142+
def resample_tumor_ablation(tumor_segmentation_sitk, ablation_segmentation_sitk, source_ct_ablation_sitk):
143+
"""
144+
145+
:param tumor_segmentation_sitk:
146+
:param ablation_segmentation_sitk:
147+
:param source_ct_ablation_sitk:
148+
:return: resampled tumor and ablation segmentations to the same dimensions
149+
"""
150+
151+
# RESAMPLE THE TUMOR MASK AND ABLATION MASK TO MATCH THE SOURCE CT ABLATION VALIDATION
152+
# so that we can extract metrics from the two combined
153+
resizer = ResizeSegmentation(ablation_segmentation_sitk,
154+
tumor_segmentation_sitk,
155+
source_ct_ablation_sitk)
156+
157+
tumor_segmentation_resampled, ablation_segmentation_resampled = resizer.resample_segmentation()
158+
return tumor_segmentation_resampled, ablation_segmentation_resampled
139159

140160

141161
# %%
@@ -145,25 +165,63 @@ def preprocess_call_main_metrics(df_paths_mapping, plots_dir):
145165
ap.add_argument("-i", "--rootdir", required=False, help="path to the patient folder to be processed")
146166
ap.add_argument("-o", "--plots_dir", required=True, help="path to the output images")
147167
ap.add_argument("-b", "--input_batch_proc", required=False, help="input csv file for batch processing")
168+
ap.add_argument("-m", "--metatags_mapping", required=False,
169+
help="Flag to specify whether the ablation tumor mapping was previously embedded in metatags")
170+
ap.add_argument("-t", "--tumor_path", required=False, help="path to tumor image files")
171+
ap.add_argument("-a", "--ablation_path", required=False, help="path to ablation image files")
172+
ap.add_argument("-c", "--source_ct_tumor", required=False, help="path to tumor source CT")
173+
ap.add_argument("-d", "--source_ct_ablation", required=False, help="path to ablation source CT")
174+
ap.add_argument("-p", "--patient_id", required=False, help="subject id")
175+
ap.add_argument("-l", "--lesion_id", required=False, help="lesion id")
148176

149177
args = vars(ap.parse_args())
150178

151179
if args["rootdir"] is not None:
152180
print("Single patient folder processing, path to folder: ", args["rootdir"])
153181
print(args["plots_dir"])
154182
elif (args["input_batch_proc"]) is not None and (args["rootdir"] is None):
155-
print("Batch Processing Enabled, path to csv: ", args["input_batch_proc"])
183+
print("Batch Processing Enabled, path to Excel: ", args["input_batch_proc"])
156184
else:
157185
print("no input values provided either for single patient processing or batch processing. System Exiting")
158186
sys.exit()
159187

188+
# one patient one lesion folder
160189
if args["input_batch_proc"] is None and args["rootdir"] is not None:
161-
# single patient folder
162-
list_all_ct_series = create_paths(args["rootdir"])
163-
df_paths_mapping = pd.DataFrame(list_all_ct_series)
164-
preprocess_call_main_metrics(df_paths_mapping, args["plots_dir"])
190+
if args["metatags_mapping"] is True:
191+
list_all_ct_series = create_paths(args["rootdir"])
192+
df_paths_mapping = pd.DataFrame(list_all_ct_series)
193+
tumor_path, source_ct_tumor_path, ablation_path, source_ct_ablation_path, \
194+
lesion_number, ablation_date, patient_id = get_paths_from_metatags(df_paths_mapping)
195+
else:
196+
tumor_path = args["tumor_path"]
197+
ablation_path = args["ablation_path"]
198+
source_ct_tumor_path = args["source_ct_tumor"]
199+
source_ct_ablation_path = args["source_ct_ablation"]
200+
patient_id = args["patient_id"]
201+
lesion_id = args["lesion_id"]
202+
ablation_date = None
203+
204+
tumor_segmentation_sitk, source_ct_tumor_sitk, ablation_segmentation_sitk, source_ct_ablation_sitk \
205+
= read_dcm_imgs(tumor_path, source_ct_tumor_path, ablation_path, source_ct_ablation_path)
206+
tumor_segmentation_resampled, ablation_segmentation_resampled = \
207+
resample_tumor_ablation(tumor_segmentation_sitk, ablation_segmentation_sitk, source_ct_ablation_sitk)
208+
# plot the histogram of distances and extract radiomics
209+
main_distance_volume_metrics(patient_id,
210+
source_ct_ablation_sitk, source_ct_tumor_sitk,
211+
ablation_segmentation_resampled, tumor_segmentation_resampled,
212+
lesion_number,
213+
ablation_date,
214+
args["plots_dir"],
215+
FLAG_SAVE_TO_EXCEL=True, title='Ablation to Tumor Euclidean Distances',
216+
calculate_volume_metrics=True, calculate_radiomics=True
217+
)
218+
165219
print('Extracted metrics from the patient dir: ', args["rootdir"])
220+
221+
# UBELIX
222+
sys.stdout.flush()
166223
sys.exit()
224+
167225
else:
168226
# batch processing option
169227
df = pd.read_excel(args["input_batch_proc"])
@@ -189,5 +247,5 @@ def preprocess_call_main_metrics(df_paths_mapping, plots_dir):
189247
continue
190248
else:
191249
# call function to resample images and output csv for main metrics.
192-
preprocess_call_main_metrics(df_paths_mapping, args["plots_dir"])
250+
get_paths_from_metatags(df_paths_mapping, args["plots_dir"])
193251
print('Extracted metrics from the patient dir: ', rootdir)

0 commit comments

Comments
 (0)