def test_unknown():
    a = awkward1.from_json("[[], [], []]", highlevel=False)
    assert awkward1.to_list(a) == [[], [], []]
    assert str(awkward1.type(a)) == "var * unknown"
    assert awkward1.type(a) == awkward1.types.ListType(
        awkward1.types.UnknownType())
    assert not awkward1.type(a) == awkward1.types.PrimitiveType("float64")

    a = awkward1.from_json("[[], [[], []], [[], [], []]]", highlevel=False)
    assert awkward1.to_list(a) == [[], [[], []], [[], [], []]]
    assert str(awkward1.type(a)) == "var * var * unknown"
    assert awkward1.type(a) == awkward1.types.ListType(
        awkward1.types.ListType(awkward1.types.UnknownType()))

    a = awkward1.layout.ArrayBuilder()
    a.beginlist()
    a.endlist()
    a.beginlist()
    a.endlist()
    a.beginlist()
    a.endlist()
    assert awkward1.to_list(a) == [[], [], []]
    assert str(awkward1.type(a)) == "var * unknown"
    assert awkward1.type(a) == awkward1.types.ListType(
        awkward1.types.UnknownType())
    assert not awkward1.type(a) == awkward1.types.PrimitiveType("float64")

    a = a.snapshot()
    assert awkward1.to_list(a) == [[], [], []]
    assert str(awkward1.type(a)) == "var * unknown"
    assert awkward1.type(a) == awkward1.types.ListType(
        awkward1.types.UnknownType())
    assert not awkward1.type(a) == awkward1.types.PrimitiveType("float64")
Пример #2
0
def test_getitem():
    a = awkward1.from_json("[]")
    a = awkward1.from_json("[[], [[], []], [[], [], []]]")
    assert awkward1.to_list(a[2]) == [[], [], []]

    assert awkward1.to_list(a[2, 1]) == []
    with pytest.raises(ValueError) as excinfo:
        a[2, 1, 0]
    assert str(excinfo.value).endswith(" attempting to get 0, index out of range")
    assert awkward1.to_list(a[2, 1][()]) == []
    with pytest.raises(ValueError) as excinfo:
        a[2, 1][0]
    assert str(excinfo.value).endswith(" attempting to get 0, index out of range")
    assert awkward1.to_list(a[2, 1][100:200]) == []
    assert awkward1.to_list(a[2, 1, 100:200]) == []
    assert awkward1.to_list(a[2, 1][numpy.array([], dtype=int)]) == []
    assert awkward1.to_list(a[2, 1, numpy.array([], dtype=int)]) == []
    with pytest.raises(ValueError) as excinfo:
        a[2, 1, numpy.array([0], dtype=int)]
    assert str(excinfo.value).endswith(" attempting to get 0, index out of range")
    with pytest.raises(ValueError) as excinfo:
        a[2, 1][100:200, 0]
    assert str(excinfo.value).endswith(", too many dimensions in slice")
    with pytest.raises(ValueError) as excinfo:
        a[2, 1][100:200, 200:300]
    assert str(excinfo.value).endswith(", too many dimensions in slice")
    with pytest.raises(ValueError) as excinfo:
        a[2, 1][100:200, numpy.array([], dtype=int)]
    assert str(excinfo.value).endswith(", too many dimensions in slice")

    assert awkward1.to_list(a[1:, 1:]) == [[[]], [[], []]]
    with pytest.raises(ValueError) as excinfo:
        a[1:, 1:, 0]
    assert str(excinfo.value).endswith(" attempting to get 0, index out of range")
