Python numba.from_dtype() Examples

The following are 13 code examples of numba.from_dtype(). 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: hpat_pandas_functions.py    From sdc with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def get_numba_array_types_for_csv(df):
    """Extracts Numba array types from the given DataFrame."""
    result = []
    for numpy_type in df.dtypes.values:
        try:
            numba_type = numpy_support.from_dtype(numpy_type)
        except NotImplementedError:
            numba_type = None

        if numba_type and numba_type != types.pyobject:
            array_type = types.Array(numba_type, 1, 'C')
        else:
            # default type for CSV is string
            array_type = string_array_type

        result.append(array_type)
    return result 
Example #2
Source File: compiler.py    From oamap with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def generic_resolve(self, typ, attr):
            if typ.matchable:
                if attr == "nullable":
                    return numba.types.boolean

                elif isinstance(typ.schema, oamap.schema.Primitive) and attr == "dtype":
                    return numba.types.DType(numba.from_dtype(typ.schema.dtype))

                elif isinstance(typ.schema, oamap.schema.List) and attr == "content":
                    return typ.content()

                elif isinstance(typ.schema, oamap.schema.Union) and attr == "possibilities":
                    return typ.unmatchable()

                elif isinstance(typ.schema, oamap.schema.Record) and attr == "fields":
                    return typ.unmatchable()

                elif isinstance(typ.schema, oamap.schema.Tuple) and attr == "types":
                    return typ.unmatchable()

                elif isinstance(typ.schema, oamap.schema.Pointer) and attr == "target":
                    return typ.target()

                else:
                    raise AssertionError("unrecognized schema type: {0} ({1})".format(typ.schema.__class__, repr(typ.schema))) 
Example #3
Source File: numba_extension.py    From sparse with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, data_dtype: np.dtype, coords_dtype: np.dtype, ndim: int):
        assert isinstance(data_dtype, np.dtype)
        assert isinstance(coords_dtype, np.dtype)
        self.data_dtype = data_dtype
        self.coords_dtype = coords_dtype
        self.ndim = ndim
        super().__init__(
            name="COOType[{!r}, {!r}, {!r}]".format(
                numba.from_dtype(data_dtype), numba.from_dtype(coords_dtype), ndim
            )
        ) 
Example #4
Source File: numba_extension.py    From sparse with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def data_type(self):
        return numba.from_dtype(self.data_dtype)[:] 
Example #5
Source File: numba_extension.py    From sparse with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def coords_type(self):
        return numba.from_dtype(self.coords_dtype)[:, :] 
Example #6
Source File: numba_extension.py    From sparse with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def fill_value_type(self):
        return numba.from_dtype(self.data_dtype) 
Example #7
Source File: compiler.py    From oamap with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def resolve_cast(self, schematype, args, kwds):
            if len(args) == 1:
                arg, = args
                if isinstance(schematype.schema, oamap.schema.Primitive):
                    if schematype.schema.nullable:
                        return numba.types.optional(numba.from_dtype(schematype.schema.dtype))(arg)
                    else:
                        return numba.from_dtype(schematype.schema.dtype)(arg)

                elif isinstance(schematype.schema, (oamap.schema.List, oamap.schema.Union, oamap.schema.Record, oamap.schema.Tuple)):
                    if isinstance(arg, UnionProxyNumbaType):
                        for datatag, datatype in enumerate(arg.generator.schema.possibilities):
                            if schematype.schema == datatype:
                                return typeof_generator(arg.generator.possibilities[datatag])(arg)

                    if isinstance(arg, numba.types.Optional) and isinstance(arg.type, UnionProxyNumbaType):
                        for datatag, datatype in enumerate(arg.type.generator.schema.possibilities):
                            if schematype.schema == datatype:
                                return typeof_generator(arg.type.generator.possibilities[datatag])(arg)

                    if arg.generator.schema == schematype.schema:
                        return typeof_generator(arg.generator)(arg)
                    else:
                        return typeof_generator(schematype.generator)(arg)

                elif isinstance(schematype.schema, oamap.schema.Pointer):
                    raise TypeError("cannot cast data as a Pointer")

                else:
                    raise AssertionError("unrecognized schema type: {0} ({1})".format(schematype.schema.__class__, repr(schematype.schema))) 
Example #8
Source File: compiler.py    From oamap with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def schema_getattr(context, builder, typ, val, attr):
        if attr == "nullable":
            return literal_boolean(1 if typ.schema.nullable else 0)

        elif attr == "dtype":
            return numba.targets.imputils.impl_ret_untracked(context, builder, numba.types.DType(numba.from_dtype(typ.schema.dtype)), context.get_dummy_value())

        else:
            return numba.cgutils.create_struct_proxy(typ)(context, builder)._getvalue() 
