#pragma once #include "colmap/util/cancellation.h" #include "colmap/util/logging.h" #include "colmap/util/string.h" #include "colmap/util/threading.h" #include "pycolmap/feature/opaque_types.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace py = pybind11; const Eigen::IOFormat vec_fmt(Eigen::StreamPrecision, Eigen::DontAlignCols, ", ", ", "); template T PyStringToEnum(const py::enum_& enm, const std::string& value) { const auto values = enm.attr("__members__").template cast(); const auto str_val = py::str(value); if (!values.contains(str_val)) { LOG(FATAL_THROW) << "Invalid string value " << value << " for enum " << enm.attr("__name__").template cast(); } return T(values[str_val].template cast()); } template void AddStringToEnumConstructor(py::enum_& enm) { enm.def(py::init([enm](const std::string& value) { return PyStringToEnum(enm, py::str(value)); // str constructor }), py::arg("name")); enm.attr("__repr__") = enm.attr("__str__"); py::implicitly_convertible(); } inline void UpdateFromDict(py::object& self, const py::dict& dict) { for (const auto& it : dict) { if (!py::isinstance(it.first)) { LOG(FATAL_THROW) << "Dictionary key is not a string: " << py::str(it.first); } const py::str name = py::reinterpret_borrow(it.first); const py::handle& value = it.second; const auto attr = self.attr(name); try { if (py::hasattr(attr, "mergedict") && py::isinstance(value)) { attr.attr("mergedict").attr("__call__")(value); } else { self.attr(name) = value; } } catch (const py::error_already_set& ex) { if (ex.matches(PyExc_TypeError)) { // If fail we try bases of the class const py::list bases = attr.attr("__class__").attr("__bases__").cast(); bool success_on_base = false; for (auto& base : bases) { try { self.attr(name) = base(value); success_on_base = true; break; } catch (const py::error_already_set&) { continue; // We anyway throw afterwards } } if (success_on_base) { continue; } std::ostringstream ss; ss << self.attr("__class__") .attr("__name__") .template cast() << "." << name.template cast() << ": Could not convert " << py::type::of(value.cast()) .attr("__name__") .template cast() << ": " << py::str(value).template cast() << " to '" << py::type::of(attr).attr("__name__").template cast() << "'."; // We write the err message to give info even if exceptions // is catched outside, e.g. in function overload resolve LOG(ERROR) << "Internal TypeError: " << ss.str(); throw(py::type_error(std::string("Failed to merge dict into class: ") + "Could not assign " + name.template cast())); } else if (ex.matches(PyExc_AttributeError) && py::str(ex.value()).cast() == std::string("can't set attribute")) { std::ostringstream ss; ss << self.attr("__class__") .attr("__name__") .template cast() << "." << name.template cast() << " defined readonly."; throw py::attribute_error(ss.str()); } else if (ex.matches(PyExc_AttributeError)) { LOG(ERROR) << "Internal AttributeError: " << py::str(ex.value()).cast(); throw; } else { LOG(ERROR) << "Internal Error: " << py::str(ex.value()).cast(); throw; } } } } inline bool AttributeIsFunction(const std::string& name, const py::object& value) { return (name.find("__") == 0 || name.rfind("__") != std::string::npos || py::hasattr(value, "__func__") || py::hasattr(value, "__call__")); } inline std::vector ListObjectAttributes(const py::object& pyself) { std::vector attributes; for (const auto& handle : pyself.attr("__dir__")()) { const py::str attribute = py::reinterpret_borrow(handle); const auto value = pyself.attr(attribute); if (AttributeIsFunction(attribute, value)) { continue; } attributes.push_back(attribute); } return attributes; } template py::dict ConvertToDict(const T& self, std::vector attributes, const bool recursive) { const py::object pyself = py::cast(self); if (attributes.empty()) { attributes = ListObjectAttributes(pyself); } py::dict dict; for (const auto& attr : attributes) { const auto value = pyself.attr(attr.c_str()); if (recursive && py::hasattr(value, "todict")) { dict[attr.c_str()] = value.attr("todict").attr("__call__")().template cast(); } else { dict[attr.c_str()] = value; } } return dict; } template std::string CreateSummary(const T& self, bool write_type) { std::ostringstream ss; auto pyself = py::cast(self); const std::string prefix = " "; bool after_subsummary = false; ss << pyself.attr("__class__").attr("__name__").template cast() << ":"; for (auto& handle : pyself.attr("__dir__")()) { const py::str name = py::reinterpret_borrow(handle); py::object attribute; try { attribute = pyself.attr(name); } catch (const std::exception&) { // Some properties are not valid for some uninitialized objects. continue; } if (AttributeIsFunction(name, attribute)) { continue; } ss << "\n"; if (!after_subsummary) { ss << prefix; } ss << name.template cast(); if (py::hasattr(attribute, "summary")) { std::string summ = attribute.attr("summary") .attr("__call__")(write_type) .template cast(); static const std::regex newline_regex("\n"); // NOLINTNEXTLINE(performance-inefficient-string-concatenation) summ = std::regex_replace(summ, newline_regex, "\n" + prefix); ss << ": " << summ; } else { if (write_type) { const std::string type_str = py::str(py::type::of(attribute).attr("__name__")); ss << ": " << type_str; after_subsummary = true; } std::string value = py::str(attribute); if (value.length() > 80 && py::hasattr(attribute, "__len__")) { const int length = attribute.attr("__len__")().template cast(); value = colmap::StringPrintf( "%c ... %d elements ... %c", value.front(), length, value.back()); } ss << " = " << value; after_subsummary = false; } } return ss.str(); } template std::string CreateRepresentationFromAttributes(const T& self) { std::ostringstream ss; auto pyself = py::cast(self); ss << pyself.attr("__class__").attr("__name__").template cast() << "("; bool is_first = true; for (auto& handle : pyself.attr("__dir__")()) { const py::str name = py::reinterpret_borrow(handle); py::object attribute; try { attribute = pyself.attr(name); } catch (const std::exception&) { // Some properties are not valid for some uninitialized objects. continue; } if (AttributeIsFunction(name, attribute)) { continue; } if (!is_first) { ss << ", "; } is_first = false; ss << name.template cast() << "="; if (py::isinstance(attribute)) { ss << "'" << py::str(attribute) << "'"; } else { ss << py::str(attribute); } } ss << ")"; return ss.str(); } template struct IsOstreamable : std::false_type {}; template struct IsOstreamable< T, std::void_t() << std::declval())>> : std::true_type {}; template std::string CreateRepresentation(const T& self) { if constexpr (IsOstreamable::value) { std::ostringstream ss; ss << self; return ss.str(); } else { return CreateRepresentationFromAttributes(self); } } template void AddDefaultsToDocstrings(py::classh cls) { auto obj = cls(); for (auto& handle : obj.attr("__dir__")()) { const std::string attribute = py::str(handle); py::object member; try { member = obj.attr(attribute.c_str()); } catch (const std::exception&) { // Some properties are not valid for some uninitialized objects. continue; } auto prop = cls.attr(attribute.c_str()); if (AttributeIsFunction(attribute, member)) { continue; } const auto type_name = py::type::of(member).attr("__name__"); const std::string doc = colmap::StringPrintf("%s (%s, default: %s)", py::str(prop.doc()).cast().c_str(), type_name.template cast().c_str(), py::str(member).cast().c_str()); prop.doc() = py::str(doc); } } template struct has_equality_operator : std::false_type {}; template struct has_equality_operator< T, std::void_t() == std::declval())>> : std::true_type {}; template struct has_less_than_operator : std::false_type {}; template struct has_less_than_operator< T, std::void_t() < std::declval())>> : std::true_type {}; template struct is_hashable : std::false_type {}; template struct is_hashable{}(std::declval()))>> : std::true_type {}; template void MakeDataclass(py::classh cls, const std::vector& attributes = {}) { AddDefaultsToDocstrings(cls); if (!py::hasattr(cls, "summary")) { cls.def("summary", &CreateSummary, py::arg("write_type") = false); } if (!cls.attr("__dict__").contains("__repr__")) { cls.def("__repr__", &CreateRepresentation); } cls.def("mergedict", &UpdateFromDict, py::arg("kwargs")); cls.def( "todict", [attributes](const T& self, const bool recursive) { return ConvertToDict(self, attributes, recursive); }, py::arg("recursive") = true); if constexpr (std::is_copy_constructible_v) { cls.def(py::init([cls](const py::dict& dict) { py::object self = cls(); self.attr("mergedict").attr("__call__")(dict); return self.cast(); })); cls.def(py::init([cls](const py::kwargs& kwargs) { py::dict dict = kwargs.cast(); return cls(dict).template cast(); })); py::implicitly_convertible(); py::implicitly_convertible(); if (!cls.attr("__dict__").contains("__copy__")) { cls.def("__copy__", [](const T& self) { return T(self); }); } if (!cls.attr("__dict__").contains("__deepcopy__")) { cls.def("__deepcopy__", [](const T& self, const py::dict&) { return T(self); }); } cls.def(py::pickle( [attributes](const T& self) { return ConvertToDict(self, attributes, /*recursive=*/false); }, [cls](const py::dict& dict) { py::object self = cls(); self.attr("mergedict").attr("__call__")(dict); return self.cast(); })); } if constexpr (has_equality_operator::value) { cls.def(py::self == py::self); if constexpr (is_hashable::value) { cls.def("__hash__", [](const T& self) { return std::hash()(self); }); } else { cls.attr("__hash__") = py::none(); } } else { if constexpr (std::is_copy_constructible_v) { cls.def("__eq__", [attributes](const T& self, const py::object& other) { if (!py::isinstance(other)) { return false; } py::dict self_dict = ConvertToDict(self, attributes, true); py::dict other_dict = ConvertToDict(other.cast(), attributes, true); return self_dict.equal(other_dict); }); } cls.attr("__hash__") = py::none(); } if constexpr (has_less_than_operator::value) { cls.def(py::self < py::self); } } // Catch python keyboard interrupts /* // single if (PyInterrupt().Raised()) { // stop the execution and raise an exception throw py::error_already_set(); } // loop PyInterrupt py_interrupt = PyInterrupt(2.0) for (...) { if (py_interrupt.Raised()) { // stop the execution and raise an exception throw py::error_already_set(); } // Do your workload here } */ struct PyInterrupt { using clock = std::chrono::steady_clock; using sec = std::chrono::duration; explicit PyInterrupt(double gap = -1.0) : start(clock::now()), gap_(gap) {} inline bool Raised(); private: std::mutex mutex_; bool found = false; colmap::Timer timer_; clock::time_point start; double gap_; }; inline bool PyInterrupt::Raised() { const sec duration = clock::now() - start; if (!found && duration.count() > gap_) { std::lock_guard lock(mutex_); py::gil_scoped_acquire acq; found = (PyErr_CheckSignals() != 0); start = clock::now(); } return found; } [[noreturn]] inline void ThrowPythonError() { py::gil_scoped_acquire acquire; throw py::error_already_set(); } [[noreturn]] inline void ThrowCancelled() { py::gil_scoped_acquire acquire; PyErr_SetString(PyExc_InterruptedError, "Operation cancelled"); throw py::error_already_set(); } class PyInterruptChecker { public: explicit PyInterruptChecker( std::shared_ptr cancellation_token = nullptr, const double gap = 1.0) : py_interrupt_(gap), cancellation_token_(std::move(cancellation_token)), calling_thread_id_(std::this_thread::get_id()) {} // The callback may run on worker threads, but Python signals may only be // checked from the thread that created this object. The returned callback // borrows this object and must not outlive it. std::function Callback() { return [this]() { if (std::this_thread::get_id() == calling_thread_id_ && py_interrupt_.Raised()) { python_interrupt_raised_.store(true, std::memory_order_relaxed); } return python_interrupt_raised_.load(std::memory_order_relaxed) || (cancellation_token_ && cancellation_token_->IsCancelled()); }; } void CheckAndThrow() const { if (python_interrupt_raised_.load(std::memory_order_relaxed)) { ThrowPythonError(); } if (cancellation_token_ && cancellation_token_->IsCancelled()) { ThrowCancelled(); } } private: PyInterrupt py_interrupt_; std::shared_ptr cancellation_token_; std::thread::id calling_thread_id_; std::atomic python_interrupt_raised_{false}; }; // Instead of thread.Wait() call this to allow interrupts through Python. // `poll_interval` controls lightweight thread-state and cancellation checks, // while `gap` throttles Python signal checks that require acquiring the GIL. inline void PyWait(colmap::Thread* thread, const std::shared_ptr& cancellation_token = nullptr, double gap = 1.0, double poll_interval = 0.05) { PyInterrupt py_interrupt(gap); while (thread->IsRunning()) { if (cancellation_token && cancellation_token->IsCancelled()) { thread->Stop(); thread->Wait(); ThrowCancelled(); } if (py_interrupt.Raised()) { LOG(ERROR) << "Stopping thread..."; thread->Stop(); thread->Wait(); ThrowPythonError(); } std::this_thread::sleep_for(PyInterrupt::sec(poll_interval)); } // after finishing join the thread to avoid abort thread->Wait(); if (cancellation_token && cancellation_token->IsCancelled()) { ThrowCancelled(); } } // Test if pyceres is available inline bool IsPyceresAvailable() { try { py::module::import("pyceres"); } catch (const py::error_already_set&) { return false; } return true; } template inline void DefDeprecation( Parent& parent, std::string old_name, std::string new_name, std::optional custom_warning = std::nullopt) { const std::string doc = colmap::StringPrintf("Deprecated, use ``%s`` instead.", new_name.c_str()); parent.def( old_name.c_str(), [parent, old_name, new_name = std::move(new_name), custom_warning = std::move(custom_warning)](const py::args& args, const py::kwargs& kwargs) { if (custom_warning) { PyErr_WarnEx(PyExc_DeprecationWarning, custom_warning->c_str(), 1); } else { std::ostringstream warning; warning << old_name << "() is deprecated, use " << new_name << "() instead."; PyErr_WarnEx(PyExc_DeprecationWarning, warning.str().c_str(), 1); } return parent.attr(new_name.c_str())(*args, **kwargs); }, doc.c_str()); }