def test_fromiter():
    builder = awkward1.layout.ArrayBuilder()

    builder.integer(0)
    builder.integer(1)
    builder.integer(2)
    builder.beginlist()
    builder.endlist()
    builder.beginlist()
    builder.real(1.1)
    builder.endlist()
    builder.beginlist()
    builder.real(1.1)
    builder.real(2.2)
    builder.endlist()
    builder.beginlist()
    builder.real(1.1)
    builder.real(2.2)
    builder.real(3.3)
    builder.endlist()

    assert awkward1.to_list(builder.snapshot()) == [
        0, 1, 2, [], [1.1], [1.1, 2.2], [1.1, 2.2, 3.3]
    ]

    assert awkward1.to_list(
        awkward1.from_iter(
            [0, 1, 2, [], [1.1], [1.1, 2.2],
             [1.1, 2.2,
              3.3]])) == [0, 1, 2, [], [1.1], [1.1, 2.2], [1.1, 2.2, 3.3]]
    assert awkward1.to_list(
        awkward1.from_iter([
            0, 1, 2, [], "zero", [1.1], "one", [1.1, 2.2], "two",
            [1.1, 2.2, 3.3], "three"
        ])) == [
            0, 1, 2, [], "zero", [1.1], "one", [1.1, 2.2], "two",
            [1.1, 2.2, 3.3], "three"
        ]
    assert awkward1.to_list(
        awkward1.from_json(
            '[0, 1, 2, [], "zero", [1.1], "one", [1.1, 2.2], "two", [1.1, 2.2, 3.3], "three"]'
        )) == [
            0, 1, 2, [], "zero", [1.1], "one", [1.1, 2.2], "two",
            [1.1, 2.2, 3.3], "three"
        ]
    assert awkward1.to_json(
        awkward1.from_json(
            '[0,1,2,[],"zero",[1.1],"one",[1.1,2.2],"two",[1.1,2.2,3.3],"three"]'
        )
    ) == '[0,1,2,[],"zero",[1.1],"one",[1.1,2.2],"two",[1.1,2.2,3.3],"three"]'
Пример #4
0
def test_fromfile(tmp_path):
    with open(os.path.join(str(tmp_path), "tmp1.json"), "w") as f:
        f.write("[[1.1, 2.2, 3], [], [4, 5.5]]")

    a = awkward1.from_json(os.path.join(str(tmp_path), "tmp1.json"))
    assert awkward1.to_list(a) == [[1.1, 2.2, 3.0], [], [4.0, 5.5]]

    with pytest.raises(ValueError):
        awkward1.from_json("nonexistent.json")

    with open(os.path.join(str(tmp_path), "tmp2.json"), "w") as f:
        f.write("[[1.1, 2.2, 3], []], [4, 5.5]]")

    with pytest.raises(ValueError):
        awkward1.from_json(os.path.join(str(tmp_path), "tmp2.json"))
def test_fromiter_fromjson():
    assert awkward1.to_list(awkward1.from_iter(["one", "two", "three"
                                                ])) == ["one", "two", "three"]
    assert awkward1.to_list(
        awkward1.from_iter([["one", "two", "three"], [],
                            ["four", "five"]])) == [["one", "two", "three"],
                                                    [], ["four", "five"]]

    assert awkward1.to_list(awkward1.from_json('["one", "two", "three"]')) == [
        "one", "two", "three"
    ]
    assert awkward1.to_list(
        awkward1.from_json(
            '[["one", "two", "three"], [], ["four", "five"]]')) == [[
                "one", "two", "three"
            ], [], ["four", "five"]]
Пример #6
0
def test_tostring():
    content = awkward1.layout.NumpyArray(
        numpy.arange(2 * 3 * 5 * 7).reshape(-1, 7))
    offsetsA = numpy.arange(0, 2 * 3 * 5 + 5, 5)
    offsetsB = numpy.arange(0, 2 * 3 + 3, 3)
    startsA, stopsA = offsetsA[:-1], offsetsA[1:]
    startsB, stopsB = offsetsB[:-1], offsetsB[1:]

    listoffsetarrayA32 = awkward1.layout.ListOffsetArray32(
        awkward1.layout.Index32(offsetsA), content)
    listarrayA32 = awkward1.layout.ListArray32(
        awkward1.layout.Index32(startsA), awkward1.layout.Index32(stopsA),
        content)
    modelA = numpy.arange(2 * 3 * 5 * 7).reshape(2 * 3, 5, 7)

    listoffsetarrayB32 = awkward1.layout.ListOffsetArray32(
        awkward1.layout.Index32(offsetsB), listoffsetarrayA32)
    listarrayB32 = awkward1.layout.ListArray32(
        awkward1.layout.Index32(startsB), awkward1.layout.Index32(stopsB),
        listarrayA32)
    modelB = numpy.arange(2 * 3 * 5 * 7).reshape(2, 3, 5, 7)

    assert content.tojson() == json.dumps(awkward1.to_list(content),
                                          separators=(",", ":"))
    assert listoffsetarrayA32.tojson() == json.dumps(modelA.tolist(),
                                                     separators=(",", ":"))
    assert listoffsetarrayB32.tojson() == json.dumps(modelB.tolist(),
                                                     separators=(",", ":"))
    awkward1.to_json(awkward1.from_json(
        "[[1.1,2.2,3],[],[4,5.5]]")) == "[[1.1,2.2,3],[],[4,5.5]]"
Пример #7
0
def test_json():
    dataset = [
        '[{"one":1,"two":1.1},{"one":2,"two":2.2},{"one":3,"two":3.3}]',
        '[{"one":1,"two":[1.1,2.2,3.3]},{"one":2,"two":[]},{"one":3,"two":[4.4,5.5]}]',
        '[[{"one":1,"two":1.1},{"one":2,"two":2.2},{"one":3,"two":3.3}],[],[{"one":4,"two":4.4},{"one":5,"two":5.5}]]',
        '[{"one":{"x":1,"y":1},"two":1.1},{"one":{"x":2,"y":2},"two":2.2},{"one":{"x":3,"y":3},"two":3.3}]',
    ]
    for datum in dataset:
        assert awkward1.to_json(awkward1.from_json(datum)) == datum
def test_empty_array_slice():
    # inspired by PR021::test_getitem
    a = awkward1.from_json("[[], [[], []], [[], [], []]]")
    assert awkward1.to_list(a[2, 1, numpy.array([], dtype=int)]) == []
    assert awkward1.to_list(a[2, numpy.array([1], dtype=int), numpy.array([], dtype=int)]) == []

    # inspired by PR015::test_deep_numpy
    content = awkward1.layout.NumpyArray(numpy.array([[0.0, 1.1], [2.2, 3.3], [4.4, 5.5], [6.6, 7.7], [8.8, 9.9]]))
    listarray = awkward1.layout.ListArray64(awkward1.layout.Index64(numpy.array([0, 3, 3])), awkward1.layout.Index64(numpy.array([3, 3, 5])), content)
    assert awkward1.to_list(listarray[[2, 0, 0, -1], [1, -1, 0, 0], [0, 1, 0, 1]]) == [8.8, 5.5, 0.0, 7.7]
    assert awkward1.to_list(listarray[2, 1, numpy.array([], dtype=int)]) == []
    assert awkward1.to_list(listarray[2, 1, []]) == []
    assert awkward1.to_list(listarray[2, [1], []]) == []
    assert awkward1.to_list(listarray[2, [], []]) == []
Пример #9
0
 def __init__(self, ak_arr=None):
     if isinstance(ak_arr, type(self)):
         self.data = ak_arr.data  # no copy
     elif isinstance(ak_arr, ak.Array):
         self.data = ak_arr
     elif isinstance(ak_arr, str):
         self.data = ak.from_json(ak_arr)
     elif isinstance(ak_arr, Iterable):
         self.data = ak.from_iter(None if a is pd.NA else a for a in ak_arr)
     elif ak_arr is None:
         # empty series
         self.data = ak.Array(data=[])
     else:
         raise ValueError
for path in obs:
    string = "Orig: "
    f = "Empty Path, should not happen"
    for arc_index in path:
        string += f"-{arc_index + 1}- => "
    string += ": Dest"

    print(string)

obs_fname = "my_networks_obs2.json"
write_obs_to_json(obs_fname, obs, allow_rewrite=True)

# np.core.arrayprint._line_width = 500
# obs_fname = 'my_networks_obs.json'
obs_lil = load_obs_from_json(obs_fname)
obs_ak = ak.from_json(obs_fname)
print("len ", len(obs_ak))

# silly levels of inefficiency but will fix later

# obs = np.array(obs_lil)
# obs = scipy.sparse.dok_matrix(obs_lil)

