@@ -15,19 +15,11 @@ import tempfile
1515import shutil
1616import argparse
1717
18- # to filter the warning:
19- # WARNING:root:The given value for groups will be overwritten.
20- import logging
21- class Filter (logging .Filter ):
22- def filter (self , record ):
23- return 'The given value for groups will be overwritten.' not in record .getMessage ()
24-
25- logging .getLogger ().addFilter (Filter ())
26-
2718from LST_AI .strip import run_hdbet , apply_mask
2819from LST_AI .register import mni_registration , apply_warp , rigid_reg
2920from LST_AI .segment import unet_segmentation
3021from LST_AI .annotate import annotate_lesions
22+ from LST_AI .stats import compute_stats
3123from LST_AI .utils import download_data
3224
3325if __name__ == "__main__" :
@@ -135,10 +127,10 @@ if __name__ == "__main__":
135127 os .makedirs (work_dir )
136128
137129 # Define Image Paths (original space)
138- path_org_t1w = os .path .join (work_dir , 'sub-X_ses-Y_space-orig_T1w .nii.gz' )
139- path_org_flair = os .path .join (work_dir , 'sub-X_ses-Y_space-orig_FLAIR .nii.gz' )
140- path_org_stripped_t1w = os .path .join (work_dir , 'sub-X_ses-Y_space-orig_desc -stripped_T1w.nii.gz' )
141- path_org_stripped_flair = os .path .join (work_dir , 'sub-X_ses-Y_space-orig_desc -stripped_FLAIR.nii.gz' )
130+ path_org_t1w = os .path .join (work_dir , 'sub-X_ses-Y_space-t1w_T1w .nii.gz' )
131+ path_org_flair = os .path .join (work_dir , 'sub-X_ses-Y_space-flair_FLAIR .nii.gz' )
132+ path_org_stripped_t1w = os .path .join (work_dir , 'sub-X_ses-Y_space-t1w_desc -stripped_T1w.nii.gz' )
133+ path_org_stripped_flair = os .path .join (work_dir , 'sub-X_ses-Y_space-flair_desc -stripped_FLAIR.nii.gz' )
142134
143135 # Define Image Paths (MNI space)
144136 path_mni_t1w = os .path .join (work_dir , 'sub-X_ses-Y_space-mni_T1w.nii.gz' )
@@ -147,15 +139,23 @@ if __name__ == "__main__":
147139 path_mni_stripped_flair = os .path .join (work_dir , 'sub-X_ses-Y_space-mni_desc-stripped_FLAIR.nii.gz' )
148140
149141 # Masks
150- path_orig_brainmask_t1w = os .path .join (work_dir , 'sub-X_ses-Y_space-org_T1w_mask .nii.gz' )
151- path_orig_brainmask_flair = os .path .join (work_dir , 'sub-X_ses-Y_space-org_FLAIR_mask .nii.gz' )
142+ path_orig_brainmask_t1w = os .path .join (work_dir , 'sub-X_ses-Y_space-t1w_brainmask .nii.gz' )
143+ path_orig_brainmask_flair = os .path .join (work_dir , 'sub-X_ses-Y_space-flair_brainmask .nii.gz' )
152144 path_mni_brainmask = os .path .join (work_dir , 'sub-X_ses-Y_space-mni_brainmask.nii.gz' )
153145
154- # Segmentation results
155- path_orig_segmentation = os .path .join (work_dir , 'sub-X_ses-Y_space-orig_seg.nii.gz' )
156- path_mni_segmentation = os .path .join (work_dir , 'sub-X_ses-Y_space-mni_seg.nii.gz' )
157- path_orig_annotated_segmentation = os .path .join (work_dir , 'sub-X_ses-Y_space-orig_seg-annotated.nii.gz' )
158- path_mni_annotated_segmentation = os .path .join (work_dir , 'sub-X_ses-Y_space-mni_seg-annotated.nii.gz' )
146+ # Temp Segmentation results
147+ path_orig_segmentation = os .path .join (work_dir , 'sub-X_ses-Y_space-flair_seg-lst.nii.gz' )
148+ path_mni_segmentation = os .path .join (work_dir , 'sub-X_ses-Y_space-mni_seg-lst.nii.gz' )
149+ path_orig_annotated_segmentation = os .path .join (work_dir , 'sub-X_ses-Y_space-flair_desc-annotated_seg-lst.nii.gz' )
150+ path_mni_annotated_segmentation = os .path .join (work_dir , 'sub-X_ses-Y_space-mni_desc-annotated_seg-lst.nii.gz' )
151+
152+ # Output paths (in original space)
153+ filename_output_segmentation = "space-flair_seg-lst.nii.gz"
154+ filename_output_annotated_segmentation = "space-flair_desc-annotated_seg-lst.nii.gz"
155+
156+ # Stats
157+ filename_output_stats_segmentation = "lesion_stats.csv"
158+ filename_output_stats_annotated_segmentation = "annotated_lesion_stats.csv"
159159
160160 # affines
161161 path_affine_mni_t1w = os .path .join (work_dir , 'affine_t1w_to_mni.mat' )
@@ -187,6 +187,7 @@ if __name__ == "__main__":
187187
188188 # Annotation only
189189 if args .annotate_only :
190+ print ("LST-AI assumes existing segmentation to be in FLAIR space." )
190191 if os .path .isfile (args .existing_seg ):
191192 shutil .copy (args .existing_seg , path_orig_segmentation )
192193 else :
@@ -240,7 +241,7 @@ if __name__ == "__main__":
240241 out_annotated_native = path_orig_annotated_segmentation )
241242
242243 shutil .copy (path_orig_annotated_segmentation ,
243- os .path .join (args .output , "space-orig_desc-annotated_seg-lst.nii.gz" ))
244+ os .path .join (args .output , filename_output_annotated_segmentation ))
244245
245246
246247 # Segmentation only + (opt. Annotation)
@@ -283,8 +284,7 @@ if __name__ == "__main__":
283284
284285 # move processed mask to correct naming convention
285286 hdbet_mask = path_mni_stripped_t1w .replace (".nii.gz" , "_mask.nii.gz" )
286- print (hdbet_mask )
287- shutil .copy (hdbet_mask , path_mni_brainmask )
287+ shutil .move (hdbet_mask , path_mni_brainmask )
288288
289289 # then apply brain mask to FLAIR
290290 apply_mask (input_image = path_mni_flair ,
@@ -333,7 +333,7 @@ if __name__ == "__main__":
333333 n_threads = args .threads )
334334
335335 # store the segmentations
336- shutil .copy (path_orig_segmentation , os .path .join (args .output , "space-orig_seg-lst.nii.gz" ))
336+ shutil .copy (path_orig_segmentation , os .path .join (args .output , filename_output_segmentation ))
337337
338338 # Annotation
339339 if not args .segment_only :
@@ -354,8 +354,18 @@ if __name__ == "__main__":
354354 n_threads = args .threads )
355355
356356 # store the segmentations
357- shutil .copy (path_orig_annotated_segmentation , os .path .join (args .output , "space-orig_desc-annotated_seg-lst.nii.gz" ))
358-
357+ shutil .copy (path_orig_annotated_segmentation , os .path .join (args .output , filename_output_annotated_segmentation ))
358+
359+ # Compute Stats of (annotated) segmentation if they exist
360+ if os .path .exists (path_orig_segmentation ):
361+ compute_stats (mask_file = path_orig_segmentation ,
362+ output_file = os .path .join (args .output , filename_output_stats_segmentation ),
363+ multi_class = False )
364+
365+ if os .path .exists (path_orig_annotated_segmentation ):
366+ compute_stats (mask_file = path_orig_annotated_segmentation ,
367+ output_file = os .path .join (args .output , filename_output_stats_annotated_segmentation ),
368+ multi_class = True )
359369
360370 print (f"Results in { work_dir } " )
361371 if not args .temp :
0 commit comments