Skip to content

Instantly share code, notes, and snippets.

@margulies
Created November 29, 2016 11:13
Show Gist options
  • Save margulies/899fe9bee8fd20ce79f67ff5c69a5a80 to your computer and use it in GitHub Desktop.
Save margulies/899fe9bee8fd20ce79f67ff5c69a5a80 to your computer and use it in GitHub Desktop.
def write_cifti(data, fname, dtype = 'dscalar', labels = None, tpl = None):
import subprocess
import os
assert data.shape[0] == 91282, "You must have 91282 vertices on the first dimension (rows)"
if tpl is None:
tpl = '/Users/marcel/projects/HCP/Connectivity_coordinate_system/rsFC_eigenvectors.dscalar.nii'
print "Saving temporary file to disk..."
np.savetxt('temp_data.txt',data)
print "Generating the CIFTI..."
cmd = ['wb_command','-cifti-convert','-from-text','temp_data.txt', tpl, fname]
if dtype == 'scalar':
cmd.append('-reset-scalars')
subprocess.call(cmd)
if labels is not None and dtype is not 'dtseries':
assert data.shape[1] == len(labels), "The number of labels needs to be equal to the number of columns (%d)" % data.shape[1]
print "Setting the labels..."
np.savetxt('temp_labels.txt', labels)
subprocess.call(['wb_command','-set-map-names',fname,'-name-file', 'temp_labels.txt'])
os.remove('temp_labels.txt')
os.remove('temp_data.txt')
print "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment