Skip to content

Commit fe94896

Browse files
authored
[src] Spawn python for numpy-array-test (#4561)
When python3 and numpy are available, verify that the generated file is successfully loaded by numpy without exceptions by invoking 'python3 -c ...' via a system() call. Don't leave behind the temporary file. If python3 and numpy aren't both available, print a warning and kill the file.
1 parent ca6d133 commit fe94896

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/matrix/numpy-array-test.cc

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919

2020
#include "matrix/numpy-array.h"
2121

22+
#include <cstdio>
23+
#include <cstdlib>
24+
#include <fstream>
25+
#include <sstream>
26+
27+
2228
using namespace kaldi;
2329

2430
namespace {
@@ -62,12 +68,24 @@ void test_numpy() {
6268
std::ifstream in("test_data/float_matrix.npy");
6369
a.Read(in, true);
6470

65-
std::ofstream os("t.bin", std::ios::binary);
71+
std::ofstream os("numpy-array.tmp", std::ios::binary);
6672
a.Write(os, true);
6773
os.close();
68-
// we can use
69-
// python -c 'import numpy as np; print(np.load("t.bin"))'
70-
// to see that numpy can load t.bin
74+
75+
// Make sure to use external double quotes for cmd.exe on Windows:
76+
// `python -c "print('Hi')"` good, `python -c 'print("Hi")'` bad.
77+
int rc = std::system("python3 -c \"import numpy\"");
78+
if (rc != 0) {
79+
rc = 0;
80+
KALDI_LOG << "python3 or numpy unavailable, array file load test SKIPPED";
81+
} else {
82+
rc = std::system("python3 -c \"import numpy; "
83+
"numpy.load('numpy-array.tmp')\"");
84+
KALDI_LOG << "python3 numpy array file load test "
85+
<< (rc == 0 ? "PASSED" : "FAILED");
86+
}
87+
std::remove("numpy-array.tmp");
88+
KALDI_ASSERT(rc == 0);
7189
}
7290

7391
template <typename Real>

0 commit comments

Comments
 (0)