Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d75ec5d
commit 10bf85d
Showing
5 changed files
with
275 additions
and
0 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
54 changes: 54 additions & 0 deletions
54
Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/CropWorkspaceByMask.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,54 @@ | ||
#ifndef MANTID_DATAHANDLING_CROPWORKSPACEBYMASK_H_ | ||
#define MANTID_DATAHANDLING_CROPWORKSPACEBYMASK_H_ | ||
|
||
#include "MantidKernel/System.h" | ||
#include "MantidAPI/Algorithm.h" | ||
|
||
namespace Mantid { | ||
namespace DataHandling { | ||
|
||
/** CropWorkspaceByMask crops a MatrixWorkspace by a mask. It removes all masked | ||
spectra. | ||
Copyright © 2015 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 CropWorkspaceByMask : public API::Algorithm { | ||
public: | ||
CropWorkspaceByMask(); | ||
virtual ~CropWorkspaceByMask(); | ||
|
||
virtual const std::string name() const; | ||
virtual int version() const; | ||
virtual const std::string category() const; | ||
virtual const std::string summary() const; | ||
|
||
private: | ||
void init(); | ||
void exec(); | ||
/// Fill in a vector with spectra indices to be extracted. | ||
void makeIndexList(std::vector<size_t>& indices, const API::MatrixWorkspace* maskedWorkspace); | ||
}; | ||
|
||
} // namespace DataHandling | ||
} // namespace Mantid | ||
|
||
#endif /* MANTID_DATAHANDLING_CROPWORKSPACEBYMASK_H_ */ |
113 changes: 113 additions & 0 deletions
113
Code/Mantid/Framework/DataHandling/src/CropWorkspaceByMask.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,113 @@ | ||
#include "MantidDataHandling/CropWorkspaceByMask.h" | ||
#include "MantidDataObjects/MaskWorkspace.h" | ||
#include "MantidAPI/WorkspaceFactory.h" | ||
|
||
namespace Mantid { | ||
namespace DataHandling { | ||
|
||
using namespace Kernel; | ||
using namespace API; | ||
|
||
// Register the algorithm into the AlgorithmFactory | ||
DECLARE_ALGORITHM(CropWorkspaceByMask) | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/// Constructor | ||
CropWorkspaceByMask::CropWorkspaceByMask() {} | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/// Destructor | ||
CropWorkspaceByMask::~CropWorkspaceByMask() {} | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
|
||
/// Algorithms name for identification. @see Algorithm::name | ||
const std::string CropWorkspaceByMask::name() const { | ||
return "CropWorkspaceByMask"; | ||
} | ||
|
||
/// Algorithm's version for identification. @see Algorithm::version | ||
int CropWorkspaceByMask::version() const { return 1; }; | ||
|
||
/// Algorithm's category for identification. @see Algorithm::category | ||
const std::string CropWorkspaceByMask::category() const { | ||
return "Transforms\\Splitting"; | ||
} | ||
|
||
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary | ||
const std::string CropWorkspaceByMask::summary() const { | ||
return "Extracts unmasked spectra from a workspace and places them in a new " | ||
"workspace."; | ||
} | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/// Initialize the algorithm's properties. | ||
void CropWorkspaceByMask::init() { | ||
declareProperty( | ||
new WorkspaceProperty<>("InputWorkspace", "", Direction::Input), | ||
"An input workspace."); | ||
declareProperty(new WorkspaceProperty<>("MaskedWorkspace", "", | ||
Direction::Input, | ||
PropertyMode::Optional), | ||
"If given but not as a MaskWorkspace, the masking from " | ||
"this workspace will be used. If given as a " | ||
"MaskWorkspace, the masking is read from its Y values."); | ||
declareProperty( | ||
new WorkspaceProperty<>("OutputWorkspace", "", Direction::Output), | ||
"An output workspace."); | ||
} | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/// Execute the algorithm. | ||
void CropWorkspaceByMask::exec() { | ||
MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace"); | ||
MatrixWorkspace_sptr maskedWorkspace = getProperty("MaskedWorkspace"); | ||
|
||
if ( !maskedWorkspace ) maskedWorkspace = inputWorkspace; | ||
|
||
std::vector<size_t> indices; | ||
makeIndexList(indices, maskedWorkspace.get()); | ||
|
||
// Number of spectra in the cropped workspace. | ||
size_t nSpectra = indices.size(); | ||
// Number of bins/data points in the cropped workspace. | ||
size_t nBins = inputWorkspace->blocksize(); | ||
size_t histogram = inputWorkspace->isHistogramData()? 1 : 0; | ||
|
||
// Create the output workspace | ||
MatrixWorkspace_sptr outputWorkspace = WorkspaceFactory::Instance().create( | ||
inputWorkspace, nSpectra, nBins, nBins - histogram); | ||
|
||
} | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/// Fill in a vector with spectra indices to be extracted. | ||
/// @param indices :: A reference to a vector to fill with the indices. | ||
/// @param maskedWorkspace :: A workspace with masking information. | ||
void CropWorkspaceByMask::makeIndexList( | ||
std::vector<size_t> &indices, const API::MatrixWorkspace *maskedWorkspace) { | ||
auto mask = dynamic_cast<const DataObjects::MaskWorkspace *>(maskedWorkspace); | ||
if (mask) { | ||
for (size_t i = 0; i < mask->getNumberHistograms(); ++i) { | ||
if (mask->readY(i)[0] == 0.0) { | ||
indices.push_back(i); | ||
} | ||
} | ||
} else { | ||
for (size_t i = 0; i < maskedWorkspace->getNumberHistograms(); ++i) { | ||
Geometry::IDetector_const_sptr det; | ||
try { | ||
det = maskedWorkspace->getDetector(i); | ||
} catch (Exception::NotFoundError &) { | ||
continue; | ||
} | ||
|
||
if (det->isMasked()) { | ||
indices.push_back(i); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} // namespace DataHandling | ||
} // namespace Mantid |
61 changes: 61 additions & 0 deletions
61
Code/Mantid/Framework/DataHandling/test/CropWorkspaceByMaskTest.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,61 @@ | ||
#ifndef MANTID_DATAHANDLING_CROPWORKSPACEBYMASKTEST_H_ | ||
#define MANTID_DATAHANDLING_CROPWORKSPACEBYMASKTEST_H_ | ||
|
||
#include <cxxtest/TestSuite.h> | ||
|
||
#include "MantidDataHandling/CropWorkspaceByMask.h" | ||
|
||
using Mantid::DataHandling::CropWorkspaceByMask; | ||
using namespace Mantid::API; | ||
|
||
class CropWorkspaceByMaskTest : public CxxTest::TestSuite | ||
{ | ||
public: | ||
// This pair of boilerplate methods prevent the suite being created statically | ||
// This means the constructor isn't called when running other tests | ||
static CropWorkspaceByMaskTest *createSuite() { return new CropWorkspaceByMaskTest(); } | ||
static void destroySuite( CropWorkspaceByMaskTest *suite ) { delete suite; } | ||
|
||
|
||
void test_Init() | ||
{ | ||
CropWorkspaceByMask alg; | ||
TS_ASSERT_THROWS_NOTHING( alg.initialize() ) | ||
TS_ASSERT( alg.isInitialized() ) | ||
} | ||
|
||
void test_exec() | ||
{ | ||
// Name of the output workspace. | ||
std::string outWSName("CropWorkspaceByMaskTest_OutputWS"); | ||
|
||
CropWorkspaceByMask alg; | ||
TS_ASSERT_THROWS_NOTHING( alg.initialize() ) | ||
TS_ASSERT( alg.isInitialized() ) | ||
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("REPLACE_PROPERTY_NAME_HERE!!!!", "value") ); | ||
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName) ); | ||
TS_ASSERT_THROWS_NOTHING( alg.execute(); ); | ||
TS_ASSERT( alg.isExecuted() ); | ||
|
||
// Retrieve the workspace from data service. TODO: Change to your desired type | ||
Workspace_sptr ws; | ||
TS_ASSERT_THROWS_NOTHING( ws = AnalysisDataService::Instance().retrieveWS<Workspace>(outWSName) ); | ||
TS_ASSERT(ws); | ||
if (!ws) return; | ||
|
||
// TODO: Check the results | ||
|
||
// Remove workspace from the data service. | ||
AnalysisDataService::Instance().remove(outWSName); | ||
} | ||
|
||
void test_Something() | ||
{ | ||
TSM_ASSERT( "You forgot to write a test!", 0); | ||
} | ||
|
||
|
||
}; | ||
|
||
|
||
#endif /* MANTID_DATAHANDLING_CROPWORKSPACEBYMASKTEST_H_ */ |
44 changes: 44 additions & 0 deletions
44
Code/Mantid/docs/source/algorithms/CropWorkspaceByMask-v1.rst
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,44 @@ | ||
|
||
.. algorithm:: | ||
|
||
.. summary:: | ||
|
||
.. alias:: | ||
|
||
.. properties:: | ||
|
||
Description | ||
----------- | ||
|
||
TODO: Enter a full rst-markup description of your algorithm here. | ||
|
||
|
||
Usage | ||
----- | ||
.. Try not to use files in your examples, | ||
but if you cannot avoid it then the (small) files must be added to | ||
autotestdata\UsageData and the following tag unindented | ||
.. include:: ../usagedata-note.txt | ||
**Example - CropWorkspaceByMask** | ||
|
||
.. testcode:: CropWorkspaceByMaskExample | ||
|
||
# Create a host workspace | ||
ws = CreateWorkspace(DataX=range(0,3), DataY=(0,2)) | ||
or | ||
ws = CreateSampleWorkspace() | ||
|
||
wsOut = CropWorkspaceByMask() | ||
|
||
# Print the result | ||
print "The output workspace has %i spectra" % wsOut.getNumberHistograms() | ||
|
||
Output: | ||
|
||
.. testoutput:: CropWorkspaceByMaskExample | ||
|
||
The output workspace has ?? spectra | ||
|
||
.. categories:: | ||
|