Skip to content

Commit

Permalink
Refs #4399. Generalise conversion from numpy with templated converter.
Browse files Browse the repository at this point in the history
Also moved policies so that the correct behaviour can be requested.
  • Loading branch information
martyngigg committed Feb 17, 2012
1 parent d560984 commit d58a260
Show file tree
Hide file tree
Showing 13 changed files with 450 additions and 238 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef MANTID_PYTHONINTERFACE_WORKSPACETONUMPY_H_
#define MANTID_PYTHONINTERFACE_WORKSPACETONUMPY_H_
#ifndef MANTID_PYTHONINTERFACE_CLONEMATRIXWORKSPACE__H_
#define MANTID_PYTHONINTERFACE_CLONEMATRIXWORKSPACE_H_
/*
Copyright © 2011 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
Expand Down Expand Up @@ -33,9 +33,6 @@ namespace Mantid

namespace PythonInterface
{
namespace Numpy
{

//** @name Numpy clones of data*/
///{
/// Create a numpy array from the X values of the given workspace reference
Expand All @@ -48,9 +45,8 @@ namespace Mantid
PyObject *cloneDx(API::MatrixWorkspace &self);
///@}

}
}
}


#endif /* MANTID_PYTHONINTERFACE_WORKSPACETONUMPY_H_ */
#endif /* MANTID_PYTHONINTERFACE_CLONEMATRIXWORKSPACE_H_ */
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef MANTID_PYTHONINTERFACE_CONVERTERS_PYARRAYTYPE_H_
#define MANTID_PYTHONINTERFACE_CONVERTERS_PYARRAYTYPE_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 <boost/python/detail/prefix.hpp>

namespace Mantid
{
namespace PythonInterface
{
namespace Converters
{
//
// It is important that the numpy/arrayobject header
// does not appear in any of our headers as it
// contains some static definitions that cannot be
// allowed to be defined in other translation units

// Numpy array type
DLLExport PyTypeObject * getNDArrayType();
}
}
}



#endif /* PYARRAYTYPE_H_ */
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#ifndef MANTID_PYTHONINTERFACE_VECTORTONDARRAY_H_
#define MANTID_PYTHONINTERFACE_VECTORTONDARRAY_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>
#include <vector>

namespace Mantid
{
namespace PythonInterface
{
namespace Converters
{

//-----------------------------------------------------------------------
// Converter implementation
//-----------------------------------------------------------------------
/**
* Converter that takes a std::vector 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 VectorType, typename ConversionPolicy>
struct VectorToNDArray
{
inline PyObject * operator()(const VectorType & cvector) const
{
typedef typename ConversionPolicy::template apply<VectorType> policy;
return policy::create(cvector);
}
};

//-----------------------------------------------------------------------
// 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 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>
struct apply
{
/**
* Returns a Numpy array that has a copy of the vectors data
* @param cvector
* @return
*/
static PyObject * create(const VectorType & cvector)
{
return Impl::cloneToNDArray<VectorType>(cvector);
}
};
};

}
}
}


#endif /* MANTID_PYTHONINTERFACE_VECTORTONDARRAY_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,8 @@ namespace Mantid
{
namespace Numpy
{
/**@name Array type */
DLLExport PyTypeObject * getNDArrayType();

/** @name Create Numpy arrays */
//@{
/// Create a numpy array wrapper around existing vector. This is only possible for contiguous data
DLLExport PyObject *wrapWithNumpy(const std::vector<double> & data);
/// Create a read-only numpy array wrapper around existing data.
DLLExport PyObject *wrapWithReadOnlyNumpy(const std::vector<double> & data);
/// Create a read-only array wrapper around a double Matrix
DLLExport PyObject *wrapWithNumpy(const Kernel::DblMatrix & data);
/// Create a read-only array wrapper around a double Matrix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
#include "MantidKernel/System.h"
#include "MantidPythonInterface/kernel/NumpyConverters.h"
#include "MantidPythonInterface/kernel/Converters/VectorToNDArray.h"
#include "MantidPythonInterface/kernel/Converters/PyArrayType.h"

#include <boost/type_traits/integral_constant.hpp>
#include <boost/type_traits/is_reference.hpp>
#include <boost/type_traits/remove_reference.hpp>
Expand All @@ -36,54 +38,6 @@ namespace Mantid
{
namespace Policies
{
//-----------------------------------------------------------------------
// Conversion Policies
//-----------------------------------------------------------------------

/**
* WrapReadOnly is a policy for VectorToPython
* 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 Numpy::wrapWithReadOnlyNumpy(cvector);
}
};
};

/**
* WrapReadWrite is a policy for VectorToPython
* 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 Numpy::wrapWithNumpy(cvector);
}
};
};

namespace // anonymous
{
Expand All @@ -96,13 +50,12 @@ namespace Mantid
{
inline PyObject * operator()(const VectorType & cvector) const
{
typedef typename ConversionPolicy::template apply<VectorType> policy;
return policy::create(cvector);
return Converters::VectorToNDArray<VectorType, ConversionPolicy>()(cvector);
}

inline PyTypeObject const* get_pytype() const
{
return Numpy::getNDArrayType();
return Converters::getNDArrayType();
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ set ( EXPORT_FILES
# Files containing additional helper code that are not related to exporting class/functions
set ( SRC_FILES
src/AlgorithmWrapper.cpp
src/WorkspaceToNumpy.cpp
src/CloneMatrixWorkspace.cpp
)

set ( INC_FILES
${HEADER_DIR}/api/AlgorithmWrapper.h
${HEADER_DIR}/api/BinaryOperations.h
${HEADER_DIR}/api/WorkspaceToNumpy.h
${HEADER_DIR}/api/CloneMatrixWorkspace.h
)

set ( PY_FILES
Expand Down

0 comments on commit d58a260

Please sign in to comment.