From 788d3e04df76b502a1b8f0b47d694f2edc0ee1c0 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Wed, 12 Jun 2024 15:09:46 -0400 Subject: [PATCH 1/2] Return lists of defaced t1w and t2w and define it for t2w Otherwise if no t1w files found, the variable t1w would not get populated in the loop at all and cause the crash. But it also does not make much sense to return only the last hit in the loop. Checking the workflow, it seems outputs are not used at all anyways, so we are gonna try to return a list --- bidsonym/reports.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bidsonym/reports.py b/bidsonym/reports.py index 367a3c2..f4bd697 100644 --- a/bidsonym/reports.py +++ b/bidsonym/reports.py @@ -47,6 +47,7 @@ def plot_defaced(bids_dir, subject_label, session=None, t2w=None): plt.savefig(opj(bidsonym_path, t1w[t1w.rfind('/')+1:t1w.rfind('.nii')] + '_desc-brainmaskdeid.png')) + defaced_t2w = None if t2w is not None: if session is not None: defaced_t2w = layout.get(subject=subject_label, extension='nii.gz', suffix='T2w', @@ -67,7 +68,7 @@ def plot_defaced(bids_dir, subject_label, session=None, t2w=None): plt.savefig(opj(bids_dir, 'sourcedata/bidsonym/sub-%s' % subject_label, t2w[t2w.rfind('/')+1:t2w.rfind('.nii')] + '_desc-brainmaskdeid.png')) - return (t1w, t2w) + return (defaced_t1w, defaced_t2w) def gif_defaced(bids_dir, subject_label, session=None, t2w=None): From 5783af46bd7ce8a55c7716f12cc6a7e8166afc6a Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Wed, 12 Jun 2024 22:04:05 -0400 Subject: [PATCH 2/2] BF: more of defining defaults to not cause obnoxious crashes --- bidsonym/run_deeid.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/bidsonym/run_deeid.py b/bidsonym/run_deeid.py index bd22baf..8720a37 100644 --- a/bidsonym/run_deeid.py +++ b/bidsonym/run_deeid.py @@ -107,13 +107,15 @@ def run_deeid(): subjects_to_analyze = layout.get(return_type='id', target='subject') list_part_prob = [] + part = None + session = None + for part in subjects_to_analyze: if part not in layout.get_subjects(): list_part_prob.append(part) if len(list_part_prob) >= 1: raise Exception("The participant(s) you indicated are not present in the BIDS dataset, please check again." - "This refers to:") - print(list_part_prob) + f"This refers to: {list_part_prob}") if args.session: list_sessions = args.session @@ -133,6 +135,7 @@ def run_deeid(): print(sessions_to_analyze) list_ses_prob = [] + list_t1w = [] if args.session and args.session != ["all"]: @@ -148,8 +151,7 @@ def run_deeid(): if len(list_ses_prob) >= 1: raise Exception("The session(s) you indicated are not present in the BIDS dataset, please check again." - "This refers to:") - print(list_ses_prob) + f"This refers to: {list_ses_prob}") list_t1w = layout.get(subject=subject_label, extension='nii.gz', suffix='T1w', return_type='filename', @@ -240,6 +242,7 @@ def run_deeid(): if args.deface_t2w: + list_t2w = [] if args.session and args.session != ["all"]: for ses in args.session: @@ -249,8 +252,7 @@ def run_deeid(): if len(list_ses_prob) >= 1: raise Exception("The session(s) you indicated are not present in the BIDS dataset, please check again." - "This refers to:") - print(list_ses_prob) + f"This refers to: {list_ses_prob}") list_t2w = layout.get(subject=subject_label, extension='nii.gz', suffix='T2w', @@ -262,7 +264,7 @@ def run_deeid(): extension='nii.gz', suffix='T2w', return_type='filename') - if list_t2w == []: + if not list_t2w: raise Exception("You indicated that a T2w image should be defaced as well." "However, no T2w image exists for subject %s and indicated sessions." "Please check again." % subject_label) @@ -278,6 +280,7 @@ def run_deeid(): source_t2w = copy_no_deid(args.bids_dir, subject_label, T2_file) + T1_file = None if 'ses' in T2_file: session = T2_file[T2_file.rfind('ses')+4:].split("_")[0] @@ -324,12 +327,12 @@ def run_deeid(): t2w=None) clean_up_files(args.bids_dir, subject_label, session=None) - elif len(T1_file) == 1 and args.deface_t2w: + elif len(T1_files) == 1 and args.deface_t2w: create_graphics(args.bids_dir, subject_label, session=None, t2w=True) clean_up_files(args.bids_dir, subject_label, session=None) - elif len(T1_file) >= 1 and args.deface_t2w is False: + elif len(T1_files) >= 1 and args.deface_t2w is False: for session in sessions_to_analyze: create_graphics(args.bids_dir, subject_label, @@ -337,7 +340,7 @@ def run_deeid(): clean_up_files(args.bids_dir, subject_label, session=session) - elif len(T1_file) >= 1 and args.deface_t2w: + elif len(T1_files) >= 1 and args.deface_t2w: for session in sessions_to_analyze: create_graphics(args.bids_dir, subject_label,