Skip to content

Commit

Permalink
Refs #4081 setMDusingMask algo.
Browse files Browse the repository at this point in the history
  • Loading branch information
Janik Zikovsky committed Nov 10, 2011
1 parent d410f9f commit 1698800
Show file tree
Hide file tree
Showing 4 changed files with 329 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/MDAlgorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set ( SRC_FILES
src/PowerMD.cpp
src/QuadEnBackground.cpp
src/RunParam.cpp
src/SetMDUsingMask.cpp
src/SimulateMDD.cpp
src/SimulateResolution.cpp
src/TobyFitSimulate.cpp
Expand Down Expand Up @@ -68,6 +69,7 @@ set ( INC_FILES
inc/MantidMDAlgorithms/PowerMD.h
inc/MantidMDAlgorithms/QuadEnBackground.h
inc/MantidMDAlgorithms/RunParam.h
inc/MantidMDAlgorithms/SetMDUsingMask.h
inc/MantidMDAlgorithms/SimulateMDD.h
inc/MantidMDAlgorithms/SimulateResolution.h
inc/MantidMDAlgorithms/TobyFitSimulate.h
Expand Down Expand Up @@ -102,6 +104,7 @@ set ( TEST_FILES
test/PlusMDTest.h
test/PowerMDTest.h
test/RunParamTest.h
test/SetMDUsingMaskTest.h
test/SimulateMDDTest.h
test/TobyFitSimulateTest.h
test/UnaryOperationMDTest.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef MANTID_MDALGORITHMS_SETMDUSINGMASK_H_
#define MANTID_MDALGORITHMS_SETMDUSINGMASK_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"

namespace Mantid
{
namespace MDAlgorithms
{

/** Algorithm to set a MDHistoWorkspace in points determined by a mask boolean MDHistoWorkspace.
@date 2011-11-10
Copyright © 2011 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
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://svn.mantidproject.org/mantid/trunk/Code/Mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport SetMDUsingMask : public API::Algorithm
{
public:
SetMDUsingMask();
virtual ~SetMDUsingMask();

virtual const std::string name() const;
virtual int version() const;
virtual const std::string category() const;

private:
virtual void initDocs();
void init();
void exec();


};


} // namespace MDAlgorithms
} // namespace Mantid

#endif /* MANTID_MDALGORITHMS_SETMDUSINGMASK_H_ */
158 changes: 158 additions & 0 deletions Code/Mantid/Framework/MDAlgorithms/src/SetMDUsingMask.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*WIKI*
This algorithm is used to replace values in a [[MDHistoWorkspace]] but only at particular points.
A mask MDHistoWorkspace is provided, where non-zero values indicate 'true'.
At these points, the corresponding value in the ValueWorkspace will be set.
Any 'false' points in the MaskWorkspace are skipped.
If ValueWorkspace is not specified, the you must specify Value, which is a a simple number to set.
In matlab, the equivalent function call would be WS[mask] = OtherWS[mask]
See [[MDHistoWorkspace#Boolean_Operations| this page on boolean operations]] for examples of how to create a mask.
== Usage (Python) ==
# This will zero-out any values below the threshold of 123
MaskWS = WS < 123
ModifiedWS = SetMDUsingMask(InputWorkspace=WS, Value="0", MaskWorkspace=MaskWS)
*WIKI*/

#include "MantidMDAlgorithms/SetMDUsingMask.h"
#include "MantidKernel/System.h"
#include "MantidAPI/IMDWorkspace.h"
#include "MantidAPI/IMDHistoWorkspace.h"
#include "MantidMDEvents/MDHistoWorkspace.h"
#include <float.h>

using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::MDEvents;

namespace Mantid
{
namespace MDAlgorithms
{

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SetMDUsingMask)



//----------------------------------------------------------------------------------------------
/** Constructor
*/
SetMDUsingMask::SetMDUsingMask()
{
}

//----------------------------------------------------------------------------------------------
/** Destructor
*/
SetMDUsingMask::~SetMDUsingMask()
{
}


//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string SetMDUsingMask::name() const { return "SetMDUsingMask";};

/// Algorithm's version for identification. @see Algorithm::version
int SetMDUsingMask::version() const { return 1;};

/// Algorithm's category for identification. @see Algorithm::category
const std::string SetMDUsingMask::category() const { return "Arithmetic";}

//----------------------------------------------------------------------------------------------
/// Sets documentation strings for this algorithm
void SetMDUsingMask::initDocs()
{
this->setWikiSummary("Algorithm to set a [[MDHistoWorkspace]] in points determined by a mask boolean MDHistoWorkspace.");
this->setOptionalMessage("Algorithm to set a MDHistoWorkspace in points determined by a mask boolean MDHistoWorkspace.");
}

//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void SetMDUsingMask::init()
{
declareProperty(new WorkspaceProperty<IMDHistoWorkspace>("InputWorkspace","",Direction::Input),
"An input MDHistoWorkspace.");
declareProperty(new WorkspaceProperty<IMDHistoWorkspace>("MaskWorkspace","",Direction::Input),
"A mask MDHistoWorkspace, where true indicates where to set the value.");

declareProperty(new WorkspaceProperty<IMDHistoWorkspace>("ValueWorkspace","",Direction::Input, true),
"Workspace to copy to the output workspace over the input. Optional - specify this or Value.");

declareProperty("Value", DBL_MAX,
"Single number to set in the output workspace. Optional - specify this or ValueWorkspace");

declareProperty(new WorkspaceProperty<IMDHistoWorkspace>("OutputWorkspace","",Direction::Output),
"An output MDHistoWorkspace.");
}

//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void SetMDUsingMask::exec()
{
IMDHistoWorkspace_sptr inIWS = getProperty("InputWorkspace");
IMDHistoWorkspace_sptr maskIWS = getProperty("MaskWorkspace");
IMDHistoWorkspace_sptr outIWS = getProperty("OutputWorkspace");
IMDHistoWorkspace_sptr valueIWS = getProperty("ValueWorkspace");
double value = getProperty("Value");

bool useValueWS = (value == DBL_MAX);

if (useValueWS && !valueIWS)
throw std::invalid_argument("You must specify either ValueWorkspace or Value.");
if (!useValueWS && valueIWS)
throw std::invalid_argument("You must specify either ValueWorkspace or Value, not both!");

if (maskIWS->getNumDims() != inIWS->getNumDims())
throw std::invalid_argument("Input and Mask workspace need to have the same number of dimensions.");
if (maskIWS->getNPoints() != inIWS->getNPoints())
throw std::invalid_argument("Input and Mask workspace need to have the same number of points.");
if (valueIWS)
{
if (maskIWS->getNumDims() != valueIWS->getNumDims())
throw std::invalid_argument("Input and Value workspace need to have the same number of dimensions.");
if (maskIWS->getNPoints() != valueIWS->getNPoints())
throw std::invalid_argument("Input and Value workspace need to have the same number of points.");
}

if (outIWS != inIWS)
{
// Not in-place. So clone the input to the output
IAlgorithm_sptr clone = this->createSubAlgorithm("CloneMDWorkspace", 0.0, 0.5, true);
clone->setProperty("InputWorkspace", boost::dynamic_pointer_cast<IMDWorkspace>(inIWS));
clone->setPropertyValue("OutputWorkspace", getPropertyValue("OutputWorkspace"));
clone->executeAsSubAlg();
IMDWorkspace_sptr temp = clone->getProperty("OutputWorkspace");
outIWS = boost::dynamic_pointer_cast<IMDHistoWorkspace>(temp);
}

MDHistoWorkspace_sptr outWS = boost::dynamic_pointer_cast<MDHistoWorkspace>(outIWS);
MDHistoWorkspace_sptr maskWS = boost::dynamic_pointer_cast<MDHistoWorkspace>(maskIWS);
MDHistoWorkspace_sptr valueWS = boost::dynamic_pointer_cast<MDHistoWorkspace>(valueIWS);

if (!outWS || !maskWS)
throw std::runtime_error("Error creating output workspace.");

// Set either using the WS or the single value
if (useValueWS)
outWS->setUsingMask(*maskWS, *valueWS);
else
outWS->setUsingMask(*maskWS, value, 0.0 /* assume zero error */ );

// Save the output
setProperty("OutputWorkspace", outIWS);
}



} // namespace Mantid
} // namespace MDAlgorithms
110 changes: 110 additions & 0 deletions Code/Mantid/Framework/MDAlgorithms/test/SetMDUsingMaskTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#ifndef MANTID_MDALGORITHMS_SETMDUSINGMASKTEST_H_
#define MANTID_MDALGORITHMS_SETMDUSINGMASKTEST_H_

#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/IMDHistoWorkspace.h"
#include "MantidKernel/System.h"
#include "MantidKernel/Timer.h"
#include "MantidMDAlgorithms/SetMDUsingMask.h"
#include "MantidMDEvents/MDHistoWorkspace.h"
#include "MantidTestHelpers/MDEventsTestHelper.h"
#include <cxxtest/TestSuite.h>
#include <iomanip>
#include <iostream>

using namespace Mantid;
using namespace Mantid::MDAlgorithms;
using namespace Mantid::API;
using namespace Mantid::MDEvents;

class SetMDUsingMaskTest : public CxxTest::TestSuite
{
public:
void test_Init()
{
SetMDUsingMask alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}

void do_test(std::string InputWorkspace, std::string MaskWorkspace,
std::string ValueWorkspace, std::string Value,
std::string OutputWorkspace,
double expectedSignal, double expectedError,
bool succeeds=true)
{
MDHistoWorkspace_sptr histo_A = MDEventsTestHelper::makeFakeMDHistoWorkspace(2.0, 2, 5, 10.0, 2.0);
MDHistoWorkspace_sptr histo_B = MDEventsTestHelper::makeFakeMDHistoWorkspace(3.0, 2, 5, 10.0, 3.0);
MDHistoWorkspace_sptr histo_diff = MDEventsTestHelper::makeFakeMDHistoWorkspace(2.0, 2, 4, 10.0, 2.0);
MDHistoWorkspace_sptr mask_0 = MDEventsTestHelper::makeFakeMDHistoWorkspace(0.0, 2, 5, 10.0, 0.0);
MDHistoWorkspace_sptr mask_1 = MDEventsTestHelper::makeFakeMDHistoWorkspace(1.0, 2, 5, 10.0, 0.0);
AnalysisDataService::Instance().addOrReplace("histo_A", histo_A);
AnalysisDataService::Instance().addOrReplace("histo_B", histo_B);
AnalysisDataService::Instance().addOrReplace("histo_diff", histo_diff);
AnalysisDataService::Instance().addOrReplace("mask_0", mask_0);
AnalysisDataService::Instance().addOrReplace("mask_1", mask_1);


SetMDUsingMask alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", InputWorkspace) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("MaskWorkspace", MaskWorkspace) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("ValueWorkspace", ValueWorkspace) );
if (!Value.empty()) TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Value", Value) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", OutputWorkspace) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );

if (succeeds)
{
TS_ASSERT( alg.isExecuted() );
// Retrieve the workspace from data service.
IMDHistoWorkspace_sptr ws;
TS_ASSERT_THROWS_NOTHING( ws = boost::dynamic_pointer_cast<IMDHistoWorkspace>(AnalysisDataService::Instance().retrieve(OutputWorkspace)) );
TS_ASSERT(ws);
if (!ws) return;
TS_ASSERT_DELTA( ws->signalAt(0), expectedSignal, 1e-6);
TS_ASSERT_DELTA( ws->errorSquaredAt(0), expectedError, 1e-6);
}
else
{
TS_ASSERT( !alg.isExecuted() );
}
}


void test_bad_inputs()
{
do_test("histo_A", "histo_diff", "histo_B", "", "out", 0,0, false);
do_test("histo_A", "mask_1", "histo_diff", "", "out", 0,0, false);
do_test("histo_A", "histo_diff", "histo_diff", "", "out", 0,0, false);
}

void test_not_inplace()
{
do_test("histo_A", "mask_0", "histo_B", "", "out", 2.0, 2.0);
do_test("histo_A", "mask_1", "histo_B", "", "out", 3.0, 3.0);
}

void test_not_inplace_double()
{
do_test("histo_A", "mask_0", "", "34.5", "out", 2.0, 2.0);
do_test("histo_A", "mask_1", "", "34.5", "out", 34.5, 0.);
}

void test_inplace()
{
do_test("histo_A", "mask_0", "histo_B", "", "histo_A", 2.0, 2.0);
do_test("histo_A", "mask_1", "histo_B", "", "histo_A", 3.0, 3.0);
}

void test_inplace_double()
{
do_test("histo_A", "mask_0", "", "34.5", "histo_A", 2.0, 2.0);
do_test("histo_A", "mask_1", "", "34.5", "histo_A", 34.5, 0.);
}

};


#endif /* MANTID_MDALGORITHMS_SETMDUSINGMASKTEST_H_ */

0 comments on commit 1698800

Please sign in to comment.