-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathigraphmodule.c
More file actions
1216 lines (1035 loc) · 41.7 KB
/
igraphmodule.c
File metadata and controls
1216 lines (1035 loc) · 41.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim:set ts=2 sw=2 sts=2 et: */
/*
IGraph library.
Copyright (C) 2006-2023 Tamas Nepusz <ntamas@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "preamble.h"
#include <igraph.h>
#include "arpackobject.h"
#include "attributes.h"
#include "bfsiter.h"
#include "dfsiter.h"
#include "common.h"
#include "convert.h"
#include "edgeobject.h"
#include "edgeseqobject.h"
#include "error.h"
#include "graphobject.h"
#include "pyhelpers.h"
#include "random.h"
#include "vertexobject.h"
#include "vertexseqobject.h"
#include "operators.h"
#define IGRAPH_MODULE
#include "igraphmodule_api.h"
/**
* \defgroup python_interface Python module implementation
* \brief Functions implementing a Python interface to \a igraph
*
* These functions provide a way to access \a igraph functions from Python.
* It should be of interest of \a igraph developers only. Classes, functions
* and methods exposed to Python are still to be documented. Until it is done,
* just type the following to get help about \a igraph functions in Python
* (assuming you have \c igraph.so somewhere in your Python library path):
*
* \verbatim
import igraph
help(igraph)
help(igraph.Graph)
\endverbatim
*
* Most of the functions provided here share the same calling conventions
* (which are determined by the Python/C API). Since the role of the
* arguments are the same across many functions, I won't explain them
* everywhere, just give a quick overview of the common argument names here.
*
* \param self the Python igraph.Graph object the method is working on
* \param args pointer to the Python tuple containing the arguments
* \param kwds pointer to the Python hash containing the keyword parameters
* \param type the type object of a Python igraph.Graph object. Used usually
* in constructors and class methods.
*
* Any arguments not documented here should be mentioned at the documentation
* of the appropriate method.
*
* The functions which implement a Python method always return a pointer to
* a \c PyObject. According to Python conventions, this is \c NULL if and
* only if an exception was thrown by the method (or any of the functions
* it has called). When I explain the return value of a function which
* provides interface to an \a igraph function, I won't cover the case of
* returning a \c NULL value, because this is the same for every such method.
* The conclusion is that a method can return \c NULL even if I don't state
* it explicitly.
*
* Also please take into consideration that I'm documenting the C calls
* with the abovementioned parameters here, and \em not the Python methods
* which are presented to the user using the Python interface of \a igraph.
* If you are looking for the documentation of the classes, methods and
* functions exposed to Python, please use the \c help calls from Python
* or use \c pydoc to generate a formatted version.
*
* \section weakrefs The usage of weak references in the Python interface
*
* Many classes implemented in the Python interface (e.g. VertexSeq, Vertex...)
* use weak references to keep track of the graph they are referencing to.
* The use of weak references is twofold:
*
* -# If we assign a VertexSeq or a Vertex of a given graph to a local
* variable and then destroy the graph, real references keep the graph
* alive and do not return the memory back to Python.
* -# If we use real references, a Graph object will hold a reference
* to its VertexSeq (because we don't want to allocate a new VertexSeq
* object for the same graph every time it is requested), and the
* VertexSeq will also hold a reference to the Graph. This is a circular
* reference. Python does not reclaim the memory occupied by the Graph
* back when the Graph is destroyed, because the VertexSeq is holding a
* reference to it. Similarly, VertexSeq doesn't get freed because the
* Graph is holding a reference to it. These situations can only be
* resolved by the Python garbage collector which is invoked at regular
* intervals. Unfortunately, the garbage collector refuses to break
* circular references and free the objects participating in the circle
* when any of the objects has a \c __del__ method. In this case,
* \c igraph.Graph has one (which frees the underlying \c igraph_t
* graph), therefore our graphs never get freed when we use real
* references.
*/
/**
* Whether the module was initialized already
*/
static igraph_bool_t igraphmodule_initialized = false;
/**
* Module-specific global variables
*/
struct module_state {
PyObject* progress_handler;
PyObject* status_handler;
};
static struct module_state _state = { 0, 0 };
#define GETSTATE(m) (&_state)
static int igraphmodule_traverse(PyObject *m, visitproc visit, void* arg) {
Py_VISIT(GETSTATE(m)->progress_handler);
Py_VISIT(GETSTATE(m)->status_handler);
return 0;
}
static int igraphmodule_clear(PyObject *m) {
Py_CLEAR(GETSTATE(m)->progress_handler);
Py_CLEAR(GETSTATE(m)->status_handler);
return 0;
}
static igraph_bool_t igraphmodule_igraph_interrupt_hook() {
return PyErr_CheckSignals();
}
igraph_error_t igraphmodule_igraph_progress_hook(const char* message, igraph_real_t percent,
void* data) {
PyObject* progress_handler = GETSTATE(0)->progress_handler;
PyObject *result;
if (progress_handler) {
if (PyCallable_Check(progress_handler)) {
result = PyObject_CallFunction(progress_handler, "sd", message, (double)percent);
if (result) {
Py_DECREF(result);
} else {
return IGRAPH_INTERRUPTED;
}
}
}
return IGRAPH_SUCCESS;
}
igraph_error_t igraphmodule_igraph_status_hook(const char* message, void*data) {
PyObject* status_handler = GETSTATE(0)->status_handler;
if (status_handler) {
PyObject *result;
if (PyCallable_Check(status_handler)) {
result = PyObject_CallFunction(status_handler, "s", message);
if (result)
Py_DECREF(result);
else
return IGRAPH_INTERRUPTED;
}
}
return IGRAPH_SUCCESS;
}
PyObject* igraphmodule_set_progress_handler(PyObject* self, PyObject* o) {
PyObject* progress_handler;
if (!PyCallable_Check(o) && o != Py_None) {
PyErr_SetString(PyExc_TypeError, "Progress handler must be callable.");
return NULL;
}
progress_handler = GETSTATE(self)->progress_handler;
if (o == progress_handler)
Py_RETURN_NONE;
Py_XDECREF(progress_handler);
if (o == Py_None)
o = NULL;
Py_XINCREF(o);
GETSTATE(self)->progress_handler=o;
Py_RETURN_NONE;
}
PyObject* igraphmodule_set_status_handler(PyObject* self, PyObject* o) {
PyObject* status_handler;
if (!PyCallable_Check(o) && o != Py_None) {
PyErr_SetString(PyExc_TypeError, "Status handler must be callable.");
return NULL;
}
status_handler = GETSTATE(self)->status_handler;
if (o == status_handler)
Py_RETURN_NONE;
Py_XDECREF(status_handler);
if (o == Py_None) {
o = NULL;
}
Py_XINCREF(o);
GETSTATE(self)->status_handler = o;
Py_RETURN_NONE;
}
PyObject* igraphmodule_align_layout(PyObject* self, PyObject* args, PyObject* kwds) {
static char* kwlist[] = {"graph", "layout", NULL};
PyObject *graph_o, *layout_o;
PyObject *res;
igraph_t *graph;
igraph_matrix_t layout;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &graph_o, &layout_o)) {
return NULL;
}
if (igraphmodule_PyObject_to_igraph_t(graph_o, &graph)) {
return NULL;
}
if (igraphmodule_PyObject_to_matrix_t(layout_o, &layout, "layout")) {
return NULL;
}
if (igraph_layout_align(graph, &layout)) {
igraphmodule_handle_igraph_error();
igraph_matrix_destroy(&layout);
return NULL;
}
res = igraphmodule_matrix_t_to_PyList(&layout, IGRAPHMODULE_TYPE_FLOAT);
igraph_matrix_destroy(&layout);
return res;
}
PyObject* igraphmodule_convex_hull(PyObject* self, PyObject* args, PyObject* kwds) {
static char* kwlist[] = {"vs", "coords", NULL};
PyObject *vs, *o, *o1 = 0, *o2 = 0, *o1_float, *o2_float, *coords = Py_False;
igraph_matrix_t mtrx;
igraph_vector_int_t result;
igraph_matrix_t resmat;
Py_ssize_t no_of_nodes, i;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|O", kwlist, &PyList_Type, &vs, &coords))
return NULL;
no_of_nodes = PyList_Size(vs);
if (igraph_matrix_init(&mtrx, no_of_nodes, 2)) {
igraphmodule_handle_igraph_error();
return NULL;
}
for (i = 0; i < no_of_nodes; i++) {
o = PyList_GetItem(vs, i);
if (PySequence_Check(o)) {
if (PySequence_Size(o) >= 2) {
o1 = PySequence_GetItem(o, 0);
if (!o1) {
igraph_matrix_destroy(&mtrx);
return NULL;
}
o2 = PySequence_GetItem(o, 1);
if (!o2) {
Py_DECREF(o1);
igraph_matrix_destroy(&mtrx);
return NULL;
}
if (PySequence_Size(o) > 2) {
PY_IGRAPH_WARN("vertex with more than 2 coordinates found, considering only the first 2");
}
} else {
PyErr_SetString(PyExc_TypeError, "vertex with less than 2 coordinates found");
igraph_matrix_destroy(&mtrx);
return NULL;
}
} else {
PyErr_SetString(PyExc_TypeError, "convex_hull() must receive a list of indexable sequences");
igraph_matrix_destroy(&mtrx);
return NULL;
}
if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) {
PyErr_SetString(PyExc_TypeError, "vertex coordinates must be numeric");
Py_DECREF(o2);
Py_DECREF(o1);
igraph_matrix_destroy(&mtrx);
return NULL;
}
o1_float = PyNumber_Float(o1);
if (!o1_float) {
Py_DECREF(o2);
Py_DECREF(o1);
igraph_matrix_destroy(&mtrx);
return NULL;
}
Py_DECREF(o1);
o2_float = PyNumber_Float(o2);
if (!o2_float) {
Py_DECREF(o2);
igraph_matrix_destroy(&mtrx);
return NULL;
}
Py_DECREF(o2);
MATRIX(mtrx, i, 0) = PyFloat_AsDouble(o1_float);
MATRIX(mtrx, i, 1) = PyFloat_AsDouble(o2_float);
Py_DECREF(o1_float);
Py_DECREF(o2_float);
}
if (!PyObject_IsTrue(coords)) {
if (igraph_vector_int_init(&result, 0)) {
igraphmodule_handle_igraph_error();
igraph_matrix_destroy(&mtrx);
return NULL;
}
if (igraph_convex_hull_2d(&mtrx, &result, 0)) {
igraphmodule_handle_igraph_error();
igraph_matrix_destroy(&mtrx);
igraph_vector_int_destroy(&result);
return NULL;
}
o = igraphmodule_vector_int_t_to_PyList(&result);
igraph_vector_int_destroy(&result);
} else {
if (igraph_matrix_init(&resmat, 0, 0)) {
igraphmodule_handle_igraph_error();
igraph_matrix_destroy(&mtrx);
return NULL;
}
if (igraph_convex_hull_2d(&mtrx, 0, &resmat)) {
igraphmodule_handle_igraph_error();
igraph_matrix_destroy(&mtrx);
igraph_matrix_destroy(&resmat);
return NULL;
}
o = igraphmodule_matrix_t_to_PyList(&resmat, IGRAPHMODULE_TYPE_FLOAT);
igraph_matrix_destroy(&resmat);
}
igraph_matrix_destroy(&mtrx);
return o;
}
PyObject* igraphmodule_community_to_membership(PyObject *self,
PyObject *args, PyObject *kwds) {
static char* kwlist[] = { "merges", "nodes", "steps", "return_csize", NULL };
PyObject *merges_o, *return_csize = Py_False, *result_o;
igraph_matrix_int_t merges;
igraph_vector_int_t result, csize, *csize_p = 0;
Py_ssize_t nodes, steps;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "Onn|O", kwlist,
&merges_o, &nodes, &steps, &return_csize)) return NULL;
if (igraphmodule_PyObject_to_matrix_int_t_with_minimum_column_count(merges_o, &merges, 2, "merges")) {
return NULL;
}
CHECK_SSIZE_T_RANGE(nodes, "number of nodes");
CHECK_SSIZE_T_RANGE(steps, "number of steps");
if (igraph_vector_int_init(&result, nodes)) {
igraphmodule_handle_igraph_error();
igraph_matrix_int_destroy(&merges);
return NULL;
}
if (PyObject_IsTrue(return_csize)) {
igraph_vector_int_init(&csize, 0);
csize_p = &csize;
}
if (igraph_community_to_membership(&merges, nodes, steps, &result, csize_p)) {
igraphmodule_handle_igraph_error();
igraph_vector_int_destroy(&result);
if (csize_p) igraph_vector_int_destroy(csize_p);
igraph_matrix_int_destroy(&merges);
return NULL;
}
igraph_matrix_int_destroy(&merges);
result_o = igraphmodule_vector_int_t_to_PyList(&result);
igraph_vector_int_destroy(&result);
if (csize_p) {
PyObject* csize_o = igraphmodule_vector_int_t_to_PyList(csize_p);
igraph_vector_int_destroy(csize_p);
if (csize_o) return Py_BuildValue("NN", result_o, csize_o);
Py_DECREF(result_o);
return NULL;
}
return result_o;
}
PyObject* igraphmodule_compare_communities(PyObject *self,
PyObject *args, PyObject *kwds) {
static char* kwlist[] = { "comm1", "comm2", "method", NULL };
PyObject *comm1_o, *comm2_o, *method_o = Py_None;
igraph_vector_int_t comm1, comm2;
igraph_community_comparison_t method = IGRAPH_COMMCMP_VI;
igraph_real_t result;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O", kwlist,
&comm1_o, &comm2_o, &method_o)) {
return NULL;
}
if (igraphmodule_PyObject_to_community_comparison_t(method_o, &method)) {
return NULL;
}
if (igraphmodule_PyObject_to_vector_int_t(comm1_o, &comm1)) {
return NULL;
}
if (igraphmodule_PyObject_to_vector_int_t(comm2_o, &comm2)) {
igraph_vector_int_destroy(&comm1);
return NULL;
}
if (igraph_compare_communities(&comm1, &comm2, &result, method)) {
igraphmodule_handle_igraph_error();
igraph_vector_int_destroy(&comm1);
igraph_vector_int_destroy(&comm2);
return NULL;
}
igraph_vector_int_destroy(&comm1);
igraph_vector_int_destroy(&comm2);
return igraphmodule_real_t_to_PyObject(result, IGRAPHMODULE_TYPE_FLOAT);
}
PyObject* igraphmodule_is_degree_sequence(PyObject *self,
PyObject *args, PyObject *kwds) {
static char* kwlist[] = { "out_deg", "in_deg", NULL };
PyObject *out_deg_o = 0, *in_deg_o = 0;
igraph_vector_int_t out_deg, in_deg;
igraph_bool_t is_directed, result;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist,
&out_deg_o, &in_deg_o))
return NULL;
is_directed = (in_deg_o != 0 && in_deg_o != Py_None);
if (igraphmodule_PyObject_to_vector_int_t(out_deg_o, &out_deg))
return NULL;
if (is_directed && igraphmodule_PyObject_to_vector_int_t(in_deg_o, &in_deg)) {
igraph_vector_int_destroy(&out_deg);
return NULL;
}
if (igraph_is_graphical(&out_deg, is_directed ? &in_deg : 0, IGRAPH_LOOPS_SW | IGRAPH_MULTI_SW, &result)) {
igraphmodule_handle_igraph_error();
igraph_vector_int_destroy(&out_deg);
if (is_directed)
igraph_vector_int_destroy(&in_deg);
return NULL;
}
igraph_vector_int_destroy(&out_deg);
if (is_directed)
igraph_vector_int_destroy(&in_deg);
if (result)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
PyObject* igraphmodule_is_graphical_degree_sequence(PyObject *self,
PyObject *args, PyObject *kwds) {
static char* kwlist[] = { "out_deg", "in_deg", NULL };
PyObject *out_deg_o = 0, *in_deg_o = 0;
igraph_vector_int_t out_deg, in_deg;
igraph_bool_t is_directed, result;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist,
&out_deg_o, &in_deg_o))
return NULL;
is_directed = (in_deg_o != 0 && in_deg_o != Py_None);
if (igraphmodule_PyObject_to_vector_int_t(out_deg_o, &out_deg))
return NULL;
if (is_directed && igraphmodule_PyObject_to_vector_int_t(in_deg_o, &in_deg)) {
igraph_vector_int_destroy(&out_deg);
return NULL;
}
if (igraph_is_graphical(&out_deg, is_directed ? &in_deg : 0, IGRAPH_SIMPLE_SW, &result)) {
igraphmodule_handle_igraph_error();
igraph_vector_int_destroy(&out_deg);
if (is_directed)
igraph_vector_int_destroy(&in_deg);
return NULL;
}
igraph_vector_int_destroy(&out_deg);
if (is_directed)
igraph_vector_int_destroy(&in_deg);
if (result)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
static PyObject* igraphmodule_i_is_graphical_or_bigraphical(
PyObject *self, PyObject *args, PyObject *kwds, igraph_bool_t is_bigraphical
);
PyObject* igraphmodule_is_graphical(PyObject *self, PyObject *args, PyObject *kwds) {
return igraphmodule_i_is_graphical_or_bigraphical(self, args, kwds, /* is_bigraphical = */ false);
}
PyObject* igraphmodule_is_bigraphical(PyObject *self, PyObject *args, PyObject *kwds) {
return igraphmodule_i_is_graphical_or_bigraphical(self, args, kwds, /* is_bigraphical = */ true);
}
static PyObject* igraphmodule_i_is_graphical_or_bigraphical(
PyObject *self, PyObject *args, PyObject *kwds, igraph_bool_t is_bigraphical
) {
static char* kwlist_graphical[] = { "out_deg", "in_deg", "loops", "multiple", NULL };
static char* kwlist_bigraphical[] = { "degrees1", "degrees2", "multiple", NULL };
PyObject *out_deg_o = 0, *in_deg_o = 0;
PyObject *loops = Py_False, *multiple = Py_False;
igraph_vector_int_t out_deg, in_deg;
igraph_bool_t is_directed, result;
int allowed_edge_types;
igraph_error_t retval;
if (is_bigraphical) {
if (!PyArg_ParseTupleAndKeywords(
args, kwds, "OO|O", kwlist_bigraphical,
&out_deg_o, &in_deg_o, &multiple
)) {
return NULL;
}
} else {
if (!PyArg_ParseTupleAndKeywords(
args, kwds, "O|OOO", kwlist_graphical,
&out_deg_o, &in_deg_o, &loops, &multiple
)) {
return NULL;
}
}
is_directed = (in_deg_o != 0 && in_deg_o != Py_None) || is_bigraphical;
if (igraphmodule_PyObject_to_vector_int_t(out_deg_o, &out_deg))
return NULL;
if (is_directed && igraphmodule_PyObject_to_vector_int_t(in_deg_o, &in_deg)) {
igraph_vector_int_destroy(&out_deg);
return NULL;
}
allowed_edge_types = IGRAPH_SIMPLE_SW;
if (PyObject_IsTrue(loops)) {
allowed_edge_types |= IGRAPH_LOOPS_SW;
}
if (PyObject_IsTrue(multiple)) {
allowed_edge_types |= IGRAPH_MULTI_SW;
}
retval = is_bigraphical
? igraph_is_bigraphical(&out_deg, is_directed ? &in_deg : 0, allowed_edge_types, &result)
: igraph_is_graphical(&out_deg, is_directed ? &in_deg : 0, allowed_edge_types, &result);
if (retval) {
igraphmodule_handle_igraph_error();
igraph_vector_int_destroy(&out_deg);
if (is_directed) {
igraph_vector_int_destroy(&in_deg);
}
return NULL;
}
igraph_vector_int_destroy(&out_deg);
if (is_directed) {
igraph_vector_int_destroy(&in_deg);
}
if (result)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
PyObject* igraphmodule_power_law_fit(PyObject *self, PyObject *args, PyObject *kwds) {
static char* kwlist[] = { "data", "xmin", "force_continuous", "p_precision", NULL };
PyObject *data_o, *force_continuous_o = Py_False;
igraph_vector_t data;
igraph_plfit_result_t result;
double xmin = -1, p_precision = 0.01;
igraph_real_t p;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|dOd", kwlist, &data_o,
&xmin, &force_continuous_o, &p_precision))
return NULL;
if (igraphmodule_PyObject_float_to_vector_t(data_o, &data))
return NULL;
if (igraph_power_law_fit(&data, &result, xmin, PyObject_IsTrue(force_continuous_o))) {
igraphmodule_handle_igraph_error();
igraph_vector_destroy(&data);
return NULL;
}
if (igraph_plfit_result_calculate_p_value(&result, &p, p_precision)) {
igraphmodule_handle_igraph_error();
igraph_vector_destroy(&data);
return NULL;
}
igraph_vector_destroy(&data);
return Py_BuildValue("Oddddd", result.continuous ? Py_True : Py_False,
result.alpha, result.xmin, result.L, result.D, (double) p);
}
PyObject* igraphmodule_split_join_distance(PyObject *self,
PyObject *args, PyObject *kwds) {
static char* kwlist[] = { "comm1", "comm2", NULL };
PyObject *comm1_o, *comm2_o;
igraph_vector_int_t comm1, comm2;
igraph_int_t distance12, distance21;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist,
&comm1_o, &comm2_o))
return NULL;
if (igraphmodule_PyObject_to_vector_int_t(comm1_o, &comm1)) {
return NULL;
}
if (igraphmodule_PyObject_to_vector_int_t(comm2_o, &comm2)) {
igraph_vector_int_destroy(&comm1);
return NULL;
}
if (igraph_split_join_distance(&comm1, &comm2, &distance12, &distance21)) {
igraphmodule_handle_igraph_error();
igraph_vector_int_destroy(&comm1);
igraph_vector_int_destroy(&comm2);
return NULL;
}
igraph_vector_int_destroy(&comm1);
igraph_vector_int_destroy(&comm2);
/* sizeof(Py_ssize_t) is most likely the same as sizeof(igraph_int_t),
* but even if it isn't, we cast explicitly so we are safe */
return Py_BuildValue("nn", (Py_ssize_t)distance12, (Py_ssize_t)distance21);
}
/** \ingroup python_interface_graph
* \brief Compute weights for Uniform Manifold Approximation and Projection (UMAP)
* \return the weights given that graph
* \sa igraph_umap_compute_weights
*/
PyObject *igraphmodule_umap_compute_weights(
PyObject *self, PyObject * args, PyObject * kwds)
{
static char *kwlist[] = { "graph", "dist", NULL };
igraph_vector_t *dist = 0;
igraph_vector_t weights;
PyObject *dist_o = Py_None, *graph_o = Py_None;
PyObject *result_o;
igraphmodule_GraphObject * graph;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O", kwlist, igraphmodule_GraphType, &graph_o, &dist_o))
return NULL;
/* Initialize distances */
if (dist_o != Py_None) {
dist = (igraph_vector_t*)malloc(sizeof(igraph_vector_t));
if (!dist) {
PyErr_NoMemory();
return NULL;
}
if (igraphmodule_PyObject_to_vector_t(dist_o, dist, 0)) {
free(dist);
return NULL;
}
}
/* Extract graph from Python object */
graph = (igraphmodule_GraphObject*)graph_o;
/* Initialize weights */
if (igraph_vector_init(&weights, 0)) {
igraph_vector_destroy(dist); free(dist);
PyErr_NoMemory();
return NULL;
}
/* Call the function */
if (igraph_layout_umap_compute_weights(&graph->g, dist, &weights)) {
igraph_vector_destroy(&weights);
igraph_vector_destroy(dist); free(dist);
PyErr_NoMemory();
return NULL;
}
igraph_vector_destroy(dist); free(dist);
/* Convert output to Python list */
result_o = igraphmodule_vector_t_to_PyList(&weights, IGRAPHMODULE_TYPE_FLOAT);
igraph_vector_destroy(&weights);
return (PyObject *) result_o;
}
#define LOCALE_CAPSULE_TYPE "igraph._igraph.locale_capsule"
void igraphmodule__destroy_locale_capsule(PyObject *capsule) {
igraph_safelocale_t* loc = (igraph_safelocale_t*) PyCapsule_GetPointer(capsule, LOCALE_CAPSULE_TYPE);
if (loc) {
PyMem_Free(loc);
}
}
PyObject* igraphmodule__enter_safelocale(PyObject* self, PyObject* Py_UNUSED(_null)) {
igraph_safelocale_t* loc;
PyObject* capsule;
loc = PyMem_Malloc(sizeof(loc));
if (loc == NULL) {
PyErr_NoMemory();
return NULL;
}
capsule = PyCapsule_New(loc, LOCALE_CAPSULE_TYPE, igraphmodule__destroy_locale_capsule);
if (capsule == NULL) {
return NULL;
}
if (igraph_enter_safelocale(loc)) {
Py_DECREF(capsule);
igraphmodule_handle_igraph_error();
}
return capsule;
}
PyObject* igraphmodule__exit_safelocale(PyObject *self, PyObject *capsule) {
igraph_safelocale_t* loc;
if (!PyCapsule_IsValid(capsule, LOCALE_CAPSULE_TYPE)) {
PyErr_SetString(PyExc_TypeError, "expected locale capsule");
return NULL;
}
loc = (igraph_safelocale_t*) PyCapsule_GetPointer(capsule, LOCALE_CAPSULE_TYPE);
if (loc != NULL) {
igraph_exit_safelocale(loc);
}
Py_RETURN_NONE;
}
/** \ingroup python_interface
* \brief Method table for the igraph Python module
*/
static PyMethodDef igraphmodule_methods[] =
{
{"community_to_membership", (PyCFunction)igraphmodule_community_to_membership,
METH_VARARGS | METH_KEYWORDS,
"community_to_membership(merges, nodes, steps, return_csize=False)\n--\n\n"
},
{"_compare_communities", (PyCFunction)igraphmodule_compare_communities,
METH_VARARGS | METH_KEYWORDS,
"_compare_communities(comm1, comm2, method=\"vi\")\n--\n\n"
},
{"_power_law_fit", (PyCFunction)igraphmodule_power_law_fit,
METH_VARARGS | METH_KEYWORDS,
"_power_law_fit(data, xmin=-1, force_continuous=False, p_precision=0.01)\n--\n\n"
},
{"_align_layout", (PyCFunction)igraphmodule_align_layout,
METH_VARARGS | METH_KEYWORDS,
"_align_layout(graph, layout)\n--\n\n"
},
{"convex_hull", (PyCFunction)igraphmodule_convex_hull,
METH_VARARGS | METH_KEYWORDS,
"convex_hull(vs, coords=False)\n--\n\n"
"Calculates the convex hull of a given point set.\n\n"
"@param vs: the point set as a list of lists\n"
"@param coords: if C{True}, the function returns the\n"
" coordinates of the corners of the convex hull polygon,\n"
" otherwise returns the corner indices.\n"
"@return: either the hull's corner coordinates or the point\n"
" indices corresponding to them, depending on the C{coords}\n"
" parameter."
},
{"is_degree_sequence", (PyCFunction)igraphmodule_is_degree_sequence,
METH_VARARGS | METH_KEYWORDS,
"is_degree_sequence(out_deg, in_deg=None)\n--\n\n"
"Deprecated since 0.9 in favour of L{is_graphical()}.\n\n"
"Returns whether a list of degrees can be a degree sequence of some graph.\n\n"
"Note that it is not required for the graph to be simple; in other words,\n"
"this function may return C{True} for degree sequences that can be realized\n"
"using one or more multiple or loop edges only.\n\n"
"In particular, this function checks whether\n\n"
" - all the degrees are non-negative\n"
" - for undirected graphs, the sum of degrees are even\n"
" - for directed graphs, the two degree sequences are of the same length and\n"
" equal sums\n\n"
"@param out_deg: the list of degrees. For directed graphs, this list must\n"
" contain the out-degrees of the vertices.\n"
"@param in_deg: the list of in-degrees for directed graphs. This parameter\n"
" must be C{None} for undirected graphs.\n"
"@return: C{True} if there exists some graph that can realize the given degree\n"
" sequence, C{False} otherwise.\n"
},
{"is_bigraphical", (PyCFunction)igraphmodule_is_bigraphical,
METH_VARARGS | METH_KEYWORDS,
"is_bigraphical(degrees1, degrees2, multiple=False)\n--\n\n"
"Returns whether two sequences of integers can be the degree sequences of a\n"
"bipartite graph.\n\n"
"The bipartite graph may or may not have multiple edges, depending\n"
"on the allowed edge types in the remaining arguments.\n\n"
"@param degrees1: the list of degrees in the first partition.\n"
"@param degrees2: the list of degrees in the second partition.\n"
"@param multiple: whether multiple edges are allowed.\n"
"@return: C{True} if there exists some bipartite graph that can realize the\n"
" given degree sequences with or without multiple edges, C{False} otherwise.\n"
},
{"is_graphical", (PyCFunction)igraphmodule_is_graphical,
METH_VARARGS | METH_KEYWORDS,
"is_graphical(out_deg, in_deg=None, loops=False, multiple=False)\n--\n\n"
"Returns whether a list of degrees can be a degree sequence of some graph,\n"
"with or without multiple and loop edges, depending on the allowed edge types\n"
"in the remaining arguments.\n\n"
"@param out_deg: the list of degrees. For directed graphs, this list must\n"
" contain the out-degrees of the vertices.\n"
"@param in_deg: the list of in-degrees for directed graphs. This parameter\n"
" must be C{None} for undirected graphs.\n"
"@param loops: whether loop edges are allowed.\n"
"@param multiple: whether multiple edges are allowed.\n"
"@return: C{True} if there exists some graph that can realize the given\n"
" degree sequence with the given edge types, C{False} otherwise.\n"
},
{"is_graphical_degree_sequence", (PyCFunction)igraphmodule_is_graphical_degree_sequence,
METH_VARARGS | METH_KEYWORDS,
"is_graphical_degree_sequence(out_deg, in_deg=None)\n--\n\n"
"Deprecated since 0.9 in favour of L{is_graphical()}.\n\n"
"Returns whether a list of degrees can be a degree sequence of some simple graph.\n\n"
"Note that it is required for the graph to be simple; in other words,\n"
"this function will return C{False} for degree sequences that cannot be realized\n"
"without using one or more multiple or loop edges.\n\n"
"@param out_deg: the list of degrees. For directed graphs, this list must\n"
" contain the out-degrees of the vertices.\n"
"@param in_deg: the list of in-degrees for directed graphs. This parameter\n"
" must be C{None} for undirected graphs.\n"
"@return: C{True} if there exists some simple graph that can realize the given\n"
" degree sequence, C{False} otherwise.\n"
},
{"umap_compute_weights", (PyCFunction)igraphmodule_umap_compute_weights,
METH_VARARGS | METH_KEYWORDS,
"umap_compute_weights(graph, dist)\n--\n\n"
"Compute undirected UMAP weights from directed distance graph.\n"
"UMAP is a layout algorithm that usually takes as input a directed\n"
"distance graph, for instance a k nearest neighbor graph based on Euclidean\n"
"distance between points in a vector space. The graph is directed\n"
"because vertex v1 might consider vertex v2 a close neighbor, but v2\n"
"itself might have many neighbors that are closer than v1.\n"
"This function computes the symmetrized weights from the distance graph\n"
"using union as the symmetry operator. In simple terms, if either vertex\n"
"considers the other a close neighbor, they will be treated as close\n"
"neighbors.\n\n"
"This function can be used as a separate preprocessing step to\n"
"Graph.layout_umap(). For efficiency reasons, the returned weights have the\n"
"same length as the input distances, however because of the symmetryzation\n"
"some information is lost. Therefore, the weight of one of the edges is set\n"
"to zero whenever edges in opposite directions are found in the input\n"
"distance graph. You can pipe the output of this function directly into\n"
"Graph.layout_umap() as follows:\n"
"C{weights = igraph.umap_compute_weights(graph, dist)}\n"
"C{layout = graph.layout_umap(weights=weights)}\n\n"
"@param graph: directed graph to compute weights for.\n"
"@param dist: distances associated with the graph edges.\n"
"@return: Symmetrized weights associated with each edge. If the distance\n"
" graph has both directed edges between a pair of vertices, one of the\n"
" returned weights will be set to zero.\n\n"
"@see: Graph.layout_umap()\n"
},
{"set_progress_handler", igraphmodule_set_progress_handler, METH_O,
"set_progress_handler(handler)\n--\n\n"
"Sets the handler to be called when igraph is performing a long operation.\n"
"@param handler: the progress handler function. It must accept two\n"
" arguments, the first is the message informing the user about\n"
" what igraph is doing right now, the second is the actual\n"
" progress information (a percentage).\n"
},
{"set_random_number_generator", igraph_rng_Python_set_generator, METH_O,
"set_random_number_generator(generator)\n--\n\n"
"Sets the random number generator used by igraph.\n"
"@param generator: the generator to be used. It must be a Python object\n"
" with at least three attributes: C{random}, C{randint} and C{gauss}.\n"
" Each of them must be callable and their signature and behaviour\n"
" must be identical to C{random.random}, C{random.randint} and\n"
" C{random.gauss}. Optionally, the object can provide a function named\n"
" C{getrandbits} with a signature identical to C{randpm.getrandbits}\n"
" that provides a given number of random bits on demand. By default,\n"
" igraph uses the C{random} module for random number generation, but\n"
" you can supply your alternative implementation here. If the given\n"
" generator is C{None}, igraph reverts to the default PCG32 generator\n"
" implemented in the C layer, which might be slightly faster than\n"
" calling back to Python for random numbers, but you cannot set its\n"
" seed or save its state.\n"
},
{"set_status_handler", igraphmodule_set_status_handler, METH_O,
"set_status_handler(handler)\n--\n\n"
"Sets the handler to be called when igraph tries to display a status\n"
"message.\n\n"
"This is used to communicate the progress of some calculations where\n"
"no reasonable progress percentage can be given (so it is not possible\n"
"to use the progress handler).\n\n"
"@param handler: the status handler function. It must accept a single\n"
" argument, the message that informs the user about what igraph is\n"
" doing right now.\n"
},
{"_split_join_distance", (PyCFunction)igraphmodule_split_join_distance,
METH_VARARGS | METH_KEYWORDS,
"_split_join_distance(comm1, comm2)\n--\n\n"
},
{"_disjoint_union", (PyCFunction)igraphmodule__disjoint_union,
METH_VARARGS | METH_KEYWORDS,
"_disjoint_union(graphs)\n--\n\n"
},
{"_union", (PyCFunction)igraphmodule__union,
METH_VARARGS | METH_KEYWORDS,
"_union(graphs, edgemaps)\n--\n\n"
},
{"_intersection", (PyCFunction)igraphmodule__intersection,
METH_VARARGS | METH_KEYWORDS,
"_intersection(graphs, edgemaps)\n--\n\n"
},
{"_enter_safelocale", (PyCFunction)igraphmodule__enter_safelocale,
METH_NOARGS,
"_enter_safelocale()\n--\n\n"
"Helper function for the L{safe_locale()} context manager. Do not use\n"
"directly in your own code."
},
{"_exit_safelocale", (PyCFunction)igraphmodule__exit_safelocale,
METH_O,
"_exit_safelocale(locale)\n--\n\n"
"Helper function for the L{safe_locale()} context manager. Do not use\n"
"directly in your own code."
},
{NULL, NULL, 0, NULL}
};
#define MODULE_DOCS \