Skip to content

Commit

Permalink
Cleanup the logging and helper utils (#33)
Browse files Browse the repository at this point in the history
* Rename functions

* Improve readability

* Update helpers

* Update logging

* Make Logging local

* Make logging.Level local

* Pull changes from COLMAP
  • Loading branch information
sarlinpe authored Feb 5, 2024
1 parent c0028b4 commit bdaf557
Show file tree
Hide file tree
Showing 15 changed files with 750 additions and 697 deletions.
11 changes: 6 additions & 5 deletions _pyceres/bindings.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "_pyceres/core/bindings.h"

#include "_pyceres/factors/bindings.h"
#include "_pyceres/glog.h"
#include "_pyceres/helpers.h"
#include "_pyceres/logging.h"

#include <pybind11/iostream.h>
#include <pybind11/pybind11.h>
Expand All @@ -12,12 +12,13 @@
namespace py = pybind11;

PYBIND11_MODULE(pyceres, m) {
m.doc() = "PyCeres";
m.doc() = "PyCeres - Python bindings for the Ceres solver.";
m.attr("__version__") = py::str(VERSION_INFO);

py::add_ostream_redirect(m, "ostream_redirect");
init_glog(m);
bind_core(m);
BindLogging(m);
BindCore(m);

py::module_ f = m.def_submodule("factors");
bind_factors(f);
BindFactors(f);
}
18 changes: 9 additions & 9 deletions _pyceres/core/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

namespace py = pybind11;

void bind_core(py::module& m) {
init_types(m);
init_callbacks(m);
init_covariance(m);
init_solver(m);
init_loss_functions(m);
init_cost_functions(m);
init_manifold(m);
init_problem(m);
void BindCore(py::module& m) {
BindTypes(m);
BindCallbacks(m);
BindCovariance(m);
BindSolver(m);
BindLossFunctions(m);
BindCostFunctions(m);
BindManifold(m);
BindProblem(m);
}
2 changes: 1 addition & 1 deletion _pyceres/core/callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PyIterationCallback : public ceres::IterationCallback {

PYBIND11_MAKE_OPAQUE(std::vector<ceres::IterationCallback*>);

void init_callbacks(py::module& m) {
void BindCallbacks(py::module& m) {
py::class_<ceres::IterationCallback,
PyIterationCallback /* <--- trampoline*/>(m, "IterationCallback")
.def(py::init<>())
Expand Down
19 changes: 9 additions & 10 deletions _pyceres/core/cost_functions.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "_pyceres/helpers.h"
#include "_pyceres/log_exceptions.h"
#include "_pyceres/logging.h"

#include <ceres/ceres.h>
#include <pybind11/pybind11.h>
Expand All @@ -12,7 +12,7 @@ namespace py = pybind11;
// This allows use to create python based cost functions.
class PyCostFunction : public ceres::CostFunction {
public:
/* Inherit the constructors */
// Inherit the constructors.
using ceres::CostFunction::CostFunction;
using ceres::CostFunction::set_num_residuals;

Expand Down Expand Up @@ -82,16 +82,15 @@ class PyCostFunction : public ceres::CostFunction {
// Mutable so they can be modified by the const function.
mutable std::vector<py::array_t<double>> parameters_vec;
mutable std::vector<py::array_t<double>> jacobians_vec;
mutable bool cached_flag = false; // Flag used to determine if the vectors
// need to be resized
mutable py::array_t<double>
residuals_wrap; // Buffer to contain the residuals
// pointer
mutable py::str no_copy; // Dummy variable for pybind11 to avoid copy
// copy
// Flag used to determine if the vectors need to be resized.
mutable bool cached_flag = false;
// Buffer to contain the residuals pointer.
mutable py::array_t<double> residuals_wrap;
// Dummy variable for pybind11 to avoid a copy.
mutable py::str no_copy;
};

void init_cost_functions(py::module& m) {
void BindCostFunctions(py::module& m) {
py::class_<ceres::CostFunction, PyCostFunction /* <--- trampoline*/>(
m, "CostFunction")
.def(py::init<>())
Expand Down
42 changes: 20 additions & 22 deletions _pyceres/core/covariance.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
#pragma once

#include "_pyceres/helpers.h"
#include "_pyceres/log_exceptions.h"
#include "_pyceres/logging.h"

#include <ceres/ceres.h>
#include <pybind11/pybind11.h>

namespace py = pybind11;

void init_covariance(py::module& m) {
using c_options = ceres::Covariance::Options;
auto co = py::class_<ceres::Covariance::Options>(m, "CovarianceOptions")
.def(py::init<>())
.def_property(
"num_threads",
[](const c_options& self) { return self.num_threads; },
[](c_options& self, int n_threads) {
int effective_n_threads =
GetEffectiveNumThreads(n_threads);
self.num_threads = effective_n_threads;
})
.def_readwrite("sparse_linear_algebra_library_type",
&c_options::sparse_linear_algebra_library_type)
.def_readwrite("algorithm_type", &c_options::algorithm_type)
.def_readwrite("min_reciprocal_condition_number",
&c_options::min_reciprocal_condition_number)
.def_readwrite("null_space_rank", &c_options::null_space_rank)
.def_readwrite("apply_loss_function",
&c_options::apply_loss_function);
make_dataclass(co);
void BindCovariance(py::module& m) {
using Options = ceres::Covariance::Options;
py::class_<Options> PyOptions(m, "CovarianceOptions");
PyOptions.def(py::init<>())
.def_property(
"num_threads",
[](const Options& self) { return self.num_threads; },
[](Options& self, int n_threads) {
int effective_n_threads = GetEffectiveNumThreads(n_threads);
self.num_threads = effective_n_threads;
})
.def_readwrite("sparse_linear_algebra_library_type",
&Options::sparse_linear_algebra_library_type)
.def_readwrite("algorithm_type", &Options::algorithm_type)
.def_readwrite("min_reciprocal_condition_number",
&Options::min_reciprocal_condition_number)
.def_readwrite("null_space_rank", &Options::null_space_rank)
.def_readwrite("apply_loss_function", &Options::apply_loss_function);
MakeDataclass(PyOptions);

py::class_<ceres::Covariance>(m, "Covariance")
.def(py::init<ceres::Covariance::Options>())
Expand Down
6 changes: 3 additions & 3 deletions _pyceres/core/loss_functions.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "_pyceres/helpers.h"
#include "_pyceres/log_exceptions.h"
#include "_pyceres/logging.h"

#include <string>

Expand Down Expand Up @@ -29,7 +29,7 @@ class PyLossFunction : public ceres::LossFunction {
if (overload) {
overload.operator()(sq_norm, out_arr);
} else {
THROW_EXCEPTION(std::runtime_error, "<Evaluate> not implemented.")
LOG_FATAL_THROW(std::runtime_error) << "<Evaluate> not implemented.";
}
}

Expand Down Expand Up @@ -87,7 +87,7 @@ std::shared_ptr<ceres::LossFunction> CreateLossFunctionFromDict(py::dict dict) {
}
}

void init_loss_functions(py::module& m) {
void BindLossFunctions(py::module& m) {
py::class_<LossFunction,
PyLossFunction /*<--- trampoline*/,
std::shared_ptr<LossFunction>>(m, "LossFunction")
Expand Down
14 changes: 8 additions & 6 deletions _pyceres/core/manifold.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "_pyceres/helpers.h"
#include "_pyceres/log_exceptions.h"
#include "_pyceres/logging.h"

#include <ceres/ceres.h>
#include <pybind11/pybind11.h>
Expand All @@ -14,23 +14,25 @@ class PyManifold : public ceres::Manifold {
bool Plus(const double* x,
const double* delta,
double* x_plus_delta) const override {
THROW_EXCEPTION(std::runtime_error, "<Plus> not implemented.");
LOG_FATAL_THROW(std::runtime_error) << "<Plus> not implemented.";
return true;
}

bool PlusJacobian(const double* x, double* jacobian) const override {
THROW_EXCEPTION(std::runtime_error, "<PlusJacobian> not implemented.");
LOG_FATAL_THROW(std::runtime_error) << "<PlusJacobian> not implemented.";
return true;
}

bool Minus(const double* y,
const double* x,
double* y_minus_x) const override {
THROW_EXCEPTION(std::runtime_error, "<Minus> not implemented.");
LOG_FATAL_THROW(std::runtime_error) << "<Minus> not implemented.";
return true;
}

bool MinusJacobian(const double* x, double* jacobian) const override {
THROW_EXCEPTION(std::runtime_error, "<MinusJacobian> not implemented.");
LOG_FATAL_THROW(std::runtime_error) << "<MinusJacobian> not implemented.";
return true;
}

// Size of x.
Expand All @@ -56,7 +58,7 @@ class PyManifold : public ceres::Manifold {

using namespace ceres;

void init_manifold(py::module& m) {
void BindManifold(py::module& m) {
py::class_<Manifold, PyManifold /* <--- trampoline*/>(m, "Manifold")
.def(py::init<>())
.def("ambient_size", &Manifold::AmbientSize)
Expand Down
4 changes: 2 additions & 2 deletions _pyceres/core/problem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "_pyceres/core/wrappers.h"
#include "_pyceres/helpers.h"
#include "_pyceres/log_exceptions.h"
#include "_pyceres/logging.h"

#include <ceres/ceres.h>
#include <pybind11/pybind11.h>
Expand All @@ -26,7 +26,7 @@ std::unique_ptr<ceres::Problem> CreatePythonProblem() {
return std::unique_ptr<ceres::Problem>(new ceres::Problem(options));
}

void init_problem(py::module& m) {
void BindProblem(py::module& m) {
using options = ceres::Problem::Options;
py::class_<ceres::Problem::Options>(m, "ProblemOptions")
.def(py::init(&CreateProblemOptions)) // Ensures default is that
Expand Down
Loading

0 comments on commit bdaf557

Please sign in to comment.