Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

python-ogdf bindings

22 views
Skip to first unread message

Karol Janic

unread,
Dec 7, 2024, 10:45:47 AM12/7/24
to ogdf
Hello,
How to use PlanarSubgraphCactus in Python code? I have problem with template specialization. I use ogdf-python and ogdf-wheel.

Best regards,
Karol Janic

Niko Fink

unread,
Dec 7, 2024, 12:09:47 PM12/7/24
to ogdf

Hello Karol,

unfortunately I do not have an example for this class at hand. But if you give me more details on what exactly is the issue and provide me with a minimal example with which I can reproduce your issue, I may be able to help you solve this.

Cheers
Niko

Karol Janic

unread,
Dec 8, 2024, 4:29:48 AM12/8/24
to ogdf

I have C++ code:
```
ogdf::Graph G;
ogdf::randomPlanarTriconnectedGraph(G, 20, 40);

ogdf::PlanarSubgraphCactus<double> psc;

ogdf::EdgeArray<double> costs(G);
calculateEdgeCosts(G, costs);

ogdf::List<ogdf::edge> delEdges;
ogdf::List<ogdf::edge> preferredEdges;
psc.call(G, costs, preferredEdges, delEdges, false);
```

I tried to run this in Python (installed packages: ogdf-python, ogdf-wheel):
```
cppinclude("ogdf/basic/graph_generators/randomized.h")
cppinclude("ogdf/planarity/PlanarSubgraphCactus.h")

G = ogdf.Graph()
ogdf.randomPlanarTriconnectedGraph(G, 20, 40)

psc = ogdf.PlanarSubgraphCactus()

costs = ogdf.EdgeArrayFloat(len(G.nodes))

preferred_edges = ogdf.ListEdge()
del_edges = ogdf.ListEdge()

psc.call(G, costs, preferred_edges, del_edges, False)
```

Error I've received: too few arguments for template instantiation

Niko Fink

unread,
Dec 8, 2024, 6:40:28 AM12/8/24
to ogdf
In cppyy and thus also ogdf-python, the C++ `namespace::TemplateClass<Arg1, ns::Arg2>` becomes (`cppyy.gbl.`)`namespace.TemplateClass["Arg1", "ns::Arg2"]` in Python. If ArgN is a type that exists both in C++ and Python (e.g. int, bool, str (for std::string), ogdf.Graph, ogdf.edge,…) you should also be able to use the Python type directly instead of passing the C++ type/template argument as string. So you should use
```
ogdf::PlanarSubgraphCactus<double> -> ogdf.PlanarSubgraphCactus["double"]

ogdf::EdgeArray<double> -> ogdf.EdgeArray["double"], especially
ogdf.EdgeArrayFloat(len(G.nodes)) -> ogdf.EdgeArray["double"](G)

ogdf::List<ogdf::edge> -> ogdf.List[ogdf.edge] or ogdf.List["ogdf::edge"]
```
Instead of ["double"] you could also try [float], cppyy should translate that to the same type as Pythons floats are double precision.

Cheers
Niko

Karol Janic

unread,
Dec 8, 2024, 8:19:36 AM12/8/24
to ogdf
Great! Thank you very much for help and explanation.

Cheers
Karol

Reply all
Reply to author
Forward
0 new messages