#
# DATA
distances = np.array(
    [[4, 3.5, 4.5, 3, 3, 0, 0, 0],
     [3.5, 3, 4, 0, 2.5, 3, 3, 0],
     [4.5, 4, 5, 0, 0, 0, 4, 3.5],
     [3, 0, 0, 2, 2, 2.5, 0, 2],
     [3, 2.5, 0, 2, 2, 2.5, 2.5, 0],
Пример #11
0
def test_lazy_arrayset():
    array = ak.from_json("""
    [
        {
            "listcollection": [
                {"item1": 1, "item2": 2},
                {"item1": 2, "item2": 4},
                {"item1": 3, "item2": 6}
            ],
            "collection": {"item1": 3, "item2": 4},
            "singleton": 5,
            "listsingleton": [1, 2, 3],
            "unioncollection": {"item1": 3},
            "masked": null
        },
        {
            "listcollection": [
                {"item1": 1, "item2": 2},
                {"item1": 2, "item2": 4},
                {"item1": 3, "item2": 6}
            ],
            "collection": {"item1": 3, "item2": 4},
            "singleton": 5,
            "listsingleton": [1, 2, 3],
            "unioncollection": [{"item1": 2}],
            "masked": 4
        },
        {
            "listcollection": [
                {"item1": 1, "item2": 2},
                {"item1": 2, "item2": 4},
                {"item1": 3, "item2": 6}
            ],
            "collection": {"item1": 3, "item2": 4},
            "singleton": 5,
            "listsingleton": [1, 2, 3],
            "unioncollection": {"item1": 4},
            "masked": 4
        }
    ]""")

    canary = Canary()
    prefix = "kitty"
    form, container, npart = ak.to_arrayset(array,
                                            container=canary,
                                            prefix=prefix)
    assert not any(op[0] == "get" for op in canary.ops)
    canary.ops = []

    cache = {}
    out = ak.from_arrayset(form,
                           container,
                           lazy=True,
                           lazy_cache=cache,
                           lazy_lengths=3,
                           prefix=prefix,
                           lazy_cache_key="hello")
    assert len(canary.ops) == 0
    assert len(cache) == 0

    assert len(out) == 3
    assert len(canary.ops) == 0
    assert len(cache) == 0

    assert ak.to_list(ak.num(out.listcollection)) == [3, 3, 3]
    assert set(canary.ops) == {('get', 'kitty-node1-offsets')}
    assert set(cache) == {'hello', 'hello-kitty-node1-virtual'}
    canary.ops = []
    cache.clear()

    assert ak.to_list(out.unioncollection) == [{
        'item1': 3
    }, [{
        'item1': 2
    }], {
        'item1': 4
    }]
    assert set(canary.ops) == {('get', 'kitty-node11-tags'),
                               ('get', 'kitty-node11-index'),
                               ('get', 'kitty-node14-offsets'),
                               ('get', 'kitty-node13'),
                               ('get', 'kitty-node16')}
    assert set(cache) == {
        'hello', 'hello-kitty-node11-virtual', 'hello-kitty-node13-virtual',
        'hello-kitty-node16-virtual'
    }
    canary.ops = []
    cache.clear()

    assert ak.to_list(out.masked) == [None, 4, 4]
    assert set(canary.ops) == {('get', 'kitty-node17-index'),
                               ('get', 'kitty-node18')}
    assert set(cache) == {'hello', 'hello-kitty-node17-virtual'}
    canary.ops = []
    cache.clear()
Пример #12
0
def test_tofile(tmp_path):
    awkward1.to_json(awkward1.from_json("[[1.1,2.2,3],[],[4,5.5]]"),
                     os.path.join(str(tmp_path), "tmp1.json"))

    with open(os.path.join(str(tmp_path), "tmp1.json"), "r") as f:
        f.read() == "[[1.1,2.2,3],[],[4,5.5]]"
Пример #13
0
def test_fromstring():
    a = awkward1.from_json("[[1.1, 2.2, 3], [], [4, 5.5]]")
    assert awkward1.to_list(a) == [[1.1, 2.2, 3.0], [], [4.0, 5.5]]

    with pytest.raises(ValueError):
        awkward1.from_json("[[1.1, 2.2, 3], [blah], [4, 5.5]]")