Beispiel #1
0
        def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
            if "out" in kwargs:
                raise NotImplementedError("in-place operations not supported")

            if method != "__call__":
                return NotImplemented

            torow = not any(not isinstance(x, Table.Row) and isinstance(x, Iterable) for x in inputs)

            inputs = list(inputs)
            for i in range(len(inputs)):
                if isinstance(inputs[i], Table.Row):
                    inputs[i] = inputs[i]._table[inputs[i]._index : inputs[i]._index + 1]

            result = getattr(ufunc, method)(*inputs, **kwargs)

            if torow:
                if isinstance(result, tuple):
                    out = []
                    for x in result:
                        if isinstance(x, Table):
                            out.append(awkward.array.objects.Methods.maybemixin(type(x), self._table.Table.Row)(x, 0))
                            out[-1]._table._showdict = True
                        else:
                            out.append(x)
                    return tuple(out)
                elif method == "at":
                    return None
                else:
                    out = awkward.array.objects.Methods.maybemixin(type(result), self._table.Table.Row)(result, 0)
                    out._table._showdict = True
                    return out

            else:
                return result
Beispiel #2
0
    def _encode_complex(self, obj, context):
        if callable(getattr(obj, "__awkward_serialize__", None)):
            return obj.__awkward_serialize__(self)

        if hasattr(obj, "tojson") and hasattr(type(obj), "fromjson"):
            try:
                return self.encode_call(self._obj2spec(type(obj).fromjson),
                                        self.encode_json(obj.tojson()))
            except:
                pass

        if isinstance(obj, numpy.ndarray):
            return self._encode_numpy(obj, context)

        if hasattr(obj,
                   "__module__") and (hasattr(obj, "__qualname__") or hasattr(
                       obj, "__name__")) and obj.__module__ != "__main__":
            try:
                return {"function": self._obj2spec(obj)}
            except:
                pass

        try:
            return self.encode_json(obj)
        except TypeError:
            pass

        try:
            return self.encode_python(obj)
        except:
            pass
Beispiel #3
0
def test_UnmaskedArray():
    content = ak.layout.NumpyArray(
        np.array([1.1, 2.2, 3.3, 4.4, 5.5], dtype=np.float64))
    array = ak.Array(ak.layout.UnmaskedArray(content))
    assert ak.to_list(array) == [1.1, 2.2, 3.3, 4.4, 5.5]
    assert str(ak.type(array)) == "5 * ?float64"

    @numba.njit
    def f1(x):
        return 3.14

    f1(array)

    @numba.njit
    def f2(x):
        return x

    y = f2(array)
    assert isinstance(y.layout, ak.layout.UnmaskedArray)
    assert ak.to_list(y) == ak.to_list(array)
    assert str(ak.type(y)) == str(ak.type(array))

    @numba.njit
    def f3(x, i):
        return x[i]

    assert [f3(array, i)
            for i in range(len(array))] == [1.1, 2.2, 3.3, 4.4, 5.5]
Beispiel #4
0
def typeof(obj):
    if obj is None:
        return None

    elif isinstance(obj, (bool, numpy.bool_, numpy.bool)):
        return BoolFillable
    elif isinstance(obj, (numbers.Number, awkward.numpy.number)):
        return NumberFillable
    elif isinstance(obj, bytes):
        return BytesFillable
    elif isinstance(obj, awkward.util.string):
        return StringFillable

    elif isinstance(obj, dict):
        if any(not isinstance(x, str) for x in obj):
            raise TypeError("only dicts with str-typed keys may be converted")
        if len(obj) == 0:
            return None
        else:
            return set(obj)

    elif isinstance(obj, tuple) and hasattr(obj, "_fields") and obj._fields is type(obj)._fields:
        return obj._fields, type(obj)

    elif isinstance(obj, Iterable):
        return JaggedFillable

    else:
        return set(n for n in obj.__dict__ if not n.startswith("_")), type(obj)
def test_from_iter():
    assert ak.from_iter([1 + 1j, 2 + 2j,
                         3 + 3j]).tolist() == [1 + 1j, 2 + 2j, 3 + 3j]
    assert ak.from_iter([[1 + 1j, 2 + 2j], [], [3 + 3j]]).tolist() == [
        [1 + 1j, 2 + 2j],
        [],
        [3 + 3j],
    ]

    # First encounter of a complex number should promote previous integers and
    # reals into complex numbers:
    assert ak.from_iter([1, 2.2,
                         3 + 3j]).tolist() == [1.0 + 0j, 2.2 + 0j, 3.0 + 3j]
    assert ak.from_iter([1, 3 + 3j]).tolist() == [1.0 + 0j, 3.0 + 3j]

    # Just as the first encounter of a real number promotes previous integers
    # into reals:
    assert str(ak.from_iter([1, 2.2]).type) == "2 * float64"
    assert ak.from_iter([1, 2.2]).tolist() == [1.0, 2.2]
    builder = ak.ArrayBuilder()
    assert str(ak.type(builder)) == "0 * unknown"
    builder.integer(1)
    assert str(ak.type(builder)) == "1 * int64"
    builder.real(2.2)
    assert str(ak.type(builder)) == "2 * float64"

    # For that matter, ArrayBuilder is missing a high-level interface to complex:
    builder.complex(3 + 3j)
    assert str(ak.type(builder)) == "3 * complex128"
Beispiel #6
0
    def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
        import awkward.array.objects

        if method != "__call__":
            return NotImplemented

        tokeep = None
        for x in inputs:
            if isinstance(x, MaskedArray):
                x._valid()
                if tokeep is None:
                    tokeep = x.boolmask(maskedwhen=False)
                else:
                    tokeep = tokeep & x.boolmask(maskedwhen=False)

        assert tokeep is not None

        inputs = list(inputs)
        for i in range(len(inputs)):
            if isinstance(inputs[i], IndexedMaskedArray):
                inputs[i] = inputs[i]._content[inputs[i]._mask[tokeep]]
            elif isinstance(inputs[i], MaskedArray):
                inputs[i] = inputs[i]._content[tokeep]
            elif isinstance(
                    inputs[i],
                (awkward.util.numpy.ndarray, awkward.array.base.AwkwardArray)):
                inputs[i] = inputs[i][tokeep]
            else:
                try:
                    for first in inputs[i]:
                        break
                except TypeError:
                    pass
                else:
                    inputs[i] = awkward.util.numpy.array(inputs[i],
                                                         copy=False)[tokeep]

        # compute only the non-masked elements
        result = getattr(ufunc, method)(*inputs, **kwargs)

        # put the masked out values back
        index = awkward.util.numpy.full(len(tokeep),
                                        -1,
                                        dtype=awkward.util.INDEXTYPE)
        index[tokeep] = awkward.util.numpy.arange(
            awkward.util.numpy.count_nonzero(tokeep))

        if isinstance(result, tuple):
            return tuple(
                awkward.array.objects.Methods.maybemixin(
                    type(x), IndexedMaskedArray)(index, x, maskedwhen=-1)
                if isinstance(x, (awkward.util.numpy.ndarray,
                                  awkward.array.base.AwkwardBase)) else x
                for x in result)
        elif method == "at":
            return None
        else:
            return awkward.array.objects.Methods.maybemixin(
                type(result), IndexedMaskedArray)(index, result, maskedwhen=-1)
Beispiel #7
0
def test_type():
    content = ak.layout.NumpyArray(np.array([0.0, 1.1, 2.2, 3.3, 4.4]))
    index = ak.layout.Index32(np.array([2, 2, 0, 3, 4], dtype=np.int32))
    array = ak.layout.IndexedArray32(index, content)
    assert ak.type(array) == ak.types.PrimitiveType("float64")
    array = ak.layout.IndexedOptionArray32(index, content)
    assert ak.type(array) == ak.types.OptionType(
        ak.types.PrimitiveType("float64"))
Beispiel #8
0
def test_ArrayBuilder_append_2():
    # issue #415
    A = ak.from_numpy(np.array([0, 1, 2], dtype=np.float32))
    B = ak.from_numpy(np.array([0, 1], dtype=np.float32))

    builder = ak.ArrayBuilder()
    with builder.list():
        builder.append(A.tolist())
    with builder.list():
        builder.append(A.tolist())
    with builder.list():
        pass
    with builder.list():
        builder.append(B.tolist())

    assert builder.snapshot().tolist() == [[[0, 1, 2]], [[0, 1, 2]], [],
                                           [[0, 1]]]
    assert str(ak.type(builder.snapshot())) == "4 * var * var * float64"

    builder = ak.ArrayBuilder()
    with builder.list():
        builder.append(A)
    with builder.list():
        builder.append(A)
    with builder.list():
        pass
    with builder.list():
        builder.append(B)

    assert builder.snapshot().tolist() == [[[0, 1, 2]], [[0, 1, 2]], [],
                                           [[0, 1]]]
    assert str(ak.type(builder.snapshot())) == "4 * var * var * float32"

    @numba.njit
    def f1(builder, A, B):
        builder.begin_list()
        builder.append(A)
        builder.end_list()

        builder.begin_list()
        builder.append(A)
        builder.end_list()

        builder.begin_list()
        builder.end_list()

        builder.begin_list()
        builder.append(B)
        builder.end_list()

        return builder

    assert f1(ak.ArrayBuilder(), A, B).snapshot().tolist() == [
        [[0, 1, 2]],
        [[0, 1, 2]],
        [],
        [[0, 1]],
    ]
Beispiel #9
0
def test_UnmaskedArray():
    content = ak.layout.NumpyArray(
        np.array([1.1, 2.2, 3.3, 4.4, 5.5], dtype=np.float64))
    array = ak.layout.UnmaskedArray(content)
    assert ak.to_list(array) == [1.1, 2.2, 3.3, 4.4, 5.5]
    assert str(ak.type(content)) == "float64"
    assert str(ak.type(ak.Array(content))) == "5 * float64"
    assert str(ak.type(array)) == "?float64"
    assert str(ak.type(ak.Array(array))) == "5 * ?float64"
def test_lorentz_vectorization(collection, events):
    mask = ak.num(events[collection]) > 0
    assert (ak.type(events[collection][mask][
        0, 0].Area).parameters["__record__"] == "LorentzVector")
    assert (ak.type(events[collection][mask][
        0, 0].SoftDroppedJet).parameters["__record__"] == "LorentzVector")
    assert (ak.type(events[collection][mask][
        0, 0].SoftDroppedSubJet1).parameters["__record__"] == "LorentzVector")
    assert (ak.type(events[collection][mask][
        0, 0].SoftDroppedSubJet2).parameters["__record__"] == "LorentzVector")
def test_EmptyArray():
    array = ak.layout.EmptyArray()
    assert ak.to_list(ak.sort(array)) == []
    assert ak.to_list(ak.argsort(array)) == []
    assert str(ak.type(ak.sort(array))) == "0 * float64"
    assert str(ak.type(ak.argsort(array))) == "0 * int64"

    array2 = ak.Array([[], [], []])
    assert ak.to_list(ak.argsort(array2)) == [[], [], []]
    assert str(ak.type(ak.argsort(array2))) == "3 * var * int64"
def test_nested_lorentz_vectorization(collection, events):
    mask = ak.num(events[collection]) > 0
    assert ak.all(ak.num(events[collection].PrunedP4_5, axis=2) == 5)
    assert (ak.type(events[collection][mask].PrunedP4_5[
        0, 0, 0]).parameters["__record__"] == "LorentzVector")

    assert ak.all(ak.num(events[collection].SoftDroppedP4_5, axis=2) == 5)
    assert (ak.type(events[collection][mask].SoftDroppedP4_5[
        0, 0, 0]).parameters["__record__"] == "LorentzVector")

    assert ak.all(ak.num(events[collection].TrimmedP4_5, axis=2) == 5)
    assert (ak.type(events[collection][mask].TrimmedP4_5[
        0, 0, 0]).parameters["__record__"] == "LorentzVector")
def test_typestr():
    assert (str(ak.type(ak.to_categorical(ak.Array(
        [1.1, 2.2, 2.2, 3.3])))) == "4 * categorical[type=float64]")
    assert (str(
        ak.type(ak.to_categorical(ak.Array(
            [1.1, 2.2, None, 2.2, 3.3])))) == "5 * categorical[type=?float64]")
    assert (str(
        ak.type(ak.to_categorical(ak.Array(
            ["one", "two", "two",
             "three"])))) == "4 * categorical[type=string]")
    assert (str(
        ak.type(
            ak.to_categorical(ak.Array(
                ["one", "two", None, "two",
                 "three"])))) == "5 * categorical[type=option[string]]")
Beispiel #14
0
def test_builder_string():
    builder = ak.ArrayBuilder()

    builder.bytestring(b"one")
    builder.bytestring(b"two")
    builder.bytestring(b"three")

    a = builder.snapshot()
    assert str(a) == "[b'one', b'two', b'three']"
    assert ak.to_list(a) == [b"one", b"two", b"three"]
    assert ak.to_json(a) == '["one","two","three"]'
    assert repr(a) == "<Array [b'one', b'two', b'three'] type='3 * bytes'>"
    assert repr(ak.type(a)) == "3 * bytes"

    builder = ak.ArrayBuilder()

    builder.string("one")
    builder.string("two")
    builder.string("three")

    a = builder.snapshot()
    assert str(a) == "['one', 'two', 'three']"
    assert ak.to_list(a) == ["one", "two", "three"]
    assert ak.to_json(a) == '["one","two","three"]'
    assert repr(a) == "<Array ['one', 'two', 'three'] type='3 * string'>"
    assert repr(ak.type(a)) == "3 * string"

    builder = ak.ArrayBuilder()

    builder.begin_list()
    builder.string("one")
    builder.string("two")
    builder.string("three")
    builder.end_list()

    builder.begin_list()
    builder.end_list()

    builder.begin_list()
    builder.string("four")
    builder.string("five")
    builder.end_list()

    a = builder.snapshot()
    assert str(a) == "[['one', 'two', 'three'], [], ['four', 'five']]"
    assert ak.to_list(a) == [["one", "two", "three"], [], ["four", "five"]]
    assert ak.to_json(a) == '[["one","two","three"],[],["four","five"]]'
    assert repr(ak.type(a)) == "3 * var * string"
def test_reshaping():
    v2 = ak._v2.contents.NumpyArray(
        np.array([0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]))
    v1 = ak.layout.NumpyArray(
        np.array([0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]))

    resultv2 = v2[ak._v2.contents.NumpyArray(
        np.array([3, 6, 9, 2, 2, 1], np.int64))]
    resultv1 = v1[ak.layout.NumpyArray(np.array([3, 6, 9, 2, 2, 1], np.int64))]
    assert ak.to_list(resultv2) == [3.3, 6.6, 9.9, 2.2, 2.2, 1.1]
    assert ak.to_list(resultv1) == [3.3, 6.6, 9.9, 2.2, 2.2, 1.1]

    resultv2 = v2[ak._v2.contents.NumpyArray(
        np.array([[3, 6, 9], [2, 2, 1]], np.int64))]
    resultv1 = v1[ak.layout.NumpyArray(
        np.array([[3, 6, 9], [2, 2, 1]], np.int64))]
    assert ak.to_list(resultv2) == [[3.3, 6.6, 9.9], [2.2, 2.2, 1.1]]
    assert ak.to_list(resultv1) == [[3.3, 6.6, 9.9], [2.2, 2.2, 1.1]]

    assert (str(
        ak.type(
            ak.Array(
                v2_to_v1(v2[ak._v2.contents.NumpyArray(
                    np.ones((2, 3), np.int64))])))) == "2 * 3 * float64")
    assert (str(
        ak.type(ak.Array(v1[ak.layout.NumpyArray(np.ones(
            (2, 3), np.int64))]))) == "2 * 3 * float64")

    assert (str(
        ak.type(
            ak.Array(
                v2_to_v1(v2[ak._v2.contents.NumpyArray(
                    np.ones((0, 3), np.int64))])))) == "0 * 3 * float64")
    assert (str(
        ak.type(ak.Array(v1[ak.layout.NumpyArray(np.ones(
            (0, 3), np.int64))]))) == "0 * 3 * float64")

    assert (str(
        ak.type(
            ak.Array(
                v2_to_v1(v2[ak._v2.contents.NumpyArray(
                    np.ones((2, 0, 3),
                            np.int64))])))) == "2 * 0 * 3 * float64")
    assert (str(
        ak.type(
            ak.Array(v1[ak.layout.NumpyArray(np.ones(
                (2, 0, 3), np.int64))]))) == "2 * 0 * 3 * float64")

    assert (str(
        ak.type(
            ak.Array(
                v2_to_v1(v2[ak._v2.contents.NumpyArray(
                    np.ones((1, 2, 0, 3),
                            np.int64))])))) == "1 * 2 * 0 * 3 * float64")
    assert (str(
        ak.type(
            ak.Array(v1[ak.layout.NumpyArray(np.ones(
                (1, 2, 0, 3), np.int64))]))) == "1 * 2 * 0 * 3 * float64")
Beispiel #16
0
 def mixin(methods, awkwardtype):
     assert issubclass(methods, Methods)
     assert not issubclass(methods, awkward.array.base.AwkwardArray)
     assert issubclass(awkwardtype, awkward.array.base.AwkwardArray)
     assert not issubclass(awkwardtype, Methods)
     return type(awkwardtype.__name__ + "Methods", (methods, awkwardtype),
                 {})
Beispiel #17
0
    def __getitem__(self, where):
        self._valid()

        if awkward.util.isstringslice(where):
            content = self._content[where]
            cls = awkward.array.objects.Methods.maybemixin(type(content), IndexedMaskedArray)
            out = cls.__new__(cls)
            out.__dict__.update(self.__dict__)
            out._content = content
            return out

        if isinstance(where, tuple) and len(where) == 0:
            return self
        if not isinstance(where, tuple):
            where = (where,)
        head, tail = where[0], where[1:]

        if isinstance(head, awkward.util.integer):
            maskindex = self._mask[head]
            if maskindex == self._maskedwhen:
                if tail != ():
                    raise ValueError("masked element ({0}) is not subscriptable".format(self.masked))
                return self.masked
            else:
                return self._content[(maskindex,) + tail]

        else:
            maskindex = self._mask[head]
            if tail != () and (maskindex == self._maskedwhen).any():
                raise ValueError("masked element ({0}) is not subscriptable".format(self.masked))
            else:
                return self.copy(mask=maskindex)
Beispiel #18
0
    def __getitem__(self, where):
        self._valid()

        if awkward.util.isstringslice(where):
            content = self._content[where]
            cls = awkward.array.objects.Methods.maybemixin(type(content), BitMaskedArray)
            out = cls.__new__(cls)
            out.__dict__.update(self.__dict__)
            out._content = content
            return out

        if isinstance(where, tuple) and len(where) == 0:
            return self
        if not isinstance(where, tuple):
            where = (where,)
        head, tail = where[0], where[1:]

        if isinstance(head, awkward.util.integer):
            if self._maskwhere(head) == self._maskedwhen:
                if tail != ():
                    raise ValueError("masked element ({0}) is not subscriptable".format(self.masked))
                return self.masked
            else:
                return self._content[(head,) + tail]

        else:
            mask = self._maskwhere(head)
            if tail != () and ((self._maskedwhen and mask.any()) or (not self._maskedwhen and not mask.all())):
                raise ValueError("masked element ({0}) is not subscriptable".format(self.masked))
            else:
                return self.copy(mask=self.bool2bit(mask, lsborder=self._lsborder), content=self._content[(head,) + tail], lsborder=self._lsborder)
Beispiel #19
0
 def maybemixin(sample, awkwardtype):
     if issubclass(sample, Methods):
         assert issubclass(sample, awkward.array.base.AwkwardArray)
         allbases = tuple(x for x in sample.__bases__ if not issubclass(x, awkward.array.base.AwkwardArray)) + (awkwardtype,)
         return type(awkwardtype.__name__ + "Methods", allbases, {})
     else:
         return awkwardtype
Beispiel #20
0
def test_concatenate_number():
    assert ak.to_list(
        ak.concatenate([ak.Array([[1, 2, 3], [], [4, 5]]), 999],
                       axis=1)) == [[1, 2, 3, 999], [999], [4, 5, 999]]
    assert ak.to_list(
        ak.concatenate(
            [ak.Array([[[1.1], [2.2, 3.3]], [[]], [[4.4], [5.5]]]), 999],
            axis=2)) == [[[1.1, 999.0], [2.2, 3.3, 999.0]], [[999.0]],
                         [[4.4, 999.0], [5.5, 999.0]]]
    assert (str(
        ak.type(
            ak.concatenate(
                [
                    ak.Array([[1, 2, 3], [], [4, 5]]),
                    ak.Array([[123], [223], [323]]),
                ],
                axis=1,
            ))) == "3 * var * int64")
    assert ak.to_list(
        ak.concatenate([
            ak.Array([[1, 2, 3], [], [4, 5]]),
            ak.Array([[123], [223], [323]])
        ],
                       axis=1)) == [[1, 2, 3, 123], [223], [4, 5, 323]]

    one = ak.Array([[1, 2, 3], [], [4, 5]])
    two = ak.Array([[123], [223], [323]])
    assert ak.to_list(ak.concatenate([one, two], axis=1)) == [
        [1, 2, 3, 123],
        [223],
        [4, 5, 323],
    ]
Beispiel #21
0
def reset_infinities(values):
    try:
        values[np.absolute(values) == np.Infinity] = 0
    except Exception as e:
        logger.error('Cannot process', ak.type(values))
        raise e
    return values
Beispiel #22
0
    def _prepare(self, ufunc, identity, dtype):
        if dtype is None and issubclass(self.dtype.type,
                                        (self.numpy.bool_, self.numpy.bool)):
            dtype = self.numpy.dtype(type(identity))
        if dtype is None:
            dtype = self.dtype

        out = None
        index = self._index[:len(self._tags)]
        for tag, content in enumerate(self._contents):
            if not isinstance(content, self.numpy.ndarray):
                content = content._prepare(ufunc, identity, dtype)

            if not isinstance(content, self.numpy.ndarray):
                raise TypeError(
                    "cannot reduce a UnionArray of non-primitive type")

            mask = (self._tags == tag)
            c = content[index[mask]]

            if out is None:
                out = self.numpy.full(self._tags.shape[:1] + c.shape[1:],
                                      identity,
                                      dtype=dtype)
            out[mask] = c

        return out
def test_highlevel():
    array = ak.virtual(lambda: [[1.1, 2.2, 3.3], [], [4.4, 5.5]])
    assert isinstance(array.layout, ak.layout.VirtualArray)
    assert ak.to_list(array) == [[1.1, 2.2, 3.3], [], [4.4, 5.5]]

    counter = [0]

    def generate():
        counter[0] += 1
        return [[1.1, 2.2, 3.3], [], [4.4, 5.5]]

    array = ak.virtual(
        generate,
        length=3,
        form={
            "class": "ListOffsetArray64",
            "offsets": "i64",
            "content": "float64"
        },
    )
    assert counter[0] == 0

    assert len(array) == 3
    assert counter[0] == 0

    assert str(ak.type(array)) == "3 * var * float64"
    assert counter[0] == 0

    assert ak.to_list(array[2]) == [4.4, 5.5]
    assert counter[0] == 1
Beispiel #24
0
    def __call__(self, obj, context=""):
        out = self._encode_primitive(obj)

        if out is not None:
            return out

        if obj in self.seen:
            return {"ref": self.seen[obj]}
        else:
            ident = self.seen[obj]

        out = self._encode_complex(obj, context)
        if out is None:
            raise TypeError("failed to encode {0} (type: {1})".format(
                repr(obj), type(obj)))

        if "id" in out:
            if out["id"] is False:
                del self.seen[obj]
            elif out["id"] != self.seen[obj]:
                raise RuntimeError("unexpected id change")
        else:
            out["id"] = ident

        return out
Beispiel #25
0
def _normalize_arrays(cls, arrays):
    length = None
    for i in range(len(arrays)):
        if isinstance(arrays[i], Iterable):
            if length is None:
                length = len(arrays[i])
                break
    if length is None:
        raise TypeError("cannot construct an array if all arguments are scalar")

    arrays = list(arrays)
    jaggedtype = [cls.awkward.JaggedArray] * len(arrays)
    starts, stops = None, None
    for i in range(len(arrays)):
        if starts is None and isinstance(arrays[i], cls.awkward.JaggedArray):
            starts, stops = arrays[i].starts, arrays[i].stops

        if isinstance(arrays[i], cls.awkward.JaggedArray):
            jaggedtype[i] = type(arrays[i])

        if not isinstance(arrays[i], Iterable):
            arrays[i] = cls.awkward.numpy.full(length, arrays[i])

        arrays[i] = cls.awkward.util.toarray(arrays[i], cls.awkward.numpy.float64)

    if starts is None:
        return arrays

    for i in range(len(arrays)):
        if not isinstance(arrays[i], cls.awkward.JaggedArray) or not (cls.awkward.numpy.array_equal(starts, arrays[i].starts) and cls.awkward.numpy.array_equal(stops, arrays[i].stops)):
            content = cls.awkward.numpy.zeros(stops.max(), dtype=cls.awkward.numpy.float64)
            arrays[i] = jaggedtype[i](starts, stops, content) + arrays[i]    # invoke jagged broadcasting to align arrays

    return arrays
Beispiel #26
0
    def _prepare(self, ufunc, identity, dtype):
        self.knowchunksizes()
        out = None
        pos = 0
        for chunkid, chunk in enumerate(self._chunks):
            if self._chunksizes[chunkid] > 0:
                this = chunk[:self._chunksizes[chunkid]]
                if out is None:
                    if dtype is None and issubclass(
                            this.dtype.type,
                        (self.numpy.bool_, self.numpy.bool)):
                        dtype = self.numpy.dtype(type(identity))
                    if dtype is None:
                        dtype = this.dtype
                    out = self.numpy.empty(
                        (sum(self._chunksizes), ) + this.shape[1:],
                        dtype=dtype)

                newpos = pos + this.shape[0]
                out[pos:newpos] = this
                pos = newpos

        if out is None:
            if dtype is None:
                dtype = self.DEFAULTTYPE
            return self.numpy.array([identity], dtype=dtype)
        else:
            return out
Beispiel #27
0
def test_string2():
    content = ak.layout.NumpyArray(
        np.array([ord(x) for x in "heythere"], dtype=np.uint8))
    listoffsetarray = ak.layout.ListOffsetArray64(
        ak.layout.Index64(np.array([0, 3, 3, 8])), content)
    a = ak.Array(listoffsetarray, check_valid=True)

    assert isinstance(a, ak.Array)
    assert not isinstance(a, ak.behaviors.string.StringBehavior)
    assert ak.to_list(a) == [[104, 101, 121], [], [116, 104, 101, 114, 101]]

    assert repr(ak.type(a)) == "3 * var * uint8"
    assert repr(ak.type(a[0])) == "3 * uint8"
    assert repr(ak.type(a[1])) == "0 * uint8"
    assert repr(ak.type(a[2])) == "5 * uint8"

    assert (
        repr(a) ==
        "<Array [[104, 101, 121], ... 101, 114, 101]] type='3 * var * uint8'>")
    assert str(a) == "[[104, 101, 121], [], [116, 104, 101, 114, 101]]"
    assert repr(a[0]) == "<Array [104, 101, 121] type='3 * uint8'>"
    assert repr(a[1]) == "<Array [] type='0 * uint8'>"
    assert repr(a[2]) == "<Array [116, 104, 101, 114, 101] type='5 * uint8'>"

    content = ak.layout.NumpyArray(
        np.array([ord(x) for x in "heythere"], dtype=np.uint8),
        parameters={
            "__array__": "char",
            "encoding": "utf-8"
        },
    )
    listoffsetarray = ak.layout.ListOffsetArray64(
        ak.layout.Index64(np.array([0, 3, 3, 8])),
        content,
        parameters={"__array__": "string"},
    )
    a = ak.Array(listoffsetarray, check_valid=True)

    a = ak.Array(listoffsetarray, check_valid=True)
    assert isinstance(a, ak.Array)
    assert ak.to_list(a) == ["hey", "", "there"]

    assert str(a) == "['hey', '', 'there']"
    assert repr(a[0]) == "'hey'"
    assert repr(a[1]) == "''"
    assert repr(a[2]) == "'there'"
Beispiel #28
0
def test_regular():
    regular = ak.Array(np.array([[i, i] for i in range(10)], dtype=np.int64))
    assert str(regular.type) == "10 * 2 * int64"

    assert ak.to_list(ak.to_regular(regular)) == ak.to_list(regular)
    assert ak.type(ak.to_regular(regular)) == ak.type(regular)

    irregular = ak.from_regular(regular)
    assert ak.to_list(irregular) == ak.to_list(regular)
    assert str(irregular.type) == "10 * var * int64"

    assert ak.to_list(ak.from_regular(irregular)) == ak.to_list(irregular)
    assert ak.type(ak.from_regular(irregular)) == ak.type(irregular)

    reregular = ak.to_regular(irregular)
    assert ak.to_list(reregular) == ak.to_list(regular)
    assert str(reregular.type) == "10 * 2 * int64"
Beispiel #29
0
 def _topandas(self, seen):
     import awkward.pandas
     if id(self) in seen:
         return seen[id(self)]
     else:
         out = seen[id(self)] = self.copy()
         out.__class__ = awkward.pandas.mixin(type(self))
         out._contents = OrderedDict((n, x._topandas(seen) if isinstance(x, awkward.array.base.AwkwardArray) else x) for n, x in out._contents.items())
         return out
def test_argsort():
    array = ak.Array([[1.1, 2.2], [3.3, 3.1]])
    assert ak.argsort(array).tolist() == [[0, 1], [1, 0]]
    assert str(ak.type(ak.argsort(array))) == "2 * var * int64"

    empty_array = ak.Array([[], []])
    assert ak.argsort(empty_array).tolist() == [[], []]
    assert str(ak.type(ak.argsort(empty_array))) == "2 * var * int64"

    select_array = array[array > 5]
    assert select_array.tolist() == [[], []]
    assert str(ak.type(select_array)) == "2 * var * float64"

    assert ak.argsort(select_array).tolist() == [[], []]
    assert str(ak.type(ak.argsort(select_array))) == "2 * var * int64"

    assert ak.argsort(array[array > 5]).tolist() == [[], []]
    assert str(ak.type(ak.argsort(array[array > 5]))) == "2 * var * int64"