Commit 7d08bc42 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Return community property in condensation_graph()

The condensation_graph() function now returns a property map which
contains the community values themselves for each vertex.
parent 0945c5af
......@@ -98,6 +98,7 @@ using namespace boost::python;
extern void community_network(GraphInterface& gi, GraphInterface& cgi,
boost::any community_property,
boost::any condensed_community_property,
boost::any vertex_count,
boost::any edge_count, boost::any weight);
......
......@@ -34,7 +34,9 @@ using namespace graph_tool;
void community_network(GraphInterface& gi, GraphInterface& cgi,
boost::any community_property, boost::any vertex_count,
boost::any community_property,
boost::any condensed_community_property,
boost::any vertex_count,
boost::any edge_count, boost::any weight)
{
typedef DynamicPropertyMapWrap<double,GraphInterface::edge_t> weight_map_t;
......@@ -70,6 +72,7 @@ void community_network(GraphInterface& gi, GraphInterface& cgi,
run_action<>()(gi, bind<void>(get_community_network(), _1,
ref(cgi.GetGraph()), cgi.GetVertexIndex(),
cgi.GetEdgeIndex(), _2,
condensed_community_property,
_3, vcount, _4),
vertex_properties(), weight_properties(),
ecount_properties())
......
......@@ -41,7 +41,7 @@ struct get_community_network
class VertexProperty, class EdgeProperty>
void operator()(const Graph& g, CommunityGraph& cg,
VertexIndex cvertex_index, EdgeIndex cedge_index,
CommunityMap s_map, WeightMap weight,
CommunityMap s_map, boost::any acs_map, WeightMap weight,
VertexProperty vertex_count, EdgeProperty edge_count) const
{
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
......@@ -53,6 +53,11 @@ struct get_community_network
typedef typename boost::property_traits<CommunityMap>::value_type
s_type;
typedef typename get_prop_type<CommunityMap, VertexIndex>::type
comm_map_t;
comm_map_t cs_map = boost::any_cast<comm_map_t>(acs_map);
tr1::unordered_map<s_type, vector<vertex_t>, hash<s_type> >
comms;
typename graph_traits<Graph>::vertex_iterator v, v_end;
......@@ -68,6 +73,10 @@ struct get_community_network
cvertex_t v = add_vertex(cg);
vertex_count[v] = iter->second.size();
comm_vertices[iter->first] = v;
put_dispatch(cs_map, v, iter->first,
typename boost::is_convertible
<typename property_traits<CommunityMap>::category,
writable_property_map_tag>::type());
}
// create edges
......@@ -105,6 +114,51 @@ struct get_community_network
}
}
}
struct get_checked_t
{
template <class PropertyMap>
struct apply
{
typedef typename PropertyMap::checked_t type;
};
};
struct get_identity
{
template <class PropertyMap>
struct apply
{
typedef PropertyMap type;
};
};
template <class PropertyMap, class IndexMap>
struct get_prop_type
{
typedef typename mpl::if_<typename is_same<PropertyMap, IndexMap>::type,
get_identity,
get_checked_t>::type extract;
typedef typename extract::template apply<PropertyMap>::type type;
};
template <class PropertyMap>
void put_dispatch(PropertyMap cs_map,
const typename property_traits<PropertyMap>::key_type& v,
const typename property_traits<PropertyMap>::value_type& val,
mpl::true_ is_writable) const
{
put(cs_map, v, val);
}
template <class PropertyMap>
void put_dispatch(PropertyMap cs_map,
const typename property_traits<PropertyMap>::key_type& v,
const typename property_traits<PropertyMap>::value_type& val,
mpl::false_ is_writable) const
{
}
};
} // graph_tool namespace
......
......@@ -298,6 +298,8 @@ def condensation_graph(g, prop, weight=None):
-------
condensation_graph : :class:`~graph_tool.Graph`
The community network
prop : :class:`~graph_tool.PropertyMap`
The community values.
vcount : :class:`~graph_tool.PropertyMap`
A vertex property map with the vertex count for each community.
ecount : :class:`~graph_tool.PropertyMap`
......@@ -312,7 +314,7 @@ def condensation_graph(g, prop, weight=None):
Notes
-----
Each vertex in the condensation graph represents one community in the
original graph (vertices with the same 'prop' value'), and the edges
original graph (vertices with the same 'prop' value), and the edges
represent existent edges between vertices of the respective communities in
the original graph.
......@@ -325,8 +327,8 @@ def condensation_graph(g, prop, weight=None):
>>> spins = gt.community_structure(g, 10000, 100)
>>> ng = gt.condensation_graph(g, spins)
>>> size = ng[0].new_vertex_property("double")
>>> size.a = log(ng[1].a+1)
>>> gt.graph_draw(ng[0], vsize=size, vcolor=size, splines=True,
>>> size.a = log(ng[2].a+1)
>>> gt.graph_draw(ng[0], vsize=size, vcolor=ng[1], splines=True,
... eprops={"len":20, "penwidth":10}, vprops={"penwidth":10},
... output="comm-network.pdf", size=(10,10))
<...>
......@@ -334,8 +336,8 @@ def condensation_graph(g, prop, weight=None):
.. figure:: comm-network.*
:align: center
Community network of a random graph. The color and sizes of the nodes
indicate the size of the corresponding community.
Community network of a random graph. The sizes of the nodes indicate the
size of the corresponding community.
"""
gp = Graph()
vcount = gp.new_vertex_property("int32_t")
......@@ -343,10 +345,12 @@ def condensation_graph(g, prop, weight=None):
ecount = gp.new_edge_property("double")
else:
ecount = gp.new_edge_property("int32_t")
cprop = gp.new_vertex_property(prop.value_type())
libgraph_tool_community.community_network(g._Graph__graph,
gp._Graph__graph,
_prop("v", g, prop),
_prop("v", gp, cprop),
_prop("v", gp, vcount),
_prop("e", gp, ecount),
_prop("e", g, weight))
return gp, vcount, ecount
return gp, cprop, vcount, ecount
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment