Skip to content

Commit a1a164b

Browse files
committed
refactor!: adapt Graph.SBM() interface to changes in C core
1 parent 0fce500 commit a1a164b

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

src/_igraph/graphobject.c

Lines changed: 13 additions & 15 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);
@@ -14754,19 +14752,19 @@ struct PyMethodDef igraphmodule_Graph_methods[] = {
1475414752
METH_VARARGS | METH_CLASS | METH_KEYWORDS,
1475514753
"SBM(n, pref_matrix, block_sizes, directed=False, loops=False)\n--\n\n"
1475614754
"Generates a graph based on a stochastic block model.\n\n"
14757-
"A given number of vertices are generated. Every vertex is assigned to a\n"
14758-
"vertex type according to the given block sizes. Vertices of the same\n"
14755+
"Every vertex is assigned to a vertex type according to the given block\n"
14756+
"sizes, which also determine the total vertex count. Vertices of the same\n"
1475914757
"type will be assigned consecutive vertex IDs. Finally, every\n"
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"
14764-
"@param pref_matrix: matrix giving the connection probabilities for\n"
14765-
" different vertex types.\n"
14761+
"@param pref_matrix: matrix giving the connection probabilities (or expected\n"
14762+
" edge multiplicities for multigraphs) between different vertex types.\n"
1476614763
"@param block_sizes: list giving the number of vertices in each block; must\n"
1476714764
" sum up to I{n}.\n"
1476814765
"@param directed: whether to generate a directed graph.\n"
14769-
"@param loops: whether loop edges are allowed.\n"},
14766+
"@param loops: whether loop edges are allowed.\n"
14767+
"@param multiple: whether multi-edges are allowed.\n"},
1477014768

1477114769
// interface to igraph_star
1477214770
{"Star", (PyCFunction) igraphmodule_Graph_Star,

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)