From 2f06bf47061f4267b07fd613ceb192010ea650bf Mon Sep 17 00:00:00 2001 From: valosekj Date: Fri, 6 Jan 2023 12:55:44 -0500 Subject: [PATCH 1/2] Make get_subject and get_ses functions more robust using regular expressions --- preprocess/utils.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/preprocess/utils.py b/preprocess/utils.py index cb6a7e5..d9bda34 100644 --- a/preprocess/utils.py +++ b/preprocess/utils.py @@ -12,22 +12,30 @@ from pathlib import Path from enum import Enum + # BIDS utility tool def get_subject(file): """ Get subject from BIDS file name - :param file: - :return: subject + :param file: input nifti filename (e.g., sub-001_ses-01_T1w.nii.gz) or file path + (e.g., /home/user/MRI/bids/derivatives/labels/sub-001/ses-01/anat/sub-001_ses-01_T1w.nii.gz + :return: subject_id: subject ID (e.g., sub-001) """ - return file.split('_')[0] + subject = re.search('sub-(.*?)[_/]', file) # [_/] slash or underscore + subject_id = subject.group(0)[:-1] if subject else "" # [:-1] removes the last underscore or slash + return subject_id + def get_ses(file): """ Get session from BIDS file name - :param file: - :return: ses + :param file: input nifti filename (e.g., sub-001_ses-01_T1w.nii.gz) or file path + (e.g., /home/user/MRI/bids/derivatives/labels/sub-001/ses-01/anat/sub-001_ses-01_T1w.nii.gz + :return: sessionID: session ID (e.g., ses-01) """ - return file.split('_')[1] + session = re.findall(r'ses-..', file) # .. - any two characters (e.g., ses-01 or ses-M0) + sessionID = session[0] if session else "" # Return None if there is no session + return sessionID def get_contrast(file): From 5c5e2ac5b87feccde2732187ed492a2af71d2b18 Mon Sep 17 00:00:00 2001 From: valosekj Date: Fri, 6 Jan 2023 13:22:38 -0500 Subject: [PATCH 2/2] allow to specify FILES-SEG suffix (e.g., _seg or _label-SC_mask) --- preprocess/package_for_correction.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/preprocess/package_for_correction.py b/preprocess/package_for_correction.py index dd9e3da..043ad1e 100644 --- a/preprocess/package_for_correction.py +++ b/preprocess/package_for_correction.py @@ -61,6 +61,11 @@ def get_parser(): help="Zip file that contains the packaged data, without the extension. Default: data_to_correct", default='data_to_correct' ) + parser.add_argument( + '-suffix-files-seg', + help="FILES-SEG suffix. For example _seg or _label-SC_mask", + default='_seg' + ) parser.add_argument( '-v', '--verbose', help="Full verbose (for debugging)", @@ -116,7 +121,7 @@ def main(): for task, files in dict_yml.items(): for file in files: if task == 'FILES_SEG': - suffix_label = '_seg' + suffix_label = args.suffix_files_seg # e.g., _seg or _label-SC_mask elif task == 'FILES_LABEL': suffix_label = None elif task == 'FILES_PMJ':