我正在尝试用pybind11包装一个npz文件。我在Python中有以下参数,其中函数路径查找是在C++中:
import testpy_path
sourcefile1 = np.load('data1.npy')
sourcefile2 = np.load('data2.npz')
testpy_path.pathlookup(sourcefile1, sourcefile2) //error at sourcefile2
在C++中使用PybDun11,我尝试生成NUMPY输入源SURCEFIL1和SURCEFILE2这样的:
void pathlookup(py::array_t<double, py::array::c_style | py::array::forecast> sourcefile1, py::array_t<double, py::array::c_style | py::array::forecast> sourcefile2){
std::vector<double> sources1(sourcefile1.size());
std::memcpy(sources1.data(), sourcefile1.data(), sourcefile1.size() * sizeof(double));
}
它适用于sourcefile1.npy文件,但不适用于numpy.npz文件。我的问题是,函数路径查找C++需要使用NPZ文件的参数,我如何将NPZ文件存储到向量中?p>
多谢各位
我对
numpy
不是很有经验,但这是我在手册中发现的:当您将
load()
与npz
文件一起使用时,将创建numpy.lib.npyio.NpzFile
实例,而不是array
实例。以下是手册中关于NpzFile
的重要部分:这意味着您可以通过以下方式访问阵列:
然后可以将
x
传递给函数https://www.kite.com/python/docs/numpy.lib.npyio.NpzFile
编辑:
如果希望通过
pybind
直接加载numpy数组,可以执行以下操作:或者将npz文件句柄作为
dict
传递相关问题 更多 >
编程相关推荐