xtensor icon indicating copy to clipboard operation
xtensor copied to clipboard

filter missing broadcasting? view missing selector?

Open tdegeus opened this issue 3 years ago • 3 comments

My eye crossed https://stackoverflow.com/q/74100940/2646505 . The goal there is to mimic

import numpy as np

keep = np.array([True, False, True])
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(A[keep, :])

In xtensor this is not possible it seems.

xt::filter does not broadcast:

#include <xtensor/xarray.hpp>
#include <xtensor/xtensor.hpp>
#include <xtensor/xsort.hpp>
#include <xtensor/xview.hpp>
#include <xtensor/xindex_view.hpp>
#include <xtensor/xio.hpp>

int main()
{
    xt::xtensor<bool, 1> keep = {{true, false, true}};
    xt::xtensor<int, 2> A = {{1, 2, 3}, {4, 5, 6}, {4, 5, 6}};
    auto c = xt::filter(A, keep);
    std::cout << c << std::endl;
    return 0;
}

Returns only the first column!?

{1, 3}

xt::view does not take logical

A syntax could be

int main()
{
    xt::xtensor<bool, 1> keep = {{true, false, true}};
    xt::xtensor<int, 2> A = {{1, 2, 3}, {4, 5, 6}, {4, 5, 6}};
    auto c = xt::view(A, xt::is_true(keep));
    std::cout << c << std::endl;
    return 0;
}

or have an overload on keep, but that might be a bit risky. What is the input is meant as logical, but given as ints?

(bonus) View should take xt::missing

I realised that it would be nice to do

import numpy as np

keep = np.array([True, False, True])
A = np.arange(3 * 4 * 5).reshape(3, 4, 5)
print(A[keep, ...])

as follows:

int main()
{
    xt::xtensor<bool, 1> keep = {{true, false, true}};
    xt::xtensor<int, 2> A = {{1, 2, 3}, {4, 5, 6}, {4, 5, 6}};
    auto c = xt::view(A, xt::is_true(keep), xt::missing);
    std::cout << c << std::endl;
    return 0;
}

tdegeus avatar Oct 21 '22 11:10 tdegeus

Yes, filter doesn't take broadcasting. I 've talked about the implementation of filter in https://github.com/xtensor-stack/xtensor/issues/2584, and IMO, it's natural to have broadcasting in filter. I 'm curious about the intent of this implementation.

27rabbit-penguin avatar Oct 24 '22 09:10 27rabbit-penguin

Sorry, I missed https://github.com/xtensor-stack/xtensor/issues/2584

tdegeus avatar Oct 24 '22 12:10 tdegeus

Hi,

I think it would be nice to have this over any possible axis, as in NumPy

auto c = xt::view(A, xt::drop(1), xt::keep(keep), xt::all());

Where keep is a dynamic array of bool the size of the relevant dimension, or a subset of indices to keep. Would broadcasting be able to solve the general (boolean) case?

AntoinePrv avatar Dec 14 '22 12:12 AntoinePrv