pythonnumba# numba :cannot determine Numba type of <class 'function'> python

Here is my code.

```
from numba import jit
import numpy as np
import time
from pandas.core.common import flatten
from numba import njit
b_wi=[[1,2,3,4],[6,7,8,9,10,11]] #b_wi is a subset of x
f_wi=[[5,4,2,7,9],[5,4,3,7,2,3,4]]
# @jit(nopython=True)
def stopF_w(x,b_wi,f,di):
if di=='left':
return f[np.searchsorted(-b_wi,-x,side='left')]
if di=='right':
return f[np.searchsorted( b_wi, x,side='right')]
# this f :need to be an element in f_nslst
#b_wi :need to be an element in b_wilst
@njit(parallel=True)
def averageF_w(x,b_wilst,f_nslst,di):
a=np.zeros(x.shape[0])
for b_wi,f in zip(b_wilst,f_nslst):
a[:] += stopF_w(x,np.asarray(b_wi),np.asarray(f),di)
return a
intval= np.unique(list(flatten(b_wi)))
x=np.concatenate(([-10000],(intval[:-1]+intval[1:])/2,[10000])) #b_wi is a subset of x. That is why I can use this.
averageF_w(x,b_wi,f_wi,'right')
```

It raises error: TypingError: cannot determine Numba type of <class 'function'>. Does anyone know how to fix it please? Thank you very much.

Solution

There are several problems to handle: first, you've commented out the `@jit`

decorator of your first function, `stopF_w`

.

If you uncomment it, you'll resolve your current error. Unfortunately, you will immediately run into several other errors. if your numba version is up to date, you'll see an error pertaining to "reflected lists".

Basically, your inputs `b_wi`

and `f_wi`

are lists of variable length lists, which cannot be converted into uniform numpy arrays. E.g.: if instead of `[[1,2,3,4],[6,7,8,9,10,11]]`

, if `b_wi`

was something like `[[1,2,3, 4, 6], [7, 8, 9, 10, 11]]`

(easily convertible to an array of shape (2, 5) then it would work without any problems. To get your variable length lists to work with numba, you need to rely on a Typed List, which is a bit cumbersome.

```
from numba import jit
import numpy as np
import time
from pandas.core.common import flatten
from numba import njit
from numba.typed import List
b_wi=[[1,2,3,4], [6,7,8,9,10,11]]
f_wi=[[5,4,2,7,9], [5,4,3,7,2,3,4]]
###########################
# Create typed Lists
###########################
b_wi_nb = List()
for i in range(len(b_wi)):
b = List()
for j in range(len(b_wi[i])):
b.append(b_wi[i][j])
b_wi_nb.append(b)
f_wi_nb = List()
for i in range(len(f_wi)):
f = List()
for j in range(len(f_wi[i])):
f.append(f_wi[i][j])
f_wi_nb.append(f)
```

We will use `b_wi_nb`

and `f_wi_nb`

as inputs later on.

Another problem: the function `stopF_w`

has two "if" blocks. If neither condition is met, is may return None, which is unacceptable to numba for your use case. So alongside uncommenting the jit decorator, you also need to change the condition to if-else, or if-elif-else as applicable.

```
@jit(nopython=True)
def stopF_w(x,b_wi,f,di):
if di=='left':
return f[np.searchsorted(-b_wi,-x,side='left')]
else:
return f[np.searchsorted( b_wi, x,side='right')]
```

After the above changes, numba should work.

```
@jit(nopython=True, parallel=True)
def averageF_w(x, b_wilst, f_nslst, di):
a = np.zeros(x.shape[0])
for b_wi, f in zip(b_wilst,f_nslst):
a += stopF_w(x, np.asarray(b_wi), np.asarray(f), di)
return a
intval= np.unique(list(flatten(b_wi)))
x=np.concatenate(([-10000],(intval[:-1]+intval[1:])/2,[10000]))
##############################
# initial compiles
##############################
stopF_w(np.arange(1),np.arange(1),np.arange(1),'right')
averageF_w(np.arange(10),np.arange(6).reshape(2, 3),np.arange(6).reshape(2, 3),'right')
##############################
averageF_w(x, b_wi_nb, f_wi_nb,'right')
```

- Python Jinja2 LaTeX Table
- Getting attributes of a class
- How can I print many significant figures in Python?
- How to allow list append() method to return the new list
- Calculate Last Friday of Month in Pandas
- Python type hint for Iterable[str] that isn't str
- How to iterate over a list in chunks
- How to exit the entire application from a Python thread?
- Running shell command and capturing the output
- How do I pass a variable by reference?
- Convert range(r) to list of strings of length 2 in python
- How can I get the start and end dates for each week?
- how to use send_message() in python-telegram-bot
- Python conditional replacement based on element type
- How can I count the number of items in an arbitrary iterable (such as a generator)?
- Find longest consecutive range of numbers in list
- Insert text in braces with asyncpg
- How does one put a link / url to the web-site's home page in Django?
- How to determine if a path is a subdirectory of another?
- Custom Keybindings for Ipython terminal
- FastAPI asynchronous background tasks blocks other requests?
- How to make sure that information from one file is duplicated into several text documents, without specific lines
- Installing a Python environment with Anaconda
- sklearn pipeline model predicting same results for all input
- Brew command not found after installing Anaconda Python
- How to get an XPath from selenium webelement or from lxml?
- Pipe PuTTY console to Python script
- How to align the axes of a figure in matplotlib?
- Persist ParentDocumentRetriever of langchain
- How to reset index in a pandas dataframe?