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
Unable to convert generator returning 1D arrays into a 2D array using fromiter/vstack #9525
Comments
Thanks for the nice report - I think this needs labelling as a feature request to support these functions. I also tried not using the list in the import numpy as np
import numba as nb
@nb.njit
def gen():
"""Generator function returning 1D-arrays."""
for i in range(3):
yield np.arange(i, i+3)
@nb.njit
def use_gen_vstack():
return np.vstack(gen())
print(use_gen_vstack()) and the error message says:
which is a bit odd, given that the list created from the generator isn't accepted, as in your original report. |
@gmarkall , thank you very much for your quick response.
The same error appears if you use pure numpy.
The overloaded Numba function in arrayobj.py expects a "BaseTuple".
As the function does not receive a BaseTuple as argument it seems to return the original numpy error message. |
It seems that numpy.fromiter is not supported in your current version of Numba. |
Observation: numpy.asarray fails to generate 2D-array from list of 1D-arrays Converting the generator into a list of 1D-arrays allows numpy.asarray to return a 2D-array.
If you apply type checking according to @overload(np.asarray) in arraymath.py from line 4291, function type_can_asarray rejects the ListType[array...).
This behavior seems to be related to: |
Unfortunately, the implementation of numpy.fromiter seems to be problematic.
@gmarkall should I open a separate issue to support advanced data types in the numpy.dtype implementation? |
The part of the message I thought was a problem was the implication that a list must be passed - your second example in the issue report appears to be passing a list but that still doesn't work.
I think if any of the snippets in that issue did run at some point, they might have fallen back to object mode - I can't get any of the code in that issue to run at present. A variation on those snippets I can get to run is: from numba import jit
import numpy as np
@jit(forceobj=True)
def func(x):
return x[0]
@jit(forceobj=True)
def error():
topo = [chr(ord('a') + i) for i in range(5)]
types = [(var_name, "int") for var_name in topo]
sampled = np.zeros(10, dtype=types)
return np.fromiter([func(x) for x in sampled], dtype=int)
print(error()) but you don't really want to be running in object mode, since its only effect is really to remove a tiny bit of interpreter loop overhead within your function. |
I think that seems like a good thing to do, to keep the discussions simpler to follow in issues - many thanks! |
@gmarkall no problem, I will open another issue for advanced use of numpy.dtype as feature request #9527 What about the issue in np.asarray where a typed list is not identified as a ListType?
This seems weird to me. Should I also open an issue on this matter or is this the expected behavior? |
What you observe above is expected. The from numba import njit, typed
@njit
def give_a_list():
return typed.List([1, 2, 3])
print(isinstance(give_a_list(), typed.List)) prints
|
@gmarkall thank you. The implementation of numpy.asarray converts a typed list of scalars into a numpy array.
|
I think that is correct. The docs are not very clear about what it might accept though: https://numba.readthedocs.io/en/stable/reference/numpysupported.html#other-functions |
@gmarkall thank you. Should I open an issue to support conversion of a typed list of 1D-arrays into a 2D-array using asarray? Edit:
Unfortunately, this method sometimes works and sometimes fails using typed lists.
Edit 2: Edit 3:
|
Feature Request
visible in the release notes
(https://numba.readthedocs.io/en/stable/release-notes-overview.html).
i.e. it's possible to run as 'python bug.py'.
I am failing to convert a generator function that returns 1D arrays into a 2D array using numba's fromiter or vstack implementation.
Here is a generator function returning 1D-arrays.
The use_gen_fromiter() function raises a TypingError.
I also tried using np.vstack() to stack the arrays obtained from the generator into a 2D array.
However, this also raises a TypingError.
A possible workaround could be converting the generator into a list and fill an array.
Can the additional step of converting the generator into a list be avoided?
Python: 3.12.0
Numpy: 1.26.4
Numba: 0.59.1
The text was updated successfully, but these errors were encountered: