def test_basic4(self): """ Test that a dispatcher object can be used as input to another function with signature as part of a tuple """ a = 1 @njit def foo1(x): return x + 1 @njit def foo2(x): return x + 2 tup = (foo1, foo2) int_int_fc = types.FunctionType(types.int64(types.int64, )) @njit(types.int64(types.UniTuple(int_int_fc, 2))) def bar(fcs): x = 0 for i in range(2): x += fcs[i](a) return x self.assertEqual(bar(tup), foo1(a) + foo2(a))
def posterior_score_XOR_NXOR_3D(Z_n, U, V, X_n, l): M, _ = V.shape D, L = U.shape score = int64(0) for d in range(D): for m in range(M): if U[d, l] == 1 and V[m, l] == 1: # NXOR cant be changed by z_nl continue num_active = np.int8(0) for l_prime in range(L): if (U[d, l_prime] + Z_n[l_prime] + V[m, l_prime] != -1) and\ (l_prime != l): num_active += 1 if num_active > 1: break if num_active == 0: score -= X_n[d, m] * U[d, l] * V[m, l] elif num_active == 1: score += X_n[d, m] * U[d, l] * V[m, l] return score
def test_count_optional_arg_type_check(self): pyfunc = count_with_start_end_usecase def try_compile_bad_optional(*args): bad_sig = types.int64(types.unicode_type, types.unicode_type, types.Optional(types.float64), types.Optional(types.float64)) njit([bad_sig])(pyfunc) with self.assertRaises(TypingError) as raises: try_compile_bad_optional('tú quis?', 'tú', 1.1, 1.1) self.assertIn('The slice indices must be an Integer or None', str(raises.exception)) error_msg = "%s\n%s" % ("'{0}'.py_count('{1}', {2}, {3}) = {4}", "'{0}'.c_count_op('{1}', {2}, {3}) = {5}") sig_optional = types.int64(types.unicode_type, types.unicode_type, types.Optional(types.int64), types.Optional(types.int64)) cfunc_optional = njit([sig_optional])(pyfunc) py_result = pyfunc('tú quis?', 'tú', 0, 8) c_result = cfunc_optional('tú quis?', 'tú', 0, 8) self.assertEqual( py_result, c_result, error_msg.format('tú quis?', 'tú', 0, 8, py_result, c_result))
def posterior_score_XOR_NAND_3D(Z_n, U, V, X_n, l): D, L = U.shape M, _ = V.shape score = int64(0) for d in range(D): for m in range(M): if U[d, l] != 1 or V[m, l] != 1: # AND continue # compute deltaXOR-NAND num_active = np.int8(0) for l_prime in range(L): if ((Z_n[l_prime] != 1) or (U[d, l_prime] != 1) or (V[m, l_prime] != 1)) and\ (l_prime != l): num_active += 1 if num_active > 1: break if num_active == 0: score += X_n[d, m] elif num_active == 1: score -= X_n[d, m] return -score raise NotImplementedError
def posterior_score_XOR_AND_3D(Z_n, U, V, X_n, l): """ Return count of correct/incorrect explanations caused by setting Z[n,l] to 1, respecting explaining away dependencies TODO: should this be given a signature? """ D, L = U.shape M, _ = V.shape score = int64(0) for d in range(D): for m in range(M): if (U[d, l] != 1) or (V[m, l] != 1): # AND continue # compute deltaXOR-AND num_active = np.int8(0) for l_prime in range(L): if (Z_n[l_prime] == 1) and\ (U[d, l_prime] == 1) and\ (V[m, l_prime] == 1) and\ (l_prime != l): num_active += 1 if num_active > 1: break if num_active == 0: score += X_n[d, m] elif num_active == 1: score -= X_n[d, m] return score
def group(keys_list: List[np.ndarray], keys_dtype: List[int], keys_index: int, indexes: List[int], inplace=False): if keys_index >= len(keys_list): return [indexes] keys = keys_list[keys_index] dtype = keys_dtype[keys_index] if dtype == dtypes.ARRAY_TYPE_INT64: keys_int = keys.view(np.int64) groups_int = Dict.empty(key_type=types.int64, value_type=int_array) for index in indexes: key = keys_int[index] key = types.int64(0) groups_int.setdefault(key, []) print(groups_int[0]) groups_int[0] += [index] res = [] for val in groups_int.values(): res.extend(group(keys_list, keys_dtype, keys_index + 1, val)) return res elif dtype == dtypes.ARRAY_TYPE_FLOAT64: keys_float = keys.view(np.float64) groups_float = Dict.empty(key_type=types.float64, value_type=int_array) for index in indexes: key = keys_float[index] groups_float.setdefault(key, []) groups_float[key].append(index) res = [] for val in groups_float.values(): res.extend(group(keys_list, keys_dtype, keys_index + 1, val)) return res elif dtype & dtypes.STRING_TYPE_OFFSET_MASK == dtypes.ARRAY_TYPE_STRING: pass
def pd_range_index_getitem_impl(self, idx): range_len = len(self._data) # FIXME_Numba#5801: Numba type unification rules make this float idx = types.int64((range_len + idx) if idx < 0 else idx) if (idx < 0 or idx >= range_len): raise IndexError("RangeIndex.getitem: index is out of bounds") return self.start + self.step * idx
def posterior_score_OR_AND_3D(Z_n, U, V, X_n, l): """ Return count of correct/incorrect explanations caused by setting Z[n,l] to 1, respecting explaining away dependencies TODO: should this be given a signature? """ D, L = U.shape M, _ = V.shape score = int64(0) for d in range(D): for m in range(M): if (U[d, l] != 1) or (V[m, l] != 1): # AND continue alrdy_active = False for l_prime in range(L): if (Z_n[l_prime] == 1) and\ (U[d, l_prime] == 1) and\ (V[m, l_prime] == 1) and\ (l_prime != l): alrdy_active = True # OR break if alrdy_active is False: score += X_n[d, m] return score
def test_issue_5540(self): @njit(types.int64(types.int64)) def foo(x): return x + 1 @njit def bar_bad(foos): f = foos[0] return f(x=1) @njit def bar_good(foos): f = foos[0] return f(1) self.assertEqual(bar_good((foo, )), 2) # new/old style error handling with self.assertRaises( (errors.UnsupportedError, errors.TypingError)) as cm: bar_bad((foo, )) self.assertRegex( str(cm.exception), r'.*first-class function call cannot use keyword arguments')
def pd_int64_index_getitem_impl(self, idx): index_len = len(self._data) # FIXME_Numba#5801: Numba type unification rules make this float idx = types.int64((index_len + idx) if idx < 0 else idx) if (idx < 0 or idx >= index_len): raise IndexError("Int64Index.getitem: index is out of bounds") return self._data[idx]
def correct_predictions_counter(Z, U, X): N, D = X.shape count = int64(0) for n in prange(N): for d in prange(D): if output_fct(Z[n, :], U[d, :]) == X[n, d]: count += 1 return count
def pd_multi_index_getitem_idx_scalar_impl(self, idx): index_len = len(self) # FIXME_Numba#5801: Numba type unification rules make this float idx = types.int64((index_len + idx) if idx < 0 else idx) if (idx < 0 or idx >= index_len): raise IndexError("MultiIndex.getitem: index is out of bounds") return _multi_index_getitem_impl(self, idx)
def pd_range_index_ctor_impl(start=None, stop=None, step=None, dtype=None, copy=False, name=None, fastpath=None): if not (dtype is None or dtype_is_numpy_signed_int or dtype_is_unicode_str and dtype in ('int8', 'int16', 'int32', 'int64')): raise ValueError( "Incorrect `dtype` passed: expected signed integer") # TODO: add support of int32 type _start = types.int64(start) if start is not None else types.int64(0) if stop is None: _start, _stop = types.int64(0), types.int64(start) else: _stop = types.int64(stop) _step = types.int64(step) if step is not None else types.int64(1) if _step == 0: raise ValueError("Step must not be zero") return init_range_index(range(_start, _stop, _step), name)
def _get_code_for_label(seen_labels, label): _code = seen_labels.get(label, -1) if _code != -1: return _code res = len(seen_labels) seen_labels[label] = res return types.int64(res)
def test_basic2(self): """ Test that a dispatcher object *without* a pre-compiled overload can be used as input to another function with locked-down signature """ a = 1 @njit def foo(x): return x + 1 int_int_fc = types.FunctionType(types.int64(types.int64, )) @njit(types.int64(int_int_fc)) def bar(fc): return fc(a) self.assertEqual(bar(foo), foo(a))
def sizeof(typingctx, src): sig = types.int64(src) def codegen(context, builder, signature, args): return context.get_constant( sig.return_type, context.get_abi_sizeof(context.get_data_type(src.instance_type)) ) if isinstance(src, types.ClassType): return sig, codegen raise TypeError()
def test_basic3(self): """ Test that a dispatcher object *without* a pre-compiled overload can be used as input to another function with locked-down signature and that it behaves as a truly generic function (foo1 does not get locked) """ a = 1 @njit def foo1(x): return x + 1 @njit def foo2(x): return x + 2 int_int_fc = types.FunctionType(types.int64(types.int64, )) @njit(types.int64(int_int_fc)) def bar(fc): return fc(a) self.assertEqual(bar(foo1) + 1, bar(foo2))
def pd_range_index_ctor_impl(start=None, stop=None, step=None, dtype=None, copy=False, name=None, fastpath=None): if not (dtype is None or dtype == 'int64' or dtype_is_np_int64): raise TypeError("Invalid to pass a non-int64 dtype to RangeIndex") _start = types.int64(start) if start is not None else types.int64(0) if stop is None: _start, _stop = types.int64(0), types.int64(start) else: _stop = types.int64(stop) _step = types.int64(step) if step is not None else types.int64(1) if _step == 0: raise ValueError("Step must not be zero") return init_range_index(range(_start, _stop, _step), name)
def posterior_score_NAND_XOR_3D(Z_n, U, V, X_n, l): M, _ = V.shape D, L = U.shape score = int64(0) for d in range(D): for m in range(M): if U[d, l] == 1 and V[m, l] == 1: # XOR cant be changed by z_nl continue explained_away = False for l_prime in range(L): if (Z_n[l_prime] + U[d, l_prime] + V[m, l_prime] != -1) and\ (l_prime != l): explained_away = True break if explained_away is False: score += X_n[d, m] * U[d, l] * V[m, l] return -score
def test_basic5(self): a = 1 @njit def foo1(x): return x + 1 @njit def foo2(x): return x + 2 @njit def bar1(x): return x / 10 @njit def bar2(x): return x / 1000 tup = (foo1, foo2) tup_bar = (bar1, bar2) int_int_fc = types.FunctionType(types.int64(types.int64, )) flt_flt_fc = types.FunctionType(types.float64(types.float64, )) @njit((types.UniTuple(int_int_fc, 2), types.UniTuple(flt_flt_fc, 2))) def bar(fcs, ffs): x = 0 for i in range(2): x += fcs[i](a) for fn in ffs: x += fn(a) return x got = bar(tup, tup_bar) expected = foo1(a) + foo2(a) + bar1(a) + bar2(a) self.assertEqual(got, expected)
def test_issue_5540(self): @njit(types.int64(types.int64)) def foo(x): return x + 1 @njit def bar_bad(foos): f = foos[0] return f(x=1) @njit def bar_good(foos): f = foos[0] return f(1) self.assertEqual(bar_good((foo, )), 2) with self.assertRaises(errors.TypingError) as cm: bar_bad((foo, )) self.assertRegex( str(cm.exception), r".*first-class function call cannot use keyword arguments", )
def posterior_score_OR_NAND_3D(Z_n, U, V, X_n, l): M, _ = V.shape D, L = U.shape score = int64(0) for d in range(D): for m in range(M): if (U[d, l] == -1) or (V[m, l] == -1): # NAND continue alrdy_active = False for l_prime in range(L): if ((Z_n[l_prime] == -1) or (U[d, l_prime] == -1) or (V[m, l_prime] == -1)) and\ (l_prime != l): alrdy_active = True # OR break if alrdy_active is False: score += X_n[d, m] return -score
def mapResultArray(targetString, k, t, resultArray): minimizerMap = typed.Dict.empty(key_type=types.unicode_type, value_type=int_array)# value_type=typed.List(types.int64)) minmerOccurrences = typed.Dict.empty(key_type=types.unicode_type, value_type=types.int64) for i in resultArray: currMinmer = targetString[i:i+k] if 'N' not in currMinmer: try: # 1) Update minmerOccurrences. Referenced for filtering out minmers that aren't rare (> self.t occurrence) minmerOccurrences[currMinmer] += 1 # 2) If minmer is rare, update minimizerMap with new position where the current minmer occurs c = minmerOccurrences[currMinmer] if c <= t: minimizerMap[currMinmer][c-1] = i # numpy.asarray([i]) # = types.int64(i) # .append(i) else: del minimizerMap[currMinmer] except: # REF: https://numba.pydata.org/numba-doc/latest/user/troubleshoot.html minmerOccurrences[currMinmer] = types.int64(1) # list([i]) minimizerMap[currMinmer] = numpy.zeros(t, dtype=numpy.int64) # typed.List([i]) # numpy.asarray([i], dtype=numpy.int64)# minimizerMap[currMinmer][0] = i return minimizerMap
xe_connect_type = XeConnectType() register_model(XeConnectType)(models.OpaqueModel) class XeDSetType(types.Opaque): def __init__(self): super(XeDSetType, self).__init__(name='XeDSetType') xe_dset_type = XeDSetType() register_model(XeDSetType)(models.OpaqueModel) get_column_size_xenon = types.ExternalFunction( "get_column_size_xenon", types.int64(xe_connect_type, xe_dset_type, types.intp)) # read_xenon_col = types.ExternalFunction( # "c_read_xenon", # types.void( # string_type, # types.intp, # types.voidptr, # types.CPointer( # types.int64))) xe_connect = types.ExternalFunction("c_xe_connect", xe_connect_type(types.voidptr)) xe_open = types.ExternalFunction("c_xe_open", xe_dset_type(xe_connect_type, types.voidptr)) xe_close = types.ExternalFunction("c_xe_close", types.void(xe_connect_type, xe_dset_type)) # TODO: fix liveness/alias in Numba to be able to use arr.ctypes directly @intrinsic
from numpy import abs, exp from scipy.stats import poisson from numba import jit, prange, types @jit(types.int64(types.int64), nopython=True) def factorial(x): """ Numba-styled implementation of factorial calculation. :param x: (int) Required. :returns n: (int). """ n = 1 for i in range(2, x + 1): n *= i return n @jit(types.float64(types.int64, types.float64), nopython=True) def pois_survival_partial(x, mu): """ Implements the partial derivative of the poisson CDF with respect to lambda. :param x: (int) Required. The value in which the CDF is calculated from. Density is integers to the right of this number. :param mu: (float) Required. The mean at which to calculate the density with.
GrB_UnaryOp = OpContainer() GrB_BinaryOp = OpContainer() ################################## # Useful collections of signatures ################################## _unary_bool = [nt.boolean(nt.boolean)] _unary_int = [ nt.uint8(nt.uint8), nt.int8(nt.int8), nt.uint16(nt.uint16), nt.int16(nt.int16), nt.uint32(nt.uint32), nt.int32(nt.int32), nt.uint64(nt.uint64), nt.int64(nt.int64) ] _unary_float = [nt.float32(nt.float32), nt.float64(nt.float64)] _unary_all = _unary_bool + _unary_int + _unary_float _binary_bool = [nt.boolean(nt.boolean, nt.boolean)] _binary_int = [ nt.uint8(nt.uint8, nt.uint8), nt.int8(nt.int8, nt.int8), nt.uint16(nt.uint16, nt.uint16), nt.int16(nt.int16, nt.int16), nt.uint32(nt.uint32, nt.uint32), nt.int32(nt.int32, nt.int32), nt.uint64(nt.uint64, nt.uint64), nt.int64(nt.int64, nt.int64) ]
@box(CharType) def box_char(typ, val, c): """ """ fnty = lir.FunctionType(lir.IntType(8).as_pointer(), [lir.IntType(8)]) fn = c.builder.module.get_or_insert_function(fnty, name="get_char_ptr") c_str = c.builder.call(fn, [val]) pystr = c.pyapi.string_from_string_and_size( c_str, c.context.get_constant(types.intp, 1)) # TODO: delete ptr return pystr del_str = types.ExternalFunction("del_str", types.void(string_type)) _hash_str = types.ExternalFunction("_hash_str", types.int64(string_type)) get_c_str = types.ExternalFunction("get_c_str", types.voidptr(string_type)) @overload_method(StringType, 'c_str') def str_c_str(str_typ): return lambda s: get_c_str(s) @overload_method(StringType, 'join') def str_join(str_typ, iterable_typ): # TODO: more efficient implementation (e.g. C++ string buffer) def str_join_impl(sep_str, str_container): res = "" counter = 0 for s in str_container:
def try_compile_wrong_end_optional(*args): wrong_sig_optional = types.int64(types.unicode_type, types.unicode_type, types.Optional(types.intp), types.Optional(types.float64)) njit([wrong_sig_optional])(rfind_with_start_end_usecase)
register_model(XeConnectType)(models.OpaqueModel) class XeDSetType(types.Opaque): def __init__(self): super(XeDSetType, self).__init__(name='XeDSetType') xe_dset_type = XeDSetType() register_model(XeDSetType)(models.OpaqueModel) get_column_size_xenon = types.ExternalFunction( "get_column_size_xenon", types.int64(xe_connect_type, xe_dset_type, types.intp)) # read_xenon_col = types.ExternalFunction( # "c_read_xenon", # types.void( # string_type, # types.intp, # types.voidptr, # types.CPointer( # types.int64))) xe_connect = types.ExternalFunction("c_xe_connect", xe_connect_type(types.voidptr)) xe_open = types.ExternalFunction("c_xe_open", xe_dset_type(xe_connect_type, types.voidptr)) xe_close = types.ExternalFunction("c_xe_close", types.void(xe_connect_type, xe_dset_type))
def try_compile_bad_optional(*args): bad_sig = types.int64(types.unicode_type, types.unicode_type, types.Optional(types.float64), types.Optional(types.float64)) njit([bad_sig])(pyfunc)
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ***************************************************************************** import numpy as np import numba import sdc from numba import types, cgutils from numba.targets.arrayobj import make_array from numba.extending import overload, intrinsic, overload_method from sdc.str_ext import string_type from numba.ir_utils import (compile_to_numba_ir, replace_arg_nodes, find_callname, guard) _get_file_size = types.ExternalFunction("get_file_size", types.int64(types.voidptr)) _file_read = types.ExternalFunction( "file_read", types.void(types.voidptr, types.voidptr, types.intp)) _file_read_parallel = types.ExternalFunction( "file_read_parallel", types.void(types.voidptr, types.voidptr, types.intp, types.intp)) file_write = types.ExternalFunction( "file_write", types.void(types.voidptr, types.voidptr, types.intp)) _file_write_parallel = types.ExternalFunction( "file_write_parallel", types.void(types.voidptr, types.voidptr, types.intp, types.intp, types.intp))