/
ThresholdMD.cpp
126 lines (102 loc) · 4.82 KB
/
ThresholdMD.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidMDAlgorithms/ThresholdMD.h"
#include "MantidAPI/Progress.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidDataObjects/MDHistoWorkspace.h"
#include "MantidKernel/EnabledWhenProperty.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MultiThreaded.h"
#include <boost/function.hpp>
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
namespace Mantid::MDAlgorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(ThresholdMD)
std::string LessThan() { return "Less Than"; }
std::string GreaterThan() { return "Greater Than"; }
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string ThresholdMD::name() const { return "ThresholdMD"; }
/// Algorithm's version for identification. @see Algorithm::version
int ThresholdMD::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string ThresholdMD::category() const { return "MDAlgorithms\\Transforms"; }
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void ThresholdMD::init() {
declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("InputWorkspace", "", Direction::Input),
"An input workspace.");
std::vector<std::string> propOptions;
propOptions.emplace_back(LessThan());
propOptions.emplace_back(GreaterThan());
declareProperty("Condition", LessThan(), std::make_shared<StringListValidator>(propOptions),
"Selected threshold condition. Any value which does meet "
"this condition with respect to the ReferenceValue will be "
"overwritten.");
declareProperty("ReferenceValue", 0.0, "Comparator value used by the Condition.");
declareProperty("OverwriteWithZero", true,
"Flag for enabling overwriting "
"with a custom value. Defaults to "
"overwrite signals with zeros.");
declareProperty("CustomOverwriteValue", 0.0, "Custom overwrite value for the signal. Defaults to zero.");
setPropertySettings("CustomOverwriteValue",
std::make_unique<EnabledWhenProperty>("OverwriteWithZero", IS_NOT_DEFAULT));
declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("OutputWorkspace", "", Direction::Output),
"Output thresholded workspace.");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void ThresholdMD::exec() {
IMDHistoWorkspace_sptr inputWS = getProperty("InputWorkspace");
const std::string condition = getProperty("Condition");
const double referenceValue = getProperty("ReferenceValue");
const bool doOverwriteWithZero = getProperty("OverwriteWithZero");
double customOverwriteValue = getProperty("CustomOverwriteValue");
if (doOverwriteWithZero) {
customOverwriteValue = 0;
}
IMDHistoWorkspace_sptr outWS = getProperty("OutputWorkspace");
if (outWS != inputWS) {
g_log.debug("Deep copy input workspace as output workspace.");
auto alg = createChildAlgorithm("CloneMDWorkspace");
alg->setProperty("InputWorkspace", inputWS);
alg->executeAsChildAlg();
IMDWorkspace_sptr temp = alg->getProperty("OutputWorkspace");
outWS = std::dynamic_pointer_cast<IMDHistoWorkspace>(temp);
}
const int64_t nPoints = inputWS->getNPoints();
using namespace std::placeholders;
boost::function<bool(double)> comparitor = std::bind(std::less<double>(), std::placeholders::_1, referenceValue);
if (condition == GreaterThan()) {
comparitor = std::bind(std::greater<double>(), std::placeholders::_1, referenceValue);
}
Progress prog(this, 0.0, 1.0, 100);
int64_t frequency = nPoints;
if (nPoints > 100) {
frequency = nPoints / 100;
}
PARALLEL_FOR_IF(Kernel::threadSafe(*inputWS, *outWS))
for (int64_t i = 0; i < nPoints; ++i) {
PARALLEL_START_INTERRUPT_REGION
const double signalAt = inputWS->getSignalAt(i);
if (comparitor(signalAt)) {
outWS->setSignalAt(i, customOverwriteValue);
}
if (i % frequency == 0) {
prog.report();
}
PARALLEL_END_INTERRUPT_REGION
}
PARALLEL_CHECK_INTERRUPT_REGION
setProperty("OutputWorkspace", outWS);
}
} // namespace Mantid::MDAlgorithms