我正在尝试 graph-tool
tiago peixoto by tiago peixoto构建图形(指向或无向)来自带有块结构的给定加权邻接矩阵。到目前为止,未成功。我的问题部分与此线程因此,它仍然没有明确的解决方案。
假设我的功能生成了我的权重 J 的块矩阵,该函数的形式为:
每个块 j /strong>是一些随机的二进制矩阵,其条目是从给定分布中绘制的。标量 s 和 g 分别表示对角块内连接的权重(即 i = j ),并阻止对角线(即> i≠j )。
我在 graph_tool
中构建图形如下:
import graph_tool.all as gt
directed = False # True if we want the graph to be directed
J = generate_adj_bmatrix(...,s=0.1,g=0.01,directed=directed) # Some function to generate the weighted adjacency matrix (here the matrix will be symmetric since we want the graph undirected)
# Define graph
G = gt.Graph(directed=directed)
indexes = J.nonzero()
G.add_edge_list(np.transpose(indexes))
# Add weight information
G.ep['weight'] = G.new_ep("double", vals=J[indexes])
如果需要,我也可以添加一些 vertexproperty
到我的 g
gragr属于。但是,如何将这些信息包括在代码中,从而可以构建圆形图?代码读取(从):
state = gt.minimize_blockmodel_dl(G) # or should I consider instead state = gt.minimize_nested_blockmodel_dl(G)?
gt.draw_hierarchy(state)
t = gt.get_hierarchy_tree(state)[0]
tpos = pos = gt.radial_tree_layout(t, t.vertex(t.num_vertices() - 1), weighted=True)
cts = gt.get_hierarchy_control_points(G, t, tpos)
pos = G.own_property(tpos)
b = state.levels[0].b
shape = b.copy()
shape.a %= 14 # Have not yet figured out what I need it for
gt.graph_draw(G, pos=pos, vertex_fill_color=b, vertex_shape=shape,
edge_control_points=cts,edge_color=[0, 0, 0, 0.3], vertex_anchor=0)
值得注意的是,上述代码目前悬挂似乎太长了。 minimize_blockmodel_dl(g)
行似乎参与了无尽的循环。理想情况下,我不应该根据我对 j 的块结构的了解,因此可以将这些信息作为属性作为属性提供给我的图形。同时, minimize_blockmodel_dl(g)
似乎是为了访问边缘捆绑选项所必需的,不是吗?
I am trying graph-tool
by Tiago Peixoto to build a graph (either directed or undirected) from a given weighted adjacency matrix with a block structure. So far, unsuccessfully. My question partly overlaps with this thread on SO, which, however, remains without a clear solution.
Suppose I have a function that generates my block matrix of weights J, which is in the form:

Each block Jij is some random binary matrix with entries drawn from a given distribution. The scalars s and g respectively denote weights for connections within diagonal blocks (i.e. when i = j) and blocks off the diagonal (i.e. i ≠ j).
I build my graph in graph_tool
as follows:
import graph_tool.all as gt
directed = False # True if we want the graph to be directed
J = generate_adj_bmatrix(...,s=0.1,g=0.01,directed=directed) # Some function to generate the weighted adjacency matrix (here the matrix will be symmetric since we want the graph undirected)
# Define graph
G = gt.Graph(directed=directed)
indexes = J.nonzero()
G.add_edge_list(np.transpose(indexes))
# Add weight information
G.ep['weight'] = G.new_ep("double", vals=J[indexes])
I can also add, if I want, some VertexProperty
to my G
graph to whose block my nodes belong. But how do I include this information in the code whereby I can build the circular graph? The code reads (pasted here from graph-tool docs):
state = gt.minimize_blockmodel_dl(G) # or should I consider instead state = gt.minimize_nested_blockmodel_dl(G)?
gt.draw_hierarchy(state)
t = gt.get_hierarchy_tree(state)[0]
tpos = pos = gt.radial_tree_layout(t, t.vertex(t.num_vertices() - 1), weighted=True)
cts = gt.get_hierarchy_control_points(G, t, tpos)
pos = G.own_property(tpos)
b = state.levels[0].b
shape = b.copy()
shape.a %= 14 # Have not yet figured out what I need it for
gt.graph_draw(G, pos=pos, vertex_fill_color=b, vertex_shape=shape,
edge_control_points=cts,edge_color=[0, 0, 0, 0.3], vertex_anchor=0)
Noteworthy is that the above code currently hangs seemingly too long. The minimize_blockmodel_dl(G)
line appears to engage in an endless loop. Ideally, I should not sample my graph for clusters, since this information could already be provided as a property to the vertexes, based on my knowledge of the block structure of J. At the same time, minimize_blockmodel_dl(G)
seems necessary in order to access the edge bundling option, doesn't it?
发布评论
评论(1)
这是我想到的解决方案。
有关圆形布局和这种构建图的方式的其他文档可以在 this
graph-tool
doc page。Here is the solution I came up with.
Additional documentation on the circular layout and this way of building the graph can be found at this
graph-tool
doc page.