Skip to content

Instantly share code, notes, and snippets.

@margulies
Last active November 29, 2016 21:07
Show Gist options
  • Save margulies/1ea42384190ece5134b6be76b4dbcaed to your computer and use it in GitHub Desktop.
Save margulies/1ea42384190ece5134b6be76b4dbcaed to your computer and use it in GitHub Desktop.
import numpy as np
import nibabel as nib # git clone --branch enh/cifti2 https://github.com/satra/nibabel.git
from sklearn.metrics import pairwise_distances
# install mapalign: (comment if already installed)
! pip install git+https://github.com/satra/mapalign
from mapalign import embed
# Load data and Fisher's z-to-r transform
# load connectivity matrix as numpy array
data = #
# If input matrix is already r-to-z transfromed, include next line.
# If input matrix is r-values, comment out.
dcon = np.tanh(data)
# Get number of nodes
N = dcon.shape[0]
# Generate percentile thresholds for 90th percentile
perc = np.array([np.percentile(x, 90) for x in dcon])
# Threshold each row of the matrix by setting values below 90th percentile to 0
for i in range(dcon.shape[0]):
print "Row %d" % i
dcon[i, dcon[i,:] < perc[i]] = 0
# Check for minimum value
print "Minimum value is %f" % dcon.min()
# The negative values are very small, but we need to know how many nodes have negative values
# Count negative values per row
neg_values = np.array([sum(dcon[i,:] < 0) for i in range(N)])
print "Negative values occur in %d rows" % sum(neg_values > 0)
# Since there are only 23 vertices with total of 5000 very small negative values, we set these to zero
dcon[dcon < 0] = 0
# Now we are dealing with sparse vectors. Cosine similarity is used as affinity metric
aff = 1 - pairwise_distances(dcon, metric = 'cosine')
# Save affinity matrix (optional)
# uncomment to save:
# np.save('gradient_data/conn_matrices/cosine_affinity.npy', aff)
# Load affinitity matrix
# if saved, uncomment:
# aff = np.load('gradient_data/conn_matrices/cosine_affinity.npy')
emb, res = embed.compute_diffusion_map(aff, alpha = 0.5)
# Save results
np.save('mbedding_dense_emb.npy', emb)
np.save('embedding_dense_res.npy', res)
# variable 'emb' connectains the connectivity gradients in order of variance explained
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment