Skip to content

Commit 3f64b59

Browse files
authored
[doc] add tutorial for kaldi pybind IO. (#3986)
1 parent 61cda6b commit 3f64b59

20 files changed

+928
-13
lines changed

src/pybind/.clang-format

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Run manually to reformat a file:
2+
# clang-format -i --style=file <file>
3+
BasedOnStyle: Google
4+
DerivePointerAlignment: false

src/pybind/doc/conf.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,25 @@
1818
# otherwise `import` will throw.
1919
import sphinx_rtd_theme
2020

21-
22-
2321
# -- Project information -----------------------------------------------------
2422

2523
project = 'Kaldi Pybind'
26-
copyright = '2020, Kaldi Pybind Authors'
24+
copyright = '2019-2020, Kaldi Pybind Authors'
2725
author = 'Kaldi Pybind Authors'
2826

29-
3027
# -- General configuration ---------------------------------------------------
3128

3229
# Add any Sphinx extension module names here, as strings. They can be
3330
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
3431
# ones.
3532
extensions = [
36-
'sphinx.ext.autodoc',
37-
'sphinx.ext.autosummary',
38-
'sphinx.ext.mathjax',
39-
'sphinx.ext.napoleon',
40-
'sphinx.ext.todo',
41-
'sphinx.ext.viewcode',
33+
'sphinx.ext.autodoc',
34+
'sphinx.ext.autosummary',
35+
'sphinx.ext.mathjax',
36+
'sphinx.ext.napoleon',
37+
'sphinx.ext.todo',
38+
'sphinx.ext.viewcode',
39+
'sphinx.ext.githubpages',
4240
]
4341

4442
# Add any paths that contain templates here, relative to this directory.
@@ -60,7 +58,6 @@
6058
# The name of the Pygments (syntax highlighting) style to use.
6159
pygments_style = 'sphinx'
6260

63-
6461
# -- Options for HTML output -------------------------------------------------
6562

6663
# The theme to use for HTML and HTML Help pages. See the documentation for

src/pybind/doc/getting_started.rst

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ from
1515

1616
Kaldi Pybind is still under active development and has not
1717
yet been merged into the master branch. You should checkout
18-
the `pybind11` branch before compilation.
18+
the ``pybind11`` branch before compilation.
1919

2020
.. Note::
2121

@@ -40,3 +40,15 @@ The following is a quick start:
4040
make
4141
make test
4242
43+
After a successful compilation, you have to modify the environment
44+
variable ``PYTHONPATH``:
45+
46+
.. code-block:: bash
47+
48+
export KALDI_ROOT=/path/to/your/kaldi
49+
export PYTHONPATH=$KALDI_ROOT/src/pybind:$PYTHONPATH
50+
51+
.. HINT::
52+
53+
There is no ``make install``. Once compiled, you are ready to
54+
use Kaldi Pybind.

src/pybind/doc/index.rst

+7
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@ Welcome to Kaldi Pybind's documentation!
1818

1919
introduction
2020
getting_started
21+
22+
.. toctree::
23+
:maxdepth: 1
24+
:caption: Tutorial
25+
26+
tutorial/working_with_kaldi_matrices
27+
tutorial/working_with_kaldi_io

src/pybind/doc/introduction.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ About Kaldi Pybind
1010

1111
Kaldi Pybind is a Python wrapper for Kaldi
1212
using `Pybind11 <https://github.com/pybind/pybind11>`_.
13+
It is still under active development.
1314

14-
Kaldi Pybind is still under active development.
1515
Everything related to Kaldi Pybind is put in the
1616
`pybind11 branch <https://github.com/kaldi-asr/kaldi/tree/pybind11>`_.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python3
2+
3+
import kaldi
4+
5+
f = kaldi.FloatMatrix(2, 3)
6+
f[1, 2] = 100
7+
print(f)
8+
9+
g = f.numpy()
10+
g[0, 0] = 200
11+
print(f)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python3
2+
3+
import kaldi
4+
import numpy as np
5+
6+
m = np.array([[1, 2, 3], [10, 20, 30]], dtype=np.float32)
7+
f = kaldi.FloatSubMatrix(m)
8+
9+
f[1, 2] = 100
10+
print(m)
11+
print()
12+
13+
g = f.numpy()
14+
g[0, 0] = 200
15+
print(m)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python3
2+
3+
import kaldi
4+
import numpy as np
5+
6+
v = np.array([10, 20, 30], dtype=np.float32)
7+
f = kaldi.FloatSubVector(v)
8+
9+
f[0] = 0
10+
print(v)
11+
12+
g = f.numpy()
13+
g[1] = 100
14+
print(v)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python3
2+
3+
import kaldi
4+
5+
f = kaldi.FloatVector(3)
6+
f[0] = 10
7+
print(f)
8+
9+
g = f.numpy()
10+
g[1] = 20
11+
print(f)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
3+
import kaldi
4+
5+
wspecifier = 'ark,scp:/tmp/ali.ark,/tmp/ali.scp'
6+
7+
writer = kaldi.IntVectorWriter(wspecifier)
8+
writer.Write(key='foo', value=[1, 2, 3])
9+
writer.Write('bar', [10, 20])
10+
writer.Close()
11+
12+
rspecifier = 'scp:/tmp/ali.scp'
13+
reader = kaldi.SequentialIntVectorReader(rspecifier)
14+
15+
for key, value in reader:
16+
print(key, value)
17+
18+
reader.Close()
19+
20+
reader = kaldi.RandomAccessIntVectorReader(rspecifier)
21+
value1 = reader['foo']
22+
print(value1)
23+
24+
value2 = reader['bar']
25+
print(value2)
26+
reader.Close()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
3+
import kaldi
4+
5+
wspecifier = 'ark,scp:/tmp/ali.ark,/tmp/ali.scp'
6+
7+
with kaldi.IntVectorWriter(wspecifier) as writer:
8+
writer.Write(key='foo', value=[1, 2, 3])
9+
writer.Write('bar', [10, 20])
10+
11+
# Note that you do NOT need to close the file.
12+
13+
rspecifier = 'scp:/tmp/ali.scp'
14+
with kaldi.SequentialIntVectorReader(rspecifier) as reader:
15+
for key, value in reader:
16+
print(key, value)
17+
18+
rspecifier = 'scp:/tmp/ali.scp'
19+
with kaldi.RandomAccessIntVectorReader(rspecifier) as reader:
20+
value1 = reader['foo']
21+
print(value1)
22+
23+
value2 = reader['bar']
24+
print(value2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python3
2+
3+
import numpy as np
4+
import kaldi
5+
6+
wspecifier = 'ark,scp:/tmp/feats.ark,/tmp/feats.scp'
7+
8+
writer = kaldi.MatrixWriter(wspecifier)
9+
10+
m = np.arange(6).reshape(2, 3).astype(np.float32)
11+
writer.Write(key='foo', value=m)
12+
13+
g = kaldi.FloatMatrix(2, 2)
14+
g[0, 0] = 10
15+
g[1, 1] = 20
16+
writer.Write('bar', g)
17+
18+
writer.Close()
19+
20+
rspecifier = 'scp:/tmp/feats.scp'
21+
reader = kaldi.SequentialMatrixReader(rspecifier)
22+
for key, value in reader:
23+
assert key in ['foo', 'bar']
24+
if key == 'foo':
25+
np.testing.assert_array_equal(value.numpy(), m)
26+
else:
27+
np.testing.assert_array_equal(value.numpy(), g.numpy())
28+
29+
reader.Close()
30+
31+
reader = kaldi.RandomAccessMatrixReader(rspecifier)
32+
assert 'foo' in reader
33+
assert 'bar' in reader
34+
np.testing.assert_array_equal(reader['foo'].numpy(), m)
35+
np.testing.assert_array_equal(reader['bar'].numpy(), g.numpy())
36+
reader.Close()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
3+
import numpy as np
4+
import kaldi
5+
6+
wspecifier = 'ark,scp:/tmp/feats.ark,/tmp/feats.scp'
7+
8+
with kaldi.MatrixWriter(wspecifier) as writer:
9+
m = np.arange(6).reshape(2, 3).astype(np.float32)
10+
writer.Write(key='foo', value=m)
11+
12+
g = kaldi.FloatMatrix(2, 2)
13+
g[0, 0] = 10
14+
g[1, 1] = 20
15+
writer.Write('bar', g)
16+
17+
rspecifier = 'scp:/tmp/feats.scp'
18+
with kaldi.SequentialMatrixReader(rspecifier) as reader:
19+
for key, value in reader:
20+
assert key in ['foo', 'bar']
21+
if key == 'foo':
22+
np.testing.assert_array_equal(value.numpy(), m)
23+
else:
24+
np.testing.assert_array_equal(value.numpy(), g.numpy())
25+
26+
with kaldi.RandomAccessMatrixReader(rspecifier) as reader:
27+
assert 'foo' in reader
28+
assert 'bar' in reader
29+
np.testing.assert_array_equal(reader['foo'].numpy(), m)
30+
np.testing.assert_array_equal(reader['bar'].numpy(), g.numpy())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python3
2+
3+
import kaldi
4+
5+
m = kaldi.FloatMatrix(2, 2)
6+
m[0, 0] = 10
7+
m[1, 1] = 20
8+
9+
xfilename = '/tmp/lda.mat'
10+
kaldi.write_mat(m, xfilename, binary=True)
11+
12+
g = kaldi.read_mat(xfilename)
13+
print(g)

0 commit comments

Comments
 (0)