Beispiel #1
0
    def test_matrix_reader_writer(self):
        kp_matrix = kaldi.FloatMatrix(2, 3)
        wspecifier = 'ark,t:test.ark'
        rspecifier = 'ark:test.ark'
        matrix_writer = kaldi.MatrixWriter(wspecifier)

        kp_matrix[0, 0] = 10

        matrix_writer.Write('id_1', kp_matrix)
        matrix_writer.Close()

        matrix_reader = kaldi.SequentialMatrixReader(rspecifier)
        key = matrix_reader.Key()
        self.assertEqual(key, 'id_1')

        value = matrix_reader.Value()
        gold = np.array([[10, 0, 0], [0, 0, 0]])
        np.testing.assert_array_equal(value.numpy(), gold)

        matrix_reader.Close()

        # test with context manager
        kp_matrix[0, 0] = 20
        with kaldi.MatrixWriter(wspecifier) as writer:
            writer.Write('id_2', kp_matrix)
        with kaldi.SequentialMatrixReader(rspecifier) as reader:
            key = reader.Key()
            self.assertEqual(key, 'id_2')
            value = reader.Value()
            gold = np.array([[20, 0, 0], [0, 0, 0]])
            np.testing.assert_array_equal(value.numpy(), gold)
        
        os.remove('test.ark')
Beispiel #2
0
    def test_read_mat(self):
        tmp = mkdtemp()
        for binary in [True, False]:
            if binary:
                wspecifier = 'ark,scp:{dir}/test.ark,{dir}/test.scp'.format(
                    dir=tmp)
            else:
                wspecifier = 'ark,scp,t:{dir}/test.ark,{dir}/test.scp'.format(
                    dir=tmp)

            data = dict()

            key1 = 'key1'
            value1 = np.arange(6 * 8).reshape(6, 8).astype(np.float32)
            writer = kaldi.MatrixWriter(wspecifier)
            writer.Write(key1, value1)
            data[key1] = value1

            key2 = 'key2'
            value2 = value1 * 10
            writer.Write(key2, value2)
            data[key2] = value2
            writer.Close()

            filename = '{}/test.scp'.format(tmp)
            with open(filename, 'r') as f:
                for line in f:
                    key, rxfilename = line.split()
                    value = kaldi.read_mat(rxfilename)
                    self.assertTrue(key in data)
                    np.testing.assert_array_equal(value, data[key])

        shutil.rmtree(tmp)
Beispiel #3
0
    def test_matrix_random_access_reader(self):
        kp_matrix = kaldi.FloatMatrix(2, 3)
        wspecifier = 'ark,t:test.ark'
        rspecifier = 'ark:test.ark'
        matrix_writer = kaldi.MatrixWriter(wspecifier)
        matrix_writer.Write('id_1', kp_matrix)
        matrix_writer.Close()

        reader = kaldi.RandomAccessMatrixReader(rspecifier)
        gold = np.array([[0, 0, 0], [0, 0, 0]])
        self.assertTrue('id_1' in reader)

        np.testing.assert_array_equal(reader['id_1'].numpy(), gold)
        self.assertFalse('id_2' in reader)
        reader.Close()
        os.remove('test.ark')
Beispiel #4
0
    def test_matrix_reader_iterator(self):
        kp_matrix = kaldi.FloatMatrix(2, 3)
        wspecifier = 'ark,t:test.ark'
        rspecifier = 'ark:test.ark'
        matrix_writer = kaldi.MatrixWriter(wspecifier)
        matrix_writer.Write('id_1', kp_matrix)
        matrix_writer.Close()

        gold_key_list = ['id_1']
        gold_value_list = [np.array([[0, 0, 0], [0, 0, 0]])]
        reader = kaldi.SequentialMatrixReader(rspecifier)
        for (key, value), gold_key, gold_value in zip(reader, gold_key_list,
                                                      gold_value_list):
            self.assertEqual(key, gold_key)
            np.testing.assert_array_equal(value.numpy(), gold_value)
        reader.Close()
        os.remove('test.ark')
Beispiel #5
0
    def test_float_matrix(self):
        tmp = mkdtemp()
        wspecifier = 'ark,scp:{dir}/test.ark,{dir}/test.scp'.format(dir=tmp)

        data = dict()

        key1 = 'key1'
        value1 = np.random.rand(6, 8).astype(np.float32)
        writer = kaldi.MatrixWriter(wspecifier)
        writer.Write(key1, value1)

        key2 = 'key2'
        value2 = np.random.rand(10, 20).astype(np.float32)
        writer.Write(key2, value2)

        writer.Close()

        data[key1] = value1
        data[key2] = value2

        rspecifier = 'scp:{}/test.scp'.format(tmp)
        reader = kaldi.SequentialMatrixReader(rspecifier)
        for key, value in reader:
            self.assertTrue(key in data)
            np.testing.assert_array_almost_equal(value.numpy(), data[key])
        reader.Close()

        reader = kaldi.RandomAccessMatrixReader(rspecifier)
        for key in data.keys():
            self.assertTrue(reader.HasKey(key))
            np.testing.assert_array_almost_equal(
                reader.Value(key).numpy(), data[key])
        reader.Close()

        # test RandomAccessReader with context manager
        with kaldi.RandomAccessMatrixReader(rspecifier) as reader:
            for key in data.keys():
                self.assertTrue(reader.HasKey(key))
                np.testing.assert_array_almost_equal(
                    reader.Value(key).numpy(), data[key])

        shutil.rmtree(tmp)