-
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->V3D converter.
- Loading branch information
1 parent
3dccec9
commit 676c2c0
Showing
6 changed files
with
143 additions
and
61 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...tid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Converters/PyObjectToV3D.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_PYOBJECTTOV3D_H_ | ||
#define MANTID_PYTHONINERFACE_PYOBJECTTOV3D_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/V3D.h" | ||
#include <boost/python/object.hpp> | ||
|
||
namespace Mantid | ||
{ | ||
namespace PythonInterface | ||
{ | ||
namespace Converters | ||
{ | ||
/** | ||
* Takes a Python object and if it supports | ||
* indexing and is of length 3 then it will | ||
* attempt to convert a Kernel::V3D object from | ||
* it | ||
*/ | ||
struct DLLExport PyObjectToV3D | ||
{ | ||
PyObjectToV3D(const boost::python::object & p); | ||
/// Produces a V3D object from the given PyObject | ||
Kernel::V3D operator()(); | ||
private: | ||
/// A reference to the object | ||
const boost::python::object & m_obj; | ||
/// Is the object a wrapped instance of V3D | ||
bool m_alreadyV3D; | ||
}; | ||
} | ||
|
||
} | ||
} | ||
|
||
#endif /* MANTID_PYTHONINERFACE_PYOBJECTTOV3D_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
82 changes: 82 additions & 0 deletions
82
Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Converters/PyObjectToV3D.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,82 @@ | ||
//---------------------------------------------------------------------------- | ||
// Includes | ||
//---------------------------------------------------------------------------- | ||
#include "MantidPythonInterface/kernel/Converters/PyObjectToV3D.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. | ||
*/ | ||
PyObjectToV3D::PyObjectToV3D(const boost::python::object & p) | ||
: m_obj(p), m_alreadyV3D(false) | ||
{ | ||
// Is it an already wrapped V3D ? | ||
boost::python::extract<Kernel::V3D> converter(p); | ||
if( converter.check() ) | ||
{ | ||
m_alreadyV3D = true; | ||
return; | ||
} | ||
// Is it a sequence | ||
try | ||
{ | ||
const size_t length = boost::python::len(p); | ||
if( length != 3 ) | ||
{ | ||
throw std::invalid_argument("Incorrect length for conversion to V3D"); | ||
} | ||
// Can we index the object | ||
p.attr("__getitem__")(0); | ||
} | ||
catch(boost::python::error_already_set&) | ||
{ | ||
throw std::invalid_argument(std::string("Cannot convert object to V3D. Expected a python sequence 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::V3D PyObjectToV3D::operator ()() | ||
{ | ||
using namespace boost::python; | ||
if(m_alreadyV3D) | ||
{ | ||
return extract<Kernel::V3D>(m_obj)(); | ||
} | ||
// Numpy arrays need to be forced to a double | ||
// as extract cannot convert from a int64->double | ||
boost::python::object obj = m_obj; | ||
if( PyArray_Check(obj.ptr()) ) | ||
{ | ||
obj = boost::python::numeric::array(obj).astype('d'); | ||
} | ||
// Must be a sequence | ||
return Kernel::V3D(extract<double>(obj[0])(), | ||
extract<double>(obj[1])(), | ||
extract<double>(obj[2])()); | ||
} | ||
|
||
} | ||
} | ||
} |
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