-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_int8_vector_writer_reader.py
72 lines (52 loc) · 1.78 KB
/
test_int8_vector_writer_reader.py
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
#!/usr/bin/env python3
# Copyright 2022 Xiaomi Corporation (authors: Fangjun Kuang)
import os
import kaldi_native_io
base = "int8_vector"
wspecifier = f"ark,scp,t:{base}.ark,{base}.scp"
rspecifier = f"scp:{base}.scp"
def test_int8_vector_writer():
with kaldi_native_io.Int8VectorWriter(wspecifier) as ko:
ko.write("a", [10, 20])
ko["b"] = [100, 120, -2]
def test_sequential_int8_vector_reader():
with kaldi_native_io.SequentialInt8VectorReader(rspecifier) as ki:
for key, value in ki:
if key == "a":
assert value == [10, 20]
elif key == "b":
assert value == [100, 120, -2]
else:
raise ValueError(f"Unknown key {key} with value {value}")
def test_random_access_int8_vector_reader():
with kaldi_native_io.RandomAccessInt8VectorReader(rspecifier) as ki:
assert "b" in ki
assert "a" in ki
assert ki["a"] == [10, 20]
assert ki["b"] == [100, 120, -2]
def test_read_single_item():
a = [10, 20]
b = [100, 120, -2]
# You can also generate a text format by adding ",t" if you like
# with kaldi_native_io.Int8VectorWriter("ark,scp,t:v.ark,v.scp") as ko:
with kaldi_native_io.Int8VectorWriter("ark,scp:v.ark,v.scp") as ko:
ko.write("a", a)
ko["b"] = b
"""
v.scp contains:
a v.ark:2
b v.ark:15
"""
va = kaldi_native_io.read_int8_vector("v.ark:2")
assert va == a
vb = kaldi_native_io.read_int8_vector("v.ark:15")
assert vb == b
def main():
test_int8_vector_writer()
test_sequential_int8_vector_reader()
test_random_access_int8_vector_reader()
test_read_single_item()
os.remove(f"{base}.scp")
os.remove(f"{base}.ark")
if __name__ == "__main__":
main()