Mantid
Loading...
Searching...
No Matches
ThresholdMD.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
14#include <boost/function.hpp>
15
16using namespace Mantid::Kernel;
17using namespace Mantid::API;
18using namespace Mantid::DataObjects;
19
20namespace Mantid::MDAlgorithms {
21
22// Register the algorithm into the AlgorithmFactory
23DECLARE_ALGORITHM(ThresholdMD)
24
25std::string LessThan() { return "Less Than"; }
26
27std::string GreaterThan() { return "Greater Than"; }
28
29//----------------------------------------------------------------------------------------------
31const std::string ThresholdMD::name() const { return "ThresholdMD"; }
32
34int ThresholdMD::version() const { return 1; }
35
37const std::string ThresholdMD::category() const { return "MDAlgorithms\\Transforms"; }
38
39//----------------------------------------------------------------------------------------------
40
41//----------------------------------------------------------------------------------------------
45 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("InputWorkspace", "", Direction::Input),
46 "An input workspace.");
47
48 std::vector<std::string> propOptions;
49 propOptions.emplace_back(LessThan());
50 propOptions.emplace_back(GreaterThan());
51
52 declareProperty("Condition", LessThan(), std::make_shared<StringListValidator>(propOptions),
53 "Selected threshold condition. Any value which does meet "
54 "this condition with respect to the ReferenceValue will be "
55 "overwritten.");
56
57 declareProperty("ReferenceValue", 0.0, "Comparator value used by the Condition.");
58
59 declareProperty("OverwriteWithZero", true,
60 "Flag for enabling overwriting "
61 "with a custom value. Defaults to "
62 "overwrite signals with zeros.");
63
64 declareProperty("CustomOverwriteValue", 0.0, "Custom overwrite value for the signal. Defaults to zero.");
65 setPropertySettings("CustomOverwriteValue",
66 std::make_unique<EnabledWhenProperty>("OverwriteWithZero", IS_NOT_DEFAULT));
67
68 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("OutputWorkspace", "", Direction::Output),
69 "Output thresholded workspace.");
70}
71
72//----------------------------------------------------------------------------------------------
76 IMDHistoWorkspace_sptr inputWS = getProperty("InputWorkspace");
77 const std::string condition = getProperty("Condition");
78 const double referenceValue = getProperty("ReferenceValue");
79 const bool doOverwriteWithZero = getProperty("OverwriteWithZero");
80 double customOverwriteValue = getProperty("CustomOverwriteValue");
81 if (doOverwriteWithZero) {
82 customOverwriteValue = 0;
83 }
84
85 IMDHistoWorkspace_sptr outWS = getProperty("OutputWorkspace");
86 if (outWS != inputWS) {
87 g_log.debug("Deep copy input workspace as output workspace.");
88 auto alg = createChildAlgorithm("CloneMDWorkspace");
89 alg->setProperty("InputWorkspace", inputWS);
90 alg->executeAsChildAlg();
91 IMDWorkspace_sptr temp = alg->getProperty("OutputWorkspace");
92 outWS = std::dynamic_pointer_cast<IMDHistoWorkspace>(temp);
93 }
94
95 const int64_t nPoints = inputWS->getNPoints();
96
97 using namespace std::placeholders;
98 boost::function<bool(double)> comparitor = std::bind(std::less<double>(), std::placeholders::_1, referenceValue);
99 if (condition == GreaterThan()) {
100 comparitor = std::bind(std::greater<double>(), std::placeholders::_1, referenceValue);
101 }
102
103 Progress prog(this, 0.0, 1.0, 100);
104 int64_t frequency = nPoints;
105 if (nPoints > 100) {
106 frequency = nPoints / 100;
107 }
108
109 PARALLEL_FOR_IF(Kernel::threadSafe(*inputWS, *outWS))
110 for (int64_t i = 0; i < nPoints; ++i) {
112 const double signalAt = inputWS->getSignalAt(i);
113 if (comparitor(signalAt)) {
114 outWS->setSignalAt(i, customOverwriteValue);
115 }
116 if (i % frequency == 0) {
117 prog.report();
118 }
120 }
122
123 setProperty("OutputWorkspace", outWS);
124}
125
126} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Kernel::Logger & g_log
Definition Algorithm.h:422
Helper class for reporting progress from algorithms.
Definition Progress.h:25
A property class for workspaces.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void setPropertySettings(const std::string &name, std::unique_ptr< IPropertySettings > settings)
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
void exec() override
Execute the algorithm.
void init() override
Initialize the algorithm's properties.
const std::string category() const override
Algorithm's category for identification.
int version() const override
Algorithm's version for identification.
const std::string name() const override
Algorithm's name for identification.
std::shared_ptr< IMDHistoWorkspace > IMDHistoWorkspace_sptr
shared pointer to Mantid::API::IMDHistoWorkspace
std::shared_ptr< IMDWorkspace > IMDWorkspace_sptr
Shared pointer to the IMDWorkspace base class.
std::enable_if< std::is_pointer< Arg >::value, bool >::type threadSafe(Arg workspace)
Thread-safety check Checks the workspace to ensure it is suitable for multithreaded access.
std::string LessThan()
std::string GreaterThan()
STL namespace.
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54