How to use the awkward.type.OptionType function in awkward

To help you get started, we’ve selected a few awkward examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scikit-hep / awkward-array / awkward / array / masked.py View on Github external
def _gettype(self, seen):
        return awkward.type.OptionType(awkward.type._fromarray(self._content, seen))
github scikit-hep / awkward-array / awkward / util.py View on Github external
tpen = awkward.type.fromarray(arrayn).to
                if isinstance(tpen, numpy.dtype):
                    columns.append(colsn)
                    tmp = arrayn

                elif isinstance(tpen, type) and issubclass(tpen, (str, bytes)):
                    columns.append(colsn)
                    tmp = arrayn

                elif isinstance(tpen, awkward.type.ArrayType) and tpen.takes == numpy.inf:
                    tmp = JaggedArray(arrayn.starts, arrayn.stops, recurse(arrayn.content, tpen.to, colsn, True))

                elif isinstance(tpen, awkward.type.TableType):
                    tmp = recurse(arrayn, tpen, colsn, True)

                elif isinstance(tpen, awkward.type.OptionType) and isinstance(arrayn.content, numpy.ndarray):
                    columns.append(colsn)
                    tmp = numpy.ma.MaskedArray(arrayn.content, arrayn.boolmask(maskedwhen=True))

                else:
                    raise ValueError("this array has unflattenable substructure:\n\n{0}".format(str(tpen)))

                if isinstance(tmp, awkward.array.jagged.JaggedArray):
                    if isinstance(tmp.content, awkward.array.jagged.JaggedArray):
                        unflattened = tmp
                        tmp = tmp.flatten(axis=1)

                    if starts is None:
                        starts, stops = tmp.starts, tmp.stops
                    elif not numpy.array_equal(starts, tmp.starts) or not numpy.array_equal(stops, tmp.stops):
                        raise ValueError("this array has more than one jagged array structure")
                    if out is None:
github scikit-hep / awkward-array / awkward / arrow.py View on Github external
def recurse(tpe, nullable):
        if isinstance(tpe, pyarrow.lib.DictionaryType):
            out = recurse(tpe.dictionary.type, nullable)
            if nullable:
                return awkward.type.OptionType(out)
            else:
                return out

        elif isinstance(tpe, pyarrow.lib.StructType):
            out = None
            for i in range(tpe.num_children):
                x = awkward.type.ArrayType(tpe[i].name, recurse(tpe[i].type, tpe[i].nullable))
                if out is None:
                    out = x
                else:
                    out = out & x
            if nullable:
                return awkward.type.OptionType(out)
            else:
                return out
github scikit-hep / awkward-array / awkward / type.py View on Github external
seen[id(array)] = placeholder = Placeholder()

        if isinstance(array, numpy.ndarray):
            if array.dtype.names is None:
                out = array.dtype

            else:
                out = TableType.__new__(TableType)
                out._fields = OrderedDict()
                for n in array.dtype.names:
                    out[n] = array.dtype[n]

            for x in array.shape[:0:-1]:
                out = ArrayType(x, out)
            if isinstance(array, numpy.ma.MaskedArray):
                out = OptionType(out)

            placeholder.value = out

        else:
            placeholder.value = array._gettype(seen)

    return seen[id(array)]
github scikit-hep / awkward-array / awkward / persist.py View on Github external
return labels[obj["ref"]]

        elif "takes" in obj and "to" in obj:
            return awkward.type.ArrayType(takes(obj["takes"]), recurse(obj["to"]))

        elif "fields" in obj:
            out = awkward.type.TableType()
            for n, x in obj["fields"]:
                out[n] = recurse(x)
            return out

        elif "possibilities" in obj:
            return awkward.type.UnionType(*[recurse(x) for x in obj["possibilities"]])

        elif "type" in obj:
            return awkward.type.OptionType(recurse(obj["type"]))

        elif "dtype" in obj:
            return json2dtype(obj["dtype"])

        elif "function" in obj:
            return spec2function(obj["function"], whitelist=whitelist)

        else:
            raise ValueError("unexpected set of keys in JSON: {0}".format(", ".join(repr(x) for x in obj)))
github scikit-hep / awkward-array / awkward / type.py View on Github external
def fromnumpy(shape, dtype, masked=False):
    if not isinstance(shape, tuple):
        shape = (shape,)
    if not isinstance(dtype, numpy.dtype):
        dtype = numpy.dtype(dtype)

    if masked:
        return OptionType(fromnumpy(shape, dtype))
    elif dtype.subdtype is not None:
        dt, sh = dtype.subdtype
        return fromnumpy(shape + sh, dt)
    else:
        return ArrayType(*(shape + (dtype,)))
github scikit-hep / awkward-array / awkward / type.py View on Github external
elif isinstance(x, TableType):
            seen[id(x)] = TableType.__new__(TableType)
            seen[id(x)]._fields = OrderedDict()
            for n, y in x._fields.items():
                seen[id(x)]._fields[n] = Type._copy(y, seen)
            return seen[id(x)]

        elif isinstance(x, UnionType):
            seen[id(x)] = UnionType.__new__(UnionType)
            seen[id(x)]._possibilities = []
            for y in x._possibilities:
                seen[id(x)]._possibilities.append(Type._copy(y, seen))
            return seen[id(x)]

        elif isinstance(x, OptionType):
            seen[id(x)] = OptionType.__new__(OptionType)
            seen[id(x)]._type = Type._copy(x._type, seen)
            return seen[id(x)]

        else:
            return x
github scikit-hep / awkward-array / awkward / arrow.py View on Github external
out = None
            for i in range(tpe.num_children):
                x = awkward.type.ArrayType(tpe[i].name, recurse(tpe[i].type, tpe[i].nullable))
                if out is None:
                    out = x
                else:
                    out = out & x
            if nullable:
                return awkward.type.OptionType(out)
            else:
                return out

        elif isinstance(tpe, pyarrow.lib.ListType):
            out = awkward.type.ArrayType(float("inf"), recurse(tpe.value_type, nullable))
            if nullable:
                return awkward.type.OptionType(out)
            else:
                return out

        elif isinstance(tpe, pyarrow.lib.UnionType):
            out = None
            for i in range(tpe.num_children):
                x = recurse(tpe[i].type, nullable)
                if out is None:
                    out = x
                else:
                    out = out | x
            if nullable:
                return awkward.type.OptionType(out)
            else:
                return out