How to use the awkward.type 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 CoffeaTeam / coffea / coffea / nanoaod / nanoevents.py View on Github external
A mapping from branch name to flat numpy array or awkward VirtualArray
            methods : dict, optional
                A mapping from collection name to class deriving from `awkward.array.objects.Methods`
                that implements additional mixins
            metadata : dict, optional
                Arbitrary metadata to embed in this NanoEvents table

        Returns a NanoEvents object
        '''
        arrays = dict(arrays)
        for k in arrays:
            if isinstance(arrays[k], awkward.VirtualArray):
                pass
            elif isinstance(arrays[k], numpy.ndarray):
                value = arrays[k]
                arrays[k] = awkward.VirtualArray(lambda: value, type=awkward.type.ArrayType(len(arrays[k]), arrays[k].dtype))
                print(arrays[k])
            else:
                raise ValueError("The array %s : %r is not a valid type" % (k, arrays[k]))
        events = cls.named('event')
        collections = {k.split('_')[0] for k in arrays.keys()}
        collections -= {k for k in collections if k.startswith('n') and k[1:] in collections}
        allmethods = {}
        allmethods.update(collection_methods)
        if methods is not None:
            allmethods.update(methods)
        for name in collections:
            methods = allmethods.get(name, None)
            events.contents[name] = NanoCollection.from_arrays(arrays, name, methods)

        for name in events.columns:
            # soft hasattr via type, to prevent materialization
github scikit-hep / awkward-array / awkward / util.py View on Github external
if isinstance(arrayn, awkward.array.objects.ObjectArray) and not isinstance(arrayn, awkward.array.objects.StringArray):
                    arrayn = arrayn.content
                if not isinstance(tpen, (numpy.dtype, str, bytes, awkward.type.Type)):
                    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
github scikit-hep / awkward-array / awkward / generate.py View on Github external
raise TypeError("chunksize must be a positive integer")

    _checkoptions(options)

    awkwardlib = awkward.util.awkwardlib(awkwardlib)
    fillable = UnknownFillable(awkwardlib)
    count = 0
    tpe = None

    for obj in iterable:
        fillable = fillable.append(obj, typeof(obj))
        count += 1

        if count == chunksize:
            out = fillable.finalize(**options)
            outtpe = awkward.type.fromarray(out).to
            if tpe is None:
                tpe = outtpe
            elif tpe != outtpe:
                raise TypeError("data type has changed after the first chunk (first chunk is not large enough to see the full generality of the data):\n\n{0}\n\nversus\n\n{1}".format(awkward.type._str(tpe, indent="    "), awkward.type._str(outtpe, indent="    ")))
            yield out

            fillable.clear()
            count = 0

    if count != 0:
        out = fillable.finalize(**options)
        outtpe = awkward.type.fromarray(out).to
        if tpe is None:
            tpe = outtpe
        elif tpe != outtpe:
            raise TypeError("data type has changed after the first chunk (first chunk is not large enough to see the full generality of the data):\n\n{0}\n\nversus\n\n{1}".format(awkward.type._str(tpe, indent="    "), awkward.type._str(outtpe, indent="    ")))
github CoffeaTeam / coffea / coffea / nanoaod / methods / leptons.py View on Github external
def _finalize(self, name, events):
        del self['mass']
        super(Photon, self)._finalize(name, events)
        if 'Electron' in events.columns:
            electrons = events['Electron']
            reftype = awkward.type.ArrayType(float('inf'), awkward.type.OptionType(electrons.type.to.to))
            reftype.check = False
            embedded_electron = type(electrons)(
                self._lazy_crossref,
                args=(self._getcolumn('electronIdx'), electrons),
                type=reftype,
            )
            embedded_electron.__doc__ = electrons.__doc__
            self['matched_electron'] = embedded_electron
            del self['electronIdx']
github CoffeaTeam / coffea / coffea / nanoaod / methods / generator.py View on Github external
def _finalize(self, name, events):
        parent_type = awkward.type.ArrayType(float('inf'), awkward.type.OptionType(events.GenPart.type.to.to))
        parent_type.check = False  # break recursion
        gen_parent = type(events.GenPart)(
            self._lazy_crossref,
            args=(self._getcolumn('genPartIdxMother'), events.GenPart),
            type=parent_type,
        )
        gen_parent.__doc__ = self.__doc__
        self['parent'] = gen_parent
        self.type.check = False
        del self['genPartIdxMother']
github scikit-hep / awkward-array / impl_flatpandas.py View on Github external
def recurse(array, tpe, cols, seriously):
        if isinstance(tpe, awkward.type.TableType):
            starts, stops = None, None
            out, deferred, unflattened = None, {}, None

            for n in tpe.columns:
                if not isinstance(n, str):
                    raise ValueError("column names must be strings")

                tpen = tpe[n]
                colsn = cols + (n,) if seriously else cols
                if isinstance(tpen, awkward.type.OptionType):
                    array = array.content
                    tpen = tpen.type

                if isinstance(tpen, numpy.dtype):
                    columns.append(colsn)
                    tmp = array[n]

                elif isinstance(tpen, type) and issubclass(tpen, (str, bytes)):
                    columns.append(colsn)
                    tmp = array[n]

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

                elif isinstance(tpen, awkward.type.TableType):
                    tmp = recurse(array[n], tpen, colsn, True)