Numba: How can I copy the contents of a heterogeneous value Python dictionary into a Numba Dict?

Viewed 170

I have a Python dictionary with homogeneous keys (strings) and heterogeneous values (floats, strings and arrays), something like:

earth = {
        'name'  : 'planet'
        'size'  : np.radians(2),
        'ofy'   : 384.4E6,
        'az'    : np.linspace(10,80,15)
        'alt'   : np.radians(-np.array([90])) 
        }

I'd like to pass this to one of my njit-ed Numba functions. First I tried passing it directly, and I got the error:

cannot determine Numba type of <class 'dict'>  

Then I learned about the Numba implementation of dictionaries, but I could only find instructions on initializing dictionaries with homogeneous values, as follows:

from numba.typed import Dict
from numba import types
d = Dict.empty(
    key_type=types.unicode_type,
    value_type=types.float64)

which obviously does not work if I try to assign values of multiple types.

Then I found "Heterogeneous Literal String Key Dictionary" in the Numba Docs. I tried initializing my dictionaries as Heterogenous Literal String Key Dictionaries by adding the @njit decorator atop the function that generates them, but it caused some issues as I frequently iterate over the dictionaries and the list in which they are stored, which raises errors such as:

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of getiter with parameters (LiteralList((LiteralStrKey[Dict]({Literal[str](name): Literal[str](PLC), ......

I tried making the LiteralList of Dicts iterable with literal_unroll(), but this did not seem to work, and as I was beginning to veer into experimental feature territory, I decided against pursuing this route:

numba.core.errors.UnsupportedError: Failed in nopython mode pipeline (step: handles literal_unroll)
Failed in literal_unroll_subpipeline mode pipeline (step: switch const list for tuples)
Found non-constant value at position 0 in a list argument to literal_unroll

Therefore I would prefer for the dictionaries to remain initialized as Python dictionaries for general purposes in my code, and for the ease of iterating over my list of dictionaries. I wish only to convert an existing dictionary to a Numba Dict if I need to pass one to a Numba function.

So my question is if there is some way to convert existing Python dicts into a Numba Heterogeneous Literal String Key Dictionary, for instance by initializing an empty LiteralStrKey[Dict] and copying the contents into it via a loop?

Thanks!

0 Answers
Related