Commit 733c6426 authored by Hahn, Steven's avatar Hahn, Steven
Browse files

Add option for setting starting indices



Signed-off-by: Hahn, Steven's avatarSteven Hahn <hahnse@ornl.gov>
parent a0bdac1f
......@@ -515,9 +515,11 @@ public:
virtual bool hasOrientedLattice() const override;
/// Find first index in Y equal to value.
/// @param value Number to find in Y/
/// @param value Number to find in Y
/// @param idx starting indices for search
/// @return Tuple with the ihistogram and bin indices.
virtual std::pair<int64_t, int64_t> findY(double value) const;
virtual std::pair<int64_t, int64_t>
findY(double value, const std::pair<int64_t, int64_t> &idx = {0, 0}) const;
//=====================================================================================
// End IMDWorkspace methods
......
......@@ -1931,13 +1931,16 @@ MantidImage_sptr MatrixWorkspace::getImage(
return image;
}
std::pair<int64_t, int64_t> MatrixWorkspace::findY(double value) const {
std::pair<int64_t, int64_t>
MatrixWorkspace::findY(double value,
const std::pair<int64_t, int64_t> &idx) 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) {
for (int64_t i = idx.first; i < numHists; ++i) {
const auto &Y = this->y(i);
// cppcheck-suppress syntaxError
if (auto it = std::find(Y.begin(), Y.end(), value); it != Y.end()) {
if (auto it = std::find(std::next(Y.begin(), idx.second), Y.end(), value);
it != Y.end()) {
out = {i, std::distance(Y.begin(), it)};
break;
}
......
......@@ -2393,6 +2393,22 @@ public:
TS_ASSERT_EQUALS(ws->isGroup(), false);
}
void test_findY() {
boost::shared_ptr<MatrixWorkspace> ws(makeWorkspaceWithDetectors(3, 1));
auto idx = ws->findY(0., {0, 0});
TS_ASSERT_EQUALS(idx.first, -1);
TS_ASSERT_EQUALS(idx.second, -1);
idx = ws->findY(1., {0, 0});
TS_ASSERT_EQUALS(idx.first, 0);
TS_ASSERT_EQUALS(idx.second, 0);
idx = ws->findY(1., {0, 1});
TS_ASSERT_EQUALS(idx.first, 0);
TS_ASSERT_EQUALS(idx.second, 1);
idx = ws->findY(1., {1, 0});
TS_ASSERT_EQUALS(idx.first, 1);
TS_ASSERT_EQUALS(idx.second, 0);
}
private:
WorkspaceTester m_workspace;
WorkspaceTester m_workspaceSans;
......
......@@ -152,25 +152,5 @@ 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
......@@ -29,6 +29,7 @@
#include <boost/python/overloads.hpp>
#include <boost/python/register_ptr_to_python.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/tuple.hpp>
#define PY_ARRAY_UNIQUE_SYMBOL API_ARRAY_API
#define NO_IMPORT_ARRAY
......@@ -287,6 +288,13 @@ object getSignalAtCoord(MatrixWorkspace &self, const NDArray &npCoords,
return object(handle<>(npSignalArray));
}
boost::python::tuple findY(MatrixWorkspace &self, double value, tuple start) {
int64_t first = extract<int64_t>(start[0]);
int64_t second = extract<int64_t>(start[1]);
auto idx = self.findY(value, std::make_pair(first, second));
return make_tuple(idx.first, idx.second);
}
} // namespace
/** Python exports of the Mantid::API::MatrixWorkspace class. */
......@@ -365,9 +373,11 @@ 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("findY", &MatrixWorkspace::findY, (arg("self"), arg("value")),
"Find first index in Y equal to value. Returns tuple with the "
"ihistogram and bin indices.")
.def("findY", &findY,
(arg("self"), arg("value"), arg("start") = make_tuple(0, 0)),
"Find first index in Y equal to value. Start may be specified to "
"begin at a specifc index. Returns tuple with the "
"histogram and bin indices.")
// Deprecated
.def("getNumberBins", &getNumberBinsDeprecated, arg("self"),
"Returns size of the Y data array (deprecated, use "
......
......@@ -10,7 +10,6 @@
#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;
......@@ -30,5 +29,4 @@ 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>();
}
......@@ -519,10 +519,18 @@ class MatrixWorkspaceTest(unittest.TestCase):
idx = self._test_ws.findY(5.)
self.assertEquals(idx[0],0)
self.assertEquals(idx[1],0)
# Check that no other elements are 5
idx = self._test_ws.findY(5., (0, 1))
self.assertEquals(idx[0],-1)
self.assertEquals(idx[1],-1)
# Check that 2. is the next element
idx = self._test_ws.findY(2.)
self.assertEquals(idx[0],0)
self.assertEquals(idx[1],1)
# Check that 2. is the next element
idx = self._test_ws.findY(2., (0, 2))
self.assertEquals(idx[0],0)
self.assertEquals(idx[1],2)
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