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

@vectorize doesn't work with Numpy arrays (NotImplementedError) #5175

Closed
tjorda01 opened this issue Jan 31, 2020 · 7 comments · Fixed by #5297
Closed

@vectorize doesn't work with Numpy arrays (NotImplementedError) #5175

tjorda01 opened this issue Jan 31, 2020 · 7 comments · Fixed by #5297
Labels
no action required No action was needed to resolve.

Comments

@tjorda01
Copy link

When I use @vectorize with Numpy arrays, Numba displays this error message: NotImplementedError: array(float64, 2d, A) cannot be represented as a Numpy dtype

I'm using Numba version 0.48.0

Here source code that reproduces the error:

import numpy as np
from numba import prange, vectorize, float64, float32
from matplotlib import pyplot as plt

@vectorize([float64[:,:](float64[:,:], float64[:,:])])
def ndvi(img_nir:np.ndarray, img_red:np.ndarray) -> np.ndarray:
    fillvalue = 0
    dims = img_nir.shape
    out_img = np.full(dims, fillvalue, dtype=img_nir.dtype)
    a = dims[0]
    b = dims[1]
    for y in range(a):
        for x in range(b):
            out_img[y,x] = (img_nir[y,x] - img_red[y,x]) / (img_nir[y,x] + img_red[y,x])
    return out_img

if __name__ == '__main__':
    tile_shape = (1024, 1024)
    array1 = np.random.default_rng().uniform(low=0.0, high=10000.0, size=tile_shape)
    array2 = np.random.default_rng().uniform(low=0.0, high=10000.0, size=tile_shape)
    print(f"  image dtype = {array1.dtype}")
    
    ndvi_array = ndvi(array1, array2)

    plt.imshow(ndvi_array, cmap='jet')
    plt.title("NDVI w/vectorize")
    plt.tight_layout()
    plt.colorbar()
    plt.show()

I also tried replacing range with prange, and removing type hints from the function signature but the results are the same.

Stacktrace

Traceback (most recent call last):
  File "C:\data\dev\git_gpa\src\examples\numba_parallel.py", line 38, in <module>
    def ndvi_njit(img_nir:np.ndarray, img_red:np.ndarray) -> np.ndarray:
  File "C:\apps\Anaconda3\envs\img_proc3\lib\site-packages\numba\npyufunc\decorators.py", line 120, in wrap
    vec.add(sig)
  File "C:\apps\Anaconda3\envs\img_proc3\lib\site-packages\numba\npyufunc\dufunc.py", line 163, in add
    return self._compile_for_argtys(args, return_type)
  File "C:\apps\Anaconda3\envs\img_proc3\lib\site-packages\numba\npyufunc\dufunc.py", line 213, in _compile_for_argtys
    cres, actual_sig)
  File "C:\apps\Anaconda3\envs\img_proc3\lib\site-packages\numba\npyufunc\ufuncbuilder.py", line 180, in _build_element_wise_ufunc_wrapper
    dtypenums = [as_dtype(a).num for a in signature.args]
  File "C:\apps\Anaconda3\envs\img_proc3\lib\site-packages\numba\npyufunc\ufuncbuilder.py", line 180, in <listcomp>
    dtypenums = [as_dtype(a).num for a in signature.args]
  File "C:\apps\Anaconda3\envs\img_proc3\lib\site-packages\numba\numpy_support.py", line 152, in as_dtype
    % (nbtype,))
NotImplementedError: array(float64, 2d, A) cannot be represented as a Numpy dtype
@esc esc added the needtriage label Jan 31, 2020
@esc
Copy link
Member

esc commented Jan 31, 2020

@tjorda01 thanks for reporting this, I can reproduce locally, but this will still need some triage.

@tjorda01
Copy link
Author

tjorda01 commented Jan 31, 2020

@esc Thanks for your prompt reply and confirming that you're able to reproduce.

@esc
Copy link
Member

esc commented Jan 31, 2020

@tjorda01 I have looked at this in more detail, and it seems you may not be using @vectorize correctly. It works on scalars and not on arrays as described here http://numba.pydata.org/numba-doc/latest/user/vectorize.html#the-vectorize-decorator --- So, if you change the signature to @vectorize([float64(float64, float64)]) the function will compile, but not run, as you access the array api, e.g. shape, within the function body. If you want to do this on arrays, please have a look at @guvectorize here: http://numba.pydata.org/numba-doc/latest/user/vectorize.html#the-guvectorize-decorator which may be more along the lines of what you want. Hope that helps?

@esc
Copy link
Member

esc commented Feb 3, 2020

@tjorda01 do you require any additional advice on this one, or may I close it?

@esc esc added no action required No action was needed to resolve. and removed needtriage labels Feb 3, 2020
@tjorda01
Copy link
Author

tjorda01 commented Feb 3, 2020 via email

@esc
Copy link
Member

esc commented Feb 3, 2020

@tjorda01 indeed, that would be a good idea. Pull-requests are welcome, as usual. 🙏

stuartarchibald added a commit to stuartarchibald/numba that referenced this issue Feb 21, 2020
@stuartarchibald
Copy link
Contributor

@tjorda01 indeed, that would be a good idea. Pull-requests are welcome, as usual. pray

done in #5297

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no action required No action was needed to resolve.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants