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

question about "filter" over rows/cols #1846

Closed
tort-dla-psa opened this issue Dec 6, 2019 · 5 comments
Closed

question about "filter" over rows/cols #1846

tort-dla-psa opened this issue Dec 6, 2019 · 5 comments

Comments

@tort-dla-psa
Copy link

tort-dla-psa commented Dec 6, 2019

How to do so? Let's say, I have an xarray and I want to get 1st col is a shape of a col ({-1,1}) and 1st row ({1,-1}). But xt::filter seems to apply bool array to whole xarray, so if we have array{2,2}, array of filter should be 4 bools in size.

Is there any way to write desired functional not like below? It's very inefficient

xt::xarray<int> arr = xt::arange(4);
arr.reshape({2,2});
xt::xarray<bool> f = {true, false, true, false};

xt::xarray<int> f_col = xt::filter(arr, f);
f = {true, true, false, false};
f_col.reshape({-1,1});

auto f_row = xt::filter(arr, f);

std::cout<<arr<<"\n"
	<<f_col<<"\n"
	<<f_row<<"\n";

{{0, 1},
{2, 3}}
{{0},
{2}}
{0, 1}

@JohanMabille
Copy link
Member

JohanMabille commented Dec 6, 2019

Maybe views can help here?

auto row = xt::view(arr, 0, xt::all());
auto col = xt::view(arr, xt::all(), 0);

@tort-dla-psa
Copy link
Author

tort-dla-psa commented Dec 6, 2019

in my example yes, but i meant something like this:

>>> import numpy as np
>>> a = np.arange(6).reshape((2,-3))
>>> a
array([[0, 1, 2],
       [3, 4, 5]])
>>> b = np.ones(3, dtype=np.bool)
>>> b[1] = False
>>> b
array([ True, False,  True])
>>> c = a[:, b]
>>> c
array([[0, 2],
       [3, 5]])

in other words, how to apply filter just for one dimension?

@JohanMabille
Copy link
Member

There is actually a bug regarding the broadcasting of the condition in the filter function. A PR has been opened about that, however it is not complete.

You can ask explicitly for the broadcasting (it's a bit verbose but it is an acceptable workaround):

xt::xarray<int> a = xt::arange(6).reshape({2, -3});
xt::xarray<bool> filter = { true, false, true };
xt::xarray<int> res = xt::filter(a, xt::broadcast(filter, a.shape()));

Then you can reshape res to the shape you need.

You can also use the drop and keep slices with views:

xt::xarray<int> res = xt::view(a, xt::all(), xt::drop(2));

@tort-dla-psa
Copy link
Author

Oh, is there any plans on fixing that? Thanks for the workaround

@JohanMabille
Copy link
Member

Well ideally we want to fix everything, it is just a matter of time and ressources ;) We are busy with the core part for now, but index views and filters should be the next big refactoring after the next release.

In the meantime, if that's something urgent for you and/or if you want to contribute, feel free to open a PR, we will be happy to review it.

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

No branches or pull requests

2 participants