示例#1
0
    def recurse(layout, row_arrays, col_names):
        if layout.purelist_depth > 1:
            offsets, flattened = layout.offsets_and_flatten(axis=1)
            offsets = numpy.asarray(offsets)
            starts, stops = offsets[:-1], offsets[1:]
            counts = stops - starts
            if awkward1._util.win:
                counts = counts.astype(numpy.int32)
            if len(row_arrays) == 0:
                newrows = [
                    numpy.repeat(numpy.arange(len(counts), dtype=counts.dtype),
                                 counts)
                ]
            else:
                newrows = [numpy.repeat(x, counts) for x in row_arrays]
            newrows.append(
                numpy.arange(offsets[-1], dtype=counts.dtype) -
                numpy.repeat(starts, counts))
            return recurse(flattened, newrows, col_names)

        elif isinstance(layout, awkward1.layout.RecordArray):
            return sum([
                recurse(layout.field(n), row_arrays, col_names + (n, ))
                for n in layout.keys()
            ], [])

        else:
            try:
                return [(awkward1.operations.convert.to_numpy(layout),
                         row_arrays, col_names)]
            except:
                return [(layout, row_arrays, col_names)]
示例#2
0
def keys(array):
    """
    Extracts record or tuple keys from `array` (many types supported,
    including all Awkward Arrays and Records).

    If the array contains nested records, only the outermost record is
    queried. If it contains tuples instead of records, the keys are
    string representations of integers, such as `"0"`, `"1"`, `"2"`, etc.
    The records or tuples may be within multiple layers of nested lists.

    If the array contains neither tuples nor records, this returns an empty
    list.
    """
    layout = awkward1.operations.convert.to_layout(array,
                                                   allow_record=True,
                                                   allow_other=False)
    return layout.keys()
示例#3
0
    def recurse(layout):
        if isinstance(layout, awkward1.partition.PartitionedArray):
            return awkward0.ChunkedArray(
                       [recurse(x) for x in layout.partitions])

        elif isinstance(layout, awkward1.layout.NumpyArray):
            return numpy.asarray(layout)

        elif isinstance(layout, awkward1.layout.EmptyArray):
            return numpy.array([])

        elif isinstance(layout, awkward1.layout.RegularArray):
            # content, size
            if keeplayout:
                raise ValueError(
                        "awkward0 has no equivalent of RegularArray; "
                        "try keeplayout=False")
            offsets = numpy.arange(0,
                                   (len(layout) + 1)*layout.size,
                                   layout.size)
            return awkward0.JaggedArray.fromoffsets(
                     offsets, recurse(layout.content))

        elif isinstance(layout, awkward1.layout.ListArray32):
            # starts, stops, content
            return awkward0.JaggedArray(
                     numpy.asarray(layout.starts),
                     numpy.asarray(layout.stops),
                     recurse(layout.content))

        elif isinstance(layout, awkward1.layout.ListArrayU32):
            # starts, stops, content
            return awkward0.JaggedArray(
                     numpy.asarray(layout.starts),
                     numpy.asarray(layout.stops),
                     recurse(layout.content))

        elif isinstance(layout, awkward1.layout.ListArray64):
            # starts, stops, content
            return awkward0.JaggedArray(
                     numpy.asarray(layout.starts),
                     numpy.asarray(layout.stops),
                     recurse(layout.content))

        elif isinstance(layout, awkward1.layout.ListOffsetArray32):
            # offsets, content
            return awkward0.JaggedArray.fromoffsets(
                     numpy.asarray(layout.offsets),
                     recurse(layout.content))

        elif isinstance(layout, awkward1.layout.ListOffsetArrayU32):
            # offsets, content
            return awkward0.JaggedArray.fromoffsets(
                     numpy.asarray(layout.offsets),
                     recurse(layout.content))

        elif isinstance(layout, awkward1.layout.ListOffsetArray64):
            # offsets, content
            return awkward0.JaggedArray.fromoffsets(
                     numpy.asarray(layout.offsets),
                     recurse(layout.content))

        elif isinstance(layout, awkward1.layout.Record):
            # istuple, numfields, field(i)
            out = []
            for i in range(layout.numfields):
                content = layout.field(i)
                if isinstance(content, (awkward1.layout.Content,
                                        awkward1.layout.Record)):
                    out.append(recurse(content))
                else:
                    out.append(content)
            if layout.istuple:
                return tuple(out)
            else:
                return dict(zip(layout.keys(), out))

        elif isinstance(layout, awkward1.layout.RecordArray):
            # istuple, numfields, field(i)
            if layout.numfields == 0 and len(layout) != 0:
                raise ValueError(
                    "cannot convert zero-field, nonzero-length RecordArray "
                    "to awkward0.Table (limitation in awkward0)")
            keys = layout.keys()
            values = [recurse(x) for x in layout.contents]
            pairs = collections.OrderedDict(zip(keys, values))
            out = awkward0.Table(pairs)
            if layout.istuple:
                out._rowname = "tuple"
            return out

        elif isinstance(layout, awkward1.layout.UnionArray8_32):
            # tags, index, numcontents, content(i)
            return awkward0.UnionArray(numpy.asarray(layout.tags),
                                       numpy.asarray(layout.index),
                                       [recurse(x) for x in layout.contents])

        elif isinstance(layout, awkward1.layout.UnionArray8_U32):
            # tags, index, numcontents, content(i)
            return awkward0.UnionArray(numpy.asarray(layout.tags),
                                       numpy.asarray(layout.index),
                                       [recurse(x) for x in layout.contents])

        elif isinstance(layout, awkward1.layout.UnionArray8_64):
            # tags, index, numcontents, content(i)
            return awkward0.UnionArray(numpy.asarray(layout.tags),
                                       numpy.asarray(layout.index),
                                       [recurse(x) for x in layout.contents])

        elif isinstance(layout, awkward1.layout.IndexedOptionArray32):
            # index, content
            index = numpy.asarray(layout.index)
            toosmall = (index < -1)
            if toosmall.any():
                index = index.copy()
                index[toosmall] = -1
            return awkward0.IndexedMaskedArray(index, recurse(layout.content))

        elif isinstance(layout, awkward1.layout.IndexedOptionArray64):
            # index, content
            index = numpy.asarray(layout.index)
            toosmall = (index < -1)
            if toosmall.any():
                index = index.copy()
                index[toosmall] = -1
            return awkward0.IndexedMaskedArray(index, recurse(layout.content))

        elif isinstance(layout, awkward1.layout.IndexedArray32):
            # index, content
            return awkward0.IndexedArray(numpy.asarray(layout.index),
                                         recurse(layout.content))

        elif isinstance(layout, awkward1.layout.IndexedArrayU32):
            # index, content
            return awkward0.IndexedArray(numpy.asarray(layout.index),
                                         recurse(layout.content))

        elif isinstance(layout, awkward1.layout.IndexedArray64):
            # index, content
            return awkward0.IndexedArray(numpy.asarray(layout.index),
                                         recurse(layout.content))

        elif isinstance(layout, awkward1.layout.ByteMaskedArray):
            # mask, content, valid_when
            return awkward0.MaskedArray(numpy.asarray(layout.mask),
                                        recurse(layout.content),
                                        maskedwhen=(not layout.valid_when))

        elif isinstance(layout, awkward1.layout.BitMaskedArray):
            # mask, content, valid_when, length, lsb_order
            return awkward0.BitMaskedArray(numpy.asarray(layout.mask),
                                           recurse(layout.content),
                                           maskedwhen=(not layout.valid_when),
                                           lsborder=layout.lsb_order)

        elif isinstance(layout, awkward1.layout.UnmaskedArray):
            # content
            return recurse(layout.content)   # no equivalent in awkward0

        else:
            raise AssertionError(
                    "missing converter for {0}".format(type(layout).__name__))