I would like to use Just-In-Time-Compiling with numba of a Python function, however my code contains a too complex dictionary for that. Its key data type is int, and its values are lists of int with different lengths.
I have a network graph with connected points (Fig. https://www.data-to-viz.com/graph/network_files/figure-html/unnamed-chunk-2-1.png). I am using the dictionary to store the neighbor points of each point. Because points can have differently many neighbors, the lists inside the dictionary have a different length (see code example below). Numba does not support this, and I am having a hard time to find an alternative.
My ideas:
- Extract the neighbor points from the pairs-list (see below) each time when I need to find the nighbors of some point. But this will slow down my code, which is the opposite of what I am trying to do with numba.
- Use 2d-numpy-array. However then I don't have variable length for each row resulting in a quite large memory usage.
I would be grateful for any ideas.
Minimum code example:
from numba.typed import Dict
from numba.typed import List
from numba import types
from numba import jit
# @jit(nopython=True) # works only without jit...
def f():
# 3 points: 0, 1, 2
# Only 0 and 1 as well as 0 and 2 are connected. 1 and 2 are not connected
pairs = [(0, 1), (2, 0)]
# Save the neighbor of each point in a dictionary
d = dict()
def add_element_to_dict_list(key: int, element: int, dict_list: dict):
if key in dict_list:
dict_list[key].append(element)
else:
dict_list[key] = [element]
for p1, p2 in pairs:
add_element_to_dict_list(p1, p2, d)
add_element_to_dict_list(p2, p1, d)
print(d)
f()