Skip to content

Commit 8c10051

Browse files
committed
feat: added node_in_weights argument to Graph.community_leiden()
1 parent c02d42d commit 8c10051

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/_igraph/graphobject.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13611,11 +13611,12 @@ PyObject *igraphmodule_Graph_community_walktrap(igraphmodule_GraphObject * self,
1361113611
PyObject *igraphmodule_Graph_community_leiden(igraphmodule_GraphObject *self,
1361213612
PyObject *args, PyObject *kwds) {
1361313613

13614-
static char *kwlist[] = {"edge_weights", "node_weights", "resolution",
13614+
static char *kwlist[] = {"edge_weights", "node_weights", "node_in_weights", "resolution",
1361513615
"normalize_resolution", "beta", "initial_membership", "n_iterations", NULL};
1361613616

1361713617
PyObject *edge_weights_o = Py_None;
1361813618
PyObject *node_weights_o = Py_None;
13619+
PyObject *node_in_weights_o = Py_None;
1361913620
PyObject *initial_membership_o = Py_None;
1362013621
PyObject *normalize_resolution = Py_False;
1362113622
PyObject *res = Py_None;
@@ -13624,14 +13625,14 @@ PyObject *igraphmodule_Graph_community_leiden(igraphmodule_GraphObject *self,
1362413625
Py_ssize_t n_iterations = 2;
1362513626
double resolution = 1.0;
1362613627
double beta = 0.01;
13627-
igraph_vector_t *edge_weights = NULL, *node_weights = NULL;
13628+
igraph_vector_t *edge_weights = NULL, *node_weights = NULL, *node_in_weights = NULL;
1362813629
igraph_vector_int_t *membership = NULL;
1362913630
igraph_bool_t start = true;
1363013631
igraph_integer_t nb_clusters = 0;
1363113632
igraph_real_t quality = 0.0;
1363213633

13633-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOdOdOn", kwlist,
13634-
&edge_weights_o, &node_weights_o, &resolution, &normalize_resolution, &beta, &initial_membership_o, &n_iterations))
13634+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOdOdOn", kwlist,
13635+
&edge_weights_o, &node_weights_o, &node_in_weights_o, &resolution, &normalize_resolution, &beta, &initial_membership_o, &n_iterations))
1363513636
return NULL;
1363613637

1363713638
if (n_iterations >= 0) {
@@ -13654,6 +13655,13 @@ PyObject *igraphmodule_Graph_community_leiden(igraphmodule_GraphObject *self,
1365413655
error = -1;
1365513656
}
1365613657

13658+
/* Get node in-weights (directed case) */
13659+
if (!error && igraphmodule_attrib_to_vector_t(node_in_weights_o, self, &node_in_weights,
13660+
ATTRIBUTE_TYPE_VERTEX)) {
13661+
igraphmodule_handle_igraph_error();
13662+
error = -1;
13663+
}
13664+
1365713665
/* Get initial membership */
1365813666
if (!error && igraphmodule_attrib_to_vector_int_t(initial_membership_o, self, &membership,
1365913667
ATTRIBUTE_TYPE_VERTEX)) {
@@ -13696,7 +13704,7 @@ PyObject *igraphmodule_Graph_community_leiden(igraphmodule_GraphObject *self,
1369613704
/* Run actual Leiden algorithm for several iterations. */
1369713705
if (!error) {
1369813706
error = igraph_community_leiden(&self->g,
13699-
edge_weights, node_weights,
13707+
edge_weights, node_weights, node_in_weights,
1370013708
resolution, beta,
1370113709
start, n_iterations, membership,
1370213710
&nb_clusters, &quality);
@@ -13710,6 +13718,10 @@ PyObject *igraphmodule_Graph_community_leiden(igraphmodule_GraphObject *self,
1371013718
igraph_vector_destroy(node_weights);
1371113719
free(node_weights);
1371213720
}
13721+
if (node_in_weights != 0) {
13722+
igraph_vector_destroy(node_in_weights);
13723+
free(node_in_weights);
13724+
}
1371313725

1371413726
if (!error && membership != 0) {
1371513727
res = igraphmodule_vector_int_t_to_PyList(membership);

src/igraph/community.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ def _community_leiden(
461461
initial_membership=None,
462462
n_iterations=2,
463463
node_weights=None,
464+
node_in_weights=None,
464465
**kwds,
465466
):
466467
"""Finds the community structure of the graph using the Leiden
@@ -491,6 +492,11 @@ def _community_leiden(
491492
If this is not provided, it will be automatically determined on the
492493
basis of whether you want to use CPM or modularity. If you do provide
493494
this, please make sure that you understand what you are doing.
495+
@param node_in_weights: the inbound node weights used in the directed
496+
variant of the Leiden algorithm. If this is not provided, it will be
497+
automatically determined on the basis of whether you want to use CPM or
498+
modularity. If you do provide this, please make sure that you understand
499+
what you are doing.
494500
@return: an appropriate L{VertexClustering} object with an extra attribute
495501
called C{quality} that stores the value of the internal quality function
496502
optimized by the algorithm.
@@ -512,6 +518,7 @@ def _community_leiden(
512518
graph,
513519
edge_weights=weights,
514520
node_weights=node_weights,
521+
node_in_weights=node_in_weights,
515522
resolution=resolution,
516523
normalize_resolution=(objective_function == "modularity"),
517524
beta=beta,

vendor/source/igraph

Submodule igraph updated 83 files

0 commit comments

Comments
 (0)