matlab import and visualization

238 views
Skip to first unread message

Li Zhu

unread,
Sep 27, 2014, 5:53:41 PM9/27/14
to cmtk-...@googlegroups.com
Hi experts,

I am a Matlab user and already have some results on the functional connectivity. Currently my results are given by two matrices. One is the a connectivity matrix which has the dimension of (channel #) * (channel #). The entries of this matrix represents the connection strength. The other matrix represents the position of the channels in Cartesian coordinates.

I am trying to import these two matrices into Connectome Viewer. I had read the on-line tutorial at http://www.cmtk.org/viewer/documentation/users/tutorials/tut_matlabnet.html
However, I found that the example Matlab data had been removed. Could anyone please let me know why it is deleted, and is there another way to generate a 3D connection mapping on a brain template based on the node-position and connection metric? 

Thank you very much for your help!

Best,
Li Zhu

Mina

unread,
Oct 15, 2014, 5:40:55 AM10/15/14
to cmtk-...@googlegroups.com
Hi, I have the same problem.
I installed connectome viewer to use it with matlab adjacency matrices but I can't find any working example to do it.
cfile.add_network_from_matrix_with_pos cfile doesn't exist anymore and Connectome File Format Library doesn't mention anything about using .mat files. 
Any help or suggestions?

Message has been deleted

Mina

unread,
Oct 23, 2014, 5:36:31 AM10/23/14
to cmtk-...@googlegroups.com

Hi again,
After all I used https://gist.github.com/unidesigner/1092080 script but changed it a little to upload matlab data like the sample data sets found here: https://sites.google.com/site/bctnet/datasets
I run the altered script with the first data set (Coactivation_matrix.mat; 638 nodes; Weighted; Undirected) which includes the adjacency matrix (Coactivation_matrix) and the coordinates matrix (Coord).
The script was executed successfully and the network 3D visualization was fine.


The script:

import numpy as np
import networkx as nx
import scipy.io

# Input: matlab .mat file that contains both the adjacency matrix and the coordinates
# Output: my_net.cff file and my_net.gpickle
matrix = 'Coactivation_matrix.mat'                         # the .mat file with the data set
adj = 'Coactivation_matrix'                                    # the adjacency matrix
coord = 'Coord'                                                    # the coordinates matrix
output_name = 'my_net'


# load matlab matrix
aa=scipy.io.loadmat(matrix)

# extract matrix
mat=aa[adj]

# convert numpy to networkx graph
net=nx.from_numpy_matrix(mat)

# load nodes coordinates
pos = aa[coord]

# index of last node
card = len(pos)-1

# update the networkx graph with the positions
for k in range(0, len(pos)):
    net.node[k]['position'] = str(tuple(pos[k]))

# relabel node ids by incrementing by one
# in order to start with one
net = nx.relabel_nodes(net, lambda x: x + 1)

# write graph out as graph pickle (binarized version
# of the networkx graph
nx.write_gpickle(net, output_name + '.gpickle')

# we need cfflib to create a connectome file
import cfflib

# create a new connectome object
newconn = cfflib.connectome()

# create connectome network object. add more parameters
# you can show the docstring in ipython with cfflib.CNetwork?
cnet = cfflib.CNetwork(output_name, src = output_name + '.gpickle', fileformat = "NXGPickle" )

# add to connectome file
newconn.add_connectome_
network(cnet)

# show the meta cml file
print newconn.to_xml()

# store out as cff, to load it in connectome viewer
cfflib.save_to_cff(newconn, output_name + '.cff')

# in the connectome viewer, you can create the visualization script with
# Code Oracle->Connectome->CNetwork->Visualization->3D Network
# You might have to update the scale factor of the Nodes to e.g. 0.05 by
# double-clicking Nodes the Mayavi Visualization Tre


Reply all
Reply to author
Forward
0 new messages