Skip to content

Commit 24fee95

Browse files
committed
chore: updated igraph core, added compatibility alias for the allowed_edge_types argument of rewire()
1 parent fef0711 commit 24fee95

File tree

5 files changed

+49
-33
lines changed

5 files changed

+49
-33
lines changed

src/_igraph/graphobject.c

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3563,24 +3563,22 @@ PyObject *igraphmodule_Graph_SBM(PyTypeObject * type,
35633563
{
35643564
igraphmodule_GraphObject *self;
35653565
igraph_t g;
3566-
Py_ssize_t n;
35673566
PyObject *block_sizes_o, *pref_matrix_o;
35683567
PyObject *directed_o = Py_False;
35693568
PyObject *loops_o = Py_False;
3569+
PyObject *multiple_o = Py_False;
35703570
igraph_matrix_t pref_matrix;
35713571
igraph_vector_int_t block_sizes;
35723572

3573-
static char *kwlist[] = { "n", "pref_matrix", "block_sizes", "directed",
3574-
"loops", NULL };
3573+
static char *kwlist[] = { "pref_matrix", "block_sizes", "directed",
3574+
"loops", "multiple", NULL };
35753575

3576-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "nOO|OO", kwlist,
3577-
&n, &pref_matrix_o,
3576+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|OOO", kwlist,
3577+
&pref_matrix_o,
35783578
&block_sizes_o,
3579-
&directed_o, &loops_o))
3579+
&directed_o, &loops_o, &multiple_o))
35803580
return NULL;
35813581

3582-
CHECK_SSIZE_T_RANGE(n, "vertex count");
3583-
35843582
if (igraphmodule_PyObject_to_matrix_t(pref_matrix_o, &pref_matrix, "pref_matrix")) {
35853583
return NULL;
35863584
}
@@ -3590,7 +3588,7 @@ PyObject *igraphmodule_Graph_SBM(PyTypeObject * type,
35903588
return NULL;
35913589
}
35923590

3593-
if (igraph_sbm_game(&g, n, &pref_matrix, &block_sizes, PyObject_IsTrue(directed_o), PyObject_IsTrue(loops_o))) {
3591+
if (igraph_sbm_game(&g, &pref_matrix, &block_sizes, PyObject_IsTrue(directed_o), PyObject_IsTrue(loops_o), PyObject_IsTrue(multiple_o))) {
35943592
igraphmodule_handle_igraph_error();
35953593
igraph_matrix_destroy(&pref_matrix);
35963594
igraph_vector_int_destroy(&block_sizes);
@@ -14760,13 +14758,15 @@ struct PyMethodDef igraphmodule_Graph_methods[] = {
1476014758
"vertex pair is evaluated and an edge is created between them with a\n"
1476114759
"probability depending on the types of the vertices involved. The\n"
1476214760
"probabilities are taken from the preference matrix.\n\n"
14763-
"@param n: the number of vertices in the graph\n"
1476414761
"@param pref_matrix: matrix giving the connection probabilities for\n"
14765-
" different vertex types.\n"
14762+
" different vertex types (when C{multiple} = C{False}) or the expected\n"
14763+
" number of edges between a vertex pair (when C{multiple} = C{True}).\n"
1476614764
"@param block_sizes: list giving the number of vertices in each block; must\n"
1476714765
" sum up to I{n}.\n"
1476814766
"@param directed: whether to generate a directed graph.\n"
14769-
"@param loops: whether loop edges are allowed.\n"},
14767+
"@param loops: whether loop edges are allowed.\n"
14768+
"@param multiple: whether multiple edges are allowed.\n"
14769+
},
1477014770

1477114771
// interface to igraph_star
1477214772
{"Star", (PyCFunction) igraphmodule_Graph_Star,
@@ -16279,18 +16279,11 @@ struct PyMethodDef igraphmodule_Graph_methods[] = {
1627916279
},
1628016280

1628116281
/* interface to igraph_rewire */
16282-
{"rewire", (PyCFunction) igraphmodule_Graph_rewire,
16282+
{"_rewire", (PyCFunction) igraphmodule_Graph_rewire,
1628316283
METH_VARARGS | METH_KEYWORDS,
16284-
"rewire(n=None, allowed_edge_types=\"simple\")\n--\n\n"
16285-
"Randomly rewires the graph while preserving the degree distribution.\n\n"
16286-
"The rewiring is done \"in-place\", so the original graph will be modified.\n"
16287-
"If you want to preserve the original graph, use the L{copy} method before\n"
16288-
"rewiring.\n\n"
16289-
"@param n: the number of rewiring trials. The default is 10 times the number\n"
16290-
" of edges.\n"
16291-
"@param allowed_edge_types: the rewiring algorithm to use. It can either be C{\"simple\"} or\n"
16292-
" C{\"loops\"}; the former does not create or destroy loop edges while the\n"
16293-
" latter does.\n"},
16284+
"_rewire(n=None, allowed_edge_types=\"simple\")\n--\n\n"
16285+
"Internal function, undocumented.\n\n"
16286+
"@see: Graph.rewire()\n\n"},
1629416287

1629516288
/* interface to igraph_rewire_edges */
1629616289
{"rewire_edges", (PyCFunction) igraphmodule_Graph_rewire_edges,

src/igraph/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@
239239
intersection,
240240
operator_method_registry as _operator_method_registry,
241241
)
242+
from igraph.rewiring import _rewire
242243
from igraph.seq import EdgeSeq, VertexSeq, _add_proxy_methods
243244
from igraph.statistics import (
244245
FittedPowerLaw,
@@ -632,6 +633,7 @@ def es(self):
632633
disjoint_union = _operator_method_registry["disjoint_union"]
633634
union = _operator_method_registry["union"]
634635
intersection = _operator_method_registry["intersection"]
636+
rewire = _rewire
635637

636638
#############################################
637639
# Adjacency/incidence
@@ -1152,6 +1154,7 @@ def write(graph, filename, *args, **kwds):
11521154
_cohesive_blocks,
11531155
_connected_components,
11541156
_add_proxy_methods,
1157+
_rewire,
11551158
)
11561159

11571160
# Re-export from _igraph for API docs
@@ -1267,8 +1270,6 @@ def write(graph, filename, *args, **kwds):
12671270
"GET_ADJACENCY_UPPER",
12681271
"IN",
12691272
"OUT",
1270-
"REWIRING_SIMPLE",
1271-
"REWIRING_SIMPLE_LOOPS",
12721273
"STAR_IN",
12731274
"STAR_MUTUAL",
12741275
"STAR_OUT",

src/igraph/rewiring.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from igraph._igraph import GraphBase
2+
3+
from .utils import deprecated
4+
5+
__all__ = ("_rewire", )
6+
7+
8+
def _rewire(graph, n=None, allowed_edge_types="simple", *, mode=None):
9+
"""Randomly rewires the graph while preserving the degree distribution.
10+
11+
The rewiring is done \"in-place\", so the original graph will be modified.
12+
If you want to preserve the original graph, use the L{copy} method before
13+
rewiring.
14+
15+
@param n: the number of rewiring trials. The default is 10 times the number
16+
of edges.
17+
@param allowed_edge_types: the rewiring algorithm to use. It can either be
18+
C{"simple"} or C{"loops"}; the former does not create or destroy
19+
loop edges while the latter does.
20+
"""
21+
if mode is not None:
22+
deprecated("The 'mode' keyword argument is deprecated, use 'allowed_edge_types' instead")
23+
allowed_edge_types = mode
24+
25+
return GraphBase._rewire(graph, n, allowed_edge_types)

tests/test_generators.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,8 @@ def testLattice(self):
429429

430430
def testSBM(self):
431431
pref_matrix = [[0.5, 0, 0], [0, 0, 0.5], [0, 0.5, 0]]
432-
n = 60
433432
types = [20, 20, 20]
434-
g = Graph.SBM(n, pref_matrix, types)
433+
g = Graph.SBM(pref_matrix, types)
435434

436435
# Simple smoke tests for the expected structure of the graph
437436
self.assertTrue(g.is_simple())
@@ -441,21 +440,19 @@ def testSBM(self):
441440
self.assertTrue(not any(e.source // 20 == e.target // 20 for e in g2.es))
442441

443442
# Check loops argument
444-
g = Graph.SBM(n, pref_matrix, types, loops=True)
443+
g = Graph.SBM(pref_matrix, types, loops=True)
445444
self.assertFalse(g.is_simple())
446445
self.assertTrue(sum(g.is_loop()) > 0)
447446

448447
# Check directedness
449-
g = Graph.SBM(n, pref_matrix, types, directed=True)
448+
g = Graph.SBM(pref_matrix, types, directed=True)
450449
self.assertTrue(g.is_directed())
451450
self.assertTrue(sum(g.is_mutual()) < g.ecount())
452451
self.assertTrue(sum(g.is_loop()) == 0)
453452

454453
# Check error conditions
455-
self.assertRaises(ValueError, Graph.SBM, -1, pref_matrix, types)
456-
self.assertRaises(InternalError, Graph.SBM, 61, pref_matrix, types)
457454
pref_matrix[0][1] = 0.7
458-
self.assertRaises(InternalError, Graph.SBM, 60, pref_matrix, types)
455+
self.assertRaises(InternalError, Graph.SBM, pref_matrix, types)
459456

460457
def testTriangularLattice(self):
461458
g = Graph.Triangular_Lattice([2, 2])

0 commit comments

Comments
 (0)