Skip to content

Commit

Permalink
Minor behavior changes and added docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfpeterson committed Feb 13, 2023
1 parent 9a1ecef commit 4e9069b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
8 changes: 6 additions & 2 deletions Framework/Kernel/inc/MantidKernel/TimeSplitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
namespace Mantid {
namespace Kernel {

/** TimeSplitter : TODO: DESCRIPTION
/**
* TimeSplitter is an object that contains a mapping of time regions [inclusive, exclusive) that map to output workspace
* indices. No time can be mapped to two output workspace indices and all time from the beginning to the end is
* accounted for. A negative workspace index indicates that the data in that region should be ignored. This object
* converts all negative indices to -1.
*/
class MANTID_KERNEL_DLL TimeSplitter {
public:
Expand All @@ -29,7 +33,7 @@ class MANTID_KERNEL_DLL TimeSplitter {

private:
void clearAndReplace(const Types::Core::DateAndTime &start, const Types::Core::DateAndTime &stop, const int value);
void debugPrint() const;
std::string debugPrint() const;
std::map<Types::Core::DateAndTime, int> m_roi_map;
};

Expand Down
47 changes: 29 additions & 18 deletions Framework/Kernel/src/TimeSplitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ TimeSplitter::TimeSplitter(const Types::Core::DateAndTime &start, const Types::C
clearAndReplace(start, stop, DEFAULT_VALUE);
}

void TimeSplitter::debugPrint() const {
std::string TimeSplitter::debugPrint() const {
std::stringstream msg;
for (const auto iter : m_roi_map)
std::cout << iter.second << "|" << iter.first << "\n";
msg << iter.second << "|" << iter.first << "\n";
return msg.str();
}

void TimeSplitter::addROI(const Types::Core::DateAndTime &start, const Types::Core::DateAndTime &stop,
Expand Down Expand Up @@ -156,28 +158,37 @@ std::vector<int> TimeSplitter::outputWorkspaceIndices() const {

/**
* Returns a Mantid::Kernel::TimeROI for the requested workspace index.
* This will return an empty TimeROI if the workspace index is <0 (ignore value)
* or does not exist in the TimeSplitter.
* This will raise an exception if the workspace index does not exist in the TimeSplitter.
*/
TimeROI TimeSplitter::getTimeROI(const int workspaceIndex) {
// convert things less than -1 to -1
const int effectiveIndex = std::max<int>(workspaceIndex, -1);

TimeROI output;
// only build a result if this is not the IGNORE_VALUE
if (workspaceIndex > IGNORE_VALUE) {
using map_value_type = std::map<DateAndTime, int>::value_type;
auto indexFinder = [workspaceIndex](const map_value_type &value) { return value.second == workspaceIndex; };
// find the first place this workspace index exists
auto iter = std::find_if(m_roi_map.begin(), m_roi_map.end(), indexFinder);
// add the ROI found then loop until we reach the end
while (iter != m_roi_map.end()) {
// add the ROI
const auto startTime = iter->first;
iter++;
using map_value_type = std::map<DateAndTime, int>::value_type;
auto indexFinder = [effectiveIndex](const map_value_type &value) { return value.second == effectiveIndex; };
// find the first place this workspace index exists
auto iter = std::find_if(m_roi_map.begin(), m_roi_map.end(), indexFinder);
// add the ROI found then loop until we reach the end
while (iter != m_roi_map.end()) {
// add the ROI
const auto startTime = iter->first;
iter++;
// if the next iterator is the end there is nothing to add
if (iter != m_roi_map.end()) {
const auto stopTime = iter->first;
output.addROI(startTime, stopTime);

// look for the next place the workspace index occurs
iter = std::find_if(iter, m_roi_map.end(), indexFinder);
}

// look for the next place the workspace index occurs
iter = std::find_if(iter, m_roi_map.end(), indexFinder);
}

// error check that something is there
// ignore index being empty is ok
if ((workspaceIndex >= 0) && (output.empty())) {
std::stringstream msg;
msg << "No regions exist for workspace index " << workspaceIndex;
}

return output;
Expand Down
4 changes: 3 additions & 1 deletion Framework/Kernel/test/TimeSplitterTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ class TimeSplitterTest : public CxxTest::TestSuite {

// add the same output index, but with a gap from the previous
splitter.addROI(FOUR, FIVE, 1);
TS_ASSERT(splitter.getTimeROI(-1).empty());
roi = splitter.getTimeROI(-2); // intentionally trying a "big" negative for ignore filter
TS_ASSERT(!roi.empty());
TS_ASSERT_EQUALS(roi.numBoundaries(), 2);
TS_ASSERT(splitter.getTimeROI(0).empty());
TS_ASSERT(splitter.getTimeROI(0).empty());
roi = splitter.getTimeROI(1);
Expand Down

0 comments on commit 4e9069b

Please # to comment.