question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement np.shape for nestedarray

See original GitHub issue

Feature request

numba currently (0.54.1) doesn’t implement np.shape for nestedarrays from numpy record types. Support for this would simplify some generic code a little bit.

import numba
import numpy as np

dtype = np.dtype([
    ("a", np.float64, (10,))
])

data = np.zeros((), dtype=dtype)[()]

@numba.njit
def foo(data):
    #return data.a.shape  # this works
    return np.shape(data.a)

foo(data)  # Throws a TypeError
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function shape at 0x7fb0b028e430>) found for signature:
 
 >>> shape(nestedarray(float64, (10,)))
 
There are 2 candidate implementations:
  - Of which 2 did not match due to:
  Overload in function 'np_shape': File: numba/np/arrayobj.py: Line 1981.
    With argument(s): '(nestedarray(float64, (10,)))':
   Rejected as the implementation raised a specific error:
     TypingError: Failed in nopython mode pipeline (step: nopython frontend)
   No implementation of function Function(<function asarray at 0x7fb0b026a040>) found for signature:
    
    >>> asarray(nestedarray(float64, (10,)))
    
   There are 2 candidate implementations:
         - Of which 2 did not match due to:
         Overload in function 'np_asarray': File: numba/np/arraymath.py: Line 4196.
           With argument(s): '(nestedarray(float64, (10,)))':
          Rejected as the implementation raised a specific error:
            LoweringError: Failed in nopython mode pipeline (step: native lowering)
          Can only insert double at [0] in [10 x double]: got i8*
          
          File "../../../miniconda3/envs/aesara-numba/lib/python3.9/site-packages/numba/np/arraymath.py", line 4208:
                      def impl(a, dtype=None):
                          return a
                          ^
          
          During: lowering "return $4return_value.1" at /home/anna/miniconda3/envs/aesara-numba/lib/python3.9/site-packages/numba/np/arraymath.py (4208)
     raised from /home/anna/miniconda3/envs/aesara-numba/lib/python3.9/site-packages/numba/core/errors.py:786
   
   During: resolving callee type: Function(<function asarray at 0x7fb0b026a040>)
   During: typing of call at /home/anna/miniconda3/envs/aesara-numba/lib/python3.9/site-packages/numba/np/arrayobj.py (1987)
   
   
   File "../../../miniconda3/envs/aesara-numba/lib/python3.9/site-packages/numba/np/arrayobj.py", line 1987:
       def impl(a):
           return np.asarray(a).shape
           ^

  raised from /home/anna/miniconda3/envs/aesara-numba/lib/python3.9/site-packages/numba/core/typeinfer.py:1074

During: resolving callee type: Function(<function shape at 0x7fb0b028e430>)
During: typing of call at /tmp/ipykernel_17638/1962980170.py (14)


File "../../../../../tmp/ipykernel_17638/1962980170.py", line 14:
<source missing, REPL/exec in use?>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gmarkallcommented, Nov 25, 2021

With #7359, the following:

import numba
import numpy as np

dtype = np.dtype([
    ("a", np.float64, (10,))
])

data = np.zeros((), dtype=dtype)[()]

@numba.njit
def foo(data):
    return np.shape(data.a)

print(foo(data))

prints

(10,)

So I think this will be resolved by #7359.

0reactions
astoerikocommented, Nov 25, 2021

So I think this will be resolved by #7359.

That sounds great, thanks for testing! I wanted to test it for a while but did not actually get around to do so.

Are there any similar use cases that ought to be checked? Can you try with #7359 and check if it works for all your intended use cases?

I will try with my use cases and get back to you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Find shape of nested array - python - Stack Overflow
I have a numpy array like this:
Read more >
How to Get NumPy Array Shape? - Spark by {Examples}
To get the shape of a Python NumPy array use numpy.ndarray.shape property. The array shape can be defined as the number of elements...
Read more >
How to Convert From Numpy Array to List - Sharp Sight
This tutorial will show you how to convert from Numpy array to list. It shows the syntax of the .tolist() function, and clear...
Read more >
NumPy Array Shape - GeeksforGeeks
In NumPy we will use an attribute called shape which returns a tuple, the elements of the tuple give the lengths of the...
Read more >
Array model - APL Wiki
An array's shape is then a vector whose elements are axis lengths, ... In nested array theory the elements can only be arrays, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found