[Essays] graft

Author

신록예찬

Published

November 9, 2023

[Essays] graft

신록예찬
2023-11-09

Imports

소스코드 다운로드: https://github.com/guebin/graft

  • 계속 업데이트할 예정
#!git clone https://github.com/guebin/graft
#!conda install -c conda-forge graph-tool -y
import numpy as np
import torch
import torch_geometric
import warnings
warnings.filterwarnings("ignore")
import graft
links = torch.tensor([[0, 1, 2, 3, 4, 3],
                      [1, 0, 3, 2, 3, 4]], dtype=torch.long)
g = torch_geometric.data.Data(
    edge_index = links
)
graft.graph.plot_undirected_unweighted(g)

# Ex – node_names

graft.graph.plot_undirected_unweighted(
    g, 
    node_names = ['a','b','c','d','e'], 
)

# Ex – node_color (continuous)

links = torch.tensor([[0, 1, 2, 3, 4, 3],
                      [1, 0, 3, 2, 3, 4]], dtype=torch.long)
g = torch_geometric.data.Data(
    edge_index = links, 
    y = np.random.randn(5)
)
graft.graph.plot_undirected_unweighted(
    g,
    node_color=g.y
)

# Ex – node_color (discrete)

links = torch.tensor([[0, 1, 2, 3, 4, 3],
                      [1, 0, 3, 2, 3, 4]], dtype=torch.long)
g = torch_geometric.data.Data(
    edge_index = links, 
    y = torch.tensor([0,1,0,0,1])
)
graft.graph.plot_undirected_unweighted(
    g,
    node_color=g.y
)

# Ex – node_color (discrete) / node_size

links = torch.tensor([[0, 1, 2, 3, 4, 3],
                      [1, 0, 3, 2, 3, 4]], dtype=torch.long)
g = torch_geometric.data.Data(
    edge_index = links, 
    y = torch.tensor([0,1,0,0,1]),
    x = torch.tensor([10,100,15,20,150])
)
graft.graph.plot_undirected_unweighted(
    g,
    node_color=g.y,
    node_size=g.x
)

# Ex – draw options

links = torch.tensor([[0, 1, 2, 3, 4, 3],
                      [1, 0, 3, 2, 3, 4]], dtype=torch.long)
g = torch_geometric.data.Data(
    edge_index = links
)
graft.graph.plot_undirected_unweighted(
    g,
)

dr_opts = {
    'vertex_size':30
}
graft.graph.plot_undirected_unweighted(
    g,
    draw_options= dr_opts
)

dr_opts = {
    'edge_marker_size': 200, 
    'output_size': (300,300)
}
graft.graph.plot_undirected_unweighted(
    g,
    draw_options= dr_opts
)

Undirected / Weighted

# Ex

links = torch.tensor([[0, 1, 2, 3, 4, 3],
                      [1, 0, 3, 2, 3, 4]], dtype=torch.long)
weights = torch.tensor([5, 5, 1.5, 1.5, 0.19, 0.19], dtype=torch.float)

g = torch_geometric.data.Data(
    edge_index=links,
    edge_attr=weights,
)
graft.graph.plot_undirected_weighted(
    g,
)

# Ex

links = torch.tensor([[0, 1, 2, 3, 4, 3],
                      [1, 0, 3, 2, 3, 4]], dtype=torch.long)
weights = torch.tensor([5, 5, 1.5, 1.5, 0.19, 0.19], dtype=torch.float)

g = torch_geometric.data.Data(
    edge_index=links,
    edge_attr=weights,
)
graft.graph.plot_undirected_weighted(
    g,
    edge_weight_text=False
)

graft.graph.plot_undirected_weighted(
    g,
    edge_weight_text=False,
    edge_weight_width=False
)

graft.graph.plot_undirected_weighted(
    g,
    edge_weight_text=True,
    edge_weight_width=True,
    edge_weight_text_format='.1f',
)

graft.graph.plot_undirected_weighted(
    g,
    edge_weight_text=True,
    edge_weight_width=True,
    edge_weight_text_format='.1f',
    edge_weight_width_scale=5.0,
)

# Ex

links = torch.tensor([[0, 1, 2, 3, 4, 3],
                      [1, 0, 3, 2, 3, 4]], dtype=torch.long)
weights = torch.tensor([5, 5, 1.5, 1.5, 0.19, 0.19], dtype=torch.float)

g = torch_geometric.data.Data(
    edge_index=links,
    edge_attr=weights,
    y = torch.tensor([1,1,0,0,0])
)
graft.graph.plot_undirected_weighted(
    g,
    node_color=g.y
)

# Ex

links = torch.tensor([[0, 1, 2, 3, 4, 3],
                      [1, 0, 3, 2, 3, 4]], dtype=torch.long)
weights = torch.tensor([5, 5, 1.5, 1.5, 0.19, 0.19], dtype=torch.float)
g = torch_geometric.data.Data(
    edge_index = links, 
    edge_attr = weights,
    y = torch.tensor([0,1,0,0,1]),
    x = torch.tensor([1,3,1,2,4])
)
graft.graph.plot_undirected_weighted(
    g,
    node_color=g.y,
    node_size=g.x,
    edge_weight_text=False,
)