PythonSTB commited on
Commit
f517158
·
verified ·
1 Parent(s): d6c7333

Upload h5py/Test_H5py.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. h5py/Test_H5py.py +118 -0
h5py/Test_H5py.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Test_H5py.py — device test for h5py 3.16.0 on Android Python STB.
2
+
3
+ Runs from the PipManager Scripts folder. Requires the h5py Android wheel
4
+ (x86_64 or arm64_v8a) and numpy installed first.
5
+
6
+ Covers: import, versions, in-memory file (core driver), temp-file roundtrip,
7
+ groups, datasets (int/float), attributes, compound dtype, variable-length
8
+ strings, gzip compression, dataset resizing/chunking.
9
+
10
+ Exit-code contract: exit 0 = ALL PASS, exit 1 = any FAIL.
11
+ Generated by RIMI
12
+ """
13
+ import os
14
+ import sys
15
+ import tempfile
16
+ import traceback
17
+
18
+ PASS = 0
19
+ FAIL = 0
20
+
21
+
22
+ def check(name, cond):
23
+ global PASS, FAIL
24
+ if cond:
25
+ print("[PASS] " + name)
26
+ PASS += 1
27
+ else:
28
+ print("[FAIL] " + name)
29
+ FAIL += 1
30
+
31
+
32
+ def main():
33
+ global PASS, FAIL
34
+ try:
35
+ import numpy as np
36
+ except ImportError:
37
+ print("[FAIL] numpy import -- h5py needs numpy>=1.21.2 installed first")
38
+ return 1
39
+ try:
40
+ import h5py
41
+ except ImportError:
42
+ print("[FAIL] import h5py")
43
+ traceback.print_exc()
44
+ return 1
45
+ check("import h5py", True)
46
+ check("h5py version 3.16.0", h5py.version.version == "3.16.0")
47
+ hv = h5py.version.hdf5_version_tuple
48
+ check("HDF5 runtime 1.14.6", tuple(hv) == (1, 14, 6))
49
+ check("built-against matches runtime",
50
+ h5py.version.hdf5_built_version_tuple == h5py.version.hdf5_version_tuple)
51
+ print("h5py %s / HDF5 %s / numpy %s" % (
52
+ h5py.version.version, h5py.version.hdf5_version, np.__version__))
53
+
54
+ # 1. in-memory file (no storage permission needed)
55
+ try:
56
+ with h5py.File("mem.h5", "w", driver="core", backing_store=False) as f:
57
+ f.create_dataset("d", data=np.arange(10))
58
+ check("core-driver roundtrip", int(f["d"][...].sum()) == 45)
59
+ except Exception:
60
+ print("[FAIL] core-driver in-memory file")
61
+ traceback.print_exc()
62
+ FAIL += 1
63
+
64
+ # 2. temp-file roundtrip
65
+ tmp = tempfile.mkdtemp()
66
+ fp = os.path.join(tmp, "test_h5py.h5")
67
+ try:
68
+ with h5py.File(fp, "w") as f:
69
+ check("create file", os.path.isfile(fp))
70
+ g = f.create_group("grp")
71
+ check("create group", "grp" in f)
72
+ g.attrs["gattr"] = "hello"
73
+ d = f.create_dataset("grp/dset", data=np.arange(100, dtype=np.int32))
74
+ check("create int dataset", d.shape == (100,))
75
+ check("dataset sum", int(d[...].sum()) == 4950)
76
+ f.attrs["fattr"] = 42
77
+ check("file attr write", True)
78
+ df = f.create_dataset("fdata", data=np.linspace(0, 1, 25))
79
+ check("float dataset", abs(float(df[...].sum()) - 12.5) < 1e-9)
80
+ dt = np.dtype([("a", np.int16), ("b", np.float64)])
81
+ c = f.create_dataset("comp", (4,), dtype=dt)
82
+ c["a"] = np.arange(4)
83
+ check("compound dtype", True)
84
+ s = f.create_dataset("vlen", (3,), dtype=h5py.string_dtype())
85
+ s[...] = [b"aa", b"bbb", b"c"]
86
+ check("vlen strings write", True)
87
+ cz = f.create_dataset("gz", data=np.arange(50.0), compression="gzip")
88
+ check("gzip write", cz.compression == "gzip")
89
+ cc = f.create_dataset("chunked", (0,), maxshape=(None,),
90
+ dtype=np.int64, chunks=(8,))
91
+ cc.resize((16,))
92
+ cc[...] = np.arange(16)
93
+ check("chunked resize", int(cc[...].sum()) == 120)
94
+ except Exception:
95
+ print("[FAIL] temp-file write phase")
96
+ traceback.print_exc()
97
+ FAIL += 1
98
+
99
+ try:
100
+ with h5py.File(fp, "r") as f:
101
+ check("reopen group", "grp" in f)
102
+ check("reopen file attr", f.attrs["fattr"] == 42)
103
+ check("reopen data", int(f["grp/dset"][...].sum()) == 4950)
104
+ check("reopen group attr", f["grp"].attrs["gattr"] == "hello")
105
+ check("reopen vlen", f["vlen"][1] == b"bbb")
106
+ check("reopen gzip", bool((f["gz"][...] == np.arange(50.0)).all()))
107
+ check("reopen float", abs(float(f["fdata"][...].sum()) - 12.5) < 1e-9)
108
+ except Exception:
109
+ print("[FAIL] temp-file read phase")
110
+ traceback.print_exc()
111
+ FAIL += 1
112
+
113
+ print("PASS=%d FAIL=%d" % (PASS, FAIL))
114
+ return 0 if FAIL == 0 else 1
115
+
116
+
117
+ if __name__ == "__main__":
118
+ sys.exit(main())