Skip to content

Commit

Permalink
Refs #4399. Return value policy for Matrix -> numpy array.
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Feb 21, 2012
1 parent 04f78b7 commit 3ae2af4
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 175 deletions.
4 changes: 4 additions & 0 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ namespace Mantid
template<typename T>
class DLLExport Matrix
{
public:
/// Enable users to retrive the element type
typedef T value_type;

private:

size_t nx; ///< Number of rows (x coordinate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
#include "MantidKernel/System.h"
#include "MantidKernel/Matrix.h"
#include "MantidPythonInterface/kernel/Converters/NumpyWrapMode.h"
#include <boost/python/detail/prefix.hpp>

namespace Mantid
Expand All @@ -46,69 +47,68 @@ namespace Mantid
{
inline PyObject * operator()(const Kernel::Matrix<ElementType> & cmatrix) const
{
typedef typename ConversionPolicy::template apply<ElementType> policy;
typedef typename ConversionPolicy::template apply<Kernel::Matrix<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);
}
};
};
// namespace Impl
// {
// /**
// * Helper functions to keep the numpy arrayobject header out
// * the header file
// */
// /// Wrap a numpy array around existing data
// template<typename Co>
// PyObject *wrapWithNDArray(const Kernel::Matrix<ElementType>&, const NumpyWrapMode);
// }

/**
* 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);
}
};
};
// /**
// * 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, 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, ReadWrite);
// }
// };
// };

}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#ifndef MANTID_PYTHONINTERFACE_NUMPYWRAPMODE_H_
#define MANTID_PYTHONINTERFACE_NUMPYWRAPMODE_H_
/**
Copyright &copy; 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 <boost/python/detail/prefix.hpp>

namespace Mantid
{
namespace PythonInterface
{
namespace Converters
{
/// Enum defining wrapping type for conversion to numpy
enum NumpyWrapMode { ReadOnly, ReadWrite };

namespace Impl
{
// Forward declare a conversion function. This should be specialized for each
// container type that is to be wrapped
template <typename ContainerType> PyObject * wrapWithNDArray(const ContainerType &, const NumpyWrapMode);
}

/**
* 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 ContainerType>
struct apply
{
/**
* Returns a read-only Numpy array wrapped around an existing vector
* @param cdata ::
* @return
*/
static PyObject * create(const ContainerType & cdata)
{
return Impl::wrapWithNDArray(cdata, ReadOnly);
}
};
};

/**
* WrapReadWrite is a policy for VectorToNDArray
* to wrap the vector in a read-write numpy array
* that looks at the original data. No copy is performed
*/
struct WrapReadWrite
{
template<typename ContainerType>
struct apply
{
/**
* Returns a read-write Numpy array wrapped around an existing vector
* @param cvector
* @return
*/
static PyObject * create(const ContainerType & cdata)
{
return Impl::wrapWithNDArray(cdata, ReadWrite);
}
};
};

}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
#include "MantidKernel/System.h"
#include <boost/python/detail/prefix.hpp>
#include "MantidPythonInterface/kernel/Converters/NumpyWrapMode.h"
#include <vector>

namespace Mantid
Expand All @@ -31,7 +31,6 @@ namespace Mantid
{
namespace Converters
{

//-----------------------------------------------------------------------
// Converter implementation
//-----------------------------------------------------------------------
Expand All @@ -42,12 +41,14 @@ namespace Mantid
* The type of conversion is specified by another struct/class that
* contains a static member create.
*/
template<typename VectorType, typename ConversionPolicy>
template<typename ElementType, typename ConversionPolicy>
struct VectorToNDArray
{
inline PyObject * operator()(const VectorType & cvector) const
inline PyObject * operator()(const std::vector<ElementType> & cvector) const
{
typedef typename ConversionPolicy::template apply<VectorType> policy;
// Round about way of calling the wrapNDArray template function that is defined
// in the cpp file
typedef typename ConversionPolicy::template apply<std::vector<ElementType> > policy;
return policy::create(cvector);
}
};
Expand All @@ -57,83 +58,33 @@ namespace Mantid
//-----------------------------------------------------------------------
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 VectorType>
PyObject *wrapWithNDArray(const VectorType &, const WrapMode);
/// Clone the data into a new array
template<typename VectorType>
PyObject *cloneToNDArray(const VectorType &);

}

/**
* 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 VectorType>
struct apply
{
/**
* Returns a read-only Numpy array wrapped around an existing vector
* @param cvector
* @return
*/
static PyObject * create(const VectorType & cvector)
{
return Impl::wrapWithNDArray(cvector, Impl::ReadOnly);
}
};
};

/**
* WrapReadWrite is a policy for VectorToNDArray
* to wrap the vector in a read-write numpy array
* that looks at the original data. No copy is performed
*/
struct WrapReadWrite
{
template<typename VectorType>
struct apply
{
/**
* Returns a read-write Numpy array wrapped around an existing vector
* @param cvector
* @return
*/
static PyObject * create(const VectorType & cvector)
{
return Impl::wrapWithNDArray(cvector, Impl::ReadWrite);
}
};
};

/**
* Clone is a policy for VectorToNDArray
* to wrap the vector in a read-write numpy array
* that looks at the original data. No copy is performed
*/
struct Clone
{
template<typename VectorType>
template<typename ContainerType>
struct apply
{
/**
* Returns a Numpy array that has a copy of the vectors data
* @param cvector
* @return
*/
static PyObject * create(const VectorType & cvector)
static PyObject * create(const ContainerType & cvector)
{
return Impl::cloneToNDArray<VectorType>(cvector);
return Impl::cloneToNDArray<ContainerType>(cvector);
}
};
};
Expand Down

0 comments on commit 3ae2af4

Please sign in to comment.