inconsistent numpy dtype in python and C++
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Reproduce the report using the provided dummy.cpp extension and run_dummpy.py script with the stated Python, NumPy, and Boost versions. Inspect how the two ndarray arguments reach the Boost.NumPy binding and verify that each retains its dtype; done means the C++ output matches float32 and float64 consistently.
Written by the indexing model from the issue text.
Description
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
- Dominant language
- C++
- Stars
- 537
- Forks
- 223
- Avg merge
- 11h 22m
- Merged PRs (30d)
- 2
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from boostorg/python
-
Difficulty 1/5 Under an hour Newbie friendliness 84/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
-
BoostDetectToolset-1.90.0.cmake file not found in an include() call in boost_python-config.cmake Open
Difficulty 3/5 1-2 days Newbie friendliness 48/100
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
AuTest Bug Tests
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
apache/trafficserver#13714 ·
-
bug build
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
facebookincubator/velox#19143 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
tenstorrent/tt-metal#57393 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
objectionary/eo-graphs#74 ·