Skip to content

Instantly share code, notes, and snippets.

@mikbuch
Last active December 6, 2019 17:02
Show Gist options
  • Save mikbuch/326171704d9c18defe9ff0452ae77df1 to your computer and use it in GitHub Desktop.
Save mikbuch/326171704d9c18defe9ff0452ae77df1 to your computer and use it in GitHub Desktop.
Script to automatize mapping neuroimaging data from volumes (NIfTI) to brain surface (workbench).
import os
import re
import subprocess as sp
import argparse
'''
Script to automatize mapping neuroimaging data from volumes (NIfTI) to brain
surface (workbench). This script is dedicated for multiple files. The simplified
version of this script, for single NIfTI file is available here:
https://gist.github.com/mikbuch/fb28aef0f77c7c7aeeff92a61b109d16
This scirpt takes all *.nii.gz files in specified directory and maps results
from them to Workbench Connectome surfaces.
Current version of this script is available here:
https://gist.github.com/mikbuch/326171704d9c18defe9ff0452ae77df1
Example usage:
python workbench_volume_to_surface.py /absolute/path/to/data/directory
'''
parser = argparse.ArgumentParser(description='Map NIfTI to workbench')
parser.add_argument('base_dir', type=str, help='Specify input directory, \
subdirectories \
will also be scanned.')
# Parse arguments
args = parser.parse_args()
# Path to NIftTI images
base_dir = args.base_dir
# Surface file location
surf = '/Applications/workbench/HCP_S1200_GroupAvg_v1/' \
'S1200.%s.midthickness_MSMAll.32k_fs_LR.surf.gii'
for dirpath, dirnames, filenames in os.walk(base_dir):
for filename in [f for f in filenames if f.endswith('.nii.gz')]:
in_file = os.path.join(dirpath, filename)
out_file = re.sub('.nii.gz', '_hemi-%s_.func.gii', in_file)
# Both hemispheres.
for i in ('L', 'R'):
cmd = 'wb_command -volume-to-surface-mapping %s %s %s ' \
'-trilinear' % (in_file, surf % i, out_file % i)
print('\n\n' + cmd + '\n')
process = sp.Popen(cmd, stdout=sp.PIPE, shell=True)
output = process.communicate()[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment