Skip to content

Instantly share code, notes, and snippets.

@JohanMabille
Created November 6, 2017 09:16
Show Gist options
  • Save JohanMabille/dc4b4a349e34b7336f4755a209457012 to your computer and use it in GitHub Desktop.
Save JohanMabille/dc4b4a349e34b7336f4755a209457012 to your computer and use it in GitHub Desktop.
#include <numeric>
#include "xtensor/xmath.hpp"
#include "xtensor/xarray.hpp"
#define FORCE_IMPORT_ARRAY
#include "xtensor-python/pyarray.hpp"
#include "xtensor-python/pyvectorize.hpp"
namespace py = pybind11;
using complex_t = std::complex<double>;
template <class T> std::string typestring() { return "Unknown"; }
template <> std::string typestring<uint8_t>() { return "uint8"; }
template <> std::string typestring<uint16_t>() { return "uint16"; }
template <> std::string typestring<uint32_t>() { return "uint32"; }
template <> std::string typestring<uint64_t>() { return "uint64"; }
template <class T>
inline std::string int_example(xt::pyarray<T>& m)
{
return typestring<T>();
}
PYBIND11_PLUGIN(xtensor_python_test)
{
xt::import_numpy();
py::module m("xtensor_python_test", "Test module for xtensor python bindings");
m.def("int_example", int_example<uint8_t>);
m.def("int_example", int_example<uint16_t>);
m.def("int_example", int_example<uint32_t>);
m.def("int_example", int_example<uint64_t>);
return m.ptr();
}
import os
import sys
import subprocess
# Build the test extension
here = os.path.abspath(os.path.dirname(__file__))
subprocess.check_call([sys.executable, os.path.join(here, 'setup.py'), 'build_ext', '--inplace'], cwd=here)
# Test it!
from unittest import TestCase
import xtensor_python_test as xt
import numpy as np
import time
class ExampleTest(TestCase):
def test_int_example(self):
print("")
print("INT EXAMPLE")
#for dtype in [np.uint8, np.uint16, np.uint32, np.uint64]:
for dtype in [np.uint32]:
print("Calling int_example with: " + str(dtype.__name__))
b = xt.int_example(np.ones((10), dtype))
print("")
self.assertEqual(str(dtype.__name__), b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment