def test_len(self): pyfunc = len_usecase cr = compile_isolated(pyfunc, [types.Tuple((types.int64, types.float32))]) self.assertPreciseEqual(cr.entry_point((4, 5)), 2) cr = compile_isolated(pyfunc, [types.UniTuple(types.int64, 3)]) self.assertPreciseEqual(cr.entry_point((4, 5, 6)), 3)
def __init__(self, dmm, fe_type): ndim = fe_type.ndim members = [ ('shape', types.UniTuple(types.intp, ndim)), ('indices', types.EphemeralArray(types.intp, ndim)), ('exhausted', types.EphemeralPointer(types.boolean)), ] super(NdIndexModel, self).__init__(dmm, fe_type, members)
def test_size_mismatch(self): # Issue #1638: tuple size should be checked when unboxing tuple_type = types.UniTuple(types.int32, 2) cr = compile_isolated(tuple_first, (tuple_type,)) with self.assertRaises(ValueError) as raises: cr.entry_point((4, 5, 6)) self.assertEqual(str(raises.exception), "size mismatch for tuple, expected 2 element(s) but got 3")
def test_optional_tuple(self): # Unify to optional tuple aty = types.none bty = types.UniTuple(i32, 2) self.assert_unify(aty, bty, types.Optional(types.UniTuple(i32, 2))) aty = types.Optional(types.UniTuple(i16, 2)) bty = types.UniTuple(i32, 2) self.assert_unify(aty, bty, types.Optional(types.UniTuple(i32, 2))) # Unify to tuple of optionals aty = types.Tuple((types.none, i32)) bty = types.Tuple((i16, types.none)) self.assert_unify( aty, bty, types.Tuple((types.Optional(i16), types.Optional(i32)))) aty = types.Tuple((types.Optional(i32), i64)) bty = types.Tuple((i16, types.Optional(i8))) self.assert_unify( aty, bty, types.Tuple((types.Optional(i32), types.Optional(i64))))
def test_tuples(self): v = (1, 2) self.assertEqual(typeof(v), types.UniTuple(types.intp, 2)) v = (1, (2.0, 3)) self.assertEqual( typeof(v), types.Tuple((types.intp, types.Tuple( (types.float64, types.intp)))))
def check_ndindex(self, flags=no_pyobj_flags): pyfunc = gen_ndindex cr = compile_isolated(pyfunc, (types.UniTuple(types.intp, 2),), flags=flags) shape = (2, 3) pygen = pyfunc(shape) cgen = cr.entry_point(shape) self.check_generator(pygen, cgen)
def check_slice(self, pyfunc): tup = (4, 5, 6, 7) cr = compile_isolated(pyfunc, [types.UniTuple(types.int64, 4)]) self.assertPreciseEqual(cr.entry_point(tup), pyfunc(tup)) cr = compile_isolated(pyfunc, [ types.Tuple((types.int64, types.int32, types.int64, types.int32)) ]) self.assertPreciseEqual(cr.entry_point(tup), pyfunc(tup))
class NdIndexIter(cgutils.Structure): """ .ndindex() implementation. """ _fields = [ ('shape', types.UniTuple(types.intp, ndim)), ('indices', types.CPointer(types.intp)), ('exhausted', types.CPointer(types.boolean)), ] def init_specific(self, context, builder, shapes): zero = context.get_constant(types.intp, 0) indices = cgutils.alloca_once(builder, zero.type, size=context.get_constant( types.intp, ndim)) exhausted = cgutils.alloca_once_value(builder, cgutils.false_byte) for dim in range(ndim): idxptr = cgutils.gep(builder, indices, dim) builder.store(zero, idxptr) # 0-sized dimensions really indicate an empty array, # but we have to catch that condition early to avoid # a bug inside the iteration logic. dim_size = shapes[dim] dim_is_empty = builder.icmp(lc.ICMP_EQ, dim_size, zero) with cgutils.if_unlikely(builder, dim_is_empty): builder.store(cgutils.true_byte, exhausted) self.indices = indices self.exhausted = exhausted self.shape = cgutils.pack_array(builder, shapes) def iternext_specific(self, context, builder, result): zero = context.get_constant(types.intp, 0) one = context.get_constant(types.intp, 1) bbend = cgutils.append_basic_block(builder, 'end') exhausted = cgutils.as_bool_bit(builder, builder.load(self.exhausted)) with cgutils.if_unlikely(builder, exhausted): result.set_valid(False) builder.branch(bbend) indices = [ builder.load(cgutils.gep(builder, self.indices, dim)) for dim in range(ndim) ] result.yield_(cgutils.pack_array(builder, indices)) result.set_valid(True) shape = cgutils.unpack_tuple(builder, self.shape, ndim) _increment_indices(context, builder, ndim, shape, self.indices, self.exhausted) builder.branch(bbend) builder.position_at_end(bbend)
def array_to_tuple(tyctx, array_or_dict, indexer_array): # noqa: U100 """Convert an array to a tuple for indexing. This function is taken from https://gist.github.com/sklam/830fe01343ba95828c3b24c391855c86 to create tuple from an array for indexing which is not possible within a Numba function. Parameters ---------- array_or_dict : numpy.ndarray or numba.typed.Dict Array for which the indexer is used. indexer_array : numpy.ndarray Array which should be converted to a tuple. """ # This is the typing level. Setup the type and constant information here. if isinstance(array_or_dict, types.DictType): tuple_size = len(array_or_dict.key_type) else: tuple_size = array_or_dict.ndim tuple_type = indexer_array.dtype typed_tuple = types.UniTuple(dtype=tuple_type, count=tuple_size) function_signature = typed_tuple(array_or_dict, indexer_array) def codegen(cgctx, builder, signature, args): # This is the implementation defined using LLVM builder. lltupty = cgctx.get_value_type(typed_tuple) tup = cgutils.get_null_value(lltupty) [_, idxaryval] = args def array_checker(a): if a.size != tuple_size: raise IndexError("index array size mismatch") # Compile and call array_checker. cgctx.compile_internal(builder, array_checker, types.none(indexer_array), [idxaryval]) def array_indexer(a, i): return a[i] # loop to fill the tuple for i in range(tuple_size): dataidx = cgctx.get_constant(types.intp, i) # compile and call array_indexer data = cgctx.compile_internal( builder, array_indexer, indexer_array.dtype(indexer_array, types.intp), [idxaryval, dataidx], ) tup = builder.insert_value(tup, data, i) return tup return function_signature, codegen
def resolve_data_type(self, val): """ Return the numba type of a Python value representing data (e.g. a number or an array, but not more sophisticated types such as functions, etc.) This function can return None to if it cannot decide. """ if val is True or val is False: return types.boolean # Under 2.x, we must guard against numpy scalars (np.intXY # subclasses Python int but get_number_type() wouldn't infer the # right bit width -- perhaps it should?). elif (not isinstance(val, numpy.number) and isinstance(val, utils.INT_TYPES + (float, ))): return self.get_number_type(val) elif val is None: return types.none elif isinstance(val, str): return types.string elif isinstance(val, complex): return types.complex128 elif isinstance(val, tuple): tys = [self.resolve_value_type(v) for v in val] distinct_types = set(tys) if len(distinct_types) == 1: return types.UniTuple(tys[0], len(tys)) else: return types.Tuple(tys) else: try: return numpy_support.map_arrayscalar_type(val) except NotImplementedError: pass if numpy_support.is_array(val): ary = val try: dtype = numpy_support.from_dtype(ary.dtype) except NotImplementedError: return if ary.flags.c_contiguous: layout = 'C' elif ary.flags.f_contiguous: layout = 'F' else: layout = 'A' return types.Array(dtype, ary.ndim, layout) return
def test_default_args_starargs_and_keyonly(self): spec = [('x', int32), ('y', int32), ('z', int32), ('args', types.UniTuple(int32, 2)), ('a', int32)] with self.assertRaises(errors.UnsupportedError) as raises: jitclass(spec)(TestClass2) msg = "VAR_POSITIONAL argument type unsupported" self.assertIn(msg, str(raises.exception))
def _typeof_tuple(val, c): tys = [typeof_impl(v, c) for v in val] if not tys: return types.Tuple(()) first = tys[0] for ty in tys[1:]: if ty != first: return types.Tuple(tys) return types.UniTuple(first, len(tys))
def __init__(self, dmm, fe_type): n_cols = len(fe_type.columns) members = [ ('data', types.Tuple(fe_type.data)), ('index', fe_type.index), ('columns', types.UniTuple(string_type, n_cols)), ('parent', types.pyobject), ] super(DataFrameModel, self).__init__(dmm, fe_type, members)
def __call__(self, context, typevars): tsets = [typevars[i.name].get() for i in self.items] oset = typevars[self.target] for vals in itertools.product(*tsets): if all(vals[0] == v for v in vals): tup = types.UniTuple(dtype=vals[0], count=len(vals)) else: tup = types.Tuple(vals) oset.add_types(tup)
def __call__(self, context, typevars): oset = typevars[self.target] for tp in typevars[self.iterator.name].get(): if isinstance(tp, types.BaseTuple): if len(tp) == self.count: oset.add_types(tp) elif isinstance(tp, types.IterableType): oset.add_types(types.UniTuple(dtype=tp.iterator_type.yield_type, count=self.count))
def test_index(self): pyfunc = tuple_index cr = compile_isolated(pyfunc, [types.UniTuple(types.int64, 3), types.int64]) tup = (4, 3, 6) for i in range(len(tup)): self.assertPreciseEqual(cr.entry_point(tup, i), tup[i]) # With a compile-time static index (the code generation path is different) pyfunc = tuple_index_static for typ in (types.UniTuple(types.int64, 4), types.Tuple((types.int64, types.int32, types.int64, types.int32))): cr = compile_isolated(pyfunc, (typ,)) tup = (4, 3, 42, 6) self.assertPreciseEqual(cr.entry_point(tup), pyfunc(tup)) typ = types.UniTuple(types.int64, 1) with self.assertTypingError(): cr = compile_isolated(pyfunc, (typ,))
def resolve_indices(self, ty, args, kws): assert not kws if len(args) != 1: raise TypeError("indices() takes exactly one argument (%d given)" % len(args)) typ, = args if not isinstance(typ, types.Integer): raise TypeError("'%s' object cannot be interpreted as an integer" % typ) return signature(types.UniTuple(types.intp, 3), types.intp)
def check_minmax_3(self, pyfunc, flags): def check(argty): cr = compile_isolated(pyfunc, (argty, ), flags=flags) cfunc = cr.entry_point # Check that the algorithm matches Python's with a non-total order tup = (1.5, float('nan'), 2.5) for val in [tup, tup[::-1]]: self.assertPreciseEqual(cfunc(val), pyfunc(val)) check(types.UniTuple(types.float64, 3)) check(types.Tuple((types.float32, types.float64, types.float32)))
def test_bool(self): pyfunc = bool_usecase cr = compile_isolated(pyfunc, [types.Tuple((types.int64, types.int32))]) args = ((4, 5), ) self.assertPreciseEqual(cr.entry_point(*args), pyfunc(*args)) cr = compile_isolated(pyfunc, [types.UniTuple(types.int64, 3)]) args = ((4, 5, 6), ) self.assertPreciseEqual(cr.entry_point(*args), pyfunc(*args)) cr = compile_isolated(pyfunc, [types.Tuple(())]) self.assertPreciseEqual(cr.entry_point(()), pyfunc(()))
def __call__(self, typeinfer): typevars = typeinfer.typevars tsets = [typevars[i.name].get() for i in self.items] oset = typevars[self.target] for vals in itertools.product(*tsets): if vals and all(vals[0] == v for v in vals): tup = types.UniTuple(dtype=vals[0], count=len(vals)) else: # empty tuples fall here as well tup = types.Tuple(vals) typeinfer.add_type(self.target, tup)
def _normalize_indices(context, builder, indty, inds): """ Convert integer indices into tuple of intp """ if indty in types.integer_domain: indty = types.UniTuple(dtype=indty, count=1) indices = [inds] else: indices = cgutils.unpack_tuple(builder, inds, count=len(indty)) indices = [context.cast(builder, i, t, types.intp) for t, i in zip(indty, indices)] return indty, indices
def __call__(self, typeinfer): with new_error_context("typing of tuple at {0}", self.loc): typevars = typeinfer.typevars tsets = [typevars[i.name].get() for i in self.items] for vals in itertools.product(*tsets): if vals and all(vals[0] == v for v in vals): tup = types.UniTuple(dtype=vals[0], count=len(vals)) else: # empty tuples fall here as well tup = types.Tuple(vals) assert tup.is_precise() typeinfer.add_type(self.target, tup, loc=self.loc)
def __init__(self, dmm, fe_type): by_series_dtype = fe_type.parent.data[ fe_type.col_id.literal_value].dtype ty_data = types.containers.DictType( by_series_dtype, types.containers.ListType(types.int64)) n_target_cols = len(fe_type.target_columns) members = [('parent', fe_type.parent), ('col_id', types.int64), ('data', ty_data), ('sort', types.bool_), ('target_default', types.bool_), ('target_columns', types.UniTuple(string_type, n_target_cols))] super(DataFrameGroupByModel, self).__init__(dmm, fe_type, members)
def test_namedtuple(self): v = Point(1, 2) tp_point = typeof(v) self.assertEqual(tp_point, types.NamedUniTuple(types.intp, 2, Point)) v = Point(1, 2.0) self.assertEqual(typeof(v), types.NamedTuple([types.intp, types.float64], Point)) w = Rect(3, 4) tp_rect = typeof(w) self.assertEqual(tp_rect, types.NamedUniTuple(types.intp, 2, Rect)) self.assertNotEqual(tp_rect, tp_point) self.assertNotEqual(tp_rect, types.UniTuple(tp_rect.dtype, tp_rect.count))
def test_index(self): pyfunc = tuple_index cr = compile_isolated(pyfunc, [types.UniTuple(types.int64, 3), types.int64]) tup = (4, 3, 6) for i in range(len(tup)): self.assertPreciseEqual(cr.entry_point(tup, i), tup[i]) # test negative indexing for i in range(len(tup) + 1): self.assertPreciseEqual(cr.entry_point(tup, -i), tup[-i]) # oob indexes, +ve then -ve with self.assertRaises(IndexError) as raises: cr.entry_point(tup, len(tup)) self.assertEqual("tuple index out of range", str(raises.exception)) with self.assertRaises(IndexError) as raises: cr.entry_point(tup, -(len(tup) + 1)) self.assertEqual("tuple index out of range", str(raises.exception)) # Test empty tuple cr = compile_isolated(pyfunc, [types.UniTuple(types.int64, 0), types.int64]) with self.assertRaises(IndexError) as raises: cr.entry_point((), 0) self.assertEqual("tuple index out of range", str(raises.exception)) # With a compile-time static index (the code generation path is different) pyfunc = tuple_index_static for typ in (types.UniTuple(types.int64, 4), types.Tuple( (types.int64, types.int32, types.int64, types.int32))): cr = compile_isolated(pyfunc, (typ, )) tup = (4, 3, 42, 6) self.assertPreciseEqual(cr.entry_point(tup), pyfunc(tup)) typ = types.UniTuple(types.int64, 1) with self.assertTypingError(): cr = compile_isolated(pyfunc, (typ, ))
def local_array(shape, dtype): ndim = 1 if isinstance(shape, tuple): ndim = len(shape) fname = "ptx.lmem.alloc" restype = types.Array(dtype, ndim, 'C') if ndim == 1: sig = typing.signature(restype, types.intp, types.Any) else: sig = typing.signature(restype, types.UniTuple(types.intp, ndim), types.Any) return ir.Intrinsic(fname, sig, args=(shape, dtype))
def __init__(self, dmm, fe_type): # TODO: types other than Array and StringArray? if fe_type.dtype == string_type: members = [ ('num_items', types.uint64), ('num_total_chars', types.uint64), ('offsets', types.CPointer(offset_typ)), ('data', types.CPointer(char_typ)), ('meminfo', types.MemInfoPointer(str_arr_payload_type)), ] else: ndim = 1 members = [ ('meminfo', types.MemInfoPointer(fe_type.dtype)), ('parent', types.pyobject), ('nitems', types.intp), ('itemsize', types.intp), ('data', types.CPointer(fe_type.dtype)), ('shape', types.UniTuple(types.intp, ndim)), ('strides', types.UniTuple(types.intp, ndim)), ] super(SeriesModel, self).__init__(dmm, fe_type, members)
def transform(self, X, y=None): """Transform data into SFA words. Parameters ---------- X : pandas DataFrame or 3d numpy array, input time series. y : array_like, target values (optional, ignored). Returns ------- List of dictionaries containing SFA words """ self.check_is_fitted() X = check_X(X, enforce_univariate=True, coerce_to_numpy=True) X = X.squeeze(1) with warnings.catch_warnings(): warnings.simplefilter("ignore", category=NumbaTypeSafetyWarning) transform = Parallel(n_jobs=self.n_jobs)( delayed(self._transform_case)( X[i, :], supplied_dft=self.binning_dft[i] if self.keep_binning_dft else None, ) for i in range(X.shape[0]) ) dim, words = zip(*transform) if self.save_words: self.words = list(words) # cant pickle typed dict if self.typed_dict and self.n_jobs != 1: nl = [None] * len(dim) for i, pdict in enumerate(dim): ndict = ( Dict.empty( key_type=types.UniTuple(types.int64, 2), value_type=types.uint32 ) if self.levels > 1 else Dict.empty(key_type=types.int64, value_type=types.uint32) ) for key, val in pdict.items(): ndict[key] = val nl[i] = pdict dim = nl bags = pd.DataFrame() if self.return_pandas_data_series else [None] bags[0] = list(dim) return bags
def _predict(self, X): """Predict class values of all instances in X. Parameters ---------- X : 3D np.array of shape = [n_instances, n_dimensions, series_length] The data to make predictions for. Returns ------- y : array-like, shape = [n_instances] Predicted class labels. """ num_cases = X.shape[0] if self.n_dims_ > 1: words = ([ Dict.empty(key_type=types.UniTuple(types.int64, 2), value_type=types.uint32) for _ in range(num_cases) ] if self.typed_dict else [defaultdict(int) for _ in range(num_cases)]) for i, dim in enumerate(self._dims): X_dim = X[:, dim, :].reshape(num_cases, 1, self.series_length_) dim_words = self._transformers[i].transform(X_dim) dim_words = dim_words[0] for n in range(num_cases): if self.typed_dict: for word, count in dim_words[n].items(): if self.levels > 1: words[n][(word[0], word[1] << self._highest_dim_bit | dim)] = count else: words[n][(word, dim)] = count else: for word, count in dim_words[n].items(): words[n][word << self._highest_dim_bit | dim] = count test_bags = words else: test_bags = self._transformers[0].transform(X) test_bags = test_bags[0] classes = Parallel(n_jobs=self._threads_to_use)( delayed(self._test_nn)(test_bag, ) for test_bag in test_bags) return np.array(classes)
def __setstate__(self, state): """Set current state using input pickling, required for typed Dict objects.""" self.__dict__.update(state) if self.typed_dict: nl = [None] * len(self._transformed_data) for i, pdict in enumerate(self._transformed_data): ndict = (Dict.empty(key_type=types.UniTuple(types.int64, 2), value_type=types.uint32) if self.levels > 1 or self.n_dims_ > 1 else Dict.empty( key_type=types.int64, value_type=types.uint32)) for key, val in pdict.items(): ndict[key] = val nl[i] = ndict self._transformed_data = nl