xtensor-python icon indicating copy to clipboard operation
xtensor-python copied to clipboard

Bindings for views (or xstrided_views)

Open tarcisiofischer opened this issue 7 years ago • 3 comments

I was trying to get an array view from cpp and call a python function back with it. After some help, I managed to do it by converting the view to a xt::pytensor before proceeding with the Python call (See code below).

void cpp_f(xt::pytensor<double, 2> my_arr, py::object py_f) {
	auto v = xt::pytensor<double, 1>(xt::view(my_arr, 0, xt::all()));
	py_f(v);
}
def test_view():
    def py_f(v):
        print(v)
    cpp_f(np.array([ [1,2,3], [4,5,6], ]), py_f)

It would be nice to have some way to pass the view directly to the python function, instead of having to convert them. It seems that it is not possible to make bindings for xt::view directly, though. I'll leave the original discussion here for reference:

Tarcísio Fischer @tarcisiofischer 14:49
Do you know if there'll be bindings for the xt::view anytime soon?

Johan Mabille @JohanMabille 14:50
the thing is numpy view and xtensor views are not exactly the same so we cannot bind view directly

Sylvain Corlay @SylvainCorlay 14:50
Not as a numpy array, but there is some hope for strided views on pyarrays / pytensor.

Johan Mabille @JohanMabille 14:50
we have xstrided_views that works like numpy views, we can make bindings for them

tarcisiofischer avatar Jan 04 '18 16:01 tarcisiofischer

With some of the recent additions this is definitly possible (at least for "easy" views that don't use the new islice), and for the strided view.

wolfv avatar Jun 21 '18 08:06 wolfv

I am interested in working on this. I wonder if there's a pointer (like a file or a PR) to the "easy" views that don't use the new islice?

My project needs to share large buffers among C++ and Python. It seems that, without the requested feature, xtensor-python always copies the buffer when an array is passed across the wrapper.

c = xt.C()
self.assertEqual([0.]*10, c.array.tolist())
# use __setitem__ implemented in C++ to set the element.
c[0] = 3
self.assertEqual([3.] + [0.]*9, c.array.tolist())
# c.array returns a copy while I want a view.
arr = c.array
arr[1] = 1
# FIXME: this assertion fails
self.assertEqual([3., 1.] + [0.]*8, c.array.tolist())

yungyuc avatar Oct 01 '18 00:10 yungyuc

The view mentioned here is the strided view. The idea would be to have a similar class in xtensor-python that embeds a xensor-python container and the python objetc for view

JohanMabille avatar Oct 02 '18 04:10 JohanMabille