-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #4399. Move Matrix->numpy functions to templated objects.
As with the vector converters this will provide much more flexibility in the conversion.
- Loading branch information
1 parent
6a60c84
commit b7049d7
Showing
7 changed files
with
182 additions
and
39 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
...d/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Converters/MatrixToNDArray.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#ifndef MANTID_PYTHONINTERFACE_MATRIXTONDARRAY_H_ | ||
#define MANTID_PYTHONINTERFACE_MATRIXTONDARRAY_H_ | ||
/** | ||
Copyright © 2012 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory | ||
This file is part of Mantid. | ||
Mantid is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. | ||
Mantid is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid> | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
#include "MantidKernel/System.h" | ||
#include "MantidKernel/Matrix.h" | ||
#include <boost/python/detail/prefix.hpp> | ||
|
||
namespace Mantid | ||
{ | ||
namespace PythonInterface | ||
{ | ||
namespace Converters | ||
{ | ||
//----------------------------------------------------------------------- | ||
// Converter implementation | ||
//----------------------------------------------------------------------- | ||
/** | ||
* Converter that takes a Mantid Matrix and converts it into a | ||
* numpy array. | ||
* | ||
* The type of conversion is specified by another struct/class that | ||
* contains a static member create. | ||
*/ | ||
template<typename ElementType, typename ConversionPolicy> | ||
struct DLLExport MatrixToNDArray | ||
{ | ||
inline PyObject * operator()(const Kernel::Matrix<ElementType> & cmatrix) const | ||
{ | ||
typedef typename ConversionPolicy::template apply<ElementType> policy; | ||
return policy::create(cmatrix); | ||
} | ||
}; | ||
|
||
//----------------------------------------------------------------------- | ||
// Conversion Policies | ||
//----------------------------------------------------------------------- | ||
namespace Impl | ||
{ | ||
enum WrapMode { ReadOnly, ReadWrite }; | ||
/** | ||
* Helper functions to keep the numpy arrayobject header out | ||
* the header file | ||
*/ | ||
/// Wrap a numpy array around existing data | ||
template<typename ElementType> | ||
PyObject *wrapWithNDArray(const Kernel::Matrix<ElementType>&, const WrapMode); | ||
} | ||
|
||
/** | ||
* WrapReadOnly is a policy for VectorToNDArray | ||
* to wrap the vector in a read-only numpy array | ||
* that looks at the original data. No copy is performed | ||
*/ | ||
struct WrapReadOnly | ||
{ | ||
template<typename ElementType> | ||
struct apply | ||
{ | ||
/** | ||
* Returns a read-only Numpy array wrapped around an existing vector | ||
* @param cvector | ||
* @return | ||
*/ | ||
static PyObject * create(const Kernel::Matrix<ElementType> & cmatrix) | ||
{ | ||
return Impl::wrapWithNDArray(cmatrix, Impl::ReadOnly); | ||
} | ||
}; | ||
}; | ||
|
||
/** | ||
* WrapReadWrite is a policy for MatrixToNDArray | ||
* to wrap the vector in a read-write numpy array | ||
* that looks at the original data. No copy is performed | ||
*/ | ||
struct WrapReadWrite | ||
{ | ||
template<typename ElementType> | ||
struct apply | ||
{ | ||
/** | ||
* Returns a read-write Numpy array wrapped around an existing vector | ||
* @param cvector | ||
* @return | ||
*/ | ||
static PyObject * create(const Kernel::Matrix<ElementType> & cmatrix) | ||
{ | ||
return Impl::wrapWithNDArray(cmatrix, Impl::ReadWrite); | ||
} | ||
}; | ||
}; | ||
|
||
} | ||
} | ||
} | ||
|
||
#endif /// MANTID_PYTHONINTERFACE_MATRIXTONDARRAY_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Converters/MatrixToNDArray.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//----------------------------------------------------------------------------- | ||
// Includes | ||
//----------------------------------------------------------------------------- | ||
#include "MantidPythonInterface/kernel/Converters/MatrixToNDArray.h" | ||
#include "MantidPythonInterface/kernel/Converters/NDArrayTypeIndex.h" | ||
|
||
#define PY_ARRAY_UNIQUE_SYMBOL KERNEL_ARRAY_API | ||
#define NO_IMPORT_ARRAY | ||
#include <numpy/arrayobject.h> | ||
|
||
namespace Mantid { namespace PythonInterface | ||
{ | ||
namespace Converters | ||
{ | ||
namespace Impl | ||
{ | ||
/** | ||
* Wraps a vector in a numpy array structure without copying the data | ||
* @param cvector :: 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 ElementType> | ||
PyObject *wrapWithNDArray(const Kernel::Matrix<ElementType> & cmatrix, const WrapMode mode) | ||
{ | ||
std::pair<size_t,size_t> matrixDims = cmatrix.size(); | ||
npy_intp dims[2] = {matrixDims.first, matrixDims.second}; | ||
int datatype = NDArrayTypeIndex<ElementType>::typenum; | ||
PyObject * ndarray = PyArray_SimpleNewFromData(2, dims, datatype,(void*)&(cmatrix[0][0])); | ||
if( mode == ReadOnly ) | ||
{ | ||
PyArrayObject * np = (PyArrayObject *)ndarray; | ||
np->flags &= ~NPY_WRITEABLE; | ||
} | ||
return ndarray; | ||
} | ||
//----------------------------------------------------------------------- | ||
// Explicit instantiations | ||
//----------------------------------------------------------------------- | ||
#define INSTANTIATE(ElementType) \ | ||
template DLLExport PyObject * wrapWithNDArray<ElementType>(const Kernel::Matrix<ElementType> &, const WrapMode); | ||
|
||
INSTANTIATE(int16_t); | ||
INSTANTIATE(uint16_t); | ||
INSTANTIATE(int32_t); | ||
INSTANTIATE(uint32_t); | ||
INSTANTIATE(int64_t); | ||
INSTANTIATE(uint64_t); | ||
#ifdef __APPLE__ | ||
INSTANTIATE(unsigned long); | ||
#endif | ||
INSTANTIATE(double); | ||
|
||
} | ||
} | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters