Adding C++ Scripts¶
StructuralGT aims to offer optimized graph theoretic analysis code combined with the python ecosystem. For the most part, graph theoretic algorithms are made available, on the python side, via access to the igraph.Graph attribute of the Network class. Calling methods of the Graph attribute calls an optimized C routine, implemented in igraph. This section is for users who want to implement their own graph theoretic calculations in C and combine them with analysis in python. A few of these routines have already been implemented in StrucutralGT, and can be used as templates. They are made possible because the igraph library allows the user to access the pointer to the underlying C igraph_t object. To leverage this in your own code, the following are generally required:
Python Wrapper Method¶
This is called by the user and should be a method of a Compute subclass. These methods should create a deep copy of the graph to pass to the C++ script. The graph is passed to the C++ script via its pointer, which can be accessed from igraph’s _raw_pointer() method.
Cython Module¶
All graph pointers are passed to a C++ script via a cython module, which takes the same name as the python wrapper method which generated it, except with a preceding underscore and leading cast label, e.g. _average_nodal_connectivity_cast. This module is defined in its corresponding .pyx file. Although the .pyx may vary depending on the module, they have a few things in common. Namely, in these files, a cython extension type which holds a C++ instance as an attribute is created. The cython extension type is called PyCast and its C++ instance is called c_cast. Finally, the C++ instance holds the graph’s pointer as an attribute, called G_ptr. This pointer may be accessed from a C++ script, via c_cast. Note that the pointer must be cast to a void pointer using cython’s PyLong_AsVoidPtr funtion; using python’s PyLong_AsVoidPtr function will allocate memory incorrectly.
C++ Script¶
A copy of the graph may be made in your C++ script with
igraph_t* g = (igraph_t*)this->G_ptr
Results from subsequent computations may be passed back to python via attributes of the c_cast instance. These steps follow conventional cython usage and are documented in the cython documentation. This includes creating the necessary header files and adding the module to setup.py. Details for the use of igraph in your C++ script can be found at the igraph C documentation.