Skip to content

Commit

Permalink
refs #5716. Implemented in API and used in MergeRuns
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Aug 13, 2012
1 parent 981dbfa commit cc62b6e
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 34 deletions.
3 changes: 2 additions & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/WorkspaceGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ class MANTID_API_DLL WorkspaceGroup : public Workspace
bool areNamesSimilar() const;
/// Posts a notification informing the ADS observers that group was modified
void updated() const;
/// Inidicates that the workspace group can be treated as multiperiod.
bool isMultiperiod() const;


private:
/// Private, unimplemented copy constructor
WorkspaceGroup(const WorkspaceGroup& ref);
Expand Down
46 changes: 46 additions & 0 deletions Code/Mantid/Framework/API/src/WorkspaceGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/IPropertyManager.h"
Expand Down Expand Up @@ -262,6 +263,51 @@ void WorkspaceGroup::updated() const
}
}

//------------------------------------------------------------------------------
/**
Determine in the WorkspaceGroup is multiperiod.
* @return True if the WorkspaceGroup instance is multiperiod.
*/
bool WorkspaceGroup::isMultiperiod() const
{
if(m_workspaces.size() < 1)
{
g_log.debug("Not a multiperiod-group with < 1 nested workspace.");
return false;
}
std::vector<Workspace_sptr>::const_iterator iterator = m_workspaces.begin();
// Loop through all inner workspaces, checking each one in turn.
while(iterator != m_workspaces.end())
{
if(MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>(*iterator))
{
try
{
Kernel::Property* nPeriodsProp = ws->run().getLogData("nperiods");
int num = -1;
Kernel::Strings::convert(nPeriodsProp->value(), num);
if(num < 1)
{
g_log.debug("Not a multiperiod-group with nperiods log < 1.");
return false;
}
}
catch(Kernel::Exception::NotFoundError&)
{
g_log.debug("Not a multiperiod-group without nperiods log on all nested workspaces.");
return false;
}
}
else
{
g_log.debug("Not a multiperiod-group unless all inner workspaces are Matrix Workspaces.");
return false;
}
++iterator;
}
return true;
}

} // namespace API
} // namespace Mantid

Expand Down
65 changes: 65 additions & 0 deletions Code/Mantid/Framework/API/test/WorkspaceGroupTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <cxxtest/TestSuite.h>
#include <iomanip>
#include <iostream>
#include <gmock/gmock.h>
#include <gtest/gtest.h>

using namespace Mantid;
using namespace Mantid::API;
Expand All @@ -36,6 +38,30 @@ class WorkspaceGroupTest_WorkspaceGroupObserver

class WorkspaceGroupTest : public CxxTest::TestSuite
{
private:

/// Helper method to add an 'nperiods' log value to each workspace in a group.
void add_periods_logs(WorkspaceGroup_sptr ws, int nperiods = -1)
{
for(size_t i = 0; i < ws->size(); ++i)
{
MatrixWorkspace_sptr currentWS = boost::dynamic_pointer_cast<MatrixWorkspace>(ws->getItem(i));

PropertyWithValue<int>* nperiodsProp = new PropertyWithValue<int>("nperiods", nperiods);
currentWS->mutableRun().addLogData(nperiodsProp);
}
}

// Helper type, representing some concrete workspace type.
class MockWorkspace : public Mantid::API::Workspace
{
MOCK_CONST_METHOD0(id, const std::string());
MOCK_CONST_METHOD0(name, const std::string());
MOCK_CONST_METHOD0(threadSafe, bool());
MOCK_CONST_METHOD0(toString, std::string());
MOCK_CONST_METHOD0(getMemorySize, size_t());
};

public:

/// Make a simple group
Expand Down Expand Up @@ -201,6 +227,45 @@ class WorkspaceGroupTest : public CxxTest::TestSuite
AnalysisDataService::Instance().clear();
}

