1

I made this function, but numba always give me error. Both chr_pos and pos are 1D arrays. What can be the problem?

@nb.njit
def create_needed_pos(chr_pos, pos):
    needed_pos=[]
    needed_pos=np.array(needed_pos,dtype=np.float64)
    for i in range(len(chr_pos)):
        for k in range(len(pos)):
            if chr_pos[i] == pos[k]:
                if i==1 and k==1:
                    needed_pos=pos[k]
                else:
                    a=pos[k]
                    needed_pos=np.append(needed_pos,[a])
    return needed_pos

needed_pos=create_needed_pos(chr_pos, pos)

The errors:

warnings.warn(errors.NumbaDeprecationWarning(msg,
<input>:1: NumbaWarning: 
Compilation is falling back to object mode WITHOUT looplifting enabled because Function "create_needed_pos" failed type inference due to: Cannot unify array(float64, 1d, C) and int32 for 'needed_pos.1', defined at <input> (5)
File "<input>", line 5:
<source missing, REPL/exec in use?>
During: typing of intrinsic-call at <input> (9)
File "<input>", line 9:
<source missing, REPL/exec in use?>

1 Answer 1

2

The message

Cannot unify array(float64, 1d, C) and int32 for 'needed_pos.1'

is telling you that you are trying to assign an integer variable to an array. That happens in this line:

        needed_pos=pos[k]

You can do that in normal Python, but Numba requires static types. You must assign an array of floats to an array of floats. For example, replacing the line by

        needed_pos = pos[k:k+1]

The same error message says you are trying to assign an int, and this indicates that pos receives an array of ints. You must pass an array of floats instead.

After those changes, Numba still complains here:

needed_pos = []
needed_pos = np.array(needed_pos, dtype=np.float64)

with the message

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Cannot infer the type of variable 'needed_pos', have imprecise type: list(undefined)<iv=None>.

because it doesn't know the type of the elements that needed_pos will contain.

You can replace those two lines with one that creates an array of size zero with a known type:

needed_pos = np.array((0,), dtype=np.float64)

Now the program compiles and produces the same result with or without Numba.

But a problem remains. Numpy arrays work best when they have a fixed size. If you are continuously adding elements you'd better use lists (Numba lists in this case). This way for example:

@nb.njit
def create_needed_pos(chr_pos, pos):
    needed_pos = nb.typed.List.empty_list(nb.float64)
    for i in range(len(chr_pos)):
        for k in range(len(pos)):
            if chr_pos[i] == pos[k]:
                if i == k == 1:
                    needed_pos = nb.typed.List([pos[k]])
                else:
                    needed_pos.append(pos[k])
    return needed_pos
Sign up to request clarification or add additional context in comments.

4 Comments

thank you for your fast answer. I tried to run your function, but get this error: "Traceback (most recent call last): File "<input>", line 2, in <module> File "C:\Users\User\PycharmProjects\pythonProject\venv\lib\site-packages\numba\core\dispatcher.py", line 420, in _compile_for_args error_rewrite(e, 'typing') numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend) Cannot unify ListType[float64] and ListType[int32] for 'needed_pos.4', defined at <input> (10) "
My example follows the types in your original example. But Cannot unify ListType[float64] and ListType[int32] implies you are passing an array of int32 in argument pos. If you need to pass int32 values, you must declare needed_pos as nb.typed.List.empty_list(nb.int32).
thank you, it's working perfectly! Thanks for help!
Please mark the answer as accepted if you're happy with it. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.