Commit 26e770f5 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Fix bug with edge property sums in condensation_graph()

parent 1494f7e4
Pipeline #184 passed with stage
in 1636 minutes and 37 seconds
......@@ -263,7 +263,8 @@ struct get_edge_community_property_sum
template <class Graph, class CommunityGraph, class CommunityMap,
class CCommunityMap, class Eprop, class CEprop>
void operator()(const Graph& g, CommunityGraph& cg, CommunityMap s_map,
CCommunityMap cs_map, Eprop eprop, CEprop ceprop) const
CCommunityMap cs_map, Eprop eprop, CEprop ceprop,
bool self_loops, bool parallel_edges) const
{
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
typedef typename graph_traits<CommunityGraph>::vertex_descriptor
......@@ -291,12 +292,21 @@ struct get_edge_community_property_sum
{
cvertex_t cs = comms[get(s_map, source(e, g))];
cvertex_t ct = comms[get(s_map, target(e, g))];
auto& ces = comm_edges[make_pair(cs, ct)];
if (ces.empty())
continue;
ceprop[ces.back()] += eprop[e];
if (ces.size() > 1)
ces.pop_back();
if (cs == ct && !self_loops)
continue; // self-loops not allowed
auto* ces = &comm_edges[make_pair(cs, ct)];
if (ces->empty() && !is_directed::apply<Graph>::type::value)
ces = &comm_edges[make_pair(ct, cs)];
if (ces->empty())
{
throw GraphException("Bug: condensed edge not found! " +
lexical_cast<string>(cs) + " " +
lexical_cast<string>(ct));
}
ceprop[ces->back()] += eprop[e];
if (parallel_edges)
ces->pop_back();
}
}
};
......
......@@ -50,14 +50,14 @@ struct get_weighted_edge_property_dispatch
void sum_eprops(GraphInterface& gi, GraphInterface& cgi,
boost::any community_property,
boost::any condensed_community_property,
boost::any ceprop, boost::any eprop);
boost::any condensed_community_property, boost::any ceprop,
boost::any eprop, bool self_loops, bool parallel_edges);
void community_network_eavg(GraphInterface& gi, GraphInterface& cgi,
boost::any community_property,
boost::any condensed_community_property,
boost::any eweight,
boost::python::list aeprops)
boost::any eweight, boost::python::list aeprops,
bool self_loops, bool parallel_edges)
{
typedef boost::mpl::push_back<writable_edge_scalar_properties, no_eweight_map_t>::type
eweight_properties;
......@@ -85,7 +85,7 @@ void community_network_eavg(GraphInterface& gi, GraphInterface& cgi,
if (!no_weight)
{
// compute weighted values to temp
run_action<graph_tool::detail::always_directed>()
run_action<>()
(gi, std::bind(get_weighted_edge_property_dispatch(),
std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, temp),
......@@ -94,14 +94,15 @@ void community_network_eavg(GraphInterface& gi, GraphInterface& cgi,
// sum weighted values
sum_eprops(gi, cgi, community_property,
condensed_community_property, ceprop, temp);
condensed_community_property, ceprop, temp,
self_loops, parallel_edges);
}
else
{
// sum unweighted values
sum_eprops(gi, cgi, community_property,
condensed_community_property,
ceprop, eprop);
condensed_community_property, ceprop, eprop,
self_loops, parallel_edges);
}
}
......
......@@ -41,18 +41,20 @@ struct get_edge_sum_dispatch
template <class Graph, class CommunityGraph, class CommunityMap,
class Eprop>
void operator()(const Graph& g, CommunityGraph& cg, CommunityMap s_map,
boost::any acs_map, Eprop eprop, boost::any aceprop) const
boost::any acs_map, Eprop eprop, boost::any aceprop,
bool self_loops, bool parallel_edges) const
{
typename CommunityMap::checked_t cs_map = boost::any_cast<typename CommunityMap::checked_t>(acs_map);
typename Eprop::checked_t ceprop = boost::any_cast<typename Eprop::checked_t>(aceprop);
get_edge_community_property_sum()(g, cg, s_map, cs_map, eprop, ceprop);
get_edge_community_property_sum()(g, cg, s_map, cs_map, eprop, ceprop,
self_loops, parallel_edges);
}
};
void sum_eprops(GraphInterface& gi, GraphInterface& cgi,
boost::any community_property,
boost::any condensed_community_property,
boost::any ceprop, boost::any eprop)
boost::any condensed_community_property, boost::any ceprop,
boost::any eprop, bool self_loops, bool parallel_edges)
{
typedef boost::mpl::insert_range<writable_edge_scalar_properties,
boost::mpl::end<writable_edge_scalar_properties>::type,
......@@ -61,12 +63,12 @@ void sum_eprops(GraphInterface& gi, GraphInterface& cgi,
eprop_map_t<boost::python::object>::type >::type
eprops_t;
run_action<graph_tool::detail::always_directed>()
run_action<>()
(gi, std::bind(get_edge_sum_dispatch(),
std::placeholders::_1, std::ref(cgi.get_graph()),
std::placeholders::_2,
condensed_community_property, std::placeholders::_3,
ceprop),
ceprop, self_loops, parallel_edges),
writable_vertex_properties(), eprops_t())
(community_property, eprop);
}
......@@ -96,7 +96,7 @@ void community_network_vavg(GraphInterface& gi, GraphInterface& cgi,
if (!no_weight)
{
// compute weighted values to temp
run_action<graph_tool::detail::always_directed_never_reversed>()
run_action<>()
(gi, std::bind(get_weighted_vertex_property_dispatch(),
std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, temp),
......@@ -104,7 +104,7 @@ void community_network_vavg(GraphInterface& gi, GraphInterface& cgi,
(vweight, vprop);
// sum weighted values
run_action<graph_tool::detail::always_directed_never_reversed>()
run_action<>()
(gi, std::bind(get_vertex_sum_dispatch(),
std::placeholders::_1, std::ref(cgi.get_graph()),
std::placeholders::_2,
......@@ -116,7 +116,7 @@ void community_network_vavg(GraphInterface& gi, GraphInterface& cgi,
else
{
// sum unweighted values
run_action<graph_tool::detail::always_directed_never_reversed>()
run_action<>()
(gi, std::bind(get_vertex_sum_dispatch(),
std::placeholders::_1, std::ref(cgi.get_graph()),
std::placeholders::_2,
......
......@@ -111,7 +111,8 @@ void community_network_vavg(GraphInterface& gi, GraphInterface& cgi,
void community_network_eavg(GraphInterface& gi, GraphInterface& cgi,
boost::any community_property,
boost::any condensed_community_property,
boost::any eweight, boost::python::list aeprops);
boost::any eweight, boost::python::list aeprops,
bool self_loops, bool parallel_edges);
using namespace boost::python;
......
......@@ -1798,13 +1798,13 @@ def condensation_graph(g, prop, vweight=None, eweight=None, avprops=None,
_prop("v", g, vweight),
avp)
u = GraphView(g, directed=True)
libgraph_tool_generation.community_network_eavg(u._Graph__graph,
libgraph_tool_generation.community_network_eavg(g._Graph__graph,
gp._Graph__graph,
_prop("v", g, prop),
_prop("v", gp, cprop),
_prop("e", g, eweight),
aep)
aep, self_loops,
parallel_edges)
return gp, cprop, vcount, ecount, r_avp, r_aep
class Sampler(libgraph_tool_generation.Sampler):
......
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