`graph-tool`边缘捆绑圆形布局图形图

发布于 2025-01-28 05:34:51 字数 2401 浏览 5 评论 0 原文

我正在尝试 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:

enter image description here

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城旧梦 2025-02-04 05:34:51

这是我想到的解决方案。

def visualize_network(J,N_sizes):
"""
Visualize a network from weighted block adjacency matrix in a circular layout with FEB.

Input arguments:
-- J : Weighted adjacency matrix (in block-matrix form, but can be any, as far as it is square). 
-- N_sizes : {<block1_label>: size; <block2_label>: size,...} such that node indexes of block n follow immediately those of block n-1.  
"""

import numpy as np
import matplotlib.colors as mcolors
import graph_tool.all as gt

# Generate the graph
G = gt.Graph(directed=True) # In my case, network edges are oriented
eindexes = J.nonzero()
G.add_edge_list(np.transpose(eindexes))
# Add weight information
weight = G.new_ep("double", vals = J[eindexes])

# Assign color to each vertex based on the block it belongs to 
colors = {'B1' : 'k',
          'B2' : 'r',
          'B3' : 'g',
          'B4' : 'b'}
regs = np.asarray(list(N_sizes.keys()))
rindexes = np.cumsum(list(N_sizes.values()))
iidd = regs[np.searchsorted(rindexes,np.arange(np.shape(J)[0]))]
region_id = G.new_vp("string",vals=iidd)
vcolors = [colors[id] for id in iidd]
vertex_color = G.new_vp("string",vals=vcolors)

# Assigns edge colors by out-node.
eid = regs[np.searchsorted(rindexes,np.arange(np.shape(J)[0]))]
ecolors = [mcolors.to_hex(c) for c in regs[np.searchsorted(rindexes,eindexes[0]]] 
edge_color = G.new_ep("string",vals=ecolors)

# Construct a graph in a circular layout with FEB
G = gt.GraphView(G, vfilt=gt.label_largest_component(G))
state = gt.minimize_nested_blockmodel_dl(G)
t = gt.get_hierarchy_tree(state)[0]
tpos = gt.radial_tree_layout(t, t.vertex(t.num_vertices() - 1, use_index=False), weighted=True)
cts = gt.get_hierarchy_control_points(G, t, tpos)
pos = G.own_property(tpos)

gt.graph_draw(G,
              pos = pos,
              vertex_fill_color = vertex_color,
              edge_control_points = cts,
              edge_color = edge_color,
              vertex_anchor = 0)

有关圆形布局和这种构建图的方式的其他文档可以在 this graph-tool doc page。

Here is the solution I came up with.

def visualize_network(J,N_sizes):
"""
Visualize a network from weighted block adjacency matrix in a circular layout with FEB.

Input arguments:
-- J : Weighted adjacency matrix (in block-matrix form, but can be any, as far as it is square). 
-- N_sizes : {<block1_label>: size; <block2_label>: size,...} such that node indexes of block n follow immediately those of block n-1.  
"""

import numpy as np
import matplotlib.colors as mcolors
import graph_tool.all as gt

# Generate the graph
G = gt.Graph(directed=True) # In my case, network edges are oriented
eindexes = J.nonzero()
G.add_edge_list(np.transpose(eindexes))
# Add weight information
weight = G.new_ep("double", vals = J[eindexes])

# Assign color to each vertex based on the block it belongs to 
colors = {'B1' : 'k',
          'B2' : 'r',
          'B3' : 'g',
          'B4' : 'b'}
regs = np.asarray(list(N_sizes.keys()))
rindexes = np.cumsum(list(N_sizes.values()))
iidd = regs[np.searchsorted(rindexes,np.arange(np.shape(J)[0]))]
region_id = G.new_vp("string",vals=iidd)
vcolors = [colors[id] for id in iidd]
vertex_color = G.new_vp("string",vals=vcolors)

# Assigns edge colors by out-node.
eid = regs[np.searchsorted(rindexes,np.arange(np.shape(J)[0]))]
ecolors = [mcolors.to_hex(c) for c in regs[np.searchsorted(rindexes,eindexes[0]]] 
edge_color = G.new_ep("string",vals=ecolors)

# Construct a graph in a circular layout with FEB
G = gt.GraphView(G, vfilt=gt.label_largest_component(G))
state = gt.minimize_nested_blockmodel_dl(G)
t = gt.get_hierarchy_tree(state)[0]
tpos = gt.radial_tree_layout(t, t.vertex(t.num_vertices() - 1, use_index=False), weighted=True)
cts = gt.get_hierarchy_control_points(G, t, tpos)
pos = G.own_property(tpos)

gt.graph_draw(G,
              pos = pos,
              vertex_fill_color = vertex_color,
              edge_control_points = cts,
              edge_color = edge_color,
              vertex_anchor = 0)

Additional documentation on the circular layout and this way of building the graph can be found at this graph-tool doc page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文