Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .azure-pipelines/azure-pipelines-osx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ jobs:
- job: 'OSX'
strategy:
matrix:
macOS_10_14:
image_name: 'macOS-10.14'
macOS_10_15:
image_name: 'macOS-10.15'
macOS_11:
image_name: 'macOS-11'
pool:
vmImage: $(image_name)
variables:
Expand Down
4 changes: 2 additions & 2 deletions include/xtensor-python/pycontainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ namespace xt
inline auto pycontainer<D>::get_buffer_size() const -> size_type
{
const size_type& (*min)(const size_type&, const size_type&) = std::min<size_type>;
size_type min_stride = this->strides().empty() ? size_type(1) :
size_type min_stride = this->strides().empty() ? size_type(1) :
std::max(size_type(1), std::accumulate(this->strides().cbegin(),
this->strides().cend(),
this->strides().cend(),
std::numeric_limits<size_type>::max(),
min));
return min_stride * static_cast<size_type>(PyArray_SIZE(this->python_array()));
Expand Down
6 changes: 6 additions & 0 deletions include/xtensor-python/pynative_casters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ namespace pybind11
{
};

// Type caster for casting xt::xtensor_fixed to ndarray
template <class T, class FSH, xt::layout_type L>
struct type_caster<xt::xtensor_fixed<T, FSH, L>> : xtensor_type_caster_base<xt::xtensor_fixed<T, FSH, L>>
{
};

// Type caster for casting xt::xstrided_view to ndarray
template <class CT, class S, xt::layout_type L, class FST>
struct type_caster<xt::xstrided_view<CT, S, L, FST>> : xtensor_type_caster_base<xt::xstrided_view<CT, S, L, FST>>
Expand Down
46 changes: 46 additions & 0 deletions include/xtensor-python/xtensor_type_caster_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <vector>

#include "xtensor/xtensor.hpp"
#include "xtensor/xfixed.hpp"

#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
Expand Down Expand Up @@ -64,6 +65,15 @@ namespace pybind11
}
};

template <class T, class FSH, xt::layout_type L>
struct pybind_array_getter<xt::xtensor_fixed<T, FSH, L>>
{
static auto run(handle src)
{
return pybind_array_getter_impl<T, L>::run(src);
}
};

template <class CT, class S, xt::layout_type L, class FST>
struct pybind_array_getter<xt::xstrided_view<CT, S, L, FST>>
{
Expand Down Expand Up @@ -113,6 +123,37 @@ namespace pybind11
}
};

template <class T, class FSH, xt::layout_type L>
struct pybind_array_dim_checker<xt::xtensor_fixed<T, FSH, L>>
{
template <class B>
static bool run(const B& buf)
{
return buf.ndim() == FSH::size();
}
};


template <class T>
struct pybind_array_shape_checker
{
template <class B>
static bool run(const B& buf)
{
return true;
}
};

template <class T, class FSH, xt::layout_type L>
struct pybind_array_shape_checker<xt::xtensor_fixed<T, FSH, L>>
{
template <class B>
static bool run(const B& buf)
{
auto shape = FSH();
return std::equal(shape.begin(), shape.end(), buf.shape());
}
};

// Casts a strided expression type to numpy array.If given a base,
// the numpy array references the src data, otherwise it'll make a copy.
Expand Down Expand Up @@ -215,6 +256,11 @@ namespace pybind11
return false;
}

if (!pybind_array_shape_checker<Type>::run(buf))
{
return false;
}

std::vector<size_t> shape(buf.ndim());
std::copy(buf.shape(), buf.shape() + buf.ndim(), shape.begin());
value = Type::from_shape(shape);
Expand Down
20 changes: 20 additions & 0 deletions test_python/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "xtensor/xmath.hpp"
#include "xtensor/xarray.hpp"
#include "xtensor/xfixed.hpp"
#define FORCE_IMPORT_ARRAY
#include "xtensor-python/pyarray.hpp"
#include "xtensor-python/pytensor.hpp"
Expand Down Expand Up @@ -60,6 +61,22 @@ xt::xtensor<int, 2, xt::layout_type::column_major> example3_xtensor2_colmajor(
return xt::transpose(m) + 2;
}

xt::xtensor_fixed<int, xt::xshape<4, 3, 2>> example3_xfixed3(const xt::xtensor_fixed<int, xt::xshape<2, 3, 4>>& m)
{
return xt::transpose(m) + 2;
}

xt::xtensor_fixed<int, xt::xshape<3, 2>> example3_xfixed2(const xt::xtensor_fixed<int, xt::xshape<2, 3>>& m)
{
return xt::transpose(m) + 2;
}

xt::xtensor_fixed<int, xt::xshape<3, 2>, xt::layout_type::column_major> example3_xfixed2_colmajor(
const xt::xtensor_fixed<int, xt::xshape<2, 3>, xt::layout_type::column_major>& m)
{
return xt::transpose(m) + 2;
}

// Readme Examples

double readme_example1(xt::pyarray<double>& m)
Expand Down Expand Up @@ -281,6 +298,9 @@ PYBIND11_MODULE(xtensor_python_test, m)
m.def("example3_xtensor3", example3_xtensor3);
m.def("example3_xtensor2", example3_xtensor2);
m.def("example3_xtensor2_colmajor", example3_xtensor2_colmajor);
m.def("example3_xfixed3", example3_xfixed3);
m.def("example3_xfixed2", example3_xfixed2);
m.def("example3_xfixed2_colmajor", example3_xfixed2_colmajor);

m.def("complex_overload", no_complex_overload);
m.def("complex_overload", complex_overload);
Expand Down
13 changes: 12 additions & 1 deletion test_python/test_pyarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,20 @@ def test_example3(self):
np.testing.assert_array_equal(xt.example3_xtensor2(y[1:, 1:, 0]), v.T + 2)
np.testing.assert_array_equal(xt.example3_xtensor2_colmajor(xc), xc.T + 2)

np.testing.assert_array_equal(xt.example3_xfixed3(y), y.T + 2)
np.testing.assert_array_equal(xt.example3_xfixed2(x), x.T + 2)
np.testing.assert_array_equal(xt.example3_xfixed2_colmajor(xc), xc.T + 2)

with self.assertRaises(TypeError):
xt.example3_xtensor3(x)

with self.assertRaises(TypeError):
xt.example3_xfixed3(x)

with self.assertRaises(TypeError):
x = np.arange(3*2).reshape(3, 2)
xt.example3_xfixed2(x)

def test_vectorize(self):
x1 = np.array([[0, 1], [2, 3]])
x2 = np.array([0, 1])
Expand Down Expand Up @@ -85,7 +96,7 @@ def test_readme_example2(self):
x = np.arange(15).reshape(3, 5)
y = [1, 2, 3, 4, 5]
z = xt.readme_example2(x, y)
np.testing.assert_allclose(z,
np.testing.assert_allclose(z,
[[-0.540302, 1.257618, 1.89929 , 0.794764, -1.040465],
[-1.499227, 0.136731, 1.646979, 1.643002, 0.128456],
[-1.084323, -0.583843, 0.45342 , 1.073811, 0.706945]], 1e-5)
Expand Down