| | #include <chrono> |
| | #include <cstring> |
| | #include <iostream> |
| | #include <map> |
| | #include <memory> |
| | #include <string> |
| | #include <vector> |
| |
|
| | #include "util.h" |
| | #include <pybind11/numpy.h> |
| | #include <pybind11/pybind11.h> |
| | #include <pybind11/pytypes.h> |
| |
|
| | #if !defined(NDEBUG) |
| | |
| | #endif |
| |
|
| | #include <tiledb/tiledb> |
| |
|
| | |
| | namespace { |
| |
|
| | namespace py = pybind11; |
| |
|
| | bool issubdtype(py::dtype t1, py::dtype t2) { |
| | |
| | auto np = py::module::import("numpy"); |
| | auto npsubdtype = np.attr("issubdtype"); |
| |
|
| | return py::cast<bool>(npsubdtype(t1, t2)); |
| | } |
| |
|
| | template <typename T> py::dtype get_dtype(T obj) { |
| | auto &api = py::detail::npy_api::get(); |
| |
|
| | if (api.PyArray_Check_(obj.ptr())) { |
| | return py::cast<py::array>(obj).dtype(); |
| | } |
| |
|
| | return py::reinterpret_steal<py::dtype>( |
| | api.PyArray_DescrFromScalar_(obj.ptr())); |
| | } |
| |
|
| | |
| | |
| | |
| | bool dtype_equal(py::dtype d1, py::dtype d2) { |
| | auto &api = py::detail::npy_api::get(); |
| |
|
| | return api.PyArray_EquivTypes_(d1.ptr(), d2.ptr()); |
| | } |
| |
|
| | }; |
| |
|
| | namespace tiledbpy { |
| |
|
| | using namespace std; |
| | using namespace tiledb; |
| | namespace py = pybind11; |
| | using namespace pybind11::literals; |
| |
|
| | #if PY_MAJOR_VERSION >= 3 |
| | class NumpyConvert { |
| | private: |
| | bool use_iter_ = false; |
| | bool allow_unicode_ = true; |
| | size_t data_nbytes_ = 0; |
| | size_t input_len_ = 0; |
| |
|
| | py::array input_; |
| | |
| | |
| | std::vector<uint8_t> *data_buf_; |
| | std::vector<uint64_t> *offset_buf_; |
| |
|
| | void convert_unicode() { |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | assert(input_.itemsize() > 0); |
| |
|
| | |
| | offset_buf_->resize(input_len_); |
| |
|
| | |
| | data_buf_->resize(input_len_); |
| |
|
| | |
| | Py_ssize_t sz = 0; |
| | |
| | const char *input_p = nullptr; |
| |
|
| | unsigned char *output_p = nullptr; |
| | output_p = data_buf_->data(); |
| |
|
| | |
| | auto npstrencode = py::module::import("numpy").attr("str_").attr("encode"); |
| |
|
| | |
| | int rc; |
| | |
| | |
| | |
| | |
| | py::object u_encoded; |
| |
|
| | |
| | size_t idx = 0; |
| | for (auto u : input_) { |
| | |
| | if (PyUnicode_Check(u.ptr())) { |
| | |
| | u_encoded = npstrencode(u); |
| | rc = PyBytes_AsStringAndSize(u_encoded.ptr(), |
| | const_cast<char **>(&input_p), &sz); |
| | } else { |
| | rc = PyBytes_AsStringAndSize(u.ptr(), const_cast<char **>(&input_p), |
| | &sz); |
| | } |
| |
|
| | if (rc == -1) { |
| | throw std::runtime_error( |
| | "PyBytes_AsStringAndSize failed to encode string"); |
| | } |
| |
|
| | |
| | offset_buf_->data()[idx] = data_nbytes_; |
| |
|
| | if (data_buf_->size() < data_nbytes_ + sz) { |
| | data_buf_->resize(data_nbytes_ + sz); |
| | |
| | output_p = data_buf_->data() + data_nbytes_; |
| | } |
| |
|
| | memcpy(output_p, input_p, sz); |
| |
|
| | data_nbytes_ += sz; |
| | output_p += sz; |
| | idx++; |
| | } |
| |
|
| | data_buf_->resize(data_nbytes_); |
| | } |
| |
|
| | void convert_bytes() { |
| | |
| |
|
| | assert(input_.itemsize() > 0); |
| |
|
| | |
| | offset_buf_->resize(input_len_); |
| |
|
| | |
| | data_buf_->resize(input_len_); |
| |
|
| | |
| | Py_ssize_t sz = 0; |
| | |
| | const char *input_p = nullptr; |
| |
|
| | unsigned char *output_p = nullptr; |
| | output_p = data_buf_->data(); |
| |
|
| | int rc; |
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| | |
| | size_t idx = 0; |
| | for (auto obj : input_) { |
| | auto o = obj.ptr(); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | rc = PyBytes_AsStringAndSize(o, const_cast<char **>(&input_p), &sz); |
| | if (rc == -1) { |
| | throw std::runtime_error( |
| | "PyBytes_AsStringAndSize failed to encode string"); |
| | } |
| |
|
| | |
| | offset_buf_->data()[idx] = data_nbytes_; |
| |
|
| | if (data_buf_->size() < data_nbytes_ + sz) { |
| | data_buf_->resize(data_nbytes_ + sz); |
| | |
| | output_p = data_buf_->data() + data_nbytes_; |
| | } |
| |
|
| | memcpy(output_p, input_p, sz); |
| |
|
| | data_nbytes_ += sz; |
| | output_p += sz; |
| | idx++; |
| | } |
| | } |
| |
|
| | void convert_object() { |
| | |
| |
|
| | auto &api = py::detail::npy_api::get(); |
| |
|
| | offset_buf_->resize(input_len_); |
| |
|
| | auto input_unchecked = input_.unchecked<py::object, 1>(); |
| |
|
| | |
| | Py_ssize_t sz = 0; |
| | |
| | const char *input_p = nullptr; |
| |
|
| | auto input_size = input_.size(); |
| | py::dtype first_dtype; |
| |
|
| | |
| | for (int64_t idx = 0; idx < input_size; idx++) { |
| | offset_buf_->data()[idx] = data_nbytes_; |
| |
|
| | PyObject *o = input_unchecked.data(idx)->ptr(); |
| | assert(o != nullptr); |
| |
|
| | |
| |
|
| | if (PyUnicode_Check(o)) { |
| | if (!allow_unicode_) { |
| | |
| | auto errmsg = std::string( |
| | "Unexpected unicode object for TILEDB_STRING_ASCII attribute"); |
| | throw std::runtime_error(errmsg); |
| | } |
| |
|
| | if (idx < 1) |
| | first_dtype = py::dtype("unicode"); |
| |
|
| | |
| | input_p = PyUnicode_AsUTF8AndSize(o, &sz); |
| | if (!input_p) { |
| | TPY_ERROR_LOC("Internal error: failed to convert unicode to UTF-8"); |
| | } |
| | } else if (PyBytes_Check(o)) { |
| | |
| | auto res = |
| | PyBytes_AsStringAndSize(o, const_cast<char **>(&input_p), &sz); |
| |
|
| | if (idx < 1) |
| | first_dtype = py::dtype("bytes"); |
| |
|
| | if (res == -1) { |
| | |
| | throw std::runtime_error( |
| | "Internal error: failed to get char* from bytes object"); |
| | } |
| | } else if (api.PyArray_Check_(o)) { |
| | auto a = py::cast<py::array>(o); |
| | |
| | if (idx < 1) { |
| | first_dtype = get_dtype(a); |
| | } else if (!dtype_equal(get_dtype(a), first_dtype)) { |
| | throw py::type_error( |
| | "Mismatched dtype in object array to buffer conversion!"); |
| | } |
| |
|
| | sz = a.nbytes(); |
| | } else if (PyBool_Check(o)) { |
| | if (idx < 1) |
| | first_dtype = py::dtype("bool"); |
| |
|
| | auto a = py::cast<py::bool_>(o); |
| | sz = sizeof(bool); |
| | bool bool_value = a; |
| | input_p = reinterpret_cast<const char *>(&bool_value); |
| | } else { |
| | |
| | |
| | |
| | auto errmsg = |
| | std::string("Unexpected object type in string conversion"); |
| | TPY_ERROR_LOC(errmsg); |
| | } |
| |
|
| | data_nbytes_ += sz; |
| | } |
| |
|
| | data_buf_->resize(data_nbytes_); |
| |
|
| | |
| | unsigned char *output_p = data_buf_->data(); |
| |
|
| | |
| | for (int64_t idx = 0; idx < input_size; idx++) { |
| | PyObject *pyobj_p = input_unchecked.data(idx)->ptr(); |
| |
|
| | assert(pyobj_p != nullptr); |
| |
|
| | if (PyUnicode_Check(pyobj_p)) { |
| | input_p = PyUnicode_AsUTF8AndSize(pyobj_p, &sz); |
| | assert(input_p != nullptr); |
| | } else if (PyBytes_Check(pyobj_p)) { |
| | |
| | PyBytes_AsStringAndSize(pyobj_p, const_cast<char **>(&input_p), &sz); |
| | } else if (api.PyArray_Check_(pyobj_p)) { |
| | auto arr = py::cast<py::array>(pyobj_p); |
| | sz = arr.nbytes(); |
| | input_p = (const char *)arr.data(); |
| | } else if (PyBool_Check(pyobj_p)) { |
| | py::bool_ bool_obj = py::cast<py::bool_>(pyobj_p); |
| | sz = sizeof(bool); |
| | bool bool_value = bool_obj; |
| | input_p = reinterpret_cast<const char *>(&bool_value); |
| | } else { |
| | |
| | TPY_ERROR_LOC("Unexpected object type in buffer conversion"); |
| | } |
| |
|
| | memcpy(output_p, input_p, sz); |
| | |
| | output_p += sz; |
| | } |
| | } |
| |
|
| | void convert_iter() { |
| | |
| | |
| | |
| | |
| |
|
| | auto &npy_api = py::detail::npy_api::get(); |
| |
|
| | offset_buf_->resize(input_.size()); |
| |
|
| | auto iter = input_.attr("flat"); |
| |
|
| | |
| | Py_ssize_t sz = 0; |
| | |
| | const char *input_p = nullptr; |
| |
|
| | size_t idx = 0; |
| |
|
| | py::dtype first_dtype; |
| |
|
| | for (auto obj_h : iter) { |
| | if (idx < 1) { |
| | |
| | first_dtype = get_dtype(obj_h); |
| | } |
| | offset_buf_->data()[idx] = data_nbytes_; |
| |
|
| | PyObject *obj_p = obj_h.ptr(); |
| |
|
| | |
| | |
| | auto cur_dtype = get_dtype(obj_h); |
| | auto err_str = |
| | std::string("Mismatched element type in buffer conversion!"); |
| | if ((first_dtype.kind() == cur_dtype.kind()) || |
| | (first_dtype.kind() == cur_dtype.kind())) { |
| | |
| | } else if (!dtype_equal(cur_dtype, first_dtype)) { |
| | throw py::type_error(err_str); |
| | } |
| |
|
| | if (PyUnicode_Check(obj_p)) { |
| | if (!allow_unicode_) { |
| | |
| | auto errmsg = std::string( |
| | "Unexpected unicode object for TILEDB_STRING_ASCII attribute"); |
| | throw std::runtime_error(errmsg); |
| | } |
| |
|
| | |
| | input_p = PyUnicode_AsUTF8AndSize(obj_p, &sz); |
| | if (!input_p) { |
| | TPY_ERROR_LOC("Internal error: failed to convert unicode to UTF-8"); |
| | } |
| | } else if (PyBytes_Check(obj_p)) { |
| | |
| | auto res = |
| | PyBytes_AsStringAndSize(obj_p, const_cast<char **>(&input_p), &sz); |
| |
|
| | if (res == -1) { |
| | |
| | throw std::runtime_error( |
| | "Internal error: failed to get char* from bytes object"); |
| | } |
| | } else if (npy_api.PyArray_Check_(obj_p)) { |
| | |
| | sz = py::cast<py::array>(obj_p).nbytes(); |
| | } else if (PyBool_Check(obj_p)) { |
| | if (idx < 1) |
| | first_dtype = py::dtype("bool"); |
| |
|
| | py::bool_ bool_obj = py::cast<py::bool_>(obj_p); |
| | sz = sizeof(bool); |
| | bool bool_value = bool_obj; |
| | input_p = reinterpret_cast<const char *>(&bool_value); |
| | } else { |
| | auto errmsg = |
| | std::string("Unexpected object type in string conversion"); |
| | TPY_ERROR_LOC(errmsg); |
| | } |
| | data_nbytes_ += sz; |
| | idx++; |
| | } |
| |
|
| | data_buf_->resize(data_nbytes_); |
| | |
| | unsigned char *output_p = data_buf_->data(); |
| |
|
| | |
| | iter = input_.attr("flat"); |
| |
|
| | |
| | for (auto obj_h : iter) { |
| | auto obj_p = obj_h.ptr(); |
| |
|
| | if (PyUnicode_Check(obj_p)) { |
| | input_p = PyUnicode_AsUTF8AndSize(obj_p, &sz); |
| | assert(input_p != nullptr); |
| | } else if (PyBytes_Check(obj_p)) { |
| | |
| | PyBytes_AsStringAndSize(obj_p, const_cast<char **>(&input_p), &sz); |
| | } else if (npy_api.PyArray_Check_(obj_p)) { |
| | |
| | |
| | |
| | auto o_a = py::cast<py::array>(obj_h); |
| | sz = o_a.nbytes(); |
| | input_p = (const char *)o_a.data(); |
| | } else if (PyBool_Check(obj_p)) { |
| | py::bool_ bool_obj = py::cast<py::bool_>(obj_p); |
| | sz = sizeof(bool); |
| | bool bool_value = bool_obj; |
| | input_p = reinterpret_cast<const char *>(&bool_value); |
| | } else { |
| | TPY_ERROR_LOC("Unexpected object type in buffer conversion"); |
| | } |
| |
|
| | memcpy(output_p, input_p, sz); |
| | |
| | output_p += sz; |
| | } |
| | } |
| |
|
| | public: |
| | |
| | |
| | |
| | NumpyConvert(py::array input) { |
| | |
| | if (input.ndim() != 1) { |
| | |
| | auto v = input.attr("view")(); |
| | |
| | |
| | try { |
| | v.attr("shape") = py::int_(input.size()); |
| | } catch (py::error_already_set &e) { |
| | if (e.matches(PyExc_AttributeError)) { |
| | use_iter_ = true; |
| | } else { |
| | throw; |
| | } |
| | } catch (std::exception &e) { |
| | std::cout << e.what() << std::endl; |
| | } |
| | input_ = v; |
| | } else { |
| | input_ = input; |
| | } |
| |
|
| | input_len_ = py::len(input_); |
| |
|
| | data_buf_ = new std::vector<uint8_t>(); |
| | offset_buf_ = new std::vector<uint64_t>(input_len_); |
| | } |
| |
|
| | ~NumpyConvert() { |
| | if (data_buf_) |
| | delete data_buf_; |
| | if (offset_buf_) |
| | delete offset_buf_; |
| | } |
| |
|
| | |
| | |
| | |
| | bool allow_unicode() { return allow_unicode_; } |
| | void allow_unicode(bool allow_unicode) { allow_unicode_ = allow_unicode; } |
| |
|
| | |
| | |
| | |
| | |
| | py::tuple get() { |
| | auto input_dtype = input_.dtype(); |
| |
|
| | if (use_iter_) { |
| | |
| | convert_iter(); |
| | } else if (issubdtype(input_dtype, py::dtype("unicode"))) { |
| | if (allow_unicode_) { |
| | convert_unicode(); |
| | } else { |
| | throw std::runtime_error("Unexpected fixed-length unicode array"); |
| | } |
| | } else if (issubdtype(input_dtype, py::dtype("bytes"))) { |
| | convert_bytes(); |
| | } else if (!input_dtype.equal(py::dtype("O"))) { |
| | |
| | throw std::runtime_error("expected object array"); |
| | } else { |
| | convert_object(); |
| | } |
| |
|
| | auto tmp_data_buf_p = data_buf_; |
| | auto data_ref = py::capsule(data_buf_, [](void *v) { |
| | delete reinterpret_cast<std::vector<uint8_t> *>(v); |
| | }); |
| | data_buf_ = nullptr; |
| |
|
| | auto tmp_offset_buf_p = offset_buf_; |
| | auto offset_ref = py::capsule(offset_buf_, [](void *v) { |
| | delete reinterpret_cast<std::vector<uint64_t> *>(v); |
| | }); |
| | offset_buf_ = nullptr; |
| |
|
| | auto data_np = py::array_t<uint8_t>(tmp_data_buf_p->size(), |
| | tmp_data_buf_p->data(), data_ref); |
| | auto offset_np = py::array_t<uint64_t>( |
| | tmp_offset_buf_p->size(), tmp_offset_buf_p->data(), offset_ref); |
| |
|
| | return py::make_tuple(data_np, offset_np); |
| | } |
| | }; |
| | #endif |
| |
|
| | py::tuple convert_np(py::array input, bool allow_unicode, |
| | bool use_fallback = false) { |
| | #if PY_MAJOR_VERSION >= 3 |
| | if (use_fallback) { |
| | #endif |
| | auto tiledb = py::module::import("tiledb"); |
| | auto libtiledb = tiledb.attr("libtiledb"); |
| | auto array_to_buffer = libtiledb.attr("array_to_buffer"); |
| | return array_to_buffer(input); |
| | #if PY_MAJOR_VERSION >= 3 |
| | } else { |
| | NumpyConvert cvt(input); |
| | cvt.allow_unicode(allow_unicode); |
| | return cvt.get(); |
| | } |
| | #endif |
| | } |
| |
|
| | }; |
| |
|