Commit 91231681 authored by Hahn, Steven's avatar Hahn, Steven
Browse files

Expose std::pair, Add test, rename to findY.



Signed-off-by: Hahn, Steven's avatarSteven Hahn <hahnse@ornl.gov>
parent 239f890b
......@@ -514,7 +514,10 @@ public:
// Check if this class has an oriented lattice on a sample object
virtual bool hasOrientedLattice() const override;
virtual std::pair<int64_t, int64_t> find(double value) const;
/// Find first index in Y equal to value.
/// @param value Number to find in Y/
/// @return Tuple with the ihistogram and bin indices.
virtual std::pair<int64_t, int64_t> findY(double value) const;
//=====================================================================================
// End IMDWorkspace methods
......
......@@ -1931,7 +1931,7 @@ MantidImage_sptr MatrixWorkspace::getImage(
return image;
}
std::pair<int64_t, int64_t> MatrixWorkspace::find(double value) const {
std::pair<int64_t, int64_t> MatrixWorkspace::findY(double value) const {
std::pair<int64_t, int64_t> out(-1, -1);
const int64_t numHists = static_cast<int64_t>(this->getNumberHistograms());
for (int64_t i = 0; i < numHists; ++i) {
......
......@@ -152,5 +152,25 @@ template <typename ElementType> struct std_set_exporter {
;
}
};
// From https://www.boost.org/doc/libs/1_62_0/libs/python/example/std_pair.cpp
// Converts a std::pair instance to a Python tuple.
template <typename T1, typename T2> struct std_pair_to_tuple {
static PyObject *convert(std::pair<T1, T2> const &p) {
return boost::python::incref(
boost::python::make_tuple(p.first, p.second).ptr());
}
static PyTypeObject const *get_pytype() { return &PyTuple_Type; }
};
// Helper for convenience.
template <typename T1, typename T2> struct std_pair_to_python_converter {
std_pair_to_python_converter() {
boost::python::to_python_converter<std::pair<T1, T2>,
std_pair_to_tuple<T1, T2>,
true // std_pair_to_tuple has get_pytype
>();
}
};
} // namespace PythonInterface
} // namespace Mantid
......@@ -365,7 +365,9 @@ void export_MatrixWorkspace() {
":class:`~mantid.api.MatrixWorkspace.hasMaskedBins` MUST be called "
"first to check if any bins are "
"masked, otherwise an exception will be thrown")
.def("find", &find, (arg("self"), arg("value")), "find index of Y value")
.def("findY", &MatrixWorkspace::findY, (arg("self"), arg("value")),
"Find first index in Y equal to value. Returns tuple with the "
"ihistogram and bin indices.")
// Deprecated
.def("getNumberBins", &getNumberBinsDeprecated, arg("self"),
"Returns size of the Y data array (deprecated, use "
......
......@@ -10,6 +10,7 @@
#include "MantidKernel/Quat.h"
#include "MantidKernel/V3D.h"
using Mantid::PythonInterface::std_pair_to_python_converter;
using Mantid::PythonInterface::std_set_exporter;
using Mantid::PythonInterface::std_vector_exporter;
......@@ -29,4 +30,5 @@ void exportStlContainers() {
// std::set
std_set_exporter<int>::wrap("std_set_int");
std_set_exporter<std::string>::wrap("std_set_str");
std_pair_to_python_converter<int64_t, int64_t>();
}
......@@ -510,7 +510,19 @@ class MatrixWorkspaceTest(unittest.TestCase):
fv = rebin.readF(1)
self.assertAlmostEqual(fv[0], 4.0)
def test_findY(self):
# Check that zero is not present
idx = self._test_ws.findY(0.)
self.assertEquals(idx[0],-1)
self.assertEquals(idx[1],-1)
# Check that 5. is the first element
idx = self._test_ws.findY(5.)
self.assertEquals(idx[0],0)
self.assertEquals(idx[1],0)
# Check that 2. is the next element
idx = self._test_ws.findY(2.)
self.assertEquals(idx[0],0)
self.assertEquals(idx[1],1)
if __name__ == '__main__':
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment