How to use the uproot.interp function in uproot

To help you get started, we’ve selected a few uproot 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 diana-hep / oamap / oamap / source / root.py View on Github external
def frominterp(name, branch, interpretation):
                if len(branch.fBranches) > 0:
                    return recurse(branch)

                elif isinstance(interpretation, uproot.interp.asdtype):
                    if interpretation.todtype.names is None:
                        return oamap.schema.Primitive(interpretation.todtype, data=name)
                    else:
                        rec = oamap.schema.Record({}); raise AssertionError
                        for n in interpretation.todtype.names:
                            rec[n] = oamap.schema.Primitive(interpretation.todtype[n], data=(name + "/" + n))
                        return rec

                elif isinstance(interpretation, uproot.interp.asjagged):
                    if interpretation.asdtype.todtype.names is None:
                        return oamap.schema.List(oamap.schema.Primitive(interpretation.asdtype.todtype, data=name), starts=name, stops=name)
                    else:
                        rec = oamap.schema.Record({}); raise AssertionError
                        for n in interpretation.asdtype.todtype.names:
                            rec[n] = oamap.schema.Primitive(interpretation.asdtype.todtype[n], data=(name + "/" + n))
                        return oamap.schema.List(rec, starts=name, stops=name)
github diana-hep / oamap / oamap / backend / root / __init__.py View on Github external
try:
                colon = name.rindex(b":")
            except ValueError:
                return name, None
            else:
                return name[:colon], name[colon + 1:]
            
        arrays = self._tree.arrays(set(chop(x)[0] for x in roles), keycache=self._keycache)

        out = {}
        for role in roles:
            branchname, leafname = chop(role)
            array = arrays[branchname]

            if leafname is not None and leafname.startswith(b"/"):
                if isinstance(array, (uproot.interp.jagged.JaggedArray, uproot.interp.strings.Strings)):
                    array = array.content

                length = array.shape[0]
                stride = 1
                for depth in range(int(leafname[1:])):
                    length *= array.shape[depth + 1]
                    stride *= array.shape[depth + 1]

                if isinstance(role, oamap.generator.StartsRole) and role not in out:
                    offsets = numpy.arange(0, (length + 1)*stride, stride)
                    out[role] = offsets[:-1]
                    out[role.stops] = offsets[1:]

                elif isinstance(role, oamap.generator.StopsRole) and role not in out:
                    offsets = numpy.arange(0, (length + 1)*stride, stride)
                    out[role.starts] = offsets[:-1]
github diana-hep / oamap / oamap / source / root.py View on Github external
def frominterp(name, branch, interpretation):
                if len(branch.fBranches) > 0:
                    return recurse(branch)

                elif isinstance(interpretation, uproot.interp.asdtype):
                    if interpretation.todtype.names is None:
                        return oamap.schema.Primitive(interpretation.todtype, data=name)
                    else:
                        rec = oamap.schema.Record({}); raise AssertionError
                        for n in interpretation.todtype.names:
                            rec[n] = oamap.schema.Primitive(interpretation.todtype[n], data=(name + "/" + n))
                        return rec

                elif isinstance(interpretation, uproot.interp.asjagged):
                    if interpretation.asdtype.todtype.names is None:
                        return oamap.schema.List(oamap.schema.Primitive(interpretation.asdtype.todtype, data=name), starts=name, stops=name)
                    else:
                        rec = oamap.schema.Record({}); raise AssertionError
                        for n in interpretation.asdtype.todtype.names:
                            rec[n] = oamap.schema.Primitive(interpretation.asdtype.todtype[n], data=(name + "/" + n))
                        return oamap.schema.List(rec, starts=name, stops=name)

                elif isinstance(interpretation, uproot.interp.asstrings):
                    return oamap.schema.List(oamap.schema.Primitive("u1", data=name), starts=name, stops=name, name="ByteString")

                else:
                    return None
github diana-hep / oamap / oamap / source / root.py View on Github external
raise AssertionError

                        elif isinstance(array, uproot.interp.jagged.JaggedArray):
                            if isinstance(name, oamap.generator.StartsRole):
                                out[name] = array.starts

                            elif isinstance(name, oamap.generator.StopsRole):
                                out[name] = array.stops

                            elif isinstance(name, oamap.generator.DataRole):
                                out[name] = array.content

                            else:
                                raise AssertionError

                        elif isinstance(array, uproot.interp.strings.Strings):
                            if isinstance(name, oamap.generator.StartsRole):
                                out[name] = array.jaggedarray.starts

                            elif isinstance(name, oamap.generator.StopsRole):
                                out[name] = array.jaggedarray.stops

                            elif isinstance(name, oamap.generator.DataRole):
                                out[name] = array.jaggedarray.content

                            else:
                                raise AssertionError

                        else:
                            raise AssertionError

                return out
github scikit-hep / uproot / uproot / interp / objects.py View on Github external
def read(self, source, cursor, context, parent):
        numitems = cursor.field(source, self._format1)

        out = {}
        for i in range(numitems):
            if isinstance(self.keycls, uproot.interp.numerical.asdtype):
                key = cursor.array(source, 1, self.keycls.fromdtype)
                if key.dtype != self.keycls.todtype:
                    key = key.astype(self.keycls.todtype)
                key = key[0]
            else:
                key = self.keycls.read(source, cursor, context, parent)

            if isinstance(self.valcls, uproot.interp.numerical.asdtype):
                val = cursor.array(source, 1, self.valcls.fromdtype)
                if val.dtype != self.valcls.todtype:
                    val = val.astype(self.valcls.todtype)
                val = val[0]
            else:
                val = self.valcls.read(source, cursor, context, parent)

            out[key] = val

        return out
