Skip to content

Instantly share code, notes, and snippets.

@satra
Created January 15, 2013 13:38
Show Gist options
  • Save satra/4538698 to your computer and use it in GitHub Desktop.
Save satra/4538698 to your computer and use it in GitHub Desktop.
Uses pysurfer to perform interactive display of correlation matrices stored in hdf or matlab or numpy arrays.
# -*- coding: utf-8 -*-
# <nbformat>3</nbformat>
# <codecell>
import argparse
import os
from surfer import Brain
import scipy.io as sio
import nibabel as nb
import numpy as np
from mayavi import mlab
from tables import openFile
overlay_added = False
brains = []
corrmats = []
combined = None
hemi = None
def do_overlay(idx):
global overlay_added
global brains
global corrmats
global combined
global hemi
if overlay_added:
for brain in brains:
brain.overlays['mean'].remove()
brain.foci['foci'].remove()
overlay_added = False
for i, brain in enumerate(brains):
mlab.figure(brain._f)
if combined:
totalverts = len(corrmats[i][0])
nverts = len(brain._geo.x)
if hemi == 'rh':
startidx = totalverts - nverts
rowidx = startidx + idx
val = corrmats[i][rowidx][startidx:]
else:
val = corrmats[i][idx][:nverts]
else:
val = corrmats[i][idx]
val[np.isnan(val)] = 0
brain.add_overlay(val, min=0.3, max=1.0, sign='abs', name='mean')
brain.add_foci(idx, coords_as_verts=True, name='foci', color=(.46,0.7,0.87))
overlay_added = True
def picker_callback(picker_object):
do_overlay(picker_object.point_id)
def display_matrices(filenames, target, hemi, surface):
print '\n'.join(filenames)
for name in filenames:
try:
corrmat = sio.loadmat(name)['corrmat']
except:
try:
corrmat = np.load(name)['corrmat']
except:
try:
corrmat = np.load(name)['arr_0']
except:
try:
corrmat = np.load(name)['avgcorr']
except:
h5file = openFile(name, 'r')
corrmat = h5file.root.corrmat
corrmats.append(corrmat)
for idx, name in enumerate(filenames):
path, name = os.path.split(name)
br = Brain(target, hemi, surface, title=name+'-%d' % idx)
brains.append(br)
for brain in brains[:-1]:
mlab.sync_camera(br._f, brain._f)
mlab.sync_camera(brain._f, br._f)
br._f.on_mouse_pick(picker_callback)
mlab.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="None")
parser.add_argument('-f','--files',
dest='files',
nargs="+",
required=True,
help='correlation matrix files'
)
parser.add_argument('--hemi',
dest = 'hemi',
required=True,
help='hemisphere')
parser.add_argument('-s','--surface',
dest = 'surface',
required=True,
help='Type of surface')
parser.add_argument('-t', '--target',
dest = 'target',
required=True,
help='Target fsaverage')
parser.add_argument('-c', '--combined',
dest = 'combined',
default = False,
action = 'store_true',
help='file contains combined surfaces')
args = parser.parse_args()
combined = args.combined
hemi = args.hemi
display_matrices(args.files, args.target, args.hemi, args.surface)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment