How to use awkward1 - 10 common examples

To help you get started, we’ve selected a few awkward1 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 CoffeaTeam / coffea / coffea / nanoevents / factory.py View on Github external
dtype = interpretation.type
            length = len(self)
        parameters = {
            "__doc__": self._tree[branch_name].title.decode("ascii"),
            "__record__": "NanoColumn",
        }
        # use hint to resolve platform-dependent format
        formhint = awkward1.forms.Form.fromjson('"%s"' % dtype)
        form = awkward1.forms.NumpyForm(
            [], formhint.itemsize, formhint.format, parameters=parameters
        )
        generator = awkward1.layout.ArrayGenerator(
            self.reader, (branch_name, parameters), {}, form=form, length=length,
        )
        source = "file"
        return awkward1.layout.VirtualArray(
            generator,
            self._cache,
            "/".join([self._keyprefix, source, branch_name]),
            parameters=parameters,
        )
github CoffeaTeam / coffea / coffea / nanoevents / factory.py View on Github external
n = len(indexers)
            out = numpy.empty(n * len(indexers[0]), dtype="int64")
            for i, idx in enumerate(indexers):
                out[i::n] = idx
            offsets = numpy.arange(0, len(out) + 1, n, dtype=numpy.int64)
            return awkward1.layout.ListOffsetArray64(
                awkward1.layout.Index64(offsets), awkward1.layout.NumpyArray(out),
            )

        form = awkward1.forms.Form.fromjson(
            '{"class": "ListOffsetArray64", "offsets": "i64", "content": "int64"}'
        )
        generator = awkward1.layout.ArrayGenerator(
            nestedindex, (), {}, form=form, length=indexers[0].generator.length,
        )
        return awkward1.layout.VirtualArray(
            generator,
            self._cache,
            cache_key="/".join([self._keyprefix, "nestedindex", name]),
        )
github CoffeaTeam / coffea / coffea / nanoevents / factory.py View on Github external
gidx = awkward1.Array(
                awkward1.layout.ListOffsetArray32(
                    awkward1.layout.Index32(source_offsets), index.generator(),
                )
            )
            gidx = gidx.mask[gidx >= 0] + target_offsets[:-1]
            return awkward1.fill_none(awkward1.flatten(gidx), -1)

        generator = awkward1.layout.ArrayGenerator(
            globalindex,
            (),
            {},
            form=awkward1.forms.Form.fromjson('"int64"'),
            length=index.generator.length,
        )
        return awkward1.layout.VirtualArray(
            generator,
            index.cache,
            "/".join([self._keyprefix, "local2global", name]),
            index.identities,
            index.parameters,
        )
github CoffeaTeam / coffea / coffea / nanoaod / nanoawkward1.py View on Github external
def _with_length(array: awkward1.layout.VirtualArray, length: int):
    return awkward1.layout.VirtualArray(
        array.generator.with_length(length),
        array.cache,
        array.cache_key,
        array.identities,
        array.parameters,
    )
github CoffeaTeam / coffea / coffea / nanoevents / factory.py View on Github external
"__array__": mixin,
                    "events_key": self._keyprefix,
                    "collection_name": name,
                }
                form = awkward1.forms.ListOffsetForm(
                    "i32", content.form, parameters=params
                )
                generator = awkward1.layout.ArrayGenerator(
                    self._listarray,
                    (offsets, content, params),
                    {},
                    form=form,
                    length=len(self),
                )
                source = "runtime"
                return awkward1.layout.VirtualArray(
                    generator,
                    self._cache,
                    cache_key="/".join([self._keyprefix, source, name]),
                    parameters=params,
                )
            elif name in arrays:
                # singleton
                return arrays[name]
            else:
                # simple collection
                content = {
                    k[len(name) + 1 :]: arrays[k]
                    for k in arrays
                    if k.startswith(name + "_")
                }
                return awkward1.layout.RecordArray(
github CoffeaTeam / coffea / coffea / nanoaod / nanoawkward1.py View on Github external
def _array(self, branch_name: bytes):
        interpretation = uproot.interpret(self._tree[branch_name])
        if isinstance(interpretation, uproot.asjagged):
            dtype = interpretation.content.type
            length = None
        else:
            dtype = interpretation.type
            length = len(self)
        form = awkward1.forms.Form.fromjson('"%s"' % dtype)
        generator = awkward1.layout.ArrayGenerator(
            self.reader, (branch_name,), {}, form=form, length=length,
        )
        return awkward1.layout.VirtualArray(
            generator,
            self._cache,
            cache_key="/".join([self._keyprefix, "file", branch_name.decode("ascii")]),
            parameters={"__doc__": self._tree[branch_name].title.decode("ascii"),},
        )
github CoffeaTeam / coffea / coffea / nanoevents / methods / base.py View on Github external
def descend(layout, depth):
            islistarray = isinstance(
                layout,
                (awkward1.layout.ListOffsetArray32, awkward1.layout.ListOffsetArray64,),
            )
            if islistarray and layout.content.parameter("collection_name") is not None:
                return lambda: layout
github CoffeaTeam / coffea / coffea / nanoevents / factory.py View on Github external
def _local2global(self, index, source_offsets, target_offsets, name):
        def globalindex():
            gidx = awkward1.Array(
                awkward1.layout.ListOffsetArray32(
                    awkward1.layout.Index32(source_offsets), index.generator(),
                )
            )
            gidx = gidx.mask[gidx >= 0] + target_offsets[:-1]
            return awkward1.fill_none(awkward1.flatten(gidx), -1)

        generator = awkward1.layout.ArrayGenerator(
            globalindex,
            (),
            {},
            form=awkward1.forms.Form.fromjson('"int64"'),
            length=index.generator.length,
        )
        return awkward1.layout.VirtualArray(
            generator,
            index.cache,
            "/".join([self._keyprefix, "local2global", name]),
            index.identities,
            index.parameters,
        )
github CoffeaTeam / coffea / coffea / nanoevents / factory.py View on Github external
- Any branches named ``n{name}`` are assumed to be counts branches and converted to offsets ``o{name}``
        - Any local index branches with names matching ``{source}_{target}Idx*`` are converted to global indexes for the event chunk
        - Any `NanoEventsFactory.nested_items` are constructed, if the necessary branches are available
        - Any `NanoEventsFactory.special_items` are constructed, if the necessary branches are available

        From those arrays, NanoAOD collections are formed as collections of branches grouped by name, where:

        - one branch exists named ``name`` and no branches start with ``name_``, interpreted as a single flat array;
        - one branch exists named ``name``, one named ``n{name}``, and no branches start with ``name_``, interpreted as a single jagged array;
        - no branch exists named ``{name}`` and many branches start with ``name_*``, interpreted as a flat table; or
        - one branch exists named ``n{name}`` and many branches start with ``name_*``, interpreted as a jagged table.

        All collections are then zipped into one `NanoEvents` record and returned.
        """
        self._build_collections()
        events = awkward1.layout.RecordArray(
            self._collections,
            parameters={
                "__record__": "NanoEvents",
                "__doc__": self._tree.title.decode("ascii"),
                "events_key": self._keyprefix,
                "metadata": self._metadata,
            },
        )
        return awkward1.Array(events)
github CoffeaTeam / coffea / coffea / nanoevents / factory.py View on Github external
self._tree.numentries, entrystart, entrystop
        )
        self._keyprefix = "/".join(
            [
                file._context.uuid.hex(),
                treename,
                str(self._entrystart),
                str(self._entrystop),
            ]
        )
        NanoEventsFactory._active[self._keyprefix] = self

        if cache is None:
            cache = awkward1.layout.ArrayCache({})
        else:
            cache = awkward1.layout.ArrayCache(cache)
        self._cache = cache

        self._mixin_map = {}
        self._mixin_map.update(self.default_mixins)
        if mixin_map is not None:
            self._mixin_map.update(mixin_map)

        self._metadata = metadata  # TODO: JSON only?
        self._branches_read = set()
        self._collections = None