Python numba.typeof() Examples

The following are 15 code examples of numba.typeof(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numba , or try the search function .
Example #1
Source File: pdimpl.py    From sdc with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _Series_category(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False):
    """
    Implementation of constructor for pandas Series via objmode.
    """
    # TODO: support other parameters (only data and dtype now)

    ty = typeof(_reconstruct_Series(data, dtype))

    from textwrap import dedent
    text = dedent(f"""
    def impl(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False):
        with objmode(series="{ty}"):
            series = pd.Series(data, index, dtype, name, copy, fastpath)
        return series
    """)
    globals, locals = {'objmode': objmode, 'pd': pd}, {}
    exec(text, globals, locals)
    impl = locals['impl']
    return impl 
Example #2
Source File: rp_trees.py    From pynndescent with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def make_dense_tree(data, rng_state, leaf_size=30, angular=False):
    indices = np.arange(data.shape[0]).astype(np.int32)

    hyperplanes = numba.typed.List.empty_list(dense_hyperplane_type)
    offsets = numba.typed.List.empty_list(offset_type)
    children = numba.typed.List.empty_list(children_type)
    point_indices = numba.typed.List.empty_list(point_indices_type)

    if angular:
        make_angular_tree(
            data,
            indices,
            hyperplanes,
            offsets,
            children,
            point_indices,
            rng_state,
            leaf_size,
        )
    else:
        make_euclidean_tree(
            data,
            indices,
            hyperplanes,
            offsets,
            children,
            point_indices,
            rng_state,
            leaf_size,
        )

    # print("Completed a tree")
    result = FlatTree(hyperplanes, offsets, children, point_indices, leaf_size)
    # print("Tree type is:", numba.typeof(result))
    return result 
Example #3
Source File: numba.py    From fluids with MIT License 5 votes vote down vote up
def infer_dictionary_types(d):
    if not d:
        raise ValueError("Empty dictionary cannot infer")
    keys = list(d.keys())
    type_keys = type(keys[0])
    for k in keys:
        if type(k) != type_keys:
            raise ValueError("Inconsistent key types in dictionary")
    values = list(d.values())
    type_values = type(values[0])
    for v in values:
        if type(v) != type_values:
            raise ValueError("Inconsistent value types in dictionary")
            
    return numba.typeof(keys[0]), numba.typeof(values[0]) 
Example #4
Source File: table.py    From awkward-array with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _Table_typeof(val, c):
    return TableType(val.rowname, OrderedDict((n, numba.typeof(x)) for n, x in val.contents.items()), special=type(val), specialrow=type(val).Row) 
Example #5
Source File: jagged.py    From awkward-array with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _JaggedArray_typeof(val, c):
    return JaggedArrayType(numba.typeof(val.starts), numba.typeof(val.stops), numba.typeof(val.content), special=type(val)) 
Example #6
Source File: table.py    From awkward-array with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _Table_typeof(val, c):
    return TableType(val.rowname, OrderedDict((n, numba.typeof(x)) for n, x in val.contents.items()), special=type(val), specialrow=type(val).Row) 
Example #7
Source File: jagged.py    From awkward-array with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _JaggedArray_typeof(val, c):
    return JaggedArrayType(numba.typeof(val.starts), numba.typeof(val.stops), numba.typeof(val.content), special=type(val)) 
Example #8
Source File: hpat_pandas_dataframe_pass.py    From sdc with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _replace_func(self, func, args, const=False, pre_nodes=None, extra_globals=None, pysig=None, kws=None):
        glbls = {'numba': numba, 'numpy': numpy, 'sdc': sdc}
        if extra_globals is not None:
            glbls.update(extra_globals)

        if pysig is not None:
            pre_nodes = [] if pre_nodes is None else pre_nodes
            scope = next(iter(self.state.func_ir.blocks.values())).scope
            loc = scope.loc

            def normal_handler(index, param, default):
                return default

            def default_handler(index, param, default):
                d_var = ir.Var(scope, ir_utils.mk_unique_var('defaults'), loc)
                self.state.typemap[d_var.name] = numba.typeof(default)
                node = ir.Assign(ir.Const(default, loc), d_var, loc)
                pre_nodes.append(node)

                return d_var

            args = numba.core.typing.fold_arguments(pysig, args, kws, normal_handler, default_handler, normal_handler)

        arg_typs = tuple(self.state.typemap[v.name] for v in args)

        if const:
            new_args = []

            for i, arg in enumerate(args):
                val = guard(ir_utils.find_const, self.state.func_ir, arg)
                if val:
                    new_args.append(types.literal(val))
                else:
                    new_args.append(arg_typs[i])
            arg_typs = tuple(new_args)

        return sdc.utilities.utils.ReplaceFunc(func, arg_typs, args, glbls, pre_nodes) 
Example #9
Source File: typeof.py    From sdc with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _typeof_Categorical(val, c):
    try:
        dtype = pandas_support.from_dtype(val.dtype)
    except NotImplementedError:
        raise ValueError("Unsupported Categorical dtype: %s" % (val.dtype,))
    codes = typeof(val.codes)
    return Categorical(dtype=dtype, codes=codes) 
Example #10
Source File: test_series_category.py    From sdc with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_typeof(self):
        pd_value = self._pd_value()
        nb_type = nb.typeof(pd_value)

        assert(isinstance(nb_type, SeriesType))
        assert(nb_type.dtype == CategoricalDtypeType(categories=[1, 2, 3], ordered=False))
        assert(nb_type.index == types.none)
        assert(nb_type.data == Categorical(CategoricalDtypeType(categories=[1, 2, 3], ordered=False))) 
Example #11
Source File: test_categorical.py    From sdc with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_typeof(self):
        pd_value = self._pd_value()
        nb_type = nb.typeof(pd_value)

        assert(isinstance(nb_type, Categorical))
        assert(nb_type.pd_dtype == CategoricalDtypeType(categories=[1, 2, 3], ordered=False))
        assert(nb_type.codes == types.Array(dtype=types.int8, ndim=1, layout='C', readonly=True)) 
Example #12
Source File: boxing.py    From sdc with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _infer_series_dtype(S):
    if S.dtype == np.dtype('O'):
        # XXX assuming the whole column is strings if 1st val is string
        # TODO: handle NA as 1st value
        i = 0
        while i < len(S) and (S.iloc[i] is np.nan or S.iloc[i] is None):
            i += 1
        if i == len(S):
            raise ValueError(
                "object dtype infer out of bounds for {}".format(S.name))

        first_val = S.iloc[i]
        if isinstance(first_val, list):
            return _infer_series_list_dtype(S)
        elif isinstance(first_val, str):
            return string_type
        else:
            raise ValueError(
                "object dtype infer: data type for column {} not supported".format(S.name))
    elif isinstance(S.dtype, pd.CategoricalDtype):
        return numba.typeof(S.dtype)
    # regular numpy types
    try:
        return numpy_support.from_dtype(S.dtype)
    except NotImplementedError:
        raise ValueError("np dtype infer: data type for column {} not supported".format(S.name)) 
Example #13
Source File: pd_categorical_ext.py    From sdc with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _get_cat_obj_items(categories, c):
    assert len(categories) > 0
    val = categories[0]
    if isinstance(val, str):
        return [c.pyapi.string_from_constant_string(item) for item in categories]

    dtype = numba.typeof(val)
    return [c.box(dtype, c.context.get_constant(dtype, item)) for item in categories] 
Example #14
Source File: _numba_utils.py    From clifford with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __call__(self, *args):
        arg_type = tuple(numba.typeof(arg) for arg in args)
        try:
            func = self.__cache[arg_type]
        except KeyError:
            func = self.__cache[arg_type] = self.__func(*arg_type)
        return func(*args) 
Example #15
Source File: util.py    From oamap with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def paramtypes(args):
    try:
        import numba as nb
    except ImportError:
        return None
    else:
        return tuple(nb.typeof(x) for x in args)