test_kaldi_io.py 1.57 KB
Newer Older
1
2
3
4
import os
import torch
import torchaudio.kaldi_io as kio
import unittest
5
import test.common_utils
6
7


8
class Test_KaldiIO(unittest.TestCase):
9
10
    data1 = [[1, 2, 3], [11, 12, 13], [21, 22, 23]]
    data2 = [[31, 32, 33], [41, 42, 43], [51, 52, 53]]
11
    test_dirpath, test_dir = test.common_utils.create_temp_assets_dir()
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

    def _test_helper(self, file_name, expected_data, fn, expected_dtype):
        """ Takes a file_name to the input data and a function fn to extract the
        data. It compares the extracted data to the expected_data. The expected_dtype
        will be used to check that the extracted data is of the right type.
        """
        test_filepath = os.path.join(self.test_dirpath, "assets", file_name)
        expected_output = {'key' + str(idx + 1): torch.tensor(val, dtype=expected_dtype)
                           for idx, val in enumerate(expected_data)}

        for key, vec in fn(test_filepath):
            self.assertTrue(key in expected_output)
            self.assertTrue(isinstance(vec, torch.Tensor))
            self.assertEqual(vec.dtype, expected_dtype)
            self.assertTrue(torch.all(torch.eq(vec, expected_output[key])))

    def test_read_vec_int_ark(self):
        self._test_helper("vec_int.ark", self.data1, kio.read_vec_int_ark, torch.int32)

    def test_read_vec_flt_ark(self):
        self._test_helper("vec_flt.ark", self.data1, kio.read_vec_flt_ark, torch.float32)

    def test_read_mat_ark(self):
        self._test_helper("mat.ark", [self.data1, self.data2], kio.read_mat_ark, torch.float32)


if __name__ == '__main__':
    unittest.main()