Package: ogdf-python
ogdf-pythonuses theblack magicof the awesomecppyylibrary to automagically generate python bindings
for the C++Open Graph Drawing Framework (OGDF).
It is available for Python>=3.6 and is Apache2 licensed.
There are no binding definitions files, no stuff that needs extra compiling, it just works™, believe me.
Templates, namespaces, cross-language callbacks and inheritance, pythonic iterators and generators, it’s all there.
If you want to learn more about the magic behind the curtains, readthis article.Useful LinksOriginal repository(GitHub) -Bugtracker and issues(GitHub) -PyPi package(PyPiogdf-python) -Try it out!(mybinder.org).Official OGDF website(ogdf.net) -Public OGDF repository(GitHub) -OGDF Documentation(GitHub / Doxygen) -cppyy Documentation(Read The Docs).QuickstartClick here to start an interactive online Jupyter Notebook with an example OGDF graph where you can try outogdf-python:Simply re-run the code cell to see the graph. You can also find further examples next to that Notebook (i.e. via the folder icon on the left).
To get a similar Jupyter Notebook with a little more compute power running on your local machine, use the following install command and open the link tolocalhost/127.0.0.1that will be printed in your browser:pipinstallogdf-python[quickstart]jupyterlabThe optional[quickstart]pulls in matplotlib and jupyter lab as well as a ready-to-use binary build of the OGDF viaogdf-wheel.
Please not that downloading and installing all dependencies (especially buildingcppyy) may take a moment.
Alternatively, see the instructionsbelowfor installingogdf-pythonwithout this if you want to use your own local build of the OGDF.Usageogdf-pythonworks very well with Jupyter:# %matplotlib widget# uncomment the above line if you want the interactive displayfromogdf_pythonimport*cppinclude("ogdf/basic/graph_generators/randomized.h")cppinclude("ogdf/layered/SugiyamaLayout.h")G=ogdf.Graph()ogdf.setSeed(1)ogdf.randomPlanarTriconnectedGraph(G,20,40)GA=ogdf.GraphAttributes(G,ogdf.GraphAttributes.all)forninG.nodes:GA.label[n]="N%s"%n.index()SL=ogdf.SugiyamaLayout()SL.call(GA)GARead thepitfalls sectionand check outdocs/examples/pitfalls.ipynbfor the more advanced Sugiyama example from the OGDF docs.
There is also a bigger example indocs/examples/ogdf-includes.ipynb.
If anything is unclear, check out the python helphelp(ogdf.Graph)and read the corresponding OGDF documentation.Installation without ogdf-wheelUse pip to install theogdf-pythonpackage locally on your machine.
Please note that buildingcppyyfrom sources may take a while.
Furthermore, you will need a local shared library build (-DBUILD_SHARED_LIBS=ON) of theOGDF.
If you didn’t install the OGDF globally on your system,
either set theOGDF_INSTALL_DIRto the prefix you configured incmake,
or setOGDF_BUILD_DIRto the subdirectory of your copy of the OGDF repo where yourout-of-source buildlives.$pipinstallogdf-python$OGDF_BUILD_DIR=~/ogdf/build-debugpython3PitfallsSee alsodocs/examples/pitfalls.ipynbfor full examples.OGDF sometimes takes ownership of objects (usually when they are passed as modules),
which may conflict with the automatic cppyy garbage collection.
Set__python_owns__ = Falseon those objects to tell cppyy that those objects
don’t need to be garbage collected, but will be cleaned up from the C++ side.SL=ogdf.SugiyamaLayout()ohl=ogdf.OptimalHierarchyLayout()ohl.__python_owns__=FalseSL.setLayout(ohl)When you overwrite a python variable pointing to a C++ object (and it is the only
python variable pointing to that object), the C++ object will usually be immediately deleted.
This might be a problem if another C++ objects depends on that old object, e.g.
aGraphAttributesinstance depending on aGraphinstance.
Now the other C++ object has a pointer to a deleted and now invalid location,
which will usually cause issues down the road (e.g. when the dependant object is
deleted and wants to deregister from its no longer alive parent).
This overwriting might easily happen if you run a Jupyter cell multiple times or some code in afor-loop.
Please ensure that you always overwrite or delete dependent C++ variables in
the reverse order of their initialization.foriinrange(5):# clean-up all variablesCGA=CG=G=None# note that order is different from C++, CGA will be deleted first, G last# now we can re-use themG=ogdf.Graph()CG=ogdf.ClusterGraph(G)CGA=ogdf.ClusterGraphAttributes(CG,ogdf.ClusterGraphAttributes.all)# alternatively manually clean up in the right orderdelCGAdelCGdelGThere seems to be memory leak in the Jupyter Lab server which causes it to use large amounts of memory
over time while working with ogdf-python. On Linux, the following command can be used to limit this memory usage:systemd-run--scope-pMemoryMax=5G--user--jupyternotebook