Example #9
Source File: compiler.py    From oamap with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def typeof_generator(generator, checkmasked=True):
        if checkmasked and isinstance(generator, oamap.generator.Masked):
            tpe = typeof_generator(generator, checkmasked=False)
            if isinstance(tpe, numba.types.Optional):
                return tpe
            else:
                return numba.types.optional(tpe)

        if isinstance(generator, oamap.generator.PrimitiveGenerator):
            return numba.from_dtype(generator.dtype)

        elif isinstance(generator, oamap.generator.ListGenerator):
            return ListProxyNumbaType(generator)

        elif isinstance(generator, oamap.generator.UnionGenerator):
            return UnionProxyNumbaType(generator)

        elif isinstance(generator, oamap.generator.RecordGenerator):
            return RecordProxyNumbaType(generator)

        elif isinstance(generator, oamap.generator.TupleGenerator):
            return TupleProxyNumbaType(generator)

        elif isinstance(generator, oamap.generator.PointerGenerator):
            return typeof_generator(generator.target)

        elif isinstance(generator, oamap.generator.ExtendedGenerator):
            return typeof_generator(generator.generic)

        else:
            raise AssertionError("unrecognized generator type: {0} ({1})".format(generator.__class__, repr(generator))) 
Example #10
Source File: compiler.py    From oamap with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def unionproxytype_eq_left_primitive(context, builder, sig, args):
        ltype, rtype = sig.args
        lval, rval = args
        lproxy = numba.cgutils.create_struct_proxy(ltype)(context, builder, value=lval)
        out_ptr = numba.cgutils.alloca_once_value(builder, literal_boolean(False))
        for li, lgen in enumerate(ltype.generator.possibilities):
            if isinstance(lgen.schema, oamap.schema.Primitive) and numba.from_dtype(lgen.schema.dtype) == rtype:
                with builder.if_then(builder.icmp_signed("==", lproxy.tag, literal_int(li, ltype.generator.tagdtype.itemsize))):
                    ldata = generate(context, builder, lgen, lproxy.baggage, lproxy.ptrs, lproxy.lens, lproxy.offset)
                    with builder.if_then(context.get_function("==", numba.types.boolean(typeof_generator(lgen), rtype))(builder, (ldata, rval))):
                        builder.store(literal_boolean(True), out_ptr)
        return builder.load(out_ptr) 
Example #11
Source File: compiler.py    From oamap with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def schema_case(context, builder, sig, args):
        schematype, argtype = sig.args
        dummy, argval = args

        if isinstance(argtype, numba.types.Optional):
            # unwrap the optval and apply the check to the contents
            optval = context.make_helper(builder, argtype, value=argval)
            out = schema_case(context, builder, numba.types.boolean(schematype, argtype.type), (dummy, optval.data))
            if schematype.schema.nullable:
                return out
            else:
                return builder.and_(optval.valid, out)

        elif isinstance(argtype, UnionProxyNumbaType):
            # do a runtime check
            for datatag, datatype in enumerate(argtype.generator.schema.possibilities):
                if schematype.schema == datatype:
                    unionproxy = numba.cgutils.create_struct_proxy(argtype)(context, builder, value=argval)
                    out_ptr = numba.cgutils.alloca_once(builder, llvmlite.llvmpy.core.Type.int(1))
                    with builder.if_else(builder.icmp_unsigned("==", unionproxy.tag, literal_int(datatag, argtype.generator.tagdtype.itemsize))) as (success, failure):
                        with success:
                            builder.store(literal_boolean(True), out_ptr)
                        with failure:
                            builder.store(literal_boolean(False), out_ptr)
                    out = builder.load(out_ptr)
                    return out

            # none of the data possibilities will ever match
            return literal_boolean(False)

        elif isinstance(argtype, primtypes):
            # do a compile-time check
            if isinstance(schematype.schema, oamap.schema.Primitive):
                return literal_boolean(numba.from_dtype(schematype.schema.dtype) == argtype)
            else:
                return literal_boolean(False)

        elif isinstance(argtype, ProxyNumbaType):
            # do a compile-time check
            return literal_boolean(schematype.schema == argtype.generator.schema)

        else:
            raise AssertionError 
