Skip to content

Commit 06fca73

Browse files
committed
fix: mode out and modularity calculation
1 parent 505f25a commit 06fca73

File tree

2 files changed

+20
-37
lines changed

2 files changed

+20
-37
lines changed

src/_igraph/graphobject.c

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13780,32 +13780,22 @@ PyObject *igraphmodule_Graph_community_fluid_communities(igraphmodule_GraphObjec
1378013780
*/
1378113781
PyObject *igraphmodule_Graph_community_voronoi(igraphmodule_GraphObject *self,
1378213782
PyObject *args, PyObject *kwds) {
13783-
static char *kwlist[] = {"modularity", "lengths", "weights", "mode", "radius", NULL};
13783+
static char *kwlist[] = {"lengths", "weights", "mode", "radius", NULL};
1378413784
PyObject *lengths_o = Py_None, *weights_o = Py_None;
1378513785
PyObject *mode_o = Py_None;
1378613786
PyObject *radius_o = Py_None;
13787-
PyObject *modularity_o = Py_None;
1378813787
igraph_vector_t *lengths_v = NULL;
1378913788
igraph_vector_t *weights_v = NULL;
1379013789
igraph_vector_int_t membership_v, generators_v;
13791-
igraph_neimode_t mode = IGRAPH_ALL;
13790+
igraph_neimode_t mode = IGRAPH_OUT;
1379213791
igraph_real_t radius = -1.0; /* negative means auto-optimize */
1379313792
igraph_real_t modularity = IGRAPH_NAN;
1379413793
PyObject *membership_o, *generators_o, *result_o;
13795-
igraph_bool_t return_modularity = false;
1379613794

13797-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOO", kwlist,
13798-
&modularity_o, &lengths_o, &weights_o, &mode_o, &radius_o))
13795+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO", kwlist,
13796+
&lengths_o, &weights_o, &mode_o, &radius_o))
1379913797
return NULL;
1380013798

13801-
if (modularity_o != Py_None){
13802-
if (igraphmodule_PyObject_to_real_t(modularity_o, &modularity))
13803-
return NULL;
13804-
}
13805-
else {
13806-
return_modularity = true;
13807-
}
13808-
1380913799
/* Handle mode parameter */
1381013800
if (igraphmodule_PyObject_to_neimode_t(mode_o, &mode))
1381113801
return NULL;
@@ -13855,7 +13845,7 @@ PyObject *igraphmodule_Graph_community_voronoi(igraphmodule_GraphObject *self,
1385513845

1385613846
/* Call the C function - pass NULL for None parameters */
1385713847
if (igraph_community_voronoi(&self->g, &membership_v, &generators_v,
13858-
return_modularity ? &modularity : NULL,
13848+
&modularity,
1385913849
lengths_v,
1386013850
weights_v,
1386113851
mode, radius)) {
@@ -13897,11 +13887,8 @@ PyObject *igraphmodule_Graph_community_voronoi(igraphmodule_GraphObject *self,
1389713887
}
1389813888

1389913889
/* Return tuple with membership, generators, and modularity */
13900-
if (return_modularity) {
13901-
result_o = Py_BuildValue("(NNd)", membership_o, generators_o, modularity);
13902-
} else {
13903-
result_o = Py_BuildValue("(NN)", membership_o, generators_o);
13904-
}
13890+
result_o = Py_BuildValue("(NNd)", membership_o, generators_o, modularity);
13891+
1390513892

1390613893
return result_o;
1390713894
}
@@ -18787,7 +18774,7 @@ struct PyMethodDef igraphmodule_Graph_methods[] = {
1878718774
{"community_voronoi",
1878818775
(PyCFunction) igraphmodule_Graph_community_voronoi,
1878918776
METH_VARARGS | METH_KEYWORDS,
18790-
"community_voronoi(lengths=None, weights=None, mode=\"all\", radius=None, modularity=None)\n\n"
18777+
"community_voronoi(lengths=None, weights=None, mode=\"out\", radius=None)\n\n"
1879118778
"Finds communities using Voronoi partitioning.\n\n"
1879218779
"This function finds communities using a Voronoi partitioning of vertices based\n"
1879318780
"on the given edge lengths divided by the edge clustering coefficient.\n"
@@ -18802,19 +18789,15 @@ struct PyMethodDef igraphmodule_Graph_methods[] = {
1880218789
"@param weights: edge weights, or C{None} to consider all edges as having\n"
1880318790
" unit weight. Weights are used when selecting generator points, as well\n"
1880418791
" as for computing modularity.\n"
18805-
"@param mode: if C{\"out\"}, distances from generator points to all other\n"
18792+
"@param mode: if C{\"out\"} (the default), distances from generator points to all other\n"
1880618793
" nodes are considered. If C{\"in\"}, the reverse distances are used.\n"
1880718794
" If C{\"all\"}, edge directions are ignored. This parameter is ignored\n"
1880818795
" for undirected graphs.\n"
1880918796
"@param radius: the radius/resolution to use when selecting generator points.\n"
1881018797
" The larger this value, the fewer partitions there will be. Pass C{None}\n"
1881118798
" to automatically select the radius that maximizes modularity.\n"
18812-
"@param modularity: if not C{None}, the modularity score will be calculated\n"
18813-
" and returned as part of the result tuple.\n"
18814-
"@return: a tuple containing the membership vector and generator vertices.\n"
18815-
" When modularity calculation is requested, also includes the modularity score\n"
18816-
" as a third element: (membership, generators, modularity).\n"
18817-
" Otherwise: (membership, generators).\n"
18799+
"@return: a tuple containing the membership vector, generator vertices, and\n"
18800+
" modularity score: (membership, generators, modularity).\n"
1881818801
"@rtype: tuple\n\n"
1881918802
"B{References}\n\n"
1882018803
" - Deritei et al., Community detection by graph Voronoi diagrams,\n"

src/igraph/community.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def _community_spinglass(graph, *args, **kwds):
327327
return VertexClustering(graph, membership, modularity_params=modularity_params)
328328

329329

330-
def _community_voronoi(graph, modularity=None, lengths=None, weights=None, mode="all", radius=None):
330+
def _community_voronoi(graph, lengths=None, weights=None, mode="out", radius=None):
331331
"""Finds communities using Voronoi partitioning.
332332
333333
This function finds communities using a Voronoi partitioning of vertices based
@@ -354,10 +354,13 @@ def _community_voronoi(graph, modularity=None, lengths=None, weights=None, mode=
354354
@param weights: edge weights, or C{None} to consider all edges as having
355355
unit weight. Weights are used when selecting generator points, as well
356356
as for computing modularity.
357-
@param mode: if C{"out"}, distances from generator points to all other
358-
nodes are considered. If C{"in"}, the reverse distances are used.
359-
If C{"all"}, edge directions are ignored. This parameter is ignored
360-
for undirected graphs.
357+
@param mode: specifies how to use the direction of edges when computing
358+
distances from generator points. If C{"out"} (the default), distances
359+
from generator points to all other nodes are considered following the
360+
direction of edges. If C{"in"}, distances are computed in the reverse
361+
direction (i.e., from all nodes to generator points). If C{"all"},
362+
edge directions are ignored and the graph is treated as undirected.
363+
This parameter is ignored for undirected graphs.
361364
@param radius: the radius/resolution to use when selecting generator points.
362365
The larger this value, the fewer partitions there will be. Pass C{None}
363366
to automatically select the radius that maximizes modularity.
@@ -372,10 +375,7 @@ def _community_voronoi(graph, modularity=None, lengths=None, weights=None, mode=
372375
else:
373376
raise ValueError(f"Invalid mode '{mode}'. Must be one of: out, in, all")
374377

375-
if modularity is None:
376-
membership, generators, modularity = GraphBase.community_voronoi(graph, modularity, lengths, weights, mode, radius)
377-
else:
378-
membership, generators = GraphBase.community_voronoi(graph, modularity, lengths, weights, mode, radius)
378+
membership, generators, modularity = GraphBase.community_voronoi(graph, lengths, weights, mode, radius)
379379

380380
params = {"generators": generators}
381381
modularity_params = {}

0 commit comments

Comments
 (0)