File: vlarray2.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 (109 lines) | stat: -rw-r--r-- 3,093 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
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
#!/usr/bin/env python3

"""Small example that shows how to work with variable length arrays of
different types, UNICODE strings and general Python objects included."""

import pickle

import numpy as np

import tables as tb

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

# A test with VL length arrays:
vlarray = fileh.create_vlarray(
    root, "vlarray1", tb.Int32Atom(), "ragged array of ints"
)
vlarray.append(np.array([5, 6]))
vlarray.append(np.array([5, 6, 7]))
vlarray.append([5, 6, 9, 8])

# Test with lists of bidimensional vectors
vlarray = fileh.create_vlarray(
    root, "vlarray2", tb.Int64Atom(shape=(2,)), "Ragged array of vectors"
)
a = np.array([[1, 2], [1, 2]], dtype=np.int64)
vlarray.append(a)
vlarray.append(np.array([[1, 2], [3, 4]], dtype=np.int64))
vlarray.append(np.zeros(dtype=np.int64, shape=(0, 2)))
vlarray.append(np.array([[5, 6]], dtype=np.int64))
# This makes an error (shape)
# vlarray.append(array([[5], [6]], dtype=int64))
# This makes an error (type)
# vlarray.append(array([[5, 6]], dtype=uint64))

# Test with strings
vlarray = fileh.create_vlarray(
    root, "vlarray3", tb.StringAtom(itemsize=3), "Ragged array of strings"
)
vlarray.append(["123", "456", "3"])
vlarray.append(["456", "3"])
# This makes an error because of different string sizes than declared
# vlarray.append(["1234", "456", "3"])

# Python flavor
vlarray = fileh.create_vlarray(
    root, "vlarray3b", tb.StringAtom(itemsize=3), "Ragged array of strings"
)
vlarray.flavor = "python"
vlarray.append(["123", "456", "3"])
vlarray.append(["456", "3"])

# Binary strings
vlarray = fileh.create_vlarray(
    root, "vlarray4", tb.UInt8Atom(), "pickled bytes"
)
data = pickle.dumps((["123", "456"], "3"))
vlarray.append(np.ndarray(buffer=data, dtype=np.uint8, shape=len(data)))

# The next is a way of doing the same than before
vlarray = fileh.create_vlarray(
    root, "vlarray5", tb.ObjectAtom(), "pickled object"
)
vlarray.append([["123", "456"], "3"])

# Boolean arrays are supported as well
vlarray = fileh.create_vlarray(
    root, "vlarray6", tb.BoolAtom(), "Boolean atoms"
)
# The next lines are equivalent...
vlarray.append([1, 0])
vlarray.append([1, 0, 3, 0])  # This will be converted to a boolean
# This gives a TypeError
# vlarray.append([1,0,1])

# Variable length strings
vlarray = fileh.create_vlarray(
    root, "vlarray7", tb.VLStringAtom(), "Variable Length String"
)
vlarray.append("asd")
vlarray.append("aaana")

# Unicode variable length strings
vlarray = fileh.create_vlarray(
    root, "vlarray8", tb.VLUnicodeAtom(), "Variable Length Unicode String"
)
vlarray.append("aaana")
vlarray.append("")  # The empty string
vlarray.append("asd")
vlarray.append("para\u0140lel")

# Close the file
fileh.close()

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

for obj in fileh.list_nodes(root, "Leaf"):
    arr = obj.read()
    print(obj.name, "-->", arr)
    print("number of objects in this row:", len(arr))

# Close the file
fileh.close()