# About extract communities from a graph

After using community_structure(), each vertex in the graph has a community
property.

However, how to extract some certain communities from this graph ?

Such as, the graph G contains communities {1,2,3,4,5}, now, I hope to get
and save a sub-graph which contains the vertices and their edge in
communities {2,4,5} of G ?

Is there any continent and fast function or algorithm in graph_tool to solve
this problem ?

You can use a graph filter:

If 'c' is a property map with your community labels, you can extract the
community subgraph with:

u = GraphView(g, vfilt=lambda v: c[v] in [2, 4, 5])

The graph view u now contains only the vertices belonging to communities [2,
4, 5].

Cheers,
Tiago

Dear Tiago,

Thanks, it works. But when I save the sub-graph, the new xml.gz file is
still the original graph.

My code seems like:

The reason is that the filtered graph is just like the unfiltered graph,
but with a filter on top. Filtering is a reversible operation, and can
be removed at any time. If you want to delete the filtered vertices you
can make:

new_g.purge_vertices()

Cheers,
Tiago

Dear Tiago,
When I use this, there is a error:

Traceback (most recent call last):
File "/home/workspace/work1/renren.py", line 127, in <module>
find_sub_g()
File "/home/workspace/work1/renren.py", line 97, in find_sub_g
new_g.purge_vertices()
File "/usr/lib/python2.7/dist-packages/graph_tool/__init__.py", line 1850,
in purge_vertices
new_g = Graph(self, prune=(True, False, False))
File "/usr/lib/python2.7/dist-packages/graph_tool/__init__.py", line 1121,
in __init__
vorder.fa = numpy.arange(gv.num_vertices())
File "/usr/lib/python2.7/dist-packages/graph_tool/__init__.py", line 567,
in <lambda>
lambda self, v: self.__get_set_f_array(v, False),
File "/usr/lib/python2.7/dist-packages/graph_tool/__init__.py", line 562,
in __get_set_f_array
a[m] = v
TypeError: array cannot be safely cast to required type

What are the reasons ?

Best.
vcongz

Looks strange... Please send a complete (minimal) example program so I
can see what is going on.

Also please tell me what operating system and version of graph-tool you
are using.

Cheers,
Tiago

My OS is Ubuntu 12.04 LTS. The graph_tool version is
python-graph-tool/precise uptodate 2.2.26-1.
The same error exists when I build animation_sirs.py at the line 31: g =
Graph(g, prune=True)

Besides, I would like to set the pos of vertices by their communities. Such
as, the vertices in the same community should locate at the near position.
Can I set pos by myself ?

Thank you.

My OS is Ubuntu 12.04 LTS. The graph_tool version is
python-graph-tool/precise uptodate 2.2.26-1.
The same error exists when I build animation_sirs.py at the line 31: g =
Graph(g, prune=True)

This seems to be a bug with the numpy version shipped with Ubuntu
precise. I've tested this here, and everything works fine (i.e. no
errors).

Are you stuck with this Ubuntu version or can you upgrade?

Besides, I would like to set the pos of vertices by their communities. Such
as, the vertices in the same community should locate at the near position.
Can I set pos by myself ?

Of course, pos is a property map like any other. For instance, to change
the position of vertex 42 you do:

v = g.vertex(42)
pos[v] = [2.3, 4.2]

Cheers,
Tiago

Dear Tiago,