void test_not_multiperiod_with_less_than_one_element()
{
WorkspaceGroup group;
TSM_ASSERT("Cannot be multiperiod without entries", !group.isMultiperiod());
}

void test_not_multiperiod_without_matrix_workspaces()
{
Workspace_sptr a = boost::make_shared<MockWorkspace>();
WorkspaceGroup group;
group.addWorkspace(a);
TSM_ASSERT("Cannot be multiperiod unless MatrixWorkspaces are used as elements.", !group.isMultiperiod());
}

void test_not_multiperiod_if_missing_nperiods_log()
{
Workspace_sptr a = boost::make_shared<WorkspaceTester>(); // workspace has no nperiods entry.
WorkspaceGroup group;
group.addWorkspace(a);
TSM_ASSERT("Cannot be multiperiod without nperiods log.", !group.isMultiperiod());
}

void test_not_multiperiod_if_nperiods_log_less_than_one()
{
Workspace_sptr a = boost::make_shared<WorkspaceTester>();
WorkspaceGroup_sptr group = boost::make_shared<WorkspaceGroup>();
group->addWorkspace(a);
add_periods_logs(group, 0); // nperiods set to 0.
TSM_ASSERT("Cannot be multiperiod without nperiods log.", !group->isMultiperiod());
}

void test_positive_identification_of_multiperiod_data()
{
Workspace_sptr a = boost::make_shared<WorkspaceTester>();
WorkspaceGroup_sptr group = boost::make_shared<WorkspaceGroup>();
group->addWorkspace(a);
add_periods_logs(group, 1);
TS_ASSERT(group->isMultiperiod());
}

};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ class DLLExport MergeRuns : public API::Algorithm
API::MatrixWorkspace_sptr rebinInput(const API::MatrixWorkspace_sptr& workspace, const std::vector<double>& params);
std::string createFormattedInputWorkspaceNames(const size_t& periodIndex) const;
void validateMultiPeriodGroupInputs(const size_t& nInputWorkspaces) const;
bool isMultiPeriodGroup(boost::shared_ptr<const Mantid::API::WorkspaceGroup> inputGroup) const;
/// Progress reporting
API::Progress* m_progress;

Expand Down
33 changes: 1 addition & 32 deletions Code/Mantid/Framework/Algorithms/src/MergeRuns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,37 +732,6 @@ void MergeRuns::validateMultiPeriodGroupInputs(const size_t& nInputWorkspaces) c
}
}

/**
Determine if the group appears to be a multiperiod group workspace.
Checks that all nested workspaces have a nperiods log and a current_period log.
@ return True only if it is a multiperiod group workspace.
*/
bool MergeRuns::isMultiPeriodGroup(WorkspaceGroup_const_sptr inputGroup) const
{
bool b_isMultiPeriod = false;
for(size_t i = 0; i < inputGroup->size(); ++i)
{
auto item = boost::dynamic_pointer_cast<MatrixWorkspace>(inputGroup->getItem(i));
try
{
Property* nPeriodsProperty = item->run().getLogData("nperiods");
int nPeriods = atoi(nPeriodsProperty->value().c_str());
if(nPeriods > 1)
{
b_isMultiPeriod = true;
}
else
{
return false;
}
}
catch(Exception::NotFoundError &)
{
}
}
return b_isMultiPeriod;
}

/** Check the input workspace properties for groups.
*
* Overriden from base Algorithm class.
Expand Down Expand Up @@ -793,7 +762,7 @@ bool MergeRuns::checkGroups()
WorkspaceGroup_sptr inputGroup = boost::dynamic_pointer_cast<WorkspaceGroup>(ws);
if(inputGroup)
{
if(isMultiPeriodGroup(inputGroup))
if(inputGroup->isMultiperiod())
{
m_multiPeriodGroups.push_back(inputGroup);
}
Expand Down

0 comments on commit cc62b6e

Please sign in to comment.