File size: 4,400 Bytes
f517158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""Test_H5py.py — device test for h5py 3.16.0 on Android Python STB.



Runs from the PipManager Scripts folder. Requires the h5py Android wheel

(x86_64 or arm64_v8a) and numpy installed first.



Covers: import, versions, in-memory file (core driver), temp-file roundtrip,

groups, datasets (int/float), attributes, compound dtype, variable-length

strings, gzip compression, dataset resizing/chunking.



Exit-code contract: exit 0 = ALL PASS, exit 1 = any FAIL.

Generated by RIMI

"""
import os
import sys
import tempfile
import traceback

PASS = 0
FAIL = 0


def check(name, cond):
    global PASS, FAIL
    if cond:
        print("[PASS] " + name)
        PASS += 1
    else:
        print("[FAIL] " + name)
        FAIL += 1


def main():
    global PASS, FAIL
    try:
        import numpy as np
    except ImportError:
        print("[FAIL] numpy import -- h5py needs numpy>=1.21.2 installed first")
        return 1
    try:
        import h5py
    except ImportError:
        print("[FAIL] import h5py")
        traceback.print_exc()
        return 1
    check("import h5py", True)
    check("h5py version 3.16.0", h5py.version.version == "3.16.0")
    hv = h5py.version.hdf5_version_tuple
    check("HDF5 runtime 1.14.6", tuple(hv) == (1, 14, 6))
    check("built-against matches runtime",
          h5py.version.hdf5_built_version_tuple == h5py.version.hdf5_version_tuple)
    print("h5py %s / HDF5 %s / numpy %s" % (
        h5py.version.version, h5py.version.hdf5_version, np.__version__))

    # 1. in-memory file (no storage permission needed)
    try:
        with h5py.File("mem.h5", "w", driver="core", backing_store=False) as f:
            f.create_dataset("d", data=np.arange(10))
            check("core-driver roundtrip", int(f["d"][...].sum()) == 45)
    except Exception:
        print("[FAIL] core-driver in-memory file")
        traceback.print_exc()
        FAIL += 1

    # 2. temp-file roundtrip
    tmp = tempfile.mkdtemp()
    fp = os.path.join(tmp, "test_h5py.h5")
    try:
        with h5py.File(fp, "w") as f:
            check("create file", os.path.isfile(fp))
            g = f.create_group("grp")
            check("create group", "grp" in f)
            g.attrs["gattr"] = "hello"
            d = f.create_dataset("grp/dset", data=np.arange(100, dtype=np.int32))
            check("create int dataset", d.shape == (100,))
            check("dataset sum", int(d[...].sum()) == 4950)
            f.attrs["fattr"] = 42
            check("file attr write", True)
            df = f.create_dataset("fdata", data=np.linspace(0, 1, 25))
            check("float dataset", abs(float(df[...].sum()) - 12.5) < 1e-9)
            dt = np.dtype([("a", np.int16), ("b", np.float64)])
            c = f.create_dataset("comp", (4,), dtype=dt)
            c["a"] = np.arange(4)
            check("compound dtype", True)
            s = f.create_dataset("vlen", (3,), dtype=h5py.string_dtype())
            s[...] = [b"aa", b"bbb", b"c"]
            check("vlen strings write", True)
            cz = f.create_dataset("gz", data=np.arange(50.0), compression="gzip")
            check("gzip write", cz.compression == "gzip")
            cc = f.create_dataset("chunked", (0,), maxshape=(None,),
                                  dtype=np.int64, chunks=(8,))
            cc.resize((16,))
            cc[...] = np.arange(16)
            check("chunked resize", int(cc[...].sum()) == 120)
    except Exception:
        print("[FAIL] temp-file write phase")
        traceback.print_exc()
        FAIL += 1

    try:
        with h5py.File(fp, "r") as f:
            check("reopen group", "grp" in f)
            check("reopen file attr", f.attrs["fattr"] == 42)
            check("reopen data", int(f["grp/dset"][...].sum()) == 4950)
            check("reopen group attr", f["grp"].attrs["gattr"] == "hello")
            check("reopen vlen", f["vlen"][1] == b"bbb")
            check("reopen gzip", bool((f["gz"][...] == np.arange(50.0)).all()))
            check("reopen float", abs(float(f["fdata"][...].sum()) - 12.5) < 1e-9)
    except Exception:
        print("[FAIL] temp-file read phase")
        traceback.print_exc()
        FAIL += 1

    print("PASS=%d FAIL=%d" % (PASS, FAIL))
    return 0 if FAIL == 0 else 1


if __name__ == "__main__":
    sys.exit(main())