Steven Bergner
February 23th, 2023
gt
conda environment
mkdir -p ~/.conda/envs
ln -s /usr/shared/CMPT/big-data/condaenv/gt ~/.conda/envs
source activate gt
graph_tool
module
python
>>> import graph_tool.all as gt
>>> print(gt.__version__.split(' ')[0])
2.46
pandas, jupyter (lab), ...
nodes
and edges
areWhat | Weight | |
Vertex | Person | #Knows |
Edge | Knows | Count |
X | Adjacent to X |
Mary | Peter, Albert, DavidF, Peter |
Judy | Bob, Alan |
Peter | Mary, DavidF, Jon |
DavidF | Albert, Joseph, Peter, Mary |
Jon | Peter, Joseph, DavidE |
DavidE | Jon, Joseph, Albert |
Joseph | DavidE, Jon, DavidF |
Bob | Judy, Alan |
Alan | Bob, Mary, Judy |
Albert | DavidF, Mary, DavidE |
g = gt.Graph()
v1 = g.add_vertex()
v2 = g.add_vertex()
e = g.add_edge(v1, v2)
v_count_p = g.new_vertex_property('int')
# store it in our graph, optionally
g.vp['count'] = v_count_p
from graph_tool import Graph
name_v_map = {}
for name in names:
v = name_v_map.get(name)
if v is None:
v = g.add_vertex()
v_count_p[v] = 0
name_v_map[name] = v
v_count_p[v] += 1
gt.graph_draw(
g,
output_path = 'output.pdf',
)
gt.graph_draw(
g,
output_path = 'output.png'
)
SIZE = 400
V_SIZE = SIZE / 2
E_PWIDTH = V_SIZE / 4
gt.graph_draw(
...
output_size = (SIZE, SIZE),
vertex_size = V_SIZE,
edge_pen_width = E_PWIDTH,
)
v_size_p = gt.prop_to_size(
v_count_p,
MI_V_SIZE,
MA_V_SIZE,
)
...
gt.graph_draw(
...
verted_size = v_size_p,
edge_pen_width = e_pwidth_p,
)
g = gt.load_graph("search_example.xml")
weight
gt.graph_draw(
...
vertex_fill_color = v_size_p,
)
Choice of measure:
Goal: Identify the people with shortest connection to all other people
e_icount_p = g.new_edge_property('int')
e_icount_p.a = e_count_p.a.max() - e_count_p.a
v_cl_p = closeness(g, weight=e_icount_p)
import numpy as np
v_cl_p_.a = np.nan_to_num(v_cl_p.a)
v_cl_size_p = gt.prop_to_size(
v_cl_p,
MI_V_SIZE,
MA_V_SIZE,
)
...
gt.graph_draw(
...
vertex_fill_color = v_cl_size_p,
)
v_pck_p = g.new_vertex_property('bool')
v_pck_p.a = v_count_p.a > v_count_p.a.mean()
g.set_vertex_filter(v_pck_p)
# g.set_vertex_filter(None) # unset
t10_idxs = v_count_p.a.argsort()[-10][::-1]
t1_idx = t10_idxs[0]
t1_v = g.vertex(t1_idx)
t1_name = v_name_p[t1_v]
t1_count = v_count_p[t1_v]
gt.graph_draw(
...
pos = gt.sfdplayout(g),
)
...
pos = gt.sfdplayout(
g, eweight=e_count_p
),
...
pos = gt.sfdplayout(
g,
eweight=e_count_p,
vweight=v_count_p
),
gt.graph_draw(
...
pos = gt.fruchterman_reingold_layout(g),
)
gt.graph_draw(
...
pos = gt.fruchterman_reingold_layout(
g, weight=e_count_p
),
)
gt.graph_draw(
...
pos = gt.arf_layout(g),
)
gt.graph_draw(
...
pos = gt.arf_layout(
g, weight=e_count_p
),
)