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

[FEATURE REQUEST] implement numpy.take #661

Closed
v923z opened this issue Feb 16, 2024 · 7 comments
Closed

[FEATURE REQUEST] implement numpy.take #661

v923z opened this issue Feb 16, 2024 · 7 comments
Labels
enhancement New feature or request

Comments

@v923z
Copy link
Owner

v923z commented Feb 16, 2024

Describe the solution you'd like
Implement fancy indexing based on https://numpy.org/doc/stable/reference/generated/numpy.take.html

Additional context
The implementation would resolve #607 in the most frequent use cases.

@v923z v923z added the enhancement New feature or request label Feb 16, 2024
@v923z
Copy link
Owner Author

v923z commented Mar 26, 2024

@Derfies
Copy link
Contributor

Derfies commented Jul 22, 2024

@v923z sorry to have dropped off. I will pick this up for testing.

@Derfies
Copy link
Contributor

Derfies commented Jul 26, 2024

Hi @v923z just built this on windows using Ubuntu and it seems to have produced the firmware as expected. Running the following snippet however:

a = np.ndarray([1, 2, 3], dtype=np.uint8)
indices = np.ndarray([0], dtype=np.uint8)
print(np.take(a, indices))

...produces None in the console. Seems like I can use the 'out' kwarg to get a result, so I tried that too:

a = np.ndarray([1, 2, 3], dtype=np.uint8)
indices = np.ndarray([1], dtype=np.uint8)
out = np.empty(indices.shape, dtype=np.uint8)
np.take(a, indices, out=out)
print(out)

...produces array([0], dtype=uint8) where I would expect the value array([2], dtype=uint8). Changing the 'indices' array to take more values does indeed give me more values in the out array, however they always appear to be 0.

I'm no C++ dev by a long shot, but I can see the only return statement in create_take returns mp_const_none, so that's my guess for the first issue. Will keep reading this PR to see if there's an obvious reason for returning zeros.

@Derfies
Copy link
Contributor

Derfies commented Jul 27, 2024

Ok had a bit of a fiddle. It seems like we create the output array using ndarray_new_dense_ndarray but don't assign the elements to it. I tried:

for(size_t i=0; i < indices_len; i++) {
    ndarray_set_value(a->dtype, out->array, i, mp_obj_new_int(indices[i]));

Which didn't give me the results I hoped for, but I have to admit I'm stumbling in the dark at the moment!

Also just wondering is https://github.com/v923z/micropython-ulab/pull/651/files comparable functionality?

@v923z
Copy link
Owner Author

v923z commented Jul 27, 2024

Thanks for taking a look at it! I know that there is a glitch in the implementation. I'll try to fix it in the coming days.

@v923z
Copy link
Owner Author

v923z commented Oct 5, 2024

@Derfies Can you check out the code again? The test cases pass.

@v923z v923z mentioned this issue Oct 7, 2024
@v923z
Copy link
Owner Author

v923z commented Oct 9, 2024

Implemented in #688.

@v923z v923z closed this as completed Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants