From c21c3b0befeb46a51b6bf3758ffa30813bea0ff0 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 9 Mar 2024 14:19:22 +0100 Subject: Adding upstream version 1.44.3. Signed-off-by: Daniel Baumann --- ml/dlib/tools/python/src/basic.cpp | 272 +++++++++++++++++++++++++++++++++++++ 1 file changed, 272 insertions(+) create mode 100644 ml/dlib/tools/python/src/basic.cpp (limited to 'ml/dlib/tools/python/src/basic.cpp') diff --git a/ml/dlib/tools/python/src/basic.cpp b/ml/dlib/tools/python/src/basic.cpp new file mode 100644 index 000000000..d87a53cc3 --- /dev/null +++ b/ml/dlib/tools/python/src/basic.cpp @@ -0,0 +1,272 @@ +// Copyright (C) 2013 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#include +#include +#include +#include +#include "opaque_types.h" + +#include +#include + +using namespace std; +using namespace dlib; +namespace py = pybind11; + + +std::shared_ptr > array_from_object(py::object obj) +{ + try { + long nr = obj.cast(); + return std::make_shared>(nr); + } catch (py::cast_error &e) { + py::list li = obj.cast(); + const long nr = len(li); + auto temp = std::make_shared>(nr); + for ( long r = 0; r < nr; ++r) + { + (*temp)[r] = li[r].cast(); + } + return temp; + } +} + +string array__str__ (const std::vector& v) +{ + std::ostringstream sout; + for (unsigned long i = 0; i < v.size(); ++i) + { + sout << v[i]; + if (i+1 < v.size()) + sout << "\n"; + } + return sout.str(); +} + +string array__repr__ (const std::vector& v) +{ + std::ostringstream sout; + sout << "dlib.array(["; + for (unsigned long i = 0; i < v.size(); ++i) + { + sout << v[i]; + if (i+1 < v.size()) + sout << ", "; + } + sout << "])"; + return sout.str(); +} + +string range__str__ (const std::pair& p) +{ + std::ostringstream sout; + sout << p.first << ", " << p.second; + return sout.str(); +} + +string range__repr__ (const std::pair& p) +{ + std::ostringstream sout; + sout << "dlib.range(" << p.first << ", " << p.second << ")"; + return sout.str(); +} + +struct range_iter +{ + std::pair range; + unsigned long cur; + + unsigned long next() + { + if (cur < range.second) + { + return cur++; + } + else + { + PyErr_SetString(PyExc_StopIteration, "No more data."); + throw py::error_already_set(); + } + } +}; + +range_iter make_range_iterator (const std::pair& p) +{ + range_iter temp; + temp.range = p; + temp.cur = p.first; + return temp; +} + +string pair__str__ (const std::pair& p) +{ + std::ostringstream sout; + sout << p.first << ": " << p.second; + return sout.str(); +} + +string pair__repr__ (const std::pair& p) +{ + std::ostringstream sout; + sout << "dlib.pair(" << p.first << ", " << p.second << ")"; + return sout.str(); +} + +string sparse_vector__str__ (const std::vector >& v) +{ + std::ostringstream sout; + for (unsigned long i = 0; i < v.size(); ++i) + { + sout << v[i].first << ": " << v[i].second; + if (i+1 < v.size()) + sout << "\n"; + } + return sout.str(); +} + +string sparse_vector__repr__ (const std::vector >& v) +{ + std::ostringstream sout; + sout << "< dlib.sparse_vector containing: \n" << sparse_vector__str__(v) << " >"; + return sout.str(); +} + +unsigned long range_len(const std::pair& r) +{ + if (r.second > r.first) + return r.second-r.first; + else + return 0; +} + +template +void resize(T& v, unsigned long n) { v.resize(n); } + +void bind_basic_types(py::module& m) +{ + { + typedef double item_type; + typedef std::vector type; + typedef std::shared_ptr type_ptr; + py::bind_vector(m, "array", "This object represents a 1D array of floating point numbers. " + "Moreover, it binds directly to the C++ type std::vector.") + .def(py::init(&array_from_object)) + .def("__str__", array__str__) + .def("__repr__", array__repr__) + .def("clear", &type::clear) + .def("resize", resize) + .def("extend", extend_vector_with_python_list) + .def(py::pickle(&getstate, &setstate)); + } + + { + typedef matrix item_type; + typedef std::vector type; + py::bind_vector(m, "vectors", "This object is an array of vector objects.") + .def("clear", &type::clear) + .def("resize", resize) + .def("extend", extend_vector_with_python_list) + .def(py::pickle(&getstate, &setstate)); + } + + { + typedef std::vector > item_type; + typedef std::vector type; + py::bind_vector(m, "vectorss", "This object is an array of arrays of vector objects.") + .def("clear", &type::clear) + .def("resize", resize) + .def("extend", extend_vector_with_python_list) + .def(py::pickle(&getstate, &setstate)); + } + + typedef pair range_type; + py::class_(m, "range", "This object is used to represent a range of elements in an array.") + .def(py::init()) + .def_readwrite("begin",&range_type::first, "The index of the first element in the range. This is represented using an unsigned integer.") + .def_readwrite("end",&range_type::second, "One past the index of the last element in the range. This is represented using an unsigned integer.") + .def("__str__", range__str__) + .def("__repr__", range__repr__) + .def("__iter__", &make_range_iterator) + .def("__len__", &range_len) + .def(py::pickle(&getstate, &setstate)); + + py::class_(m, "_range_iter") + .def("next", &range_iter::next) + .def("__next__", &range_iter::next); + + { + typedef std::pair item_type; + typedef std::vector type; + py::bind_vector(m, "ranges", "This object is an array of range objects.") + .def("clear", &type::clear) + .def("resize", resize) + .def("extend", extend_vector_with_python_list) + .def(py::pickle(&getstate, &setstate)); + } + + { + typedef std::vector > item_type; + typedef std::vector type; + py::bind_vector(m, "rangess", "This object is an array of arrays of range objects.") + .def("clear", &type::clear) + .def("resize", resize) + .def("extend", extend_vector_with_python_list) + .def(py::pickle(&getstate, &setstate)); + } + + + typedef pair pair_type; + py::class_(m, "pair", "This object is used to represent the elements of a sparse_vector.") + .def(py::init()) + .def_readwrite("first",&pair_type::first, "This field represents the index/dimension number.") + .def_readwrite("second",&pair_type::second, "This field contains the value in a vector at dimension specified by the first field.") + .def("__str__", pair__str__) + .def("__repr__", pair__repr__) + .def(py::pickle(&getstate, &setstate)); + + { + typedef std::vector type; + py::bind_vector(m, "sparse_vector", +"This object represents the mathematical idea of a sparse column vector. It is \n\ +simply an array of dlib.pair objects, each representing an index/value pair in \n\ +the vector. Any elements of the vector which are missing are implicitly set to \n\ +zero. \n\ + \n\ +Unless otherwise noted, any routines taking a sparse_vector assume the sparse \n\ +vector is sorted and has unique elements. That is, the index values of the \n\ +pairs in a sparse_vector should be listed in increasing order and there should \n\ +not be duplicates. However, some functions work with \"unsorted\" sparse \n\ +vectors. These are dlib.sparse_vector objects that have either duplicate \n\ +entries or non-sorted index values. Note further that you can convert an \n\ +\"unsorted\" sparse_vector into a properly sorted sparse vector by calling \n\ +dlib.make_sparse_vector() on it. " + ) + .def("__str__", sparse_vector__str__) + .def("__repr__", sparse_vector__repr__) + .def("clear", &type::clear) + .def("resize", resize) + .def("extend", extend_vector_with_python_list) + .def(py::pickle(&getstate, &setstate)); + } + + { + typedef std::vector item_type; + typedef std::vector type; + py::bind_vector(m, "sparse_vectors", "This object is an array of sparse_vector objects.") + .def("clear", &type::clear) + .def("resize", resize) + .def("extend", extend_vector_with_python_list) + .def(py::pickle(&getstate, &setstate)); + } + + { + typedef std::vector > item_type; + typedef std::vector type; + py::bind_vector(m, "sparse_vectorss", "This object is an array of arrays of sparse_vector objects.") + .def("clear", &type::clear) + .def("resize", resize) + .def("extend", extend_vector_with_python_list) + .def(py::pickle(&getstate, &setstate)); + } +} + -- cgit v1.2.3