In [1]:
from maybrain import utils
from maybrain import resources as rr
from maybrain import brain as mbt
import bct
a = mbt.Brain()
a.import_adj_file(rr.DUMMY_ADJ_FILE_500, nodes_to_exclude=[0,1,2,3,4,5])
a.import_spatial_info(rr.MNI_SPACE_COORDINATES_500)
a.apply_threshold()
bctpyMaybrain can be used with the pythonic version of the Brain Connectivity Toolbox which can be found here.
For this, the G networkx object needs to be saved in a specific format. We also indicate that when a connection is not present, it will be filled with the value zero:
In [2]:
bct_mat = utils.makebctmat(a, nonedge=0)
print("Shape of bct_mat: " + str(bct_mat.shape))
print("Number of nodes in a.G: " + str(a.G.number_of_nodes()))
print("Shape of the original a.adjMat: " + str(a.adjMat.shape))
print(bct_mat)
Functions can then be called on the resulting matrix:
In [3]:
bct_res = bct.modularity_louvain_und_sign(bct_mat) # calculate the Louvain modularity
print(bct_res[0])
print(bct_res[0].shape)
As you can see from the above output, the result is a simple list of numbers, one for each node of our originally a.G. We need to assign them back for each a.G node, which you can do by having a dictionary with that information (you can check from the output that our nodes start with the label 6 because when importing our adjacency matrix we didn't import the first 6 nodes):
In [4]:
dict_info = utils.assignbctresult(a, bct_res[0])
print(dict_info)