Hi Alexandre,
Thx for your reply! Indeed, your and my following optimizations are not gt-specific. I’m now here:
# set layer to be drawn
layer = 0
# create graph view
v_filter = edge_list.groupby('layer')[['i', 'j']].apply(lambda x: set(x.values.flatten())).to_dict() # create dictionary with vertices in a layer
vp_filter = g.new_vp('bool')
vp_filter.a[list(v_filter[layer])] = True
ep_filter = g.new_ep('bool')
ep_filter.a[g.ep['layer'].a == layer] = True # replaces my or Alexandre's loop
g_filter = GraphView(g, vfilt=vp_filter, efilt=ep_filter)
# draw filtered graph
graph_draw(g_filter, edge_color=g.ep.layer)
This is already rather short and nice. Still, I'm wondering if there’s a way that is more gt-esque.
Haiko
Von: graph-tool [mailto:graph-tool-bounces@skewed.de]
Im Auftrag von Alexandre Hannud Abdo
Gesendet: Donnerstag, 29. August 2019 15:03
An: Main discussion list for the graph-tool project
Betreff: Re: [graph-tool] Graph views via property maps
Ni! Hi Haiko,
GraphView can simultaneously take `vfilt` and `efilt` parameters, if that is what you wanna know. So,
g_filter = GraphView(g, vfilt=vp_filter, efilt=ep_filter)
should work just fine.
Regarding your code snippet, the 'elegance' issues are kinda unrelated to graph_tool. For example, in the second part, you iterate the dataframe instead of simply iterating the graph edges and getting the edge layer from the internal property
map. You also use an if clause to assign the boolean value of the if clause, instead of directly assigning it. So, this is equivalent to your `for` loop:
for e in g.edges():
ep_filter[e] = g.edge_properties['layer'][e] == layer
All the best,
.~´
On Thu, Aug 29, 2019 at 10:49 AM Lietz, Haiko <Haiko.Lietz@gesis.org> wrote:
Hi all,
gt allows creating graph views where vertices and/or edges are filtered out. The principle is described in the documentation:
"Vertices or edges which are to be filtered should be marked with a PropertyMap with value type bool, and then set with set_vertex_filter() or set_edge_filter() methods. By default, vertex or edges with value '1' are kept in the graphs, and those with value '0' are filtered out." (https://graph-tool.skewed.de/static/doc/quickstart.html#graph-filtering)
Given a layered graph, where layers are coded via an edge property, I want to draw the graphs layer by layer.
This is an example graph:
from graph_tool.all import *
import numpy as np
import pandas as pd
# data
edge_list = pd.DataFrame(np.array([[0, 1, 0], [1, 2, 1], [2, 0, 2]]), columns=['i', 'j', 'layer'])
# create graph
g = Graph(directed=False)
ep_layer = g.new_edge_property('int')
g.add_edge_list(edge_list.values, eprops=[ep_layer])
g.edge_properties['layer'] = ep_layer
# draw graph with edge colors showing the layers
graph_draw(g, edge_color=g.ep.layer)
It is possible to draw a single layer (in this example layer 0) in this complicated way:
# set layer to be drawn
layer = 0
# create graph view
ep_filter = g.new_edge_property('bool')
for i in range(0, len(edge_list)):
e = g.edge(edge_list['i'][i], edge_list['j'][i])
if edge_list['layer'][i] == layer:
ep_filter[e] = True
else:
ep_filter[e] = False
g_filter = GraphView(g, efilt=ep_filter)
# draw filtered graph
graph_draw(g_filter, edge_color=g.ep.layer)
If I proceed this way I will also have to create a vertex property map to filter unused vertices and do these steps for all layers I want to draw.
But isn’t there a more elegant way – preferably handling vertices and edges in the same step?
Many thanks and best wishes
Haiko
_______________________________________________
graph-tool mailing list
graph-tool@skewed.de
https://lists.skewed.de/mailman/listinfo/graph-tool