Am 20.03.20 um 11:22 schrieb Davide Cittaro:
Hello, I would like to test nSBM on bipartite graphs but before going on I need to be sure I'm able to build a bipartite graph in graph-tool starting from a matrix:
A_nodes = np.arange(data.shape[0]) #nodes for rows start from 0 B_nodes = np.arange(data.shape[1]) + data.shape[0] # nodes from columns start from the last A_node
g = gt.Graph(directed=True) # directed or not directed... maybe not important at all g.add_vertex(len(A_nodes) + len(B_nodes)) #add all needed nodes partition = g.new_vertex_property('bool') # create a property indicating the node type
for x in A_nodes: partition[g.vertex(x)] = 0 # set all A nodes to 0
for x in B_nodes: partition[g.vertex(x)] = 1 # set all B to 1
idx = np.nonzero(data) # take the edge values weights = adata.X[idx]
idx = (idx[0], idx[1] + len(A_nodes)) # node number of columns need to be augmented by the offset
g.add_edge_list(np.transpose(idx)) #add weights
ew = g.new_edge_property("double") ew.a = weights g.ep['weight'] = ew
Is there a more straightforward way to go?
You can add the weights together with the edges in Graph.add_edge_list() via the eprops parameter, but otherwise the above is fine. Note that if your objective is to do SBM inference, it's better to make the graph undirected. Best, Tiago -- Tiago de Paula Peixoto <tiago@skewed.de>