Commit 9e70cee8 authored by Tiago Peixoto's avatar Tiago Peixoto

modularity(): add ``gamma`` parameter

parent 4059c511
Pipeline #651 failed with stage
in 146 minutes and 21 seconds
......@@ -24,7 +24,8 @@ using namespace std;
using namespace boost;
using namespace graph_tool;
double modularity(GraphInterface& gi, boost::any weight, boost::any property)
double modularity(GraphInterface& gi, double gamma, boost::any weight,
boost::any property)
{
double Q = 0;
......@@ -36,7 +37,7 @@ double modularity(GraphInterface& gi, boost::any weight, boost::any property)
weight = weight_map_t();
run_action<>()
(gi, [&](auto& g, auto& w, auto& b){ Q = get_modularity(g, w, b);},
(gi, [&](auto& g, auto& w, auto& b){ Q = get_modularity(g, gamma, w, b);},
edge_props_t(), vertex_scalar_properties())
(weight, property);
......
......@@ -34,7 +34,8 @@ using namespace boost;
// get Newman's modularity of a given community partition
template <class Graph, class WeightMap, class CommunityMap>
double get_modularity(const Graph& g, WeightMap weights, CommunityMap b)
double get_modularity(const Graph& g, double gamma, WeightMap weights,
CommunityMap b)
{
size_t B = 0;
for (auto v : vertices_range(g))
......@@ -64,7 +65,7 @@ double get_modularity(const Graph& g, WeightMap weights, CommunityMap b)
double Q = 0;
for (size_t r = 0; r < B; ++r)
Q += err[r] - (er[r] * er[r]) / W;
Q += err[r] - gamma * (er[r] / W) * er[r];
Q /= W;
return Q;
};
......
......@@ -27,9 +27,9 @@ import numpy as np
from .. dl_import import dl_import
dl_import("from . import libgraph_tool_inference as libinference")
def modularity(g, b, weight=None):
def modularity(g, b, gamma=1., weight=None):
r"""
Calculate Newman's modularity of a network partition.
Calculate Newman's (generalized) modularity of a network partition.
Parameters
----------
......@@ -37,6 +37,8 @@ def modularity(g, b, weight=None):
Graph to be used.
b : :class:`~graph_tool.VertexPropertyMap`
Vertex property map with the community partition.
gamma : ``float`` (optional, default: ``1.``)
Resolution parameter.
weight : :class:`~graph_tool.EdgePropertyMap` (optional, default: None)
Edge property map with the optional edge weights.
......@@ -79,7 +81,7 @@ def modularity(g, b, weight=None):
if b.value_type() not in ["bool", "int16_t", "int32_t", "int64_t",
"unsigned long"]:
b = perfect_prop_hash([b])[0]
Q = libinference.modularity(g._Graph__graph,
Q = libinference.modularity(g._Graph__graph, gamma,
_prop("e", g, weight),
_prop("v", g, b))
return Q
......
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