★ wanayoo — archive 1999 https://github.com/xtensor-stack/xtensor-python/issues/130Nouvelle recherche | Portail wanayoo
Skip to content
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

Bindings for views (or xstrided_views) #130

Open
tarcisiofischer opened this issue Jan 4, 2018 · 3 comments
Open

Bindings for views (or xstrided_views) #130

tarcisiofischer opened this issue Jan 4, 2018 · 3 comments

Comments

@tarcisiofischer
Copy link

@tarcisiofischer tarcisiofischer commented Jan 4, 2018

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
@wolfv
Copy link
Member

@wolfv wolfv commented Jun 21, 2018

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.

@yungyuc
Copy link
Contributor

@yungyuc yungyuc commented Oct 1, 2018

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())
@JohanMabille
Copy link
Member

@JohanMabille JohanMabille commented Oct 2, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.