maybe_numba_jit

function of dascore.utils.jit source

maybe_numba_jit(
    required = False,
    _missing_numba = False,
    **compiler_kwargs ,
)

Use numba to apply JIT compilation to the decorated function.

Parameters

Parameter Description
required If True an ImportError is raised if the wrapped function is called
and the compiler module is not installed. If False, issue a warning.
_missing_numba If true, simulate missing the numba package. Only used for testing.
**compiler_kwargs Keyword arguments passed to the compiler function.

Examples

import numpy as np
from dascore.utils.jit import maybe_numba_jit

# JIT function for fast numba compilation
@maybe_numba_jit(nopython=True, nogil=True)
def jit_func(array):
    ...

# You can use the numba module inside the function without importing
# it because numba is added to the functions globals by the decorator.
# To get linters to ignore this seemingly undefined variable use #noqa
@maybe_numba_jit
def jit_func(array):
    for a in numba.prange(len(array)):  # noqa
        pass
    return array

out = jit_func(np.array([1,2,3]))
Note
  • By design, the warning/exception is only raised when calling the wrapped function. Users who don’t use the function should be unaffected.
  • After the jit the original function can be accessed via the func attribute. This is useful to testing in python mode.