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 ? -- View this message in context: http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/... Sent from the Main discussion list for the graph-tool project mailing list archive at Nabble.com.
On 11/08/2013 09:29 PM, vcongz wrote:
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: http://graph-tool.skewed.de/static/doc/quickstart.html#graph-filtering 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 -- Tiago de Paula Peixoto <tiago@skewed.de>
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: ############################################################ g = load_graph("g.xml.gz") print g.num_vertices(), g.num_edges() c = g.vertex_properties["com"] target_com = [2,4,5] new_g = GraphView(g, vfilt = lambda v: c[v] in target_com) print new_g.num_vertices(), new_g.num_edges() new_g.save("new_g.xml.gz") ############################################################ When I print the number of their edges and vertices, g and new_g have different number of edges and vertices. But, after saving the new_g, the new_g.xml.gz has same content with g.xml.gz. What is the reason ? Thank you for your help! Best. vcongz -- View this message in context: http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/... Sent from the Main discussion list for the graph-tool project mailing list archive at Nabble.com.
On 11/09/2013 01:01 AM, vcongz wrote:
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: ############################################################ g = load_graph("g.xml.gz")
print g.num_vertices(), g.num_edges()
c = g.vertex_properties["com"]
target_com = [2,4,5]
new_g = GraphView(g, vfilt = lambda v: c[v] in target_com)
print new_g.num_vertices(), new_g.num_edges()
new_g.save("new_g.xml.gz") ############################################################ When I print the number of their edges and vertices, g and new_g have different number of edges and vertices. But, after saving the new_g, the new_g.xml.gz has same content with g.xml.gz.
What is the reason ? Thank you for your help!
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 -- Tiago de Paula Peixoto <tiago@skewed.de>
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 ? Thank you for your help. Best. vcongz -- View this message in context: http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/... Sent from the Main discussion list for the graph-tool project mailing list archive at Nabble.com.
On 11/11/2013 02:28 AM, vcongz wrote:
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 ? Thank you for your help.
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 -- Tiago de Paula Peixoto <tiago@skewed.de>
example.py <http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/file/n4025204/example.py> example.xml.gz <http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/file/n4025204/example.xml.gz> 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. -- View this message in context: http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/... Sent from the Main discussion list for the graph-tool project mailing list archive at Nabble.com.
On 11/12/2013 01:28 AM, vcongz wrote:
example.py <http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/file/n4025204/example.py> example.xml.gz <http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/file/n4025204/example.xml.gz>
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 -- Tiago de Paula Peixoto <tiago@skewed.de>
Dear Tiago, Thank you for your help. I updated numpy to newest version, it works now. -- View this message in context: http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/... Sent from the Main discussion list for the graph-tool project mailing list archive at Nabble.com.
participants (2)
-
Tiago de Paula Peixoto -
vcongz