From 00696de60d84bd93a681db54ff851ad0a0c7a4ca Mon Sep 17 00:00:00 2001 From: jcohenadad Date: Thu, 21 Dec 2023 15:52:04 -0500 Subject: [PATCH 1/6] Added script to convert dataset to BIDS --- bids_conversion.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 bids_conversion.py diff --git a/bids_conversion.py b/bids_conversion.py new file mode 100644 index 0000000..37df00e --- /dev/null +++ b/bids_conversion.py @@ -0,0 +1,64 @@ +import os +import shutil + +path_in = '/Users/julien/code/rf-shimming-7t/RF_shimming_project_cleanupload/SubA' +path_out = '/Users/julien/Desktop/rf_shimming_spinalcord/sub-02' + +# Get subject name from path_out +subject = os.path.basename(path_out) + +# Convert MPRAGE data +# Get the absolute file name of the MPRAGE file that has the string "noRFshim" in it +mprage_file = [os.path.join(path_in, f) for f in os.listdir(path_in) if 'noRFshim' in f][0] +# Copy the file to the output directory with the correct file name: sub-01_acq-CP_T1w.nii.gz +shutil.copy2(mprage_file, os.path.join(path_out, f'{subject}_acq-CP_T1w.nii.gz')) + + + + + +def process_tfl_b1map(files): + sorted_files = sorted([f for f in files if 'tfl_b1map' in f]) + processed_files = [] + if len(sorted_files) >= 3: + # Append _part-mag before the file extension for the first file + name, ext = os.path.splitext(sorted_files[0]) + processed_files.append(name + '_part-mag' + ext) + # Include the third file as is + processed_files.append(sorted_files[2]) + return processed_files + +def process_gre2d(files): + sorted_files = sorted([f for f in files if 'gre2d' in f]) + return sorted_files[:-1] if len(sorted_files) > 1 else sorted_files + +def create_bids_structure(base_dir, bids_dir): + for root, dirs, files in os.walk(base_dir): + # Filter out system files like .DS_Store + files = [f for f in files if not f.startswith('.')] + + tfl_b1map_files = process_tfl_b1map(files) + gre2d_files = process_gre2d(files) + + processed_files = tfl_b1map_files + gre2d_files + [f for f in files if 'tfl_b1map' not in f and 'gre2d' not in f] + + for file in processed_files: + subject_id = '01' # Modify this as per your dataset + category = 'fmap' if 'b1map' in file else 'anat' + + new_file_name = f'sub-{subject_id}_{file}' + new_file_path = os.path.join(bids_dir, category, new_file_name) + + os.makedirs(os.path.dirname(new_file_path), exist_ok=True) + + original_file_path = os.path.join(root, file) + if os.path.exists(original_file_path): + shutil.copy2(original_file_path, new_file_path) + print(f'File {file} moved to {new_file_path}') + else: + print(f'Warning: File {original_file_path} not found. Skipping.') + +if __name__ == "__main__": + original_dir = '/Users/julien/code/rf-shimming-7t/RF_shimming_project_cleanupload/SubA' + bids_dir = '/Users/julien/Desktop/rf_shimming_spinalcord/sub-01bis' + create_bids_structure(original_dir, bids_dir) From 4995a59988648a0d13ef1f8bea0c0c63aefc475c Mon Sep 17 00:00:00 2001 From: jcohenadad Date: Thu, 21 Dec 2023 15:57:44 -0500 Subject: [PATCH 2/6] Working MPRAGE conversion --- bids_conversion.py | 66 +++++++++++----------------------------------- 1 file changed, 15 insertions(+), 51 deletions(-) diff --git a/bids_conversion.py b/bids_conversion.py index 37df00e..b487c9e 100644 --- a/bids_conversion.py +++ b/bids_conversion.py @@ -4,61 +4,25 @@ path_in = '/Users/julien/code/rf-shimming-7t/RF_shimming_project_cleanupload/SubA' path_out = '/Users/julien/Desktop/rf_shimming_spinalcord/sub-02' +# Create dictionary for shim type output +shimtype_dict = {'noRFshim': 'CP', 'CVred': 'CVred'} + # Get subject name from path_out subject = os.path.basename(path_out) -# Convert MPRAGE data -# Get the absolute file name of the MPRAGE file that has the string "noRFshim" in it -mprage_file = [os.path.join(path_in, f) for f in os.listdir(path_in) if 'noRFshim' in f][0] -# Copy the file to the output directory with the correct file name: sub-01_acq-CP_T1w.nii.gz -shutil.copy2(mprage_file, os.path.join(path_out, f'{subject}_acq-CP_T1w.nii.gz')) - - - - - -def process_tfl_b1map(files): - sorted_files = sorted([f for f in files if 'tfl_b1map' in f]) - processed_files = [] - if len(sorted_files) >= 3: - # Append _part-mag before the file extension for the first file - name, ext = os.path.splitext(sorted_files[0]) - processed_files.append(name + '_part-mag' + ext) - # Include the third file as is - processed_files.append(sorted_files[2]) - return processed_files +# Create output directory +os.makedirs(path_out, exist_ok=True) +os.makedirs(os.path.join(path_out, 'anat'), exist_ok=True) +os.makedirs(os.path.join(path_out, 'fmap'), exist_ok=True) -def process_gre2d(files): - sorted_files = sorted([f for f in files if 'gre2d' in f]) - return sorted_files[:-1] if len(sorted_files) > 1 else sorted_files - -def create_bids_structure(base_dir, bids_dir): - for root, dirs, files in os.walk(base_dir): - # Filter out system files like .DS_Store - files = [f for f in files if not f.startswith('.')] - - tfl_b1map_files = process_tfl_b1map(files) - gre2d_files = process_gre2d(files) - - processed_files = tfl_b1map_files + gre2d_files + [f for f in files if 'tfl_b1map' not in f and 'gre2d' not in f] - - for file in processed_files: - subject_id = '01' # Modify this as per your dataset - category = 'fmap' if 'b1map' in file else 'anat' - - new_file_name = f'sub-{subject_id}_{file}' - new_file_path = os.path.join(bids_dir, category, new_file_name) +# Convert MPRAGE data +# Get the absolute file name of the NIfTI and JSON files under the MPRAGE subfolder, that has the string "noRFshim" in it +for shimtype in ['noRFshim', 'CVred']: + for ext in ['nii.gz', 'json']: + mprage_file = [os.path.join(path_in, 'MPRAGE', f) for f in os.listdir(os.path.join(path_in, 'MPRAGE')) if shimtype in f and f.endswith(ext)][0] + shutil.copy2(mprage_file, os.path.join(path_out, f'anat/sub-{subject}_acq-{shimtype_dict[shimtype]}_T1w.{ext}')) - os.makedirs(os.path.dirname(new_file_path), exist_ok=True) +# Convert RF map data - original_file_path = os.path.join(root, file) - if os.path.exists(original_file_path): - shutil.copy2(original_file_path, new_file_path) - print(f'File {file} moved to {new_file_path}') - else: - print(f'Warning: File {original_file_path} not found. Skipping.') -if __name__ == "__main__": - original_dir = '/Users/julien/code/rf-shimming-7t/RF_shimming_project_cleanupload/SubA' - bids_dir = '/Users/julien/Desktop/rf_shimming_spinalcord/sub-01bis' - create_bids_structure(original_dir, bids_dir) + \ No newline at end of file From 270f3072c6ffc52223c0d7a89225abcef8ccb2a5 Mon Sep 17 00:00:00 2001 From: jcohenadad Date: Thu, 21 Dec 2023 16:12:59 -0500 Subject: [PATCH 3/6] Working GRE conversion --- bids_conversion.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/bids_conversion.py b/bids_conversion.py index b487c9e..abcd5f6 100644 --- a/bids_conversion.py +++ b/bids_conversion.py @@ -5,7 +5,9 @@ path_out = '/Users/julien/Desktop/rf_shimming_spinalcord/sub-02' # Create dictionary for shim type output -shimtype_dict = {'noRFshim': 'CP', 'CVred': 'CVred'} +shimtype_dict = {'noRFshim': 'CP', + 'Noshim': 'CP', + 'CVred': 'CVred'} # Get subject name from path_out subject = os.path.basename(path_out) @@ -19,10 +21,15 @@ # Get the absolute file name of the NIfTI and JSON files under the MPRAGE subfolder, that has the string "noRFshim" in it for shimtype in ['noRFshim', 'CVred']: for ext in ['nii.gz', 'json']: - mprage_file = [os.path.join(path_in, 'MPRAGE', f) for f in os.listdir(os.path.join(path_in, 'MPRAGE')) if shimtype in f and f.endswith(ext)][0] - shutil.copy2(mprage_file, os.path.join(path_out, f'anat/sub-{subject}_acq-{shimtype_dict[shimtype]}_T1w.{ext}')) + file = [os.path.join(path_in, 'MPRAGE', f) for f in os.listdir(os.path.join(path_in, 'MPRAGE')) if shimtype in f and f.endswith(ext)][0] + shutil.copy2(file, os.path.join(path_out, f'anat/sub-{subject}_acq-{shimtype_dict[shimtype]}_T1w.{ext}')) -# Convert RF map data +# Convert GRE data +# Get the absolute file name of the NIfTI and JSON files under each subfolder of the GRE_B1 subfolder that has the string corresponding to the name of the subfolder in it, and that has the string "gre2d" in it. Select the first pair in the list. +for shimtype in ['CVred', 'Noshim']: + for ext in ['nii.gz', 'json']: + file = [os.path.join(path_in, 'GRE_B1', shimtype, f) for f in os.listdir(os.path.join(path_in, 'GRE_B1', shimtype)) if 'gre2d' in f and f.endswith(ext)][0] + shutil.copy2(file, os.path.join(path_out, f'fmap/sub-{subject}_acq-{shimtype_dict[shimtype]}_T2starw.{ext}')) \ No newline at end of file From 1e46f3d896d1eef6c80b0a81b783132905e4ba15 Mon Sep 17 00:00:00 2001 From: jcohenadad Date: Thu, 21 Dec 2023 16:14:52 -0500 Subject: [PATCH 4/6] Fixed location of GRE scans-- should go under anat --- bids_conversion.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bids_conversion.py b/bids_conversion.py index abcd5f6..97850e5 100644 --- a/bids_conversion.py +++ b/bids_conversion.py @@ -24,12 +24,12 @@ file = [os.path.join(path_in, 'MPRAGE', f) for f in os.listdir(os.path.join(path_in, 'MPRAGE')) if shimtype in f and f.endswith(ext)][0] shutil.copy2(file, os.path.join(path_out, f'anat/sub-{subject}_acq-{shimtype_dict[shimtype]}_T1w.{ext}')) -# Convert GRE data +# Convert files in GRE_B1 subfolder # Get the absolute file name of the NIfTI and JSON files under each subfolder of the GRE_B1 subfolder that has the string corresponding to the name of the subfolder in it, and that has the string "gre2d" in it. Select the first pair in the list. for shimtype in ['CVred', 'Noshim']: for ext in ['nii.gz', 'json']: + # Convert GRE data file = [os.path.join(path_in, 'GRE_B1', shimtype, f) for f in os.listdir(os.path.join(path_in, 'GRE_B1', shimtype)) if 'gre2d' in f and f.endswith(ext)][0] - shutil.copy2(file, os.path.join(path_out, f'fmap/sub-{subject}_acq-{shimtype_dict[shimtype]}_T2starw.{ext}')) + shutil.copy2(file, os.path.join(path_out, f'anat/sub-{subject}_acq-{shimtype_dict[shimtype]}_T2starw.{ext}')) + # Convert RF map data - - \ No newline at end of file From b27fcad10be885818f696ca581d71632cb8b1220 Mon Sep 17 00:00:00 2001 From: jcohenadad Date: Thu, 21 Dec 2023 16:25:37 -0500 Subject: [PATCH 5/6] Finalized code :) --- bids_conversion.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/bids_conversion.py b/bids_conversion.py index 97850e5..fa7a72e 100644 --- a/bids_conversion.py +++ b/bids_conversion.py @@ -7,7 +7,12 @@ # Create dictionary for shim type output shimtype_dict = {'noRFshim': 'CP', 'Noshim': 'CP', - 'CVred': 'CVred'} + 'CVred': 'CVred', + 'PatSpec': 'patient', + 'PhaseOnly': 'phase', + 'SAReff': 'SAReff', + 'Target': 'target', + 'VolSpec': 'volume'} # Get subject name from path_out subject = os.path.basename(path_out) @@ -25,11 +30,18 @@ shutil.copy2(file, os.path.join(path_out, f'anat/sub-{subject}_acq-{shimtype_dict[shimtype]}_T1w.{ext}')) # Convert files in GRE_B1 subfolder -# Get the absolute file name of the NIfTI and JSON files under each subfolder of the GRE_B1 subfolder that has the string corresponding to the name of the subfolder in it, and that has the string "gre2d" in it. Select the first pair in the list. -for shimtype in ['CVred', 'Noshim']: +# Get the absolute file name of the NIfTI and JSON files under each subfolder of the GRE_B1 subfolder that has the string corresponding to the name of the subfolder in it. +for shimtype in ['CVred', 'Noshim', 'PatSpec', 'PhaseOnly', 'SAReff', 'Target', 'VolSpec']: for ext in ['nii.gz', 'json']: # Convert GRE data + # Select the files with the string "gre2d" in it. Select the first pair in the list, which are not corrected for gradient distortion. file = [os.path.join(path_in, 'GRE_B1', shimtype, f) for f in os.listdir(os.path.join(path_in, 'GRE_B1', shimtype)) if 'gre2d' in f and f.endswith(ext)][0] shutil.copy2(file, os.path.join(path_out, f'anat/sub-{subject}_acq-{shimtype_dict[shimtype]}_T2starw.{ext}')) # Convert RF map data + # Select the files with the string "tfl_b1map" in it. Select the first pair in the list, which is not corrected for gradient distortion, and which corresponds to the magnitude image. Select also the 3rd pair, which corresponds to the RF map. + file = [os.path.join(path_in, 'GRE_B1', shimtype, f) for f in os.listdir(os.path.join(path_in, 'GRE_B1', shimtype)) if 'tfl_b1map' in f and f.endswith(ext)][0] + shutil.copy2(file, os.path.join(path_out, f'fmap/sub-{subject}_acq-{shimtype_dict[shimtype]}_part-magnitude_TB1map.{ext}')) + file = [os.path.join(path_in, 'GRE_B1', shimtype, f) for f in os.listdir(os.path.join(path_in, 'GRE_B1', shimtype)) if 'tfl_b1map' in f and f.endswith(ext)][2] + shutil.copy2(file, os.path.join(path_out, f'fmap/sub-{subject}_acq-{shimtype_dict[shimtype]}_TB1map.{ext}')) + From 26d395c68c6cca7070b0e4552ad14a9e3a9bb07a Mon Sep 17 00:00:00 2001 From: jcohenadad Date: Thu, 21 Dec 2023 16:48:23 -0500 Subject: [PATCH 6/6] Fixed additional 'sub-' field --- bids_conversion.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bids_conversion.py b/bids_conversion.py index fa7a72e..a42aab8 100644 --- a/bids_conversion.py +++ b/bids_conversion.py @@ -2,12 +2,12 @@ import shutil path_in = '/Users/julien/code/rf-shimming-7t/RF_shimming_project_cleanupload/SubA' -path_out = '/Users/julien/Desktop/rf_shimming_spinalcord/sub-02' +path_out = '/Users/julien/Desktop/rf_shimming_spinalcord_test/sub-01' # Create dictionary for shim type output shimtype_dict = {'noRFshim': 'CP', 'Noshim': 'CP', - 'CVred': 'CVred', + 'CVred': 'CoV', 'PatSpec': 'patient', 'PhaseOnly': 'phase', 'SAReff': 'SAReff', @@ -27,7 +27,7 @@ for shimtype in ['noRFshim', 'CVred']: for ext in ['nii.gz', 'json']: file = [os.path.join(path_in, 'MPRAGE', f) for f in os.listdir(os.path.join(path_in, 'MPRAGE')) if shimtype in f and f.endswith(ext)][0] - shutil.copy2(file, os.path.join(path_out, f'anat/sub-{subject}_acq-{shimtype_dict[shimtype]}_T1w.{ext}')) + shutil.copy2(file, os.path.join(path_out, f'anat/{subject}_acq-{shimtype_dict[shimtype]}_T1w.{ext}')) # Convert files in GRE_B1 subfolder # Get the absolute file name of the NIfTI and JSON files under each subfolder of the GRE_B1 subfolder that has the string corresponding to the name of the subfolder in it. @@ -36,12 +36,12 @@ # Convert GRE data # Select the files with the string "gre2d" in it. Select the first pair in the list, which are not corrected for gradient distortion. file = [os.path.join(path_in, 'GRE_B1', shimtype, f) for f in os.listdir(os.path.join(path_in, 'GRE_B1', shimtype)) if 'gre2d' in f and f.endswith(ext)][0] - shutil.copy2(file, os.path.join(path_out, f'anat/sub-{subject}_acq-{shimtype_dict[shimtype]}_T2starw.{ext}')) + shutil.copy2(file, os.path.join(path_out, f'anat/{subject}_acq-{shimtype_dict[shimtype]}_T2starw.{ext}')) # Convert RF map data # Select the files with the string "tfl_b1map" in it. Select the first pair in the list, which is not corrected for gradient distortion, and which corresponds to the magnitude image. Select also the 3rd pair, which corresponds to the RF map. file = [os.path.join(path_in, 'GRE_B1', shimtype, f) for f in os.listdir(os.path.join(path_in, 'GRE_B1', shimtype)) if 'tfl_b1map' in f and f.endswith(ext)][0] - shutil.copy2(file, os.path.join(path_out, f'fmap/sub-{subject}_acq-{shimtype_dict[shimtype]}_part-magnitude_TB1map.{ext}')) + shutil.copy2(file, os.path.join(path_out, f'fmap/{subject}_acq-{shimtype_dict[shimtype]}_part-magnitude_TB1map.{ext}')) file = [os.path.join(path_in, 'GRE_B1', shimtype, f) for f in os.listdir(os.path.join(path_in, 'GRE_B1', shimtype)) if 'tfl_b1map' in f and f.endswith(ext)][2] - shutil.copy2(file, os.path.join(path_out, f'fmap/sub-{subject}_acq-{shimtype_dict[shimtype]}_TB1map.{ext}')) + shutil.copy2(file, os.path.join(path_out, f'fmap/{subject}_acq-{shimtype_dict[shimtype]}_TB1map.{ext}'))