Skip to content

Adding possibility to 'cast' or copy to xt::xarray etc #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .azure-pipelines/unix-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ steps:
displayName: Example - readme 1
workingDirectory: $(Build.SourcesDirectory)/docs/source/examples/readme_example_1

- script: |
source activate xtensor-python
cmake . -DPYTHON_EXECUTABLE=`which python`
cmake --build .
python example.py
displayName: Example - Copy 'cast'
workingDirectory: $(Build.SourcesDirectory)/docs/source/examples/copy_cast

- script: |
source activate xtensor-python
cmake . -DPYTHON_EXECUTABLE=`which python`
Expand Down
13 changes: 13 additions & 0 deletions docs/source/examples/copy_cast/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.1..3.19)

project(mymodule)

find_package(pybind11 CONFIG REQUIRED)
find_package(xtensor REQUIRED)
find_package(xtensor-python REQUIRED)
find_package(Python REQUIRED COMPONENTS NumPy)

pybind11_add_module(mymodule main.cpp)
target_link_libraries(mymodule PUBLIC pybind11::module xtensor-python Python::NumPy)

target_compile_definitions(mymodule PRIVATE VERSION_INFO=0.1.0)
7 changes: 7 additions & 0 deletions docs/source/examples/copy_cast/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import mymodule
import numpy as np

a = np.array([1, 2, 3])
assert np.isclose(np.sum(np.sin(a)), mymodule.sum_of_sines(a))
assert np.isclose(np.sum(np.cos(a)), mymodule.sum_of_cosines(a))

25 changes: 25 additions & 0 deletions docs/source/examples/copy_cast/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <numeric>
#include <xtensor.hpp>
#include <pybind11/pybind11.h>
#define FORCE_IMPORT_ARRAY
#include <xtensor-python/pyarray.hpp>

double sum_of_sines(xt::pyarray<double>& m)
{
auto sines = xt::sin(m); // sines does not actually hold values.
return std::accumulate(sines.begin(), sines.end(), 0.0);
}

double sum_of_cosines(const xt::xarray<double>& m)
{
auto cosines = xt::cos(m); // cosines does not actually hold values.
return std::accumulate(cosines.begin(), cosines.end(), 0.0);
}

PYBIND11_MODULE(mymodule, m)
{
xt::import_numpy();
m.doc() = "Test module for xtensor python bindings";
m.def("sum_of_sines", sum_of_sines, "Sum the sines of the input values");
m.def("sum_of_cosines", sum_of_cosines, "Sum the cosines of the input values");
}
24 changes: 20 additions & 4 deletions include/xtensor-python/xtensor_type_caster_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ namespace pybind11
template <class Type>
struct xtensor_type_caster_base
{
bool load(handle /*src*/, bool)
{
return false;
}

private:

Expand Down Expand Up @@ -106,6 +102,26 @@ namespace pybind11

public:

bool load(handle src, bool convert)
{
using T = typename Type::value_type;

if (!convert && !array_t<T>::check_(src)) {
return false;
}

auto buf = array_t<T, array::c_style | array::forcecast>::ensure(src);

if (!buf) {
return false;
}

type_caster<Type>::value = Type::from_shape(buf.shape());
std::copy(buf.data(), buf.data() + buf.size(), type_caster<Type>::value.begin());

return true;
}

// Normal returned non-reference, non-const value:
static handle cast(Type&& src, return_value_policy /* policy */, handle parent)
{
Expand Down