inconsistent numpy dtype in python and C++
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 35/100
研究方向
使用提供的 dummy.cpp 擴充功能和 run_dummpy.py 指令碼重現該報告,並使用所述版本的 Python、NumPy 和 Boost 執行。檢查兩個 ndarray 引數如何到達 Boost.NumPy binding,並驗證每個引數都保留其 dtype;當 C++ 輸出始終分別與 float32 和 float64 相符時,即表示完成。
由索引模型根據 Issue 內容生成。
描述
Hi,
I'm trying to use the boost numpy bindings. If I pass two ndarray with different dtype to C++, they don't preserve the correct dtype. Here is a simple example:
dummy.cpp:
#include <iostream>
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
namespace py = boost::python;
namespace np = boost::python::numpy;
void dummy(np::ndarray const& a, np::ndarray const& b) {
char const* at = py::extract<char const* >(py::str(a.get_dtype()));
char const* bt = py::extract<char const* >(py::str(b.get_dtype()));
std::cout << at << std::endl;
std::cout << bt << std::endl;
}
BOOST_PYTHON_MODULE(dummy) {
Py_Initialize(); // need initialize in BOOST_PYTHON_MODULE, or will get segmentation fault and core dump
np::initialize();
py::def("dummy", dummy);
}
compile
g++ -lboost_python38 -lboost_numpy38 -L$CONDA_PREFIX/lib -I$CONDA_PREFIX/include -I$CONDA_PREFIX/include/python3.8 -fPIC --shared -o dummy.so dummy.cpp
run_dummpy.py
import numpy as np
import dummy
a = np.array([[1, 2, 3], [4, 5, 6]], dtype = np.float32)
print("python", a.dtype)
b = np.array([[1, 2, 3], [4, 5, 6]], dtype = np.double)
print("python", b.dtype)
dummy.dummy(a, b)
The output is inconsistent. The first time I run it by python run_dummy.py it gives
python float32
python float64
__str__
float64
If I re-run it python run_dummy.py , it gives
python float32
python float64
float64
float64
Still not the right output.
I'm using python 3.8.2, numpy 1.18.1, and boost 1.72.0. boost is installed from anaconda conda install -c conda-forge boost
Could you help? Thanks
- 主要語言
- C++
- 星號
- 537
- 分支
- 223
- 平均合併
- 11 小時 22 分鐘
- 30 天內合併 PR
- 2
貢獻指南
這個儲存庫沒有索引到貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
boostorg/python 的其他 Issue
-
難度 1/5 1 小時以內 新手友好度 84/100
-
難度 2/5 1-3 小時 新手友好度 68/100
-
難度 2/5 1-3 小時 新手友好度 64/100
-
BoostDetectToolset-1.90.0.cmake file not found in an include() call in boost_python-config.cmake 未關閉
難度 3/5 1-2 天 新手友好度 48/100
-
難度 4/5 3-5 天 新手友好度 25/100
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 75/100
flutter-webrtc/flutter-webrtc#2206 ·
-
難度 2/5 1-3 小時 新手友好度 70/100
google-ai-edge/LiteRT-LM#3739 ·
-
Component: GLib
難度 2/5 1-3 小時 新手友好度 70/100
-
難度 2/5 1-3 小時 新手友好度 65/100
brave/brave-browser#59300 ·
-
Mute ydb/tests/functional/dstool/test_canonical_requests.py.Test.test_group_take_snapshot in main 未關閉ai_reviewed
難度 2/5 1-3 小時 新手友好度 70/100
ydb-platform/ydb#53974 · 3 則留言 ·