github scikit-hep / uproot / uproot / interp / numerical.py View on Github external
return self.awkward.numpy.dtype((self.awkward.numpy.float32, self.todims))

    def __repr__(self):
        args = [repr(self.low), repr(self.high), repr(self.numbits), repr(self.fromdtype), repr(self.todtype)]
        return "asfloat16(" + ", ".join(args) + ")"

    @property
    def identifier(self):
        fromdims = "(" + ",".join(repr(x) for x in self.fromdims) + ")"
        todims = "(" + ",".join(repr(x) for x in self.todims) + ")"
        return "asfloat16({0},{1},{2},{3},{4})".format(self.low, self.high, self.numbits, fromdims, todims)

    def compatible(self, other):
        return isinstance(other, asfloat16) and self.low == other.low and self.high == other.high and self.numbits == other.numbits and self.fromdtype == other.fromdtype and self.todtype == other.todtype

class asstlbitset(uproot.interp.interp.Interpretation):
    # makes __doc__ attribute mutable before Python 3.3
    __metaclass__ = type.__new__(type, "type", (uproot.interp.interp.Interpretation.__metaclass__,), {})

    @property
    def todtype(self):
        return self.awkward.numpy.dtype(self.awkward.numpy.bool_)

    def __init__(self, numbytes):
        self.numbytes = numbytes

    def __repr__(self):
        return self.identifier

    @property
    def identifier(self):
        return "asstlbitset({0})".format(self.numbytes)
github scikit-hep / uproot / uproot / _connect / _pandas.py View on Github external
array = array.content
                needbroadcasts.append(False)

            else:
                needbroadcasts.append(True)

            names.append(name)
            interpretations.append(interpretation)
            arrays.append(array)

        index = pandas.MultiIndex.from_arrays([index.tojagged(numpy.arange(entrystart, entrystop, dtype=numpy.int64)).content, index.content], names=["entry", "subentry"])

        df = outputtype(index=index)

        for name, interpretation, array, needbroadcast in zip(names, interpretations, arrays, needbroadcasts):
            if isinstance(interpretation, uproot.interp.numerical._asnumeric):
                if isinstance(array, awkwardbase.ObjectArray):
                    array = array.content

                if needbroadcast:
                    # Invoke jagged broadcasting to align arrays
                    originaldtype = array.dtype
                    originaldims = array.shape[1:]

                    if isinstance(array, awkwardbase.Table):
                        for nn in array.columns:
                            array[nn] = awkward.JaggedArray(starts, stops, awkward.numpy.empty(stops[-1], dtype=array[nn].dtype)).tojagged(array[nn]).content

                    else:
                        if len(originaldims) != 0:
                            array = array.view(awkward.numpy.dtype([(str(i), array.dtype) for i in range(functools.reduce(operator.mul, array.shape[1:]))])).reshape(array.shape[0])
github scikit-hep / uproot / uproot / interp / jagged.py View on Github external
self.content = content

def _destructive_divide(array, divisor):
    if divisor == 1:
        pass
    elif divisor == 2:
        awkward.util.numpy.right_shift(array, 1, out=array)
    elif divisor == 4:
        awkward.util.numpy.right_shift(array, 2, out=array)
    elif divisor == 8:
        awkward.util.numpy.right_shift(array, 3, out=array)
    else:
        awkward.util.numpy.floor_divide(array, divisor, out=array)
    return array

class asjagged(uproot.interp.interp.Interpretation):
    # makes __doc__ attribute mutable before Python 3.3
    __metaclass__ = type.__new__(type, "type", (uproot.interp.interp.Interpretation.__metaclass__,), {})

    def __init__(self, content, skipbytes=0, cls=None):
        self.content = content
        self.skipbytes = skipbytes
        self.cls = cls

    def __repr__(self):
        return "asjagged({0})".format(repr(self.content))

    def to(self, todtype=None, todims=None, skipbytes=None):
        if skipbytes is None:
            skipbytes = self.skipbytes
        return asjagged(self.content.to(todtype, todims), skipbytes)
github diana-hep / oamap / oamap / source / root.py View on Github external
elif isinstance(name, oamap.generator.StopsRole):
                                if name not in out:
                                    starts, stops = oamap.source.packing.ListCounts.fromcounts(array)
                                    out[oamap.generator.StartsRole(str(name.starts), str(name))] = starts
                                    out[name] = stops

                            elif isinstance(name, oamap.generator.DataRole):
                                if recarrayitem is None:
                                    out[name] = array
                                else:
                                    out[name] = array[recarrayitem]

                            else:
                                raise AssertionError

                        elif isinstance(array, uproot.interp.jagged.JaggedArray):
                            if isinstance(name, oamap.generator.StartsRole):
                                out[name] = array.starts

                            elif isinstance(name, oamap.generator.StopsRole):
                                out[name] = array.stops

                            elif isinstance(name, oamap.generator.DataRole):
                                out[name] = array.content

                            else:
                                raise AssertionError

                        elif isinstance(array, uproot.interp.strings.Strings):
                            if isinstance(name, oamap.generator.StartsRole):
                                out[name] = array.jaggedarray.starts