def run_deeid(): args = get_parser().parse_args() subjects_to_analyze = [] # special variable set in the container if os.getenv('IS_DOCKER'): exec_env = 'singularity' cgroup = Path('/proc/1/cgroup') if cgroup.exists() and 'docker' in cgroup.read_text(): exec_env = 'docker' else: exec_env = 'local' if args.brainextraction is None: raise Exception( "For post defacing quality it is required to run a form of brainextraction" "on the non-deindentified data. Thus please either indicate bet " "(--brainextration bet) or nobrainer (--brainextraction nobrainer)." ) if args.skip_bids_validation: print("Input data will not be checked for BIDS compliance.") else: print("Making sure the input data is BIDS compliant " "(warnings can be ignored in most cases).") validate_input_dir(exec_env, args.bids_dir, args.participant_label) layout = BIDSLayout(args.bids_dir) if args.analysis_level == "participant": if args.participant_label: subjects_to_analyze = args.participant_label else: print("No participant label indicated. Please do so.") else: subjects_to_analyze = layout.get(return_type='id', target='subject') list_part_prob = [] 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) sessions_to_analyze = layout.get(return_type='id', target='session') if not sessions_to_analyze: print('Processing data from one session.') else: print('Processing data from %s sessions:' % str(len(sessions_to_analyze))) print(sessions_to_analyze) list_check_meta = args.check_meta list_field_del = args.del_meta for subject_label in subjects_to_analyze: if not sessions_to_analyze: list_t1w = layout.get(subject=subject_label, extension='nii.gz', suffix='T1w', return_type='filename') else: list_t1w = layout.get(subject=subject_label, extension='nii.gz', suffix='T1w', return_type='filename', session=sessions_to_analyze) for T1_file in list_t1w: check_outpath(args.bids_dir, subject_label) if args.brainextraction == 'bet': if args.bet_frac is None: raise Exception( "If you want to use BET for pre-defacing brain extraction," "please provide a Frac value. For example: --bet_frac 0.5" ) else: run_brain_extraction_bet(T1_file, args.bet_frac[0], subject_label, args.bids_dir) elif args.brainextraction == 'nobrainer': run_brain_extraction_nb(T1_file, subject_label, args.bids_dir) check_meta_data(args.bids_dir, subject_label, list_check_meta) source_t1w = copy_no_deid(args.bids_dir, subject_label, T1_file) if args.del_meta: del_meta_data(args.bids_dir, subject_label, list_field_del) if args.deid == "pydeface": run_pydeface(source_t1w, T1_file) elif args.deid == "mri_deface": run_mri_deface(source_t1w, T1_file) elif args.deid == "quickshear": run_quickshear(source_t1w, T1_file) elif args.deid == "mridefacer": run_mridefacer(source_t1w, T1_file) elif args.deid == "deepdefacer": run_deepdefacer(source_t1w, subject_label, args.bids_dir) if args.deface_t2w: if not sessions_to_analyze: list_t2w = layout.get(subject=subject_label, extension='nii.gz', suffix='T2w', return_type='filename') else: list_t2w = layout.get(subject=subject_label, extension='nii.gz', suffix='T2w', return_type='filename', session=sessions_to_analyze) if list_t2w == []: raise Exception( "You indicated that a T2w image should be defaced as well." "However, no T2w image exists for subject %s." "Please check again." % subject_label) for T2_file in list_t2w: if args.brainextraction == 'bet': run_brain_extraction_bet(T2_file, args.bet_frac[0], subject_label, args.bids_dir) elif args.brainextraction == 'nobrainer': run_brain_extraction_nb(T2_file, subject_label, args.bids_dir) source_t2w = copy_no_deid(args.bids_dir, subject_label, T2_file) run_t2w_deface(source_t2w, T1_file, T2_file) rename_non_deid(args.bids_dir, subject_label) if not sessions_to_analyze and args.deface_t2w is False: create_graphics(args.bids_dir, subject_label, session=None, t2w=None) elif sessions_to_analyze and args.deface_t2w is False: for session in sessions_to_analyze: create_graphics(args.bids_dir, subject_label, session=session, t2w=None) elif not sessions_to_analyze and args.deface_t2w: create_graphics(args.bids_dir, subject_label, session=None, t2w=True) elif sessions_to_analyze and args.deface_t2w: for session in sessions_to_analyze: create_graphics(args.bids_dir, subject_label, session=session, t2w=True) if not sessions_to_analyze: clean_up_files(args.bids_dir, subject_label) else: for session in sessions_to_analyze: clean_up_files(args.bids_dir, subject_label, session=session)
def run_deeid(): args = get_parser().parse_args() subjects_to_analyze = [] # special variable set in the container if os.getenv('IS_DOCKER'): exec_env = 'singularity' cgroup = Path('/proc/1/cgroup') if cgroup.exists() and 'docker' in cgroup.read_text(): exec_env = 'docker' # print("Making sure the input data is BIDS compliant " # "(warnings can be ignored in most cases).") # validate_input_dir(exec_env, args.bids_dir, args.participant_label) if args.analysis_level == "participant": if args.participant_label: subjects_to_analyze = args.participant_label else: print("No participant label indicated. Please do so.") else: subject_dirs = glob(os.path.join(args.bids_dir, "sub-*")) subjects_to_analyze = [ subject_dir.split("-")[-1] for subject_dir in subject_dirs ] list_check_meta = args.check_meta list_field_del = args.del_meta modality = "T1w" if args.deface_t2w: modality = "T2w" for subject_label in subjects_to_analyze: for T1_file in glob(os.path.join(args.bids_dir, "sub-%s" % subject_label, "anat", "*_"+modality+".nii*")) + \ glob(os.path.join(args.bids_dir, "sub-%s" % subject_label, "ses-*", "anat", "*_"+modality+".nii*")): check_outpath(args.bids_dir, subject_label) if args.brainextraction == 'bet': if args.bet_frac is None: raise Exception( 'If you want to use BET for pre-defacing brain extraction, \ please provide a Frac value. For example: --bet_frac 0.5') else: run_brain_extraction_bet(T1_file, args.bet_frac[0], subject_label, args.bids_dir) elif args.brainextraction == 'nobrainer': run_brain_extraction_nb(T1_file, subject_label, args.bids_dir) if args.deid == "pydeface": if args.del_nodeface == "del": run_pydeface(T1_file, T1_file) check_meta_data(args.bids_dir, subject_label, list_check_meta) if args.del_meta: del_meta_data(args.bids_dir, subject_label, list_field_del) else: #copy_no_deid(subject_label, args.bids_dir, T1_file) T1_file_masked = T1_file[:-10] + "mod-" + modality + "_defacemask.nii.gz" run_pydeface(T1_file, T1_file_masked) check_meta_data(args.bids_dir, subject_label, list_check_meta) if args.del_meta: del_meta_data(args.bids_dir, subject_label, list_field_del) if args.deid == "mri_deface": if args.del_nodeface == "del": run_mri_deface(T1_file, T1_file) check_meta_data(args.bids_dir, subject_label, list_check_meta) if args.del_meta: del_meta_data(args.bids_dir, subject_label, list_field_del) else: # copy_no_deid(subject_label, args.bids_dir, T1_file) T1_file_masked = T1_file[:-10] + "mod-" + modality + "_defacemask.nii.gz" run_mri_deface(T1_file, T1_file_masked) check_meta_data(args.bids_dir, subject_label, list_check_meta) if args.del_meta: del_meta_data(args.bids_dir, subject_label, list_field_del) if args.deid == "quickshear": if args.del_nodeface == "del": run_quickshear(T1_file, T1_file) check_meta_data(args.bids_dir, subject_label, list_check_meta) if args.del_meta: del_meta_data(args.bids_dir, subject_label, list_field_del) else: #copy_no_deid(subject_label, args.bids_dir, T1_file) T1_file_masked = T1_file[:-10] + "mod-" + modality + "_defacemask.nii.gz" run_quickshear(T1_file, T1_file_masked) check_meta_data(args.bids_dir, subject_label, list_check_meta) if args.del_meta: del_meta_data(args.bids_dir, subject_label, list_field_del) if args.deid == "mridefacer": if args.del_nodeface == "del": run_mridefacer(T1_file, subject_label, args.bids_dir, True, modality) check_meta_data(args.bids_dir, subject_label, list_check_meta) if args.del_meta: del_meta_data(args.bids_dir, subject_label, list_field_del) else: #copy_no_deid(subject_label, args.bids_dir, T1_file) run_mridefacer(T1_file, subject_label, args.bids_dir, False, modality) check_meta_data(args.bids_dir, subject_label, list_check_meta) if args.del_meta: del_meta_data(args.bids_dir, subject_label, list_field_del) if args.deid == "deepdefacer": if args.del_nodeface == "del": run_deepdefacer(T1_file, subject_label, args.bids_dir, True, modality) check_meta_data(args.bids_dir, subject_label, list_check_meta) if args.del_meta: del_meta_data(args.bids_dir, subject_label, list_field_del) else: #copy_no_deid(subject_label, args.bids_dir, T1_file) run_deepdefacer(T1_file, subject_label, args.bids_dir, False, modality) check_meta_data(args.bids_dir, subject_label, list_check_meta) print(T1_file) if args.del_meta: del_meta_data(args.bids_dir, subject_label, list_field_del)