Repository navigation
Expand file tree
/
Copy pathpython_wrappers.h
More file actions
126 lines (108 loc) · 3.08 KB
/
Copy pathpython_wrappers.h
File metadata and controls
126 lines (108 loc) · 3.08 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* python_wrappers.h
*
* Created on: Aug 28, 2014
* Author: chorows
*/
#ifndef PYTHON_WRAPPERS_H_
#define PYTHON_WRAPPERS_H_
extern "C" {
#include "Python.h"
#include "numpy/arrayobject.h"
}
#include <boost/shared_ptr.hpp>
#include <boost/static_assert.hpp>
#include <boost/python.hpp>
#include <boost/python/operators.hpp>
#include <boost/python/stl_iterator.hpp>
#include <util/kaldi-io.h>
#include <util/kaldi-table.h>
#include <matrix/kaldi-matrix.h>
#include <matrix/kaldi-vector.h>
#include <util/table-types.h>
namespace kaldi {
//Helper to get proper np type
template <class Real>
int get_np_type() {
//BOOST_STATIC_ASSERT_MSG(false, "Call one of the explicitly instantiated templates for float or double.");
KALDI_ERR << "Call one of the explicitly instantiated templates for float or double.";
return -1;
}
template <>
int get_np_type<double>() {
return NPY_DOUBLE;
}
template <>
int get_np_type<float>() {
return NPY_FLOAT;
}
template <>
int get_np_type<kaldi::int32>() {
return NPY_INT32;
}
template<typename Real>
class NpWrapperMatrix : public kaldi::MatrixBase<Real> {
public:
NpWrapperMatrix(PyArrayObject* arr)
: kaldi::MatrixBase<Real>(),
arr_(arr) {
if (PyArray_NDIM(arr_)!=2) {
PyErr_SetString(PyExc_TypeError, "Can wrap only matrices (2D arrays)");
boost::python::throw_error_already_set();
}
if (PyArray_TYPE(arr)!=get_np_type<Real>()) {
PyErr_SetString(PyExc_TypeError, "Wrong array dtype");
boost::python::throw_error_already_set();
}
npy_intp* dims = PyArray_DIMS(arr_);
npy_intp* strides = PyArray_STRIDES(arr_);
if (strides[1]!=sizeof(Real)) {
PyErr_SetString(PyExc_TypeError, "Wrong array column stride");
boost::python::throw_error_already_set();
}
Py_INCREF(arr_);
//why do we have to use this-> in here??
this->data_ = (Real*)PyArray_DATA(arr);
this->num_rows_ = dims[0];
this->num_cols_ = dims[1];
this->stride_ = strides[0]/sizeof(Real);
}
~NpWrapperMatrix() {
Py_DECREF(arr_);
}
protected:
PyArrayObject* arr_;
};
template<typename Real>
class NpWrapperVector : public kaldi::VectorBase<Real> {
public:
NpWrapperVector(PyArrayObject* arr)
: kaldi::VectorBase<Real>(),
arr_(arr) {
if (PyArray_NDIM(arr_)!=1) {
PyErr_SetString(PyExc_TypeError, "Can wrap only vectors (1D arrays)");
boost::python::throw_error_already_set();
}
if (PyArray_TYPE(arr)!=get_np_type<Real>()) {
PyErr_SetString(PyExc_TypeError, "Wrong array dtype");
boost::python::throw_error_already_set();
}
npy_intp* dims = PyArray_DIMS(arr_);
npy_intp* strides = PyArray_STRIDES(arr_);
if (strides[0]!=sizeof(Real)) {
PyErr_SetString(PyExc_TypeError, "Wrong array column stride");
boost::python::throw_error_already_set();
}
Py_INCREF(arr_);
//why do we have to use this-> in here??
this->data_ = (Real*)PyArray_DATA(arr);
this->dim_ = dims[0];
}
~NpWrapperVector() {
Py_DECREF(arr_);
}
protected:
PyArrayObject* arr_;
};
} //namespace kaldi
#endif /* PYTHON_WRAPPERS_H_ */