File: array1.py

package info (click to toggle)
pytables 3.10.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,228 kB
  • sloc: ansic: 82,212; python: 65,296; cpp: 753; sh: 394; makefile: 100
file content (51 lines) | stat: -rw-r--r-- 1,343 bytes parent folder | download | duplicates (2)
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
import numpy as np

import tables as tb

# Open a new empty HDF5 file
fileh = tb.open_file("array1.h5", mode="w")
# Get the root group
root = fileh.root

# Create an Array
a = np.array([-1, 2, 4], np.int16)
# Save it on the HDF5 file
hdfarray = fileh.create_array(root, "array_1", a, "Signed short array")

# Create a scalar Array
a = np.array(4, np.int16)
# Save it on the HDF5 file
hdfarray = fileh.create_array(root, "array_s", a, "Scalar signed short array")

# Create a 3-d array of floats
a = np.arange(120, dtype=np.float64).reshape(20, 3, 2)
# Save it on the HDF5 file
hdfarray = fileh.create_array(root, "array_f", a, "3-D float array")

# Close the file
fileh.close()

# Open the file for reading
fileh = tb.open_file("array1.h5", mode="r")
# Get the root group
root = fileh.root

a = root.array_1.read()
print("Signed byte array -->", repr(a), a.shape)

print("Testing iterator (works even over scalar arrays):", end=" ")
arr = root.array_s
for x in arr:
    print("nrow-->", arr.nrow)
    print("Element-->", repr(x))

# print "Testing getitem:"
# for i in range(root.array_1.nrows):
#     print "array_1["+str(i)+"]", "-->", root.array_1[i]

print("array_f[:,2:3,2::2]", repr(root.array_f[:, 2:3, 2::2]))
print("array_f[1,2:]", repr(root.array_f[1, 2:]))
print("array_f[1]", repr(root.array_f[1]))

# Close the file
fileh.close()