-
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. PyObject->Matrix converter.
Removes the last hacky conversion function.
- Loading branch information
1 parent
331f38c
commit b4ffba0
Showing
8 changed files
with
149 additions
and
121 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
.../Framework/PythonInterface/inc/MantidPythonInterface/kernel/Converters/PyObjectToMatrix.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,56 @@ | ||
#ifndef MANTID_PYTHONINERFACE_PYOBJECTTOMATRIX_H_ | ||
#define MANTID_PYTHONINERFACE_PYOBJECTTOMATRIX_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/object.hpp> | ||
|
||
namespace Mantid | ||
{ | ||
namespace PythonInterface | ||
{ | ||
namespace Converters | ||
{ | ||
/** | ||
* Takes a Python object and if it supports | ||
* indexing and is two dimensional it attempts to | ||
* convert it to a Kernel::Matrix object. Note, this | ||
* currently only suuports Matrix<double> | ||
*/ | ||
struct DLLExport PyObjectToMatrix | ||
{ | ||
PyObjectToMatrix(const boost::python::object & p); | ||
/// Produces a V3D object from the given PyObject | ||
Kernel::Matrix<double> operator()(); | ||
private: | ||
/// A reference to the object | ||
const boost::python::object & m_obj; | ||
/// Is the object a wrapped instance of Matrix<double> | ||
bool m_alreadyMatrix; | ||
}; | ||
} | ||
|
||
} | ||
} | ||
|
||
#endif /* MANTID_PYTHONINERFACE_PYOBJECTTOMATRIX_H_ */ |
46 changes: 0 additions & 46 deletions
46
Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/NumpyConverters.h
This file was deleted.
Oops, something went wrong.
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
81 changes: 81 additions & 0 deletions
81
Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Converters/PyObjectToMatrix.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,81 @@ | ||
//---------------------------------------------------------------------------- | ||
// Includes | ||
//---------------------------------------------------------------------------- | ||
#include "MantidPythonInterface/kernel/Converters/PyObjectToMatrix.h" | ||
#include <boost/python/extract.hpp> | ||
#include <boost/python/numeric.hpp> | ||
|
||
// 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 NO_IMPORT_ARRAY | ||
#include <numpy/arrayobject.h> | ||
|
||
namespace Mantid | ||
{ | ||
namespace PythonInterface | ||
{ | ||
namespace Converters | ||
{ | ||
/** | ||
* Construct the converter object with the given Python object | ||
* @param p :: A boost::python object that should support | ||
* the __getitem__ and __len__ protocol or be a wrapped V3D object. | ||
* Throws std::invalid_argument if not | ||
* if that is not the case. | ||
*/ | ||
PyObjectToMatrix::PyObjectToMatrix(const boost::python::object & p) | ||
: m_obj(p), m_alreadyMatrix(false) | ||
{ | ||
// Is it an already wrapped V3D ? | ||
boost::python::extract<Kernel::Matrix<double> > converter(p); | ||
if( converter.check() ) | ||
{ | ||
m_alreadyMatrix = true; | ||
return; | ||
} | ||
// Is it a 2D numpy array | ||
if( !PyArray_Check(p.ptr()) ) | ||
{ | ||
throw std::invalid_argument(std::string("Cannot convert object to Matrix. Expected a numpy array found ") | ||
+ p.ptr()->ob_type->tp_name); | ||
} | ||
} | ||
|
||
/** | ||
* Returns a V3D object from the Python object given | ||
* to the converter | ||
* @returns A newly constructed V3D object converted | ||
* from the PyObject. | ||
*/ | ||
Kernel::Matrix<double> PyObjectToMatrix::operator ()() | ||
{ | ||
using namespace boost::python; | ||
if(m_alreadyMatrix) | ||
{ | ||
return extract<Kernel::Matrix<double> >(m_obj)(); | ||
} | ||
numeric::array numarray = numeric::array(m_obj); | ||
numarray = (boost::python::numeric::array) numarray.astype('d'); // Force the array to be of double type (in case it was int) | ||
boost::python::tuple shape( numarray.attr("shape") ); | ||
if( boost::python::len(shape) != 2 ) | ||
{ | ||
std::ostringstream msg; | ||
msg << "Error in conversion to Matrix. Expected an array with 2 dimensions but was given array with " | ||
<< boost::python::len(shape) << " dimensions."; | ||
throw std::invalid_argument(msg.str()); | ||
} | ||
size_t nx = boost::python::extract<size_t>(shape[0]); | ||
size_t ny = boost::python::extract<size_t>(shape[1]); | ||
Kernel::Matrix<double> matrix(nx,ny); | ||
for( size_t i = 0; i < nx; i++ ) | ||
{ | ||
for( size_t j = 0; j < ny; j++ ) | ||
{ | ||
matrix[i][j] = extract<double>( numarray[boost::python::make_tuple( i, j )]); | ||
} | ||
} | ||
return matrix; | ||
} | ||
} | ||
} | ||
} |
63 changes: 0 additions & 63 deletions
63
Code/Mantid/Framework/PythonInterface/mantid/kernel/src/NumpyConverters.cpp
This file was deleted.
Oops, something went wrong.