File: test_pickles.py

package info (click to toggle)
python-boost-histogram 1.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,236 kB
  • sloc: python: 7,940; cpp: 3,243; makefile: 22; sh: 1
file content (65 lines) | stat: -rw-r--r-- 1,683 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from __future__ import annotations

import pickle
from pathlib import Path

import pytest

import boost_histogram as bh

DIR = Path(__file__).parent


@pytest.mark.parametrize("version", ["0.10.2", "0.6.2", "0.11.1", "1.1.0"])
def test_read_pickle(version):
    filename = DIR / "pickles" / f"bh_{version}.pkl"
    with filename.open("rb") as f:
        d = pickle.load(f)

    assert d["version"] == version

    h1 = d["h1"]
    h2 = d["h2"]
    h3 = d["h3"]

    assert h1.storage_type == (
        bh.storage.Double if version[0] == "0" else bh.storage.Int64
    )
    assert h2.storage_type == bh.storage.Weight
    assert h3.storage_type == bh.storage.Double

    assert h1[bh.loc(-5)] == 1
    assert h1[bh.loc(1)] == 2
    assert h1[bh.loc(2)] == 1

    assert h2[0].value == 0
    assert h2[1].value == 1
    assert h2[2].value == 2
    assert h2[3].value == 3
    assert h2[4].value == 0

    assert h3[bh.loc("one"), bh.loc(3)] == 1
    assert h3[bh.loc("two"), bh.loc(2)] == 1
    assert h3[bh.loc("two"), bh.loc(1)] == 1
    assert h3[bh.loc("two"), bh.loc(3)] == 0
    assert h3[bh.loc("two"), sum] == 2

    assert isinstance(h1.axes[0], bh.axis.Regular)
    assert isinstance(h2.axes[0], bh.axis.Integer)
    assert isinstance(h3.axes[0], bh.axis.StrCategory)
    assert isinstance(h3.axes[1], bh.axis.Variable)

    assert h3.axes[0].traits.growth
    assert not h3.axes[1].traits.growth

    assert h1.axes[0].metadata is None
    assert h2.axes[0].metadata == {"hello": "world"}

    ver = tuple(map(int, version.split(".")))

    assert h1.metadata is None

    if ver < (0, 9, 0):
        assert h2.metadata is None
    else:
        assert h2.metadata == "foo"