|
| 1 | +import types |
| 2 | +from typing import Any, Callable, Dict, Optional, Tuple |
| 3 | + |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from pandas._typing import Scalar |
| 7 | +from pandas.compat._optional import import_optional_dependency |
| 8 | + |
| 9 | + |
| 10 | +def make_rolling_apply( |
| 11 | + func: Callable[..., Scalar], |
| 12 | + args: Tuple, |
| 13 | + nogil: bool, |
| 14 | + parallel: bool, |
| 15 | + nopython: bool, |
| 16 | +): |
| 17 | + """ |
| 18 | + Creates a JITted rolling apply function with a JITted version of |
| 19 | + the user's function. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + func : function |
| 24 | + function to be applied to each window and will be JITed |
| 25 | + args : tuple |
| 26 | + *args to be passed into the function |
| 27 | + nogil : bool |
| 28 | + nogil parameter from engine_kwargs for numba.jit |
| 29 | + parallel : bool |
| 30 | + parallel parameter from engine_kwargs for numba.jit |
| 31 | + nopython : bool |
| 32 | + nopython parameter from engine_kwargs for numba.jit |
| 33 | +
|
| 34 | + Returns |
| 35 | + ------- |
| 36 | + Numba function |
| 37 | + """ |
| 38 | + numba = import_optional_dependency("numba") |
| 39 | + |
| 40 | + if parallel: |
| 41 | + loop_range = numba.prange |
| 42 | + else: |
| 43 | + loop_range = range |
| 44 | + |
| 45 | + if isinstance(func, numba.targets.registry.CPUDispatcher): |
| 46 | + # Don't jit a user passed jitted function |
| 47 | + numba_func = func |
| 48 | + else: |
| 49 | + |
| 50 | + @numba.generated_jit(nopython=nopython, nogil=nogil, parallel=parallel) |
| 51 | + def numba_func(window, *_args): |
| 52 | + if getattr(np, func.__name__, False) is func or isinstance( |
| 53 | + func, types.BuiltinFunctionType |
| 54 | + ): |
| 55 | + jf = func |
| 56 | + else: |
| 57 | + jf = numba.jit(func, nopython=nopython, nogil=nogil) |
| 58 | + |
| 59 | + def impl(window, *_args): |
| 60 | + return jf(window, *_args) |
| 61 | + |
| 62 | + return impl |
| 63 | + |
| 64 | + @numba.jit(nopython=nopython, nogil=nogil, parallel=parallel) |
| 65 | + def roll_apply( |
| 66 | + values: np.ndarray, begin: np.ndarray, end: np.ndarray, minimum_periods: int, |
| 67 | + ) -> np.ndarray: |
| 68 | + result = np.empty(len(begin)) |
| 69 | + for i in loop_range(len(result)): |
| 70 | + start = begin[i] |
| 71 | + stop = end[i] |
| 72 | + window = values[start:stop] |
| 73 | + count_nan = np.sum(np.isnan(window)) |
| 74 | + if len(window) - count_nan >= minimum_periods: |
| 75 | + result[i] = numba_func(window, *args) |
| 76 | + else: |
| 77 | + result[i] = np.nan |
| 78 | + return result |
| 79 | + |
| 80 | + return roll_apply |
| 81 | + |
| 82 | + |
| 83 | +def generate_numba_apply_func( |
| 84 | + args: Tuple, |
| 85 | + kwargs: Dict[str, Any], |
| 86 | + func: Callable[..., Scalar], |
| 87 | + engine_kwargs: Optional[Dict[str, bool]], |
| 88 | +): |
| 89 | + """ |
| 90 | + Generate a numba jitted apply function specified by values from engine_kwargs. |
| 91 | +
|
| 92 | + 1. jit the user's function |
| 93 | + 2. Return a rolling apply function with the jitted function inline |
| 94 | +
|
| 95 | + Configurations specified in engine_kwargs apply to both the user's |
| 96 | + function _AND_ the rolling apply function. |
| 97 | +
|
| 98 | + Parameters |
| 99 | + ---------- |
| 100 | + args : tuple |
| 101 | + *args to be passed into the function |
| 102 | + kwargs : dict |
| 103 | + **kwargs to be passed into the function |
| 104 | + func : function |
| 105 | + function to be applied to each window and will be JITed |
| 106 | + engine_kwargs : dict |
| 107 | + dictionary of arguments to be passed into numba.jit |
| 108 | +
|
| 109 | + Returns |
| 110 | + ------- |
| 111 | + Numba function |
| 112 | + """ |
| 113 | + |
| 114 | + if engine_kwargs is None: |
| 115 | + engine_kwargs = {} |
| 116 | + |
| 117 | + nopython = engine_kwargs.get("nopython", True) |
| 118 | + nogil = engine_kwargs.get("nogil", False) |
| 119 | + parallel = engine_kwargs.get("parallel", False) |
| 120 | + |
| 121 | + if kwargs and nopython: |
| 122 | + raise ValueError( |
| 123 | + "numba does not support kwargs with nopython=True: " |
| 124 | + "https://github.com/numba/numba/issues/2916" |
| 125 | + ) |
| 126 | + |
| 127 | + return make_rolling_apply(func, args, nogil, parallel, nopython) |
0 commit comments