diff --git a/Base/Py/PyUtils.cpp b/Base/Py/PyUtils.cpp index aefead734d36763991c67764ef7b72154cf84f52..964b1fc64a31e0fcdbcdb563baf1fa2788994529 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 8af78455d75e682ac2e1f2078a393490282a014a..6096734589f5b4d2f783e3f06ff08bf356e6a9e9 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 38f4612a08d56bf0d3f904f748c46c755fe632fb..d137b7451decf981c10c28b30beca5458b2b3fde 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 1b40d367b7b392a3c0d46d0d61f3a4f8f716fc0b..0000000000000000000000000000000000000000 --- 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 5d6e365b02515b9b81c01ac060afa2f03e90d121..85511b7a34d3b0b9ad6583345a0134caf0aa7303 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 077ae4493755659e1cf1e96eff799e3c2e9e6323..1f2c711ec105be19b44a0a3010c99e576cb8bc99 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 d33ce3f469615988fbe11743a533cc6bbc51f0d7..3347bea4e7741cde4c558e73aeb5100033f2adb3 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 9f3cf381456647347ec0bb16f98c7c6a756bfe0d..ad5366d40a4959adeda06746d327727696a956bd 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 00765ed9352f4dfa37a06ceda17c1b4cfe88751b..5e87f65274c85ce8631730f89cf27ebffa4668c3 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