From 208b181210d18891214d85e2ebe51263be3b3f23 Mon Sep 17 00:00:00 2001 From: Rainer Kuemmerle Date: Sun, 25 Aug 2024 15:13:59 +0200 Subject: [PATCH] Wrap IO helpers to python --- python/core/py_io_format.cpp | 14 ++++++++++++++ python/core/py_robust_kernel.cpp | 4 ---- python/examples/simple_optimize.py | 12 ++++++++---- todo.md | 2 +- 4 files changed, 23 insertions(+), 9 deletions(-) mode change 100644 => 100755 python/examples/simple_optimize.py diff --git a/python/core/py_io_format.cpp b/python/core/py_io_format.cpp index 1780c71f6..5d7aabd74 100644 --- a/python/core/py_io_format.cpp +++ b/python/core/py_io_format.cpp @@ -4,11 +4,25 @@ namespace g2o { +namespace { +struct IoWrapper {}; +} // namespace + void declareIOFormat(py::module& m) { py::enum_(m, "IoFormat") .value("G2O", io::Format::kG2O, "G2O's custom ASCII IO format") .value("JSON", io::Format::kJson, "JSON IO format") .value("BINARY", io::Format::kBinary, "G2O's custom binary format"); + + py::class_(m, "IoFileFilter") + .def(py::init(), "filter"_a, "format"_a) + .def_readwrite("filter", &io::FileFilter::filter) + .def_readwrite("format", &io::FileFilter::format); + + py::class_(m, "IoWrapper") + .def_static("to_string", &io::to_string) + .def_static("file_filter", &io::getFileFilter) + .def_static("format_for_file_extension", &io::formatForFileExtension); } } // end namespace g2o diff --git a/python/core/py_robust_kernel.cpp b/python/core/py_robust_kernel.cpp index a64c290fb..75af76cca 100644 --- a/python/core/py_robust_kernel.cpp +++ b/python/core/py_robust_kernel.cpp @@ -7,9 +7,6 @@ namespace g2o { void declareRobustKernel(py::module& m) { py::class_>(m, "BaseRobustKernel") - //.def(py::init<>()) - //.def(py::init(), - // "delta"_a) .def("robustify", &RobustKernel::robustify, "squared_error"_a, "rho"_a) // (double, Vector3&) -> void .def("set_delta", &RobustKernel::setDelta, @@ -24,7 +21,6 @@ void declareRobustKernel(py::module& m) { .def(py::init(), "delta"_a = 1.) .def(py::init(), "kernel"_a, "delta"_a = 1., py::keep_alive<1, 2>()) - .def(py::init(), "delta"_a) .def("kernel", &RobustKernelScaleDelta::kernel) // -> RobustKernelPtr& .def("set_kernel", &RobustKernelScaleDelta::setKernel, "ptr"_a, py::keep_alive<1, 2>()) // const RobustKernelPtr& -> diff --git a/python/examples/simple_optimize.py b/python/examples/simple_optimize.py old mode 100644 new mode 100755 index b67267600..5d2d5b455 --- a/python/examples/simple_optimize.py +++ b/python/examples/simple_optimize.py @@ -1,10 +1,13 @@ +#!/usr/bin/env python3 + import argparse import g2opy as g2o def to_io_format(value: str) -> g2o.IoFormat: - return g2o.IoFormat.__members__[value.upper()] + format = g2o.IoWrapper.format_for_file_extension(value) + return format if format is not None else g2o.IoFormat.G2O def main(): @@ -19,7 +22,7 @@ def main(): parser.add_argument( "--output-format", type=str, - choices=["g2o", "json", "xml", "binary"], + choices=["g2o", "json", "bin"], default=g2o.IoFormat.G2O.name, help="define the output format", ) @@ -51,8 +54,9 @@ def main(): print(f"Final chi2: {optimizer.chi2()}") if len(args.output) > 0: - print(f"Saving to {args.output}") - optimizer.save(args.output, to_io_format(args.output_format)) + io_output_format = to_io_format(args.output_format) + print(f"Saving to {args.output} as {g2o.IoWrapper.to_string(io_output_format)}") + optimizer.save(args.output, io_output_format) if __name__ == "__main__": diff --git a/todo.md b/todo.md index 36d372b59..067d3c5f0 100644 --- a/todo.md +++ b/todo.md @@ -1,6 +1,6 @@ [x] drop cereal (replace with json directly, drop xml) [ ] install json on Windows CI -[ ] wrap io_format.h to python +[x] wrap io_format.h to python [x] wrap simulator into library [x] add config types for simulation [ ] add tests for simulator