jboy
jboy

Reputation: 150

subgraph from selected vertices using python-igraph

I'm trying to create a pruned subgraph that contains vertices from a graph with high betweenness centrality.

This is my approach:

>>> import igraph as ig
>>> import numpy as np

>>> ig.summary(graph)
IGRAPH DNW- 9358 35488 -- 
+ attr: id (v), label (v), name (v), weight (e)

>>> btwn = graph.betweenness(weights='weight')
>>> ntile = np.percentile(btwn, 95)
>>> pruned_vs = graph.vs.select([v >= ntile for v in btwn])
>>> pruned_graph = graph.subgraph(pruned_vs)

Everything runs fine. However, the resulting subgraph is not constructed properly.

>>> ig.summary(pruned_graph)
IGRAPH DNW- 0 0 -- 
+ attr: id (v), label (v), name (v), weight (e)

I've tried changing the implementation attribute for subgraph but that didn't help. What am I missing?

Upvotes: 3

Views: 5680

Answers (1)

Tamás
Tamás

Reputation: 48071

This line is incorrect:

pruned_vs = graph.vs.select([v >= ntile for v in btwn])

The list comprehension that you pass as an argument to select() returns a Boolean vector, which is unsupported by graph.vs.select() - you need to pass a vector containing the indices of the nodes to select. This can be done as follows:

pruned_vs = graph.vs.select([v for v, b in enumerate(btwn) if b >= ntile])

Upvotes: 2

Related Questions