diff --git a/libsrc/core/python_ngcore.hpp b/libsrc/core/python_ngcore.hpp index bf78fe59..79c91885 100644 --- a/libsrc/core/python_ngcore.hpp +++ b/libsrc/core/python_ngcore.hpp @@ -156,165 +156,7 @@ namespace ngcore { return std::string("sp_")+GetPyName(); } }; - template - Array makeCArray(const py::object& obj) - { - Array arr; - if(py::isinstance(obj)) - for(auto& val : py::cast(obj)) - arr.Append(py::cast(val)); - else if(py::isinstance(obj)) - for(auto& val : py::cast(obj)) - arr.Append(py::cast(val)); - else - throw py::type_error("Cannot convert Python object to C Array"); - return arr; - } - - template ::index_type> - void ExportArray (py::module &m) - { - using TFlat = FlatArray; - using TArray = Array; - std::string suffix = GetPyName() + "_" + - GetPyName(); - std::string fname = std::string("FlatArray_") + suffix; - auto flatarray_class = py::class_(m, fname.c_str(), - py::buffer_protocol()) - .def ("__len__", [] ( TFlat &self ) { return self.Size(); } ) - .def ("__getitem__", - [](TFlat & self, TIND i) -> T& - { - static constexpr int base = IndexBASE(); - if (i < base || i >= self.Size()+base) - throw py::index_error(); - return self[i]; - }, - py::return_value_policy::reference) - .def ("__setitem__", - [](TFlat & self, TIND i, T val) -> T& - { - static constexpr int base = IndexBASE(); - if (i < base || i >= self.Size()+base) - throw py::index_error(); - self[i] = val; - return self[i]; - }, - py::return_value_policy::reference) - - .def ("__setitem__", - [](TFlat & self, py::slice slice, T val) - { - size_t start, stop, step, slicelength; - if (!slice.compute(self.Size(), &start, &stop, &step, &slicelength)) - throw py::error_already_set(); - static constexpr int base = IndexBASE(); - if (start < base || start+(slicelength-1)*step >= self.Size()+base) - throw py::index_error(); - for (size_t i = 0; i < slicelength; i++, start+=step) - self[start] = val; - }) - - .def("__iter__", [] ( TFlat & self) { - return py::make_iterator (self.begin(),self.end()); - }, py::keep_alive<0,1>()) // keep array alive while iterator is used - - .def("__str__", [](TFlat& self) - { - return ToString(self); - }) - ; - - if constexpr (detail::HasPyFormat::value) - { - if(ngcore_have_numpy && !py::detail::npy_format_descriptor::dtype().is_none()) - { - flatarray_class - .def_buffer([](TFlat& self) - { - return py::buffer_info( - self.Addr(0), - sizeof(T), - py::format_descriptor::format(), - 1, - { self.Size() }, - { sizeof(T) * (self.Addr(1) - self.Addr(0)) }); - }) - .def("NumPy", [](py::object self) - { - return py::module::import("numpy") - .attr("frombuffer")(self, py::detail::npy_format_descriptor::dtype()); - }) - ; - } - } - - std::string aname = std::string("Array_") + suffix; - py::class_(m, aname.c_str()) - .def(py::init([] (size_t n) { return new TArray(n); }),py::arg("n"), "Makes array of given length") - .def(py::init([] (std::vector const & x) - { - size_t s = x.size(); - TArray tmp(s); - for (size_t i : Range(tmp)) - tmp[TIND(i)] = x[i]; - return tmp; - }), py::arg("vec"), "Makes array with given list of elements") - - ; - py::implicitly_convertible, TArray>(); - } - - template - void ExportTable (py::module &m) - { - py::class_, std::shared_ptr>> (m, ("Table_"+GetPyName()).c_str()) - .def(py::init([] (py::list blocks) - { - size_t size = py::len(blocks); - Array cnt(size); - size_t i = 0; - for (auto block : blocks) - cnt[i++] = py::len(block); - - i = 0; - Table blocktable(cnt); - for (auto block : blocks) - { - auto row = blocktable[i++]; - size_t j = 0; - for (auto val : block) - row[j++] = val.cast(); - } - // cout << "blocktable = " << *blocktable << endl; - return blocktable; - - }), py::arg("blocks"), "a list of lists") - - .def ("__len__", [] (Table &self ) { return self.Size(); } ) - .def ("__getitem__", - [](Table & self, size_t i) -> FlatArray - { - if (i >= self.Size()) - throw py::index_error(); - return self[i]; - }) - .def("__str__", [](Table & self) - { - return ToString(self); - }) - ; - } - - - void NGCORE_API SetFlag(Flags &flags, std::string s, py::object value); - // Parse python kwargs to flags - Flags NGCORE_API CreateFlagsFromKwArgs(const py::kwargs& kwargs, py::object pyclass = py::none(), - py::list info = py::list()); - // Create python dict from kwargs - py::dict NGCORE_API CreateDictFromFlags(const Flags& flags); - - // *************** Archiving functionality ************** + // *************** Archiving functionality ************** template Archive& Archive :: Shallow(T& val) @@ -429,6 +271,165 @@ namespace ngcore }); } + template + Array makeCArray(const py::object& obj) + { + Array arr; + if(py::isinstance(obj)) + for(auto& val : py::cast(obj)) + arr.Append(py::cast(val)); + else if(py::isinstance(obj)) + for(auto& val : py::cast(obj)) + arr.Append(py::cast(val)); + else + throw py::type_error("Cannot convert Python object to C Array"); + return arr; + } + + template ::index_type> + void ExportArray (py::module &m) + { + using TFlat = FlatArray; + using TArray = Array; + std::string suffix = GetPyName() + "_" + + GetPyName(); + std::string fname = std::string("FlatArray_") + suffix; + auto flatarray_class = py::class_(m, fname.c_str(), + py::buffer_protocol()) + .def ("__len__", [] ( TFlat &self ) { return self.Size(); } ) + .def ("__getitem__", + [](TFlat & self, TIND i) -> T& + { + static constexpr int base = IndexBASE(); + if (i < base || i >= self.Size()+base) + throw py::index_error(); + return self[i]; + }, + py::return_value_policy::reference) + .def ("__setitem__", + [](TFlat & self, TIND i, T val) -> T& + { + static constexpr int base = IndexBASE(); + if (i < base || i >= self.Size()+base) + throw py::index_error(); + self[i] = val; + return self[i]; + }, + py::return_value_policy::reference) + + .def ("__setitem__", + [](TFlat & self, py::slice slice, T val) + { + size_t start, stop, step, slicelength; + if (!slice.compute(self.Size(), &start, &stop, &step, &slicelength)) + throw py::error_already_set(); + static constexpr int base = IndexBASE(); + if (start < base || start+(slicelength-1)*step >= self.Size()+base) + throw py::index_error(); + for (size_t i = 0; i < slicelength; i++, start+=step) + self[start] = val; + }) + + .def("__iter__", [] ( TFlat & self) { + return py::make_iterator (self.begin(),self.end()); + }, py::keep_alive<0,1>()) // keep array alive while iterator is used + + .def("__str__", [](TFlat& self) + { + return ToString(self); + }) + ; + + if constexpr (detail::HasPyFormat::value) + { + if(ngcore_have_numpy && !py::detail::npy_format_descriptor::dtype().is_none()) + { + flatarray_class + .def_buffer([](TFlat& self) + { + return py::buffer_info( + self.Addr(0), + sizeof(T), + py::format_descriptor::format(), + 1, + { self.Size() }, + { sizeof(T) * (self.Addr(1) - self.Addr(0)) }); + }) + .def("NumPy", [](py::object self) + { + return py::module::import("numpy") + .attr("frombuffer")(self, py::detail::npy_format_descriptor::dtype()); + }) + ; + } + } + + std::string aname = std::string("Array_") + suffix; + auto arr = py::class_ (m, aname.c_str()) + .def(py::init([] (size_t n) { return new TArray(n); }),py::arg("n"), "Makes array of given length") + .def(py::init([] (std::vector const & x) + { + size_t s = x.size(); + TArray tmp(s); + for (size_t i : Range(tmp)) + tmp[TIND(i)] = x[i]; + return tmp; + }), py::arg("vec"), "Makes array with given list of elements") + ; + if constexpr(is_archivable) + arr.def(NGSPickle()); + py::implicitly_convertible, TArray>(); + } + + template + void ExportTable (py::module &m) + { + py::class_, std::shared_ptr>> (m, ("Table_"+GetPyName()).c_str()) + .def(py::init([] (py::list blocks) + { + size_t size = py::len(blocks); + Array cnt(size); + size_t i = 0; + for (auto block : blocks) + cnt[i++] = py::len(block); + + i = 0; + Table blocktable(cnt); + for (auto block : blocks) + { + auto row = blocktable[i++]; + size_t j = 0; + for (auto val : block) + row[j++] = val.cast(); + } + // cout << "blocktable = " << *blocktable << endl; + return blocktable; + + }), py::arg("blocks"), "a list of lists") + + .def ("__len__", [] (Table &self ) { return self.Size(); } ) + .def ("__getitem__", + [](Table & self, size_t i) -> FlatArray + { + if (i >= self.Size()) + throw py::index_error(); + return self[i]; + }) + .def("__str__", [](Table & self) + { + return ToString(self); + }) + ; + } + + + void NGCORE_API SetFlag(Flags &flags, std::string s, py::object value); + // Parse python kwargs to flags + Flags NGCORE_API CreateFlagsFromKwArgs(const py::kwargs& kwargs, py::object pyclass = py::none(), + py::list info = py::list()); + // Create python dict from kwargs + py::dict NGCORE_API CreateDictFromFlags(const Flags& flags); + } // namespace ngcore