// Copyright (c) 2016-2017 Josh Blum // SPDX-License-Identifier: BSL-1.0 #include #include #include #include #include /*********************************************************************** * buffer chunk to/from numpy **********************************************************************/ static Pothos::Proxy convertBufferChunkToNumpyArray(Pothos::ProxyEnvironment::Sptr env, const Pothos::BufferChunk &buffer) { auto module = env->findProxy("Pothos.Buffer"); auto dtype = module.get("dtype_to_numpy")(buffer.dtype); return module.get("pointer_to_ndarray")(buffer.address, buffer.elements(), dtype); } static Pothos::BufferChunk convertNumpyArrayToBufferChunk(const Pothos::Proxy &npArray) { //extract shape and data type information const auto shape = npArray.get("shape"); const size_t numBytes = npArray.get("nbytes"); const size_t dimension = (shape.size() > 1)? shape.at(1).convert() : 1; const auto dtypeName = npArray.get("dtype").get("name"); const Pothos::DType dtype(dtypeName, dimension); const size_t address = npArray.get("__array_interface__").call("get", "data").call("__getitem__", 0); //create a shared buffer that holds the numpy array auto sharedBuff = Pothos::SharedBuffer(address, numBytes, npArray.getHandle()); //now create a buffer chunk of that shared buffer with matching dtype auto chunk = Pothos::BufferChunk(sharedBuff); chunk.dtype = dtype; return chunk; } template static T convertNumpyIntegerToNative(const Pothos::Proxy &num) { return num.call("__int__"); } template static T convertNumpyFloatToNative(const Pothos::Proxy &num) { return num.call("__float__"); } template static std::complex convertNumpyComplexToNative(const Pothos::Proxy &num) { return std::complex(num.get("real"), num.get("imag")); } pothos_static_block(pothosRegisterNumpyBufferConversions) { Pothos::PluginRegistry::addCall("/proxy/converters/python/buffer_chunk_to_numpy_array", &convertBufferChunkToNumpyArray); Pothos::PluginRegistry::add("/proxy/converters/python/numpy_array_to_buffer_chunk", Pothos::ProxyConvertPair("numpy.ndarray", &convertNumpyArrayToBufferChunk)); //integer types Pothos::PluginRegistry::add("/proxy/converters/python/numpy_int8_to_int8", Pothos::ProxyConvertPair("numpy.int8", &convertNumpyIntegerToNative)); Pothos::PluginRegistry::add("/proxy/converters/python/numpy_uint8_to_uint8", Pothos::ProxyConvertPair("numpy.uint8", &convertNumpyIntegerToNative)); Pothos::PluginRegistry::add("/proxy/converters/python/numpy_int16_to_int16", Pothos::ProxyConvertPair("numpy.int16", &convertNumpyIntegerToNative)); Pothos::PluginRegistry::add("/proxy/converters/python/numpy_uint16_to_uint16", Pothos::ProxyConvertPair("numpy.uint16", &convertNumpyIntegerToNative)); Pothos::PluginRegistry::add("/proxy/converters/python/numpy_int32_to_int32", Pothos::ProxyConvertPair("numpy.int32", &convertNumpyIntegerToNative)); Pothos::PluginRegistry::add("/proxy/converters/python/numpy_uint32_to_uint32", Pothos::ProxyConvertPair("numpy.uint32", &convertNumpyIntegerToNative)); Pothos::PluginRegistry::add("/proxy/converters/python/numpy_int64_to_int64", Pothos::ProxyConvertPair("numpy.int64", &convertNumpyIntegerToNative)); Pothos::PluginRegistry::add("/proxy/converters/python/numpy_uint64_to_uint64", Pothos::ProxyConvertPair("numpy.uint64", &convertNumpyIntegerToNative)); //float types Pothos::PluginRegistry::add("/proxy/converters/python/numpy_float16_to_float", Pothos::ProxyConvertPair("numpy.float16", &convertNumpyFloatToNative)); Pothos::PluginRegistry::add("/proxy/converters/python/numpy_float32_to_float", Pothos::ProxyConvertPair("numpy.float32", &convertNumpyFloatToNative)); Pothos::PluginRegistry::add("/proxy/converters/python/numpy_float64_to_double", Pothos::ProxyConvertPair("numpy.float64", &convertNumpyFloatToNative)); //complex types Pothos::PluginRegistry::add("/proxy/converters/python/numpy_complex64_to_complex_float", Pothos::ProxyConvertPair("numpy.complex64", &convertNumpyComplexToNative)); Pothos::PluginRegistry::add("/proxy/converters/python/numpy_complex128_to_complex_double", Pothos::ProxyConvertPair("numpy.complex128", &convertNumpyComplexToNative)); }