Skip to content

Commit

Permalink
Working MPRAGE conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jcohenadad committed Dec 21, 2023
1 parent 00696de commit 4995a59
Showing 1 changed file with 15 additions and 51 deletions.
66 changes: 15 additions & 51 deletions bids_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 4995a59

Please sign in to comment.