Use numba typed list in a function

Viewed 588

I'm trying to speed up some simple functions with numba. The first function below works, but gives a deprecation warning, that says reflection of lists is being removed and to use numba.typed.list.

from numba import jit
from numba.typed import List as NBList
import numpy as np
from typing import List


@jit(nopython=True)
def water_added_to_pp_refl(water_added: List[int], vessel_volume: int = 1000
                           ) -> np.ndarray:
    """convert list of amounts of water in mg to array of partial pressures for a given volume"""
    wadd_array = np.array(water_added)
    print(wadd_array)
    return np.divide(wadd_array, vessel_volume)

output_reflected = water_added_to_pp_refl([10, 25, 50, 75, 100, 150, 200, 250])  # this works, but warning

When I convert my list to a numba.list, it fails to create the array:

@jit(nopython=True)
def water_added_to_pp_NBList(water_added: List[int], vessel_volume: int = 1000
                             ) -> np.ndarray:
    """convert list of amounts of water in mg to array of partial pressures for a given volume"""
    water_list = NBList()
    [water_list.append(x) for x in water_added]
    wadd_array = np.array(water_list)
    print(wadd_array)
    return np.divide(wadd_array, vessel_volume)

output_NBListed = water_added_to_pp_NBList([10, 25, 50, 75, 100, 150, 200, 250]) # fails to create array

With error:

There are 2 candidate implementations:
   - Of which 2 did not match due to:
   Overload in function 'array': File: numba\core\typing\npydecl.py: Line 482.
     With argument(s): '(ListType[int64])':
    Rejected as the implementation raised a specific error:
      NotImplementedError: ListType[int64] cannot be represented as a Numpy dtype

I tried setting the dtype of the array to np.int64, but that didnt work.

How do I use the numba.typed.list with numpy arrays? Is it possible?

0 Answers
Related