-
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.
Merge pull request #249 from mantidproject/11125_copy_detector_mappin…
…g_algorithm Copy spectra-detector mapping algorithm
- Loading branch information
Showing
9 changed files
with
331 additions
and
8 deletions.
There are no files selected for viewing
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
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
72 changes: 72 additions & 0 deletions
72
Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/CopyDetectorMapping.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,72 @@ | ||
#ifndef MANTID_ALGORITHMS_COPYDETECTORMAPPING_H_ | ||
#define MANTID_ALGORITHMS_COPYDETECTORMAPPING_H_ | ||
|
||
//---------------------------------------------------------------------- | ||
// Includes | ||
//---------------------------------------------------------------------- | ||
#include "MantidAPI/Algorithm.h" | ||
|
||
namespace Mantid { | ||
namespace Algorithms { | ||
|
||
/** | ||
Algorithm to copy spectra-detector mapping from one matrix workspace | ||
to another. | ||
@author Dan Nixon | ||
Copyright © 2010 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge | ||
National Laboratory & European Spallation Source | ||
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://github.com/mantidproject/mantid> | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
class DLLExport CopyDetectorMapping : public API::Algorithm { | ||
public: | ||
/// (Empty) Constructor | ||
CopyDetectorMapping() : API::Algorithm() {} | ||
/// Virtual destructor | ||
virtual ~CopyDetectorMapping() {} | ||
/// Algorithm's name | ||
virtual const std::string name() const { return "CopyDetectorMapping"; } | ||
/// Summary of algorithms purpose | ||
virtual const std::string summary() const { | ||
return "Copies spectra to detector mapping from one Matrix Workspace to another."; | ||
} | ||
|
||
/// Algorithm's version | ||
virtual int version() const { return (1); } | ||
/// Algorithm's category for identification | ||
virtual const std::string category() const { | ||
return "DataHandling"; | ||
} | ||
|
||
/// Input property validation | ||
virtual std::map<std::string, std::string> validateInputs(); | ||
|
||
private: | ||
/// Initialisation code | ||
void init(); | ||
/// Execution code | ||
void exec(); | ||
}; | ||
|
||
} // namespace Algorithms | ||
} // namespace Mantid | ||
|
||
#endif /*MANTID_ALGORITHMS_COPYDETECTORMAPPING_H_*/ |
71 changes: 71 additions & 0 deletions
71
Code/Mantid/Framework/Algorithms/src/CopyDetectorMapping.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,71 @@ | ||
//---------------------------------------------------------------------- | ||
// Includes | ||
//---------------------------------------------------------------------- | ||
#include "MantidAlgorithms/CopyDetectorMapping.h" | ||
#include "MantidAPI/SpectrumDetectorMapping.h" | ||
|
||
namespace Mantid { | ||
namespace Algorithms { | ||
|
||
DECLARE_ALGORITHM(CopyDetectorMapping) | ||
|
||
using namespace Kernel; | ||
using namespace API; | ||
|
||
void CopyDetectorMapping::init() { | ||
declareProperty(new WorkspaceProperty<MatrixWorkspace>("WorkspaceToMatch", "", | ||
Direction::Input)); | ||
|
||
declareProperty(new WorkspaceProperty<MatrixWorkspace>("WorkspaceToRemap", "", | ||
Direction::InOut)); | ||
|
||
declareProperty( | ||
new PropertyWithValue<bool>("IndexBySpectrumNumber", false, | ||
Direction::Input), | ||
"Will use mapping indexed by spectrum number rather than the default of" | ||
"spectrum index (recommended when both workspaces have a vertical axis " | ||
"in spectrum number)."); | ||
} | ||
|
||
void CopyDetectorMapping::exec() { | ||
MatrixWorkspace_const_sptr wsToMatch = getProperty("WorkspaceToMatch"); | ||
MatrixWorkspace_sptr wsToRemap = getProperty("WorkspaceToRemap"); | ||
bool indexBySpecNumber = getProperty("IndexBySpectrumNumber"); | ||
|
||
// Copy detector mapping | ||
SpectrumDetectorMapping detMap(wsToMatch.get(), indexBySpecNumber); | ||
wsToRemap->updateSpectraUsing(detMap); | ||
|
||
setProperty("WorkspaceToRemap", wsToRemap); | ||
} | ||
|
||
std::map<std::string, std::string> CopyDetectorMapping::validateInputs() { | ||
std::map<std::string, std::string> issues; | ||
|
||
MatrixWorkspace_sptr wsToMatch = getProperty("WorkspaceToMatch"); | ||
MatrixWorkspace_sptr wsToRemap = getProperty("WorkspaceToRemap"); | ||
|
||
// Check that the workspaces actually are MatrixWorkspaces | ||
bool validWorkspaces = true; | ||
|
||
if (wsToMatch == NULL) { | ||
issues["WorkspaceToMatch"] = "Must be a MatrixWorkspace"; | ||
validWorkspaces = false; | ||
} | ||
|
||
if (wsToRemap == NULL) { | ||
issues["WorkspaceToRemap"] = "Must be a MatrixWorkspace"; | ||
validWorkspaces = false; | ||
} | ||
|
||
// Check histohram counts match (assuming both are MatrixWorkspaces) | ||
if (validWorkspaces && | ||
wsToMatch->getNumberHistograms() != wsToRemap->getNumberHistograms()) | ||
issues["WorkspaceToRemap"] = | ||
"Number of histograms must match WorkspaceToMatch"; | ||
|
||
return issues; | ||
} | ||
|
||
} // namespace Algorithms | ||
} // namespace Mantid |
87 changes: 87 additions & 0 deletions
87
Code/Mantid/Framework/Algorithms/test/CopyDetectorMappingTest.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,87 @@ | ||
#ifndef COPYDETECTORMAPPINGTEST_H_ | ||
#define COPYDETECTORMAPPINGTEST_H_ | ||
|
||
#include <cxxtest/TestSuite.h> | ||
#include "MantidAlgorithms/CopyDetectorMapping.h" | ||
#include "MantidTestHelpers/WorkspaceCreationHelper.h" | ||
|
||
using namespace Mantid; | ||
using namespace Mantid::API; | ||
using namespace Mantid::Kernel; | ||
|
||
class CopyDetectorMappingTest : public CxxTest::TestSuite | ||
{ | ||
public: | ||
void testInit() | ||
{ | ||
Mantid::Algorithms::CopyDetectorMapping copyMapping; | ||
|
||
TS_ASSERT_THROWS_NOTHING( copyMapping.initialize() ) | ||
TS_ASSERT( copyMapping.isInitialized() ) | ||
} | ||
|
||
void testSimple() | ||
{ | ||
Mantid::Algorithms::CopyDetectorMapping copyMapping; | ||
TS_ASSERT_THROWS_NOTHING( copyMapping.initialize() ) | ||
|
||
auto toMatch = WorkspaceCreationHelper::Create2DWorkspace(10, 10); | ||
|
||
// Set the detector map for a spectra in the to match workspace | ||
std::set<detid_t> detIDs; | ||
detIDs.insert(5); | ||
detIDs.insert(9); | ||
detIDs.insert(6); | ||
detIDs.insert(2); | ||
toMatch->getSpectrum(0)->setDetectorIDs(detIDs); | ||
|
||
// Add workspaces to ADS | ||
AnalysisDataService::Instance().add("to_match", toMatch); | ||
AnalysisDataService::Instance().add("to_remap", WorkspaceCreationHelper::Create2DWorkspace(10, 10)); | ||
|
||
// Run algorithm | ||
TS_ASSERT_THROWS_NOTHING( copyMapping.setPropertyValue("WorkspaceToMatch", "to_match") ); | ||
TS_ASSERT_THROWS_NOTHING( copyMapping.setPropertyValue("WorkspaceToRemap", "to_remap") ); | ||
|
||
TS_ASSERT_THROWS_NOTHING( copyMapping.execute() ); | ||
TS_ASSERT( copyMapping.isExecuted() ); | ||
|
||
// Check the detector map in the to remap workspace matches that of the to match workspace | ||
MatrixWorkspace_const_sptr result; | ||
TS_ASSERT_THROWS_NOTHING( result = boost::dynamic_pointer_cast<MatrixWorkspace> | ||
(AnalysisDataService::Instance().retrieve("to_remap")) ); | ||
std::set<detid_t> resultDetIDs = result->getSpectrum(0)->getDetectorIDs(); | ||
TS_ASSERT( detIDs == resultDetIDs ); | ||
|
||
// Clean up workspace | ||
AnalysisDataService::Instance().remove("to_match"); | ||
AnalysisDataService::Instance().remove("to_remap"); | ||
} | ||
|
||
void testFailWithDifferingSpecSize() | ||
{ | ||
Mantid::Algorithms::CopyDetectorMapping copyMapping; | ||
TS_ASSERT_THROWS_NOTHING( copyMapping.initialize() ) | ||
|
||
// Add workspaces to ADS | ||
AnalysisDataService::Instance().add("to_match", WorkspaceCreationHelper::Create2DWorkspace(10, 10)); | ||
AnalysisDataService::Instance().add("to_remap", WorkspaceCreationHelper::Create2DWorkspace(20, 10)); | ||
|
||
// Run algorithm | ||
TS_ASSERT_THROWS_NOTHING( copyMapping.setPropertyValue("WorkspaceToMatch", "to_match") ); | ||
TS_ASSERT_THROWS_NOTHING( copyMapping.setPropertyValue("WorkspaceToRemap", "to_remap") ); | ||
|
||
auto validationIssues = copyMapping.validateInputs(); | ||
TS_ASSERT_DIFFERS( validationIssues.size(), 0 ); | ||
|
||
TS_ASSERT_THROWS_ANYTHING( copyMapping.execute() ); | ||
TS_ASSERT( !copyMapping.isExecuted() ); | ||
|
||
// Clean up workspace | ||
AnalysisDataService::Instance().remove("to_match"); | ||
AnalysisDataService::Instance().remove("to_remap"); | ||
} | ||
|
||
}; | ||
|
||
#endif /*COPYDETECTORMAPPINGTEST_H_*/ |
Oops, something went wrong.