Commit f529fc31 authored by Gigg, Martyn Anthony's avatar Gigg, Martyn Anthony Committed by Martyn Gigg
Browse files

Move numpy clone functions from kernel the python core

parent 077f9e2d
......@@ -5,7 +5,10 @@
# It should not link to any other Mantid libraries.
set(SRC_FILES
src/Converters/CloneToNDArray.cpp
src/Converters/DateAndTime.cpp
src/Converters/NDArrayTypeIndex.cpp
src/Converters/NumpyFunctions.cpp
src/Converters/WrapWithNDArray.cpp
src/ErrorHandling.cpp
src/GlobalInterpreterLock.cpp
......@@ -14,8 +17,11 @@ set(SRC_FILES
src/WrapperHelpers.cpp)
set(INC_FILES
inc/MantidPythonInterface/core/Converters/CloneToNDArray.h
inc/MantidPythonInterface/core/Converters/DateAndTime.h
inc/MantidPythonInterface/core/Converters/NDArrayTypeIndex.h
inc/MantidPythonInterface/core/Converters/ToPyList.h
inc/MantidPythonInterface/core/Converters/NumpyFunctions.h
inc/MantidPythonInterface/core/Converters/VectorToNDArray.h
inc/MantidPythonInterface/core/Converters/WrapWithNDArray.h
inc/MantidPythonInterface/core/CallMethod.h
......
......@@ -4,8 +4,8 @@
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#ifndef MANTID_PYTHONINTERFACE_CLONETONUMPY_H_
#define MANTID_PYTHONINTERFACE_CLONETONUMPY_H_
#ifndef MANTID_PYTHONINTERFACE_CLONETONDARRAY_H_
#define MANTID_PYTHONINTERFACE_CLONETONDARRAY_H_
#include "MantidKernel/System.h"
#include <boost/python/detail/prefix.hpp>
......@@ -55,4 +55,4 @@ struct Clone {
} // namespace PythonInterface
} // namespace Mantid
#endif //
#endif // MANTID_PYTHONINTERFACE_CLONETONDARRAY_H_
......@@ -4,10 +4,11 @@
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#ifndef MANTID_PYTHONINERFACE_CONVERTERS_DATEANDTIME_H_
#define MANTID_PYTHONINERFACE_CONVERTERS_DATEANDTIME_H_
#ifndef MANTID_PYTHONINERFACE_CORE_DATEANDTIME_H_
#define MANTID_PYTHONINERFACE_CORE_DATEANDTIME_H_
#include "MantidKernel/DateAndTime.h"
#include "MantidPythonInterface/core/DllConfig.h"
#include <boost/python/object.hpp>
#include <numpy/ndarraytypes.h>
......@@ -16,15 +17,16 @@ namespace PythonInterface {
namespace Converters {
/// Convert to numpy's datetime64. This is panda's name for the function.
PyObject *to_datetime64(const Types::Core::DateAndTime &dateandtime);
MANTID_PYTHONINTERFACE_CORE_DLL PyObject *
to_datetime64(const Types::Core::DateAndTime &dateandtime);
/// Total nanoseconds since the unix epoch
npy_datetime to_npy_datetime(const Types::Core::DateAndTime &dateandtime);
PyArray_Descr *descr_ns();
boost::shared_ptr<Types::Core::DateAndTime>
MANTID_PYTHONINTERFACE_CORE_DLL npy_datetime
to_npy_datetime(const Types::Core::DateAndTime &dateandtime);
MANTID_PYTHONINTERFACE_CORE_DLL PyArray_Descr *descr_ns();
MANTID_PYTHONINTERFACE_CORE_DLL boost::shared_ptr<Types::Core::DateAndTime>
to_dateandtime(const boost::python::api::object &value);
} // namespace Converters
} // namespace PythonInterface
} // namespace Mantid
#endif /* MANTID_PYTHONINERFACE_CONVERTERS_DATEANDTIME_H_ */
#endif /* MANTID_PYTHONINERFACE_CORE_DATEANDTIME_H_ */
......@@ -11,6 +11,7 @@
#pragma GCC system_header
#endif
#include "MantidPythonInterface/core/DllConfig.h"
#include <boost/python/detail/wrap_python.hpp>
// Forward declare the numpy array types
......@@ -33,13 +34,16 @@ namespace PythonInterface {
namespace Converters {
namespace Impl {
/// equivalent to macro PyArray_IterNew
PyObject *func_PyArray_IterNew(PyArrayObject *arr);
MANTID_PYTHONINTERFACE_CORE_DLL PyObject *
func_PyArray_IterNew(PyArrayObject *arr);
/// equivalent to macro PyArray_NewFromDescr
PyArrayObject *func_PyArray_NewFromDescr(int datatype, const int ndims,
Py_intptr_t *dims);
PyArrayObject *func_PyArray_NewFromDescr(const char *datadescr, const int ndims,
Py_intptr_t *dims);
PyArray_Descr *func_PyArray_Descr(const char *datadescr);
MANTID_PYTHONINTERFACE_CORE_DLL PyArrayObject *
func_PyArray_NewFromDescr(int datatype, const int ndims, Py_intptr_t *dims);
MANTID_PYTHONINTERFACE_CORE_DLL PyArrayObject *
func_PyArray_NewFromDescr(const char *datadescr, const int ndims,
Py_intptr_t *dims);
MANTID_PYTHONINTERFACE_CORE_DLL PyArray_Descr *
func_PyArray_Descr(const char *datadescr);
} // namespace Impl
} // namespace Converters
} // namespace PythonInterface
......
......@@ -7,15 +7,15 @@
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "MantidPythonInterface/kernel/Converters/CloneToNumpy.h"
#include "MantidPythonInterface/core/Converters/CloneToNDArray.h"
#include "MantidPythonInterface/core/Converters/DateAndTime.h"
#include "MantidPythonInterface/core/Converters/NDArrayTypeIndex.h"
#include "MantidPythonInterface/kernel/Converters/DateAndTime.h"
#include "MantidPythonInterface/kernel/Converters/NumpyFunctions.h"
#include "MantidPythonInterface/core/Converters/NumpyFunctions.h"
#include "MantidTypes/Core/DateAndTime.h"
#include <boost/python/list.hpp>
#include <string>
#define PY_ARRAY_UNIQUE_SYMBOL KERNEL_ARRAY_API
#define PY_ARRAY_UNIQUE_SYMBOL CORE_ARRAY_API
#define NO_IMPORT_ARRAY
#include <numpy/arrayobject.h>
......@@ -53,7 +53,8 @@ PyObject *clone1D(const std::vector<ElementType> &cvector) {
* Returns a new numpy array with the a copy of the data vector of np.datetime64
*/
template <>
PyObject *clone1D(const std::vector<Types::Core::DateAndTime> &cvector) {
MANTID_PYTHONINTERFACE_CORE_DLL PyObject *
clone1D(const std::vector<Types::Core::DateAndTime> &cvector) {
// create an empty array
PyArray_Descr *descr = Converters::descr_ns();
Py_intptr_t dims[1] = {static_cast<int>(cvector.size())};
......@@ -78,7 +79,9 @@ PyObject *clone1D(const std::vector<Types::Core::DateAndTime> &cvector) {
* @param cvector :: A reference to the cvector to clone
* @return The new cloned array
*/
template <> PyObject *clone1D(const std::vector<bool> &cvector) {
template <>
MANTID_PYTHONINTERFACE_CORE_DLL PyObject *
clone1D(const std::vector<bool> &cvector) {
Py_intptr_t dims[1] = {static_cast<int>(cvector.size())};
int datatype = NDArrayTypeIndex<bool>::typenum;
PyArrayObject *nparray = func_PyArray_NewFromDescr(datatype, 1, &dims[0]);
......
......@@ -4,13 +4,13 @@
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidPythonInterface/kernel/Converters/DateAndTime.h"
#include "MantidPythonInterface/core/Converters/DateAndTime.h"
#include "MantidKernel/WarningSuppressions.h"
#include "MantidPythonInterface/kernel/Converters/NumpyFunctions.h"
#include "MantidPythonInterface/core/Converters/NumpyFunctions.h"
#include <boost/make_shared.hpp>
#include <boost/python.hpp>
#define PY_ARRAY_UNIQUE_SYMBOL KERNEL_ARRAY_API
#define PY_ARRAY_UNIQUE_SYMBOL CORE_ARRAY_API
#define NO_IMPORT_ARRAY
#include <numpy/arrayobject.h>
#include <numpy/arrayscalars.h>
......
......@@ -4,11 +4,11 @@
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidPythonInterface/kernel/Converters/NumpyFunctions.h"
#include "MantidPythonInterface/core/Converters/NumpyFunctions.h"
// See
// http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PY_ARRAY_UNIQUE_SYMBOL
#define PY_ARRAY_UNIQUE_SYMBOL KERNEL_ARRAY_API
#define PY_ARRAY_UNIQUE_SYMBOL CORE_ARRAY_API
#define NO_IMPORT_ARRAY
#include <numpy/arrayobject.h>
......
......@@ -9,8 +9,8 @@
#include "MantidKernel/Matrix.h"
#include "MantidKernel/System.h"
#include "MantidPythonInterface/core/Converters/CloneToNDArray.h"
#include "MantidPythonInterface/core/NDArray.h"
#include "MantidPythonInterface/kernel/Converters/CloneToNumpy.h"
#include "MantidPythonInterface/kernel/Converters/MatrixToNDArray.h"
#include <type_traits>
......
......@@ -8,9 +8,9 @@
#define MANTID_PYTHONINTERFACE_VECTORTONUMPY_H_
#include "MantidKernel/System.h"
#include "MantidPythonInterface/core/Converters/CloneToNDArray.h"
#include "MantidPythonInterface/core/Converters/VectorToNDArray.h"
#include "MantidPythonInterface/core/NDArray.h"
#include "MantidPythonInterface/kernel/Converters/CloneToNumpy.h"
#include <boost/mpl/and.hpp>
#include <boost/mpl/if.hpp>
......
......@@ -5,7 +5,7 @@
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidGeometry/Crystal/IPeak.h"
#include "MantidPythonInterface/kernel/Converters/CloneToNumpy.h"
#include "MantidPythonInterface/core/Converters/CloneToNDArray.h"
#include "MantidPythonInterface/kernel/Converters/PyObjectToMatrix.h"
#include "MantidPythonInterface/kernel/GetPointer.h"
#include "MantidPythonInterface/kernel/Policies/MatrixToNumpy.h"
......
......@@ -11,9 +11,9 @@
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidKernel/V3D.h"
#include "MantidPythonInterface/core/Converters/CloneToNDArray.h"
#include "MantidPythonInterface/core/NDArray.h"
#include "MantidPythonInterface/core/VersionCompat.h"
#include "MantidPythonInterface/kernel/Converters/CloneToNumpy.h"
#include "MantidPythonInterface/kernel/Converters/NDArrayToVector.h"
#include "MantidPythonInterface/kernel/Converters/PySequenceToVector.h"
#include "MantidPythonInterface/kernel/GetPointer.h"
......
......@@ -17,9 +17,9 @@
#include "MantidKernel/Logger.h"
#include "MantidKernel/OptionalBool.h"
#include "MantidKernel/Unit.h"
#include "MantidPythonInterface/core/Converters/CloneToNDArray.h"
#include "MantidPythonInterface/core/Converters/VectorToNDArray.h"
#include "MantidPythonInterface/core/Converters/WrapWithNDArray.h"
#include "MantidPythonInterface/kernel/Converters/CloneToNumpy.h"
#include "MantidPythonInterface/kernel/Converters/NDArrayToVector.h"
#include "MantidPythonInterface/kernel/GetPointer.h"
#include "MantidPythonInterface/kernel/Registry/RegisterWorkspacePtrToPython.h"
......
......@@ -68,10 +68,7 @@ create_module(${MODULE_TEMPLATE} ${MODULE_DEFINITION} ${EXPORT_FILES})
# Helper code
set(SRC_FILES
src/Converters/CloneToNumpy.cpp
src/Converters/DateAndTime.cpp
src/Converters/NDArrayToVector.cpp
src/Converters/NumpyFunctions.cpp
src/Converters/PyObjectToMatrix.cpp
src/Converters/PyObjectToV3D.cpp
src/Converters/PyObjectToVMD.cpp
......@@ -83,12 +80,9 @@ set(SRC_FILES
set(INC_FILES
${HEADER_DIR}/kernel/Converters/CArrayToNDArray.h
${HEADER_DIR}/kernel/Converters/CloneToNumpy.h
${HEADER_DIR}/kernel/Converters/ContainerDtype.h
${HEADER_DIR}/kernel/Converters/DateAndTime.h
${HEADER_DIR}/kernel/Converters/MapToPyDictionary.h
${HEADER_DIR}/kernel/Converters/MatrixToNDArray.h
${HEADER_DIR}/kernel/Converters/NumpyFunctions.h
${HEADER_DIR}/kernel/Converters/NDArrayToVector.h
${HEADER_DIR}/kernel/Converters/PyObjectToMatrix.h
${HEADER_DIR}/kernel/Converters/PyObjectToV3D.h
......
......@@ -9,7 +9,7 @@
//-----------------------------------------------------------------------------
#include "MantidPythonInterface/kernel/Converters/NDArrayToVector.h"
#include "MantidPythonInterface/core/Converters/NDArrayTypeIndex.h"
#include "MantidPythonInterface/kernel/Converters/NumpyFunctions.h"
#include "MantidPythonInterface/core/Converters/NumpyFunctions.h"
#include <boost/python/extract.hpp>
#include <boost/python/stl_iterator.hpp>
......
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright &copy; 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "MantidPythonInterface/kernel/Converters/VectorToNDArray.h"
#include "MantidPythonInterface/kernel/Converters/NDArrayTypeIndex.h"
#include <boost/python/list.hpp>
#define PY_ARRAY_UNIQUE_SYMBOL KERNEL_ARRAY_API
#define NO_IMPORT_ARRAY
#include <numpy/arrayobject.h>
#include <string>
namespace Mantid {
namespace PythonInterface {
namespace Converters {
namespace Impl {
/**
* Defines the wrapWithNDArray specialization for vector container types
*
* Wraps a vector in a numpy array structure without copying the data
* @param cdata :: A reference to the std::vector to wrap
* @param mode :: A mode switch to define whether the final array is read
*only/read-write
* @return A pointer to a numpy ndarray object
*/
template <typename ContainerType>
PyObject *wrapWithNDArray(const ContainerType &cdata,
const NumpyWrapMode mode) {
npy_intp dims[1] = {cdata.size()};
int datatype = NDArrayTypeIndex<typename ContainerType::value_type>::typenum;
PyObject *nparray =
PyArray_SimpleNewFromData(1, dims, datatype, (void *)&(cdata[0]));
if (mode == ReadOnly) {
PyArrayObject *np = (PyArrayObject *)nparray;
np->flags &= ~NPY_WRITEABLE;
}
return nparray;
}
/**
* Returns a new numpy array with the a copy of the data from cvector. A
* specialization
* exists for strings so that they simply create a standard python list.
* @param cdata :: A reference to a std::vector
* @return
*/
template <typename ContainerType>
PyObject *cloneToNDArray(const ContainerType &cdata) {
npy_intp dims[1] = {cdata.size()};
int datatype = NDArrayTypeIndex<typename ContainerType::value_type>::typenum;
PyObject *nparray =
PyArray_NewFromDescr(&PyArray_Type, PyArray_DescrFromType(datatype),
1, // rank 1
dims, // Length in each dimension
NULL, NULL, 0, NULL);
void *arrayData = PyArray_DATA(nparray);
const void *data = cdata.data();
std::memcpy(arrayData, data, PyArray_ITEMSIZE(nparray) * dims[0]);
return (PyObject *)nparray;
}
/**
* Returns a new python list of strings from the given vector
* exists for strings so that they simply create a standard python list.
* @param cdata :: A reference to a std::vector
* @return
*/
template <> PyObject *cloneToNDArray(const std::vector<std::string> &cdata) {
boost::python::list pystrs;
for (auto iter = cdata.begin(); iter != cdata.end(); ++iter) {
pystrs.append(iter->c_str());
}
PyObject *rawptr = pystrs.ptr();
Py_INCREF(rawptr); // Make sure it survies after the wrapper decrefs the count
return rawptr;
}
//-----------------------------------------------------------------------
// Explicit instantiations
//-----------------------------------------------------------------------
#define INSTANTIATE_TONDARRAY(ElementType) \
template DLLExport PyObject *wrapWithNDArray<std::vector<ElementType>>( \
const std::vector<ElementType> &, const NumpyWrapMode); \
template DLLExport PyObject *cloneToNDArray<std::vector<ElementType>>( \
const std::vector<ElementType> &);
///@cond Doxygen doesn't seem to like this...
INSTANTIATE_TONDARRAY(int);
INSTANTIATE_TONDARRAY(long);
INSTANTIATE_TONDARRAY(long long);
INSTANTIATE_TONDARRAY(unsigned int);
INSTANTIATE_TONDARRAY(unsigned long);
INSTANTIATE_TONDARRAY(unsigned long long);
INSTANTIATE_TONDARRAY(double);
///@endcond
// std::string already has clone instantiated as it is specialized
template DLLExport PyObject *
wrapWithNDArray<std::vector<std::string>>(const std::vector<std::string> &,
const NumpyWrapMode);
} // namespace Impl
} // namespace Converters
} // namespace PythonInterface
} // namespace Mantid
......@@ -5,7 +5,7 @@
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidKernel/DateAndTime.h"
#include "MantidPythonInterface/kernel/Converters/DateAndTime.h"
#include "MantidPythonInterface/core/Converters/DateAndTime.h"
#include <boost/make_shared.hpp>
#include <boost/python/class.hpp>
#include <boost/python/make_constructor.hpp>
......
......@@ -5,8 +5,8 @@
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidPythonInterface/core/Converters/DateAndTime.h"
#include "MantidPythonInterface/kernel/Converters/ContainerDtype.h"
#include "MantidPythonInterface/kernel/Converters/DateAndTime.h"
#include "MantidPythonInterface/kernel/GetPointer.h"
#include "MantidPythonInterface/kernel/Policies/VectorToNumpy.h"
......@@ -165,4 +165,4 @@ void export_TimeSeriesPropertyStatistics() {
time_standard_deviation)
.add_property("duration",
&Mantid::Kernel::TimeSeriesPropertyStatistics::duration);
}
\ No newline at end of file
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment