Equivalent xtensor code for python numpy code
Hi All,
I am converting some python numpy code into cpp code using xtensor library. Pretty much things I understood but I am having some issues in indexing based operations. Here is the python code -
---- python code ----
boxes = decoded_boxes[keep, :]
// keep is a numpy boolean array of size 19248 (1d) // decoded_boxes is a numpy array with dim - 19248, 4 // box output dim is 24, 4
I am not able to write equivalent code for this. I tried this but did not work-
--- cpp code ---
xt::xarray<float> b1 = xt::filter(decoded_boxes, keep); // decoded_box and keep have same values and dim as in python
cout << " b1 shape "<<xt::adapt(b1.shape())<<endl; // gives only dim {24}
Any help would be helpful.
Thanks Akhilesh
It has something to do with the implementation of xt::filter. If we look at the source code we will find this piece:
template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL, class E, class O>
inline auto filter(E&& e, O&& condition) noexcept
{
auto indices = argwhere<L>(std::forward<O>(condition));
using view_type = xindex_view<xclosure_t<E>, decltype(indices)>;
return view_type(std::forward<E>(e), std::move(indices));
}
so basically what filter do is just creating a vector called indices and return a view based on indices. Then what is argwhere?
In the document, we get this:
return vector of indices where arr is not zero
In your case, arr is your keep array. Since your keep array is of length 19248, argwhere will only take the first 19248 elements into consideration, which is totally different from Numpy slices, where broadcast happens and your keep has shape 19248 * 4 after broadcasting.
So if you want to write equivalent code, maybe you can concat keep with itself for three times, just like manually broadcasting, and then use keep to filter. I 'm new to XTensor too so the solution is not elegant, I guess you can find a better way.