From a4f04839e23ec1a2460b60983a64d2dc6fe1c796 Mon Sep 17 00:00:00 2001 From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de> Date: Thu, 10 Dec 2020 13:22:36 +0100 Subject: [PATCH] mv non-template fct out of template namespace --- Base/Py/PyUtils.cpp | 24 +++++++++++- Base/Py/PyUtils.h | 3 ++ Device/Histo/Histogram1D.cpp | 8 ++-- Device/Intensity/ArrayUtils.cpp | 41 -------------------- Device/Intensity/ArrayUtils.h | 12 +++--- Sample/Multilayer/PyImport.cpp | 2 +- Tests/Functional/Python/PyEmbedded/Tests.cpp | 2 +- auto/Wrap/doxygenBase.i | 3 ++ auto/Wrap/doxygenDevice.i | 8 +--- 9 files changed, 42 insertions(+), 61 deletions(-) delete mode 100644 Device/Intensity/ArrayUtils.cpp diff --git a/Base/Py/PyUtils.cpp b/Base/Py/PyUtils.cpp index aefead734d3..964b1fc64a3 100644 --- a/Base/Py/PyUtils.cpp +++ b/Base/Py/PyUtils.cpp @@ -105,8 +105,7 @@ std::string PyUtils::pythonRuntimeInfo() { // Embedded Python details result << "Py_GetProgramName(): " << PyUtils::toString(Py_GetProgramName()) << "\n"; - result << "Py_GetProgramFullPath(): " << PyUtils::toString(Py_GetProgramFullPath()) - << "\n"; + result << "Py_GetProgramFullPath(): " << PyUtils::toString(Py_GetProgramFullPath()) << "\n"; result << "Py_GetPath(): " << PyUtils::toString(Py_GetPath()) << "\n"; result << "Py_GetPythonHome(): " << PyUtils::toString(Py_GetPythonHome()) << "\n"; @@ -163,4 +162,25 @@ std::string PyUtils::pythonStackTrace() { return result.str(); } +PyObject* PyUtils::createNumpyArray(const std::vector<double>& data) { + const size_t ndim(1); + npy_int ndim_numpy = ndim; + npy_intp* ndimsizes_numpy = new npy_intp[ndim]; + ndimsizes_numpy[0] = data.size(); + + // creating standalone numpy array + PyObject* pyarray = PyArray_SimpleNew(ndim_numpy, ndimsizes_numpy, NPY_DOUBLE); + delete[] ndimsizes_numpy; + if (pyarray == nullptr) + throw std::runtime_error("ExportOutputData() -> Panic in PyArray_SimpleNew"); + + // getting pointer to data buffer of numpy array + double* array_buffer = (double*)PyArray_DATA((PyArrayObject*)pyarray); + + for (size_t index = 0; index < data.size(); ++index) + *array_buffer++ = data[index]; + + return pyarray; +} + #endif // BORNAGAIN_PYTHON diff --git a/Base/Py/PyUtils.h b/Base/Py/PyUtils.h index 8af78455d75..6096734589f 100644 --- a/Base/Py/PyUtils.h +++ b/Base/Py/PyUtils.h @@ -49,6 +49,9 @@ std::string pythonRuntimeInfo(); //! Returns string representing python stack trace. std::string pythonStackTrace(); + +PyObject* createNumpyArray(const std::vector<double>& data); + } // namespace PyUtils #endif // BORNAGAIN_PYTHON diff --git a/Device/Histo/Histogram1D.cpp b/Device/Histo/Histogram1D.cpp index 38f4612a08d..d137b7451de 100644 --- a/Device/Histo/Histogram1D.cpp +++ b/Device/Histo/Histogram1D.cpp @@ -14,7 +14,7 @@ #include "Device/Histo/Histogram1D.h" #include "Base/Axis/VariableBinAxis.h" -#include "Device/Intensity/ArrayUtils.h" +#include "Base/Py/PyUtils.h" #include <memory> Histogram1D::Histogram1D(int nbinsx, double xlow, double xup) { @@ -59,15 +59,15 @@ std::vector<double> Histogram1D::binErrors() const { #ifdef BORNAGAIN_PYTHON PyObject* Histogram1D::binCentersNumpy() const { - return ArrayUtils::createNumpyArray(binCenters()); + return PyUtils::createNumpyArray(binCenters()); } PyObject* Histogram1D::binValuesNumpy() const { - return ArrayUtils::createNumpyArray(binValues()); + return PyUtils::createNumpyArray(binValues()); } PyObject* Histogram1D::binErrorsNumpy() const { - return ArrayUtils::createNumpyArray(binErrors()); + return PyUtils::createNumpyArray(binErrors()); } #endif // BORNAGAIN_PYTHON diff --git a/Device/Intensity/ArrayUtils.cpp b/Device/Intensity/ArrayUtils.cpp deleted file mode 100644 index 1b40d367b7b..00000000000 --- a/Device/Intensity/ArrayUtils.cpp +++ /dev/null @@ -1,41 +0,0 @@ -// ************************************************************************************************ -// -// BornAgain: simulate and fit scattering at grazing incidence -// -//! @file Device/Intensity/ArrayUtils.cpp -//! @brief Implements various functions to interact from numpy on Python side -//! -//! @homepage http://www.bornagainproject.org -//! @license GNU General Public License v3 or higher (see COPYING) -//! @copyright Forschungszentrum Jülich GmbH 2018 -//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) -// -// ************************************************************************************************ - -#ifdef BORNAGAIN_PYTHON - -#include "Device/Intensity/ArrayUtils.h" -#include "Base/Py/PyCore.h" - -PyObject* ArrayUtils::createNumpyArray(const std::vector<double>& data) { - const size_t ndim(1); - npy_int ndim_numpy = ndim; - npy_intp* ndimsizes_numpy = new npy_intp[ndim]; - ndimsizes_numpy[0] = data.size(); - - // creating standalone numpy array - PyObject* pyarray = PyArray_SimpleNew(ndim_numpy, ndimsizes_numpy, NPY_DOUBLE); - delete[] ndimsizes_numpy; - if (pyarray == nullptr) - throw std::runtime_error("ExportOutputData() -> Panic in PyArray_SimpleNew"); - - // getting pointer to data buffer of numpy array - double* array_buffer = (double*)PyArray_DATA((PyArrayObject*)pyarray); - - for (size_t index = 0; index < data.size(); ++index) - *array_buffer++ = data[index]; - - return pyarray; -} - -#endif // BORNAGAIN_PYTHON diff --git a/Device/Intensity/ArrayUtils.h b/Device/Intensity/ArrayUtils.h index 5d6e365b025..85511b7a34d 100644 --- a/Device/Intensity/ArrayUtils.h +++ b/Device/Intensity/ArrayUtils.h @@ -25,9 +25,10 @@ #include <stdexcept> #include <vector> -//! Array and Numpy utility functions getShape, createNumpyArray. +//! Array utility functions getShape namespace ArrayUtils { + //! Returns shape nrows, ncols of 2D array. template <class T> std::pair<size_t, size_t> getShape(const T& data); @@ -70,10 +71,6 @@ template <class T> CreateDataImpl::ReturnType<T> createData(const T& vec) { return CreateDataImpl::createDataImpl(vec); } -#ifdef BORNAGAIN_PYTHON -PyObject* createNumpyArray(const std::vector<double>& data); -#endif // BORNAGAIN_PYTHON - //! Creates 1D vector from OutputData. template <class T> decltype(auto) createVector1D(const T& data); @@ -82,6 +79,11 @@ template <class T> decltype(auto) createVector2D(const T& data); } // namespace ArrayUtils + +// ************************************************************************************************ +// implementation +// ************************************************************************************************ + template <class T> std::unique_ptr<OutputData<T>> ArrayUtils::CreateDataImpl::createDataImpl(const std::vector<T>& vec) { diff --git a/Sample/Multilayer/PyImport.cpp b/Sample/Multilayer/PyImport.cpp index 077ae449375..1f2c711ec10 100644 --- a/Sample/Multilayer/PyImport.cpp +++ b/Sample/Multilayer/PyImport.cpp @@ -15,8 +15,8 @@ #ifdef BORNAGAIN_PYTHON #include "Sample/Multilayer/PyImport.h" -#include "Base/Py/PyUtils.h" #include "Base/Py/PyCore.h" +#include "Base/Py/PyUtils.h" #include "Sample/Multilayer/MultiLayer.h" namespace { diff --git a/Tests/Functional/Python/PyEmbedded/Tests.cpp b/Tests/Functional/Python/PyEmbedded/Tests.cpp index d33ce3f4696..3347bea4e77 100644 --- a/Tests/Functional/Python/PyEmbedded/Tests.cpp +++ b/Tests/Functional/Python/PyEmbedded/Tests.cpp @@ -13,8 +13,8 @@ #include "BABuild.h" #include "BAVersion.h" -#include "Base/Py/PyUtils.h" #include "Base/Py/PyCore.h" +#include "Base/Py/PyUtils.h" #include "Base/Utils/SysUtils.h" #include "Core/Export/ExportToPython.h" #include "Core/Export/PyFmt.h" diff --git a/auto/Wrap/doxygenBase.i b/auto/Wrap/doxygenBase.i index 9f3cf381456..ad5366d40a4 100644 --- a/auto/Wrap/doxygenBase.i +++ b/auto/Wrap/doxygenBase.i @@ -1438,6 +1438,9 @@ Returns multi-line string representing PATH, PYTHONPATH, sys.path and other info Returns string representing python stack trace. "; +%feature("docstring") PyUtils::createNumpyArray "PyObject* PyUtils::createNumpyArray(const std::vector< double > &data) +"; + // File: namespaceStringUtils.xml %feature("docstring") StringUtils::matchesPattern "bool StringUtils::matchesPattern(const std::string &text, const std::string &wildcardPattern) diff --git a/auto/Wrap/doxygenDevice.i b/auto/Wrap/doxygenDevice.i index 00765ed9352..5e87f65274c 100644 --- a/auto/Wrap/doxygenDevice.i +++ b/auto/Wrap/doxygenDevice.i @@ -2969,7 +2969,7 @@ Returns true if area defined by two bins is inside or on border of polygon (more // File: namespace_0d10.xml -// File: namespace_0d105.xml +// File: namespace_0d104.xml // File: namespace_0d37.xml @@ -2992,9 +2992,6 @@ Returns shape nrows, ncols of 2D array. Creates OutputData array from input vector. "; -%feature("docstring") ArrayUtils::createNumpyArray "PyObject * ArrayUtils::createNumpyArray(const std::vector< double > &data) -"; - %feature("docstring") ArrayUtils::createVector1D "decltype(auto) ArrayUtils::createVector1D(const T &data) Creates 1D vector from OutputData. @@ -3438,9 +3435,6 @@ make Swappable // File: VarianceFunctions_8h.xml -// File: ArrayUtils_8cpp.xml - - // File: ArrayUtils_8h.xml -- GitLab