如何使用任何工具可视化杂质pytorch几何图?

发布于 2025-02-13 01:36:10 字数 555 浏览 2 评论 0原文

您好,可视化pyg杂种对象的好方法是什么? (类似地定义: https://pytorch-geometric.readthedocs.io/en/latest/notes/notes/heterogeneous.html#creating-html#creating-heterogeneous-gnns

)图(可以转换它,但内容却少得多)。

g = torch_geometric.utils.to_networkx(data.to_homogeneous(), to_undirected=False )

有没有人尝试与其他python lib(matplotlib)或JS(sigma.js/d3.js)一起做?

您可以分享的任何文档链接吗?

Hello what is a good way to visualize a pyg HeteroData object ?
(defined similarly: https://pytorch-geometric.readthedocs.io/en/latest/notes/heterogeneous.html#creating-heterogeneous-gnns )

I tried with networkx but I think it is restricted to homogeneous graph ( it is possible to convert it but it is much less informative).

g = torch_geometric.utils.to_networkx(data.to_homogeneous(), to_undirected=False )

Did anyone try to do it with other python lib (matplotlib) or js (sigma.js/d3.js)?

Any docs link you can share?

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

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

发布评论

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

评论(2

对岸观火 2025-02-20 01:36:10

您可以使用NetworkX执行此操作,但是您需要进行一些编码来告诉它如何格式化节点和边缘。

# Simple example of network x rendering with colored nodes and edges
import matplotlib.pyplot as plt
import networkx as nx
from torch_geometric.utils import to_networkx

graph = to_networkx(data, to_undirected=False)

# Define colors for nodes and edges
node_type_colors = {
    "Station": "#4599C3",
    "Lot": "#ED8546",
}

node_colors = []
labels = {}
for node, attrs in graph.nodes(data=True):
    node_type = attrs["type"]
    color = node_type_colors[node_type]
    node_colors.append(color)
    if attrs["type"] == "Station":
        labels[node] = f"S{node}"
    elif attrs["type"] == "Lot":
        labels[node] = f"L{node}"

# Define colors for the edges
edge_type_colors = {
    ("Lot", "SameSetup", "Station"): "#8B4D9E",
    ("Station", "ShortSetup", "Lot"): "#DFB825",
    ("Lot", "SameEnergySetup", "Station"): "#70B349",
    ("Station", "ProcessNow", "Lot"): "#DB5C64",
}

edge_colors = []
for from_node, to_node, attrs in graph.edges(data=True):
    edge_type = attrs["type"]
    color = edge_type_colors[edge_type]

    graph.edges[from_node, to_node]["color"] = color
    edge_colors.append(color)


# Draw the graph
pos = nx.spring_layout(graph, k=2)
nx.draw_networkx(
    graph,
    pos=pos,
    labels=labels,
    with_labels=True,
    node_color=node_colors,
    edge_color=edge_colors,
    node_size=600,
)
plt.show()

You can do this with networkx, but you need to do some coding to tell it how to format the nodes and edges.

# Simple example of network x rendering with colored nodes and edges
import matplotlib.pyplot as plt
import networkx as nx
from torch_geometric.utils import to_networkx

graph = to_networkx(data, to_undirected=False)

# Define colors for nodes and edges
node_type_colors = {
    "Station": "#4599C3",
    "Lot": "#ED8546",
}

node_colors = []
labels = {}
for node, attrs in graph.nodes(data=True):
    node_type = attrs["type"]
    color = node_type_colors[node_type]
    node_colors.append(color)
    if attrs["type"] == "Station":
        labels[node] = f"S{node}"
    elif attrs["type"] == "Lot":
        labels[node] = f"L{node}"

# Define colors for the edges
edge_type_colors = {
    ("Lot", "SameSetup", "Station"): "#8B4D9E",
    ("Station", "ShortSetup", "Lot"): "#DFB825",
    ("Lot", "SameEnergySetup", "Station"): "#70B349",
    ("Station", "ProcessNow", "Lot"): "#DB5C64",
}

edge_colors = []
for from_node, to_node, attrs in graph.edges(data=True):
    edge_type = attrs["type"]
    color = edge_type_colors[edge_type]

    graph.edges[from_node, to_node]["color"] = color
    edge_colors.append(color)


# Draw the graph
pos = nx.spring_layout(graph, k=2)
nx.draw_networkx(
    graph,
    pos=pos,
    labels=labels,
    with_labels=True,
    node_color=node_colors,
    edge_color=edge_colors,
    node_size=600,
)
plt.show()

Sample Graph

贪了杯 2025-02-20 01:36:10

我已经完成了以下操作:

import networkx as nx
from matplotlib import pyplot as plt
from torch_geometric.nn import to_hetero

g = torch_geometric.utils.to_networkx(data.to_homogeneous())
# Networkx seems to create extra nodes from our heterogeneous graph, so I remove them
isolated_nodes = [node for node in g.nodes() if g.out_degree(node) == 0]
[g.remove_node(i_n) for i_n in isolated_nodes]
# Plot the graph
nx.draw(g, with_labels=True)
plt.show()

”在此处输入图像描述”

但是,它是“扁平”到同质的,而例如,在不同类型的节点中使用不同的颜色会更有趣。

I have done the following:

import networkx as nx
from matplotlib import pyplot as plt
from torch_geometric.nn import to_hetero

g = torch_geometric.utils.to_networkx(data.to_homogeneous())
# Networkx seems to create extra nodes from our heterogeneous graph, so I remove them
isolated_nodes = [node for node in g.nodes() if g.out_degree(node) == 0]
[g.remove_node(i_n) for i_n in isolated_nodes]
# Plot the graph
nx.draw(g, with_labels=True)
plt.show()

enter image description here

However, it's true that it was "flattened" to a homogeneous, while it'd be more interesting to, for example, use different colors for different types of nodes.

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