Example #12
Source File: compiler.py    From oamap with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def generate_empty(context, builder, generator, baggage):
        typ = typeof_generator(generator, checkmasked=False)

        if isinstance(generator, oamap.generator.PrimitiveGenerator):
            return llvmlite.llvmpy.core.Constant.null(context.get_value_type(numba.from_dtype(generator.dtype)))

        elif isinstance(generator, oamap.generator.ListGenerator):
            listproxy = numba.cgutils.create_struct_proxy(typ)(context, builder)
            listproxy.baggage = baggage
            listproxy.ptrs = llvmlite.llvmpy.core.Constant.null(context.get_value_type(numba.types.voidptr))
            listproxy.lens = llvmlite.llvmpy.core.Constant.null(context.get_value_type(numba.types.voidptr))
            listproxy.whence = literal_int64(-1)
            listproxy.stride = literal_int64(-1)
            listproxy.length = literal_int64(-1)
            return listproxy._getvalue()

        elif isinstance(generator, oamap.generator.UnionGenerator):
            unionproxy = numba.cgutils.create_struct_proxy(typ)(context, builder)
            unionproxy.baggage = baggage
            unionproxy.ptrs = llvmlite.llvmpy.core.Constant.null(context.get_value_type(numba.types.voidptr))
            unionproxy.lens = llvmlite.llvmpy.core.Constant.null(context.get_value_type(numba.types.voidptr))
            unionproxy.tag = literal_int64(-1)
            unionproxy.offset = literal_int64(-1)
            return unionproxy._getvalue()

        elif isinstance(generator, oamap.generator.RecordGenerator):
            recordproxy = numba.cgutils.create_struct_proxy(typ)(context, builder)
            recordproxy.baggage = baggage
            recordproxy.ptrs = llvmlite.llvmpy.core.Constant.null(context.get_value_type(numba.types.voidptr))
            recordproxy.lens = llvmlite.llvmpy.core.Constant.null(context.get_value_type(numba.types.voidptr))
            recordproxy.index = literal_int64(-1)
            return recordproxy._getvalue()

        elif isinstance(generator, oamap.generator.TupleGenerator):
            tupleproxy = numba.cgutils.create_struct_proxy(typ)(context, builder)
            tupleproxy.baggage = baggage
            tupleproxy.ptrs = llvmlite.llvmpy.core.Constant.null(context.get_value_type(numba.types.voidptr))
            tupleproxy.lens = llvmlite.llvmpy.core.Constant.null(context.get_value_type(numba.types.voidptr))
            tupleproxy.index = literal_int64(-1)
            return tupleproxy._getvalue()

        elif isinstance(generator, oamap.generator.PointerGenerator):
            return generate_empty(context, builder, generator.target, baggage)

        elif isinstance(generator, oamap.generator.ExtendedGenerator):
            return generate(context, builder, generator.generic, baggage, ptrs, lens, at)

        else:
            raise AssertionError("unrecognized generator type: {0} ({1})".format(generator.__class__, repr(generator))) 
Example #13
Source File: compiler.py    From oamap with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def generic(self, args, kwds):
            lhs, rhs = args
            if isinstance(lhs, numba.types.Optional):
                lhs = lhs.type
            if isinstance(rhs, numba.types.Optional):
                rhs = rhs.type

            if isinstance(lhs, ListProxyNumbaType) and isinstance(rhs, ListProxyNumbaType):
                if lhs.generator.schema.content == rhs.generator.schema.content:
                    return numba.types.boolean(*args)

            elif isinstance(lhs, RecordProxyNumbaType) and isinstance(rhs, RecordProxyNumbaType):
                if lhs.generator.schema.fields == rhs.generator.schema.fields:
                    return numba.types.boolean(*args)

            elif isinstance(lhs, TupleProxyNumbaType) and isinstance(rhs, TupleProxyNumbaType):
                if lhs.generator.schema.types == rhs.generator.schema.types:
                    return numba.types.boolean(*args)

            elif isinstance(lhs, UnionProxyNumbaType) and isinstance(rhs, UnionProxyNumbaType):
                for x in lhs.generator.schema.possibilities:
                    for y in rhs.generator.schema.possibilities:
                        if x.copy(nullable=False) == y.copy(nullable=False):
                            return numba.types.boolean(*args)

            elif isinstance(lhs, UnionProxyNumbaType) and isinstance(rhs, ProxyNumbaType):
                for x in lhs.generator.schema.possibilities:
                    if x.copy(nullable=False) == rhs.generator.schema.copy(nullable=False):
                        return numba.types.boolean(*args)

            elif isinstance(rhs, UnionProxyNumbaType) and isinstance(lhs, ProxyNumbaType):
                for x in rhs.generator.schema.possibilities:
                    if x.copy(nullable=False) == lhs.generator.schema.copy(nullable=False):
                        return numba.types.boolean(*args)

            elif isinstance(lhs, UnionProxyNumbaType) and isinstance(rhs, primtypes):
                for x in lhs.generator.schema.possibilities:
                    if isinstance(x, oamap.schema.Primitive) and numba.from_dtype(x.dtype) == rhs:
                        return numba.types.boolean(*args)

            elif isinstance(rhs, UnionProxyNumbaType) and isinstance(lhs, primtypes):
                for x in rhs.generator.schema.possibilities:
                    if isinstance(x, oamap.schema.Primitive) and numba.from_dtype(x.dtype) == lhs:
                        return numba.types.boolean(*args)

    ################################################################ ListProxy