Mantid
Loading...
Searching...
No Matches
ResampleX.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 +
8#include "MantidAPI/Axis.h"
16
17#include <sstream>
18
19namespace Mantid::Algorithms {
20using namespace API;
21using namespace DataObjects;
22using namespace Kernel;
23using HistogramData::BinEdges;
24using std::map;
25using std::string;
26using std::stringstream;
27using std::vector;
28
29// Register the algorithm into the AlgorithmFactory
30DECLARE_ALGORITHM(ResampleX)
31
32//----------------------------------------------------------------------------------------------
34const std::string ResampleX::name() const { return "ResampleX"; }
35
37int ResampleX::version() const { return 1; }
38
39const std::string ResampleX::alias() const { return ""; }
40
41//----------------------------------------------------------------------------------------------
45 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input), "An input workspace.");
46 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
47 "An output workspace.");
48
49 declareProperty(std::make_unique<ArrayProperty<double>>("XMin"),
50 "A comma separated list of the XMin for every spectrum. (Optional)");
51 declareProperty(std::make_unique<ArrayProperty<double>>("XMax"),
52 "A comma separated list of the XMax for every spectrum. (Optional)");
53
54 auto min = std::make_shared<BoundedValidator<int>>();
55 min->setLower(1);
56 declareProperty("NumberBins", 0, min, "Number of bins to split up each spectrum into.");
57 declareProperty("LogBinning", false, "Use logarithmic binning. If false use constant step sizes.");
58
59 declareProperty("PreserveEvents", true,
60 "Keep the output workspace as an EventWorkspace, if the "
61 "input has events (default).\n"
62 "If the input and output EventWorkspace names are the same, "
63 "only the X bins are set, which is very quick.\n"
64 "If false, then the workspace gets converted to a "
65 "Workspace2D histogram.");
66}
67
71map<string, string> ResampleX::validateInputs() {
72 map<string, string> errors;
73 vector<double> xmins = getProperty("XMin");
74 vector<double> xmaxs = getProperty("XMax");
75 if ((!xmins.empty()) && (!xmaxs.empty())) {
76 if (xmins.size() != xmaxs.size()) {
77 stringstream msg;
78 msg << "XMin and XMax do not define same number of spectra (" << xmins.size() << " != " << xmaxs.size() << ")";
79 errors.emplace("XMax", msg.str());
80 } else {
81 size_t size = xmins.size();
82 for (size_t i = 0; i < size; ++i) {
83 if (xmins[i] >= xmaxs[i]) {
84 stringstream msg;
85 msg << "XMin (" << xmins[i] << ") cannot be greater than XMax (" << xmaxs[i] << ")";
86 errors.emplace("XMax", msg.str());
87 }
88 }
89 }
90 }
91
92 return errors;
93}
94
107string determineXMinMax(const MatrixWorkspace_sptr &inputWS, vector<double> &xmins, vector<double> &xmaxs) {
108 const size_t numSpectra = inputWS->getNumberHistograms();
109
110 // pad out the ranges by copying the first value to the rest that are needed
111 if (xmins.size() == 1 && numSpectra > xmins.size()) {
112 const double value = xmins.front();
113 xmins.insert(xmins.end(), numSpectra - xmins.size(), value);
114 }
115 if (xmaxs.size() == 1 && numSpectra > xmaxs.size()) {
116 const double value = xmaxs.front();
117 xmaxs.insert(xmaxs.end(), numSpectra - xmaxs.size(), value);
118 }
119
120 // should the individiual values be calculated?
121 const bool updateXMins = xmins.empty(); // they weren't set
122 const bool updateXMaxs = xmaxs.empty(); // they weren't set
123
124 stringstream msg;
125
126 // determine overall xmin/xmax
127 double xmin_wksp = inputWS->getXMin();
128 double xmax_wksp = inputWS->getXMax();
129 EventWorkspace_const_sptr inputEventWS = std::dynamic_pointer_cast<const EventWorkspace>(inputWS);
130 if (inputEventWS != nullptr && inputEventWS->getNumberEvents() > 0) {
131 xmin_wksp = inputEventWS->getTofMin();
132 xmax_wksp = inputEventWS->getTofMax();
133 }
134
135 for (size_t i = 0; i < numSpectra; ++i) {
136 // determine ranges if necessary
137 if (updateXMins || updateXMaxs) {
138 const auto &xvalues = inputWS->x(i);
139 if (updateXMins) {
140 const auto minimum = xvalues.front();
141 if (std::isnan(minimum) || minimum >= xmax_wksp) {
142 xmins.emplace_back(xmin_wksp);
143 } else {
144 xmins.emplace_back(minimum);
145 }
146 }
147 if (updateXMaxs) {
148 const auto maximum = xvalues.back();
149 if (std::isnan(maximum) || maximum <= xmin_wksp) {
150 xmaxs.emplace_back(xmax_wksp);
151 } else {
152 xmaxs.emplace_back(maximum);
153 }
154 }
155 }
156
157 // error check the ranges
158 if (xmins[i] >= xmaxs[i]) {
159 if (!msg.str().empty())
160 msg << ", ";
161 msg << "at wksp_index=" << i << " XMin >= XMax (" << xmins[i] << " >= " << xmaxs[i] << ")";
162 }
163 }
164
165 return msg.str(); // empty string means nothing went wrong
166}
167
176void ResampleX::setOptions(const int numBins, const bool useLogBins, const bool isDist) {
177 m_numBins = numBins;
178 m_useLogBinning = useLogBins;
179 m_isDistribution = isDist;
180}
181
191double ResampleX::determineBinning(MantidVec &xValues, const double xmin, const double xmax) {
192 xValues.clear(); // clear out the x-values
193
194 int numBoundaries(0);
195 int reqNumBoundaries(m_numBins);
196 int expNumBoundaries(m_numBins);
198 reqNumBoundaries -= 1; // to get the VectorHelper to do the right thing
199 else
200 expNumBoundaries += 1; // should be one more bin boundary for histograms
201
202 vector<double> params; // xmin, delta, xmax
203 params.emplace_back(xmin);
204 params.emplace_back(0.); // dummy delta value
205 params.emplace_back(xmax);
206
207 // constant binning is easy
208 if (m_useLogBinning) {
209 if (xmin == 0)
210 throw std::invalid_argument("Cannot calculate log of xmin=0");
211 if (xmax == 0)
212 throw std::invalid_argument("Cannot calculate log of xmax=0");
213 if (xmin < 0. && xmax > 0.) {
214 std::stringstream msg;
215 msg << "Cannot calculate logorithmic binning that changes sign (xmin=" << xmin << ", xmax=" << xmax << ")";
216 throw std::invalid_argument(msg.str());
217 }
218
219 const int MAX_ITER(100); // things went wrong if we get this far
220
221 // starting delta value assuming everything happens exactly
222 double delta = (log(xmax) - log(xmin)) / static_cast<double>(m_numBins);
223 double shift = .1;
224 int sign = 0;
225 for (int numIter = 0; numIter < MAX_ITER; ++numIter) {
226 params[1] = -1. * delta;
227 if (!m_isDistribution)
228 params[2] = xmax + delta;
229 numBoundaries = VectorHelper::createAxisFromRebinParams(params, xValues, true);
230
231 if (numBoundaries == expNumBoundaries) {
232 double diff = (xmax - xValues.back());
233 if (diff != 0.) {
234 g_log.debug() << "Didn't get the exact xmax value: [xmax - xValues.back()=" << diff
235 << "] [relative diff = " << fabs(100. * diff / xmax) << "%]\n";
236 g_log.debug() << "Resetting final x-value to xmax\n";
237 *(xValues.rbegin()) = xmax;
238 }
239 break;
240 } else if (numBoundaries > expNumBoundaries) // too few points
241 {
242 delta *= (1. + shift);
243 if (sign < 0)
244 shift *= .9;
245 sign = 1;
246 } else // too many points
247 {
248 delta *= (1. - shift);
249 if (sign > 0)
250 shift *= .9;
251 sign = -1;
252 }
253 }
254 } else {
255 params[1] = (xmax - xmin) / static_cast<double>(reqNumBoundaries);
256 numBoundaries = VectorHelper::createAxisFromRebinParams(params, xValues, true);
257 }
258
259 if (numBoundaries != expNumBoundaries) {
260 g_log.warning() << "Did not generate the requested number of bins: generated " << numBoundaries << " requested "
261 << expNumBoundaries << "(xmin=" << xmin << ", xmax=" << xmax << ")\n";
262 }
263
264 // return the delta value so the caller can do debug printing
265 return params[1];
266}
267
268//----------------------------------------------------------------------------------------------
272 // generically having access to the input workspace is a good idea
273 MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
274 MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");
275 bool inPlace = (inputWS == outputWS); // Rebinning in-place
276 m_isDistribution = inputWS->isDistribution();
277 m_isHistogram = inputWS->isHistogramData();
278 const auto numSpectra = static_cast<int>(inputWS->getNumberHistograms());
279
280 // the easy parameters
281 m_useLogBinning = getProperty("LogBinning");
282 m_numBins = getProperty("NumberBins");
283 m_preserveEvents = getProperty("PreserveEvents");
284
285 // determine the xmin/xmax for the workspace
286 vector<double> xmins = getProperty("XMin");
287 vector<double> xmaxs = getProperty("XMax");
288 string error = determineXMinMax(inputWS, xmins, xmaxs);
289 if (!error.empty())
290 throw std::runtime_error(error);
291
292 bool common_limits = true;
293 {
294 double xmin_common = xmins[0];
295 double xmax_common = xmaxs[0];
296 for (size_t i = 1; i < xmins.size(); ++i) {
297 if (xmins[i] != xmin_common) {
298 common_limits = false;
299 break;
300 }
301 if (xmaxs[i] != xmax_common) {
302 common_limits = false;
303 break;
304 }
305 }
306 }
307 if (common_limits) {
308 g_log.debug() << "Common limits between all spectra\n";
309 } else {
310 g_log.debug() << "Does not have common limits between all spectra\n";
311 }
312
313 // start doing actual work
314 EventWorkspace_const_sptr inputEventWS = std::dynamic_pointer_cast<const EventWorkspace>(inputWS);
315 if (inputEventWS != nullptr) {
316 if (m_preserveEvents) {
317 if (inPlace) {
318 g_log.debug() << "Rebinning event workspace in place\n";
319 } else {
320 g_log.debug() << "Rebinning event workspace out of place\n";
321 outputWS = inputWS->clone();
322 }
323 auto outputEventWS = std::dynamic_pointer_cast<EventWorkspace>(outputWS);
324
325 if (common_limits) {
326 // get the delta from the first since they are all the same
327 BinEdges xValues(0);
328 const double delta = this->determineBinning(xValues.mutableRawData(), xmins[0], xmaxs[0]);
329 g_log.debug() << "delta = " << delta << "\n";
330 outputEventWS->setAllX(xValues);
331 } else {
332 // initialize progress reporting.
333 Progress prog(this, 0.0, 1.0, numSpectra);
334
335 // do the rebinning
336 PARALLEL_FOR_IF(Kernel::threadSafe(*inputEventWS, *outputWS))
337 for (int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) {
339 BinEdges xValues(0);
340 const double delta = this->determineBinning(xValues.mutableRawData(), xmins[wkspIndex], xmaxs[wkspIndex]);
341 g_log.debug() << "delta[wkspindex=" << wkspIndex << "] = " << delta << " xmin=" << xmins[wkspIndex]
342 << " xmax=" << xmaxs[wkspIndex] << "\n";
343 outputEventWS->setHistogram(wkspIndex, xValues);
344 prog.report(name()); // Report progress
346 }
348 }
349 } // end if (m_preserveEvents)
350 else // event workspace -> matrix workspace
351 {
352 //--------- Different output, OR you're inplace but not preserving Events
353 g_log.information() << "Creating a Workspace2D from the EventWorkspace " << inputEventWS->getName() << ".\n";
354 outputWS = create<DataObjects::Workspace2D>(*inputWS, numSpectra, HistogramData::BinEdges(m_numBins + 1));
355
356 // Initialize progress reporting.
357 Progress prog(this, 0.0, 1.0, numSpectra);
358
359 // Go through all the histograms and set the data
360 PARALLEL_FOR_IF(Kernel::threadSafe(*inputEventWS, *outputWS))
361 for (int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) {
363
364 // Set the X axis for each output histogram
365 MantidVec xValues;
366 const double delta = this->determineBinning(xValues, xmins[wkspIndex], xmaxs[wkspIndex]);
367 g_log.debug() << "delta[wkspindex=" << wkspIndex << "] = " << delta << "\n";
368 outputWS->setBinEdges(wkspIndex, xValues);
369
370 // Get a const event list reference. inputEventWS->dataY() doesn't work.
371 const EventList &el = inputEventWS->getSpectrum(wkspIndex);
372 MantidVec y_data, e_data;
373 // The EventList takes care of histogramming.
374 el.generateHistogram(xValues, y_data, e_data);
375
376 // Copy the data over.
377 outputWS->mutableY(wkspIndex) = y_data;
378 outputWS->mutableE(wkspIndex) = e_data;
379
380 // Report progress
381 prog.report(name());
383 }
385
386 // Copy all the axes
387 for (int i = 1; i < inputWS->axes(); i++) {
388 outputWS->replaceAxis(i, std::unique_ptr<Axis>(inputWS->getAxis(i)->clone(outputWS.get())));
389 outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit();
390 }
391
392 // Copy the units over too.
393 for (int i = 0; i < outputWS->axes(); ++i) {
394 outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit();
395 }
396 outputWS->setYUnit(inputEventWS->YUnit());
397 outputWS->setYUnitLabel(inputEventWS->YUnitLabel());
398 }
399 // Assign it to the output workspace property
400 setProperty("OutputWorkspace", outputWS);
401 return;
402 } else // (inputeventWS != NULL)
403 {
404 // workspace2d ----------------------------------------------------------
405 if (!m_isHistogram) {
406 g_log.information() << "Rebin: Converting Data to Histogram.\n";
407 Mantid::API::Algorithm_sptr ChildAlg = createChildAlgorithm("ConvertToHistogram");
408 ChildAlg->initialize();
409 ChildAlg->setProperty("InputWorkspace", inputWS);
410 ChildAlg->execute();
411 inputWS = ChildAlg->getProperty("OutputWorkspace");
412 }
413
414 // make output Workspace the same type is the input, but with new length of
415 // signal array
416 outputWS = API::WorkspaceFactory::Instance().create(inputWS, numSpectra, m_numBins + 1, m_numBins);
417
418 // Copy over the 'vertical' axis
419 if (inputWS->axes() > 1)
420 outputWS->replaceAxis(1, std::unique_ptr<Axis>(inputWS->getAxis(1)->clone(outputWS.get())));
421
422 Progress prog(this, 0.0, 1.0, numSpectra);
423 PARALLEL_FOR_IF(Kernel::threadSafe(*inputWS, *outputWS))
424 for (int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) {
426 // get const references to input Workspace arrays (no copying)
427 // TODO: replace with HistogramX/Y/E when VectorHelper::rebin is updated
428 const MantidVec &XValues = inputWS->readX(wkspIndex);
429 const MantidVec &YValues = inputWS->readY(wkspIndex);
430 const MantidVec &YErrors = inputWS->readE(wkspIndex);
431
432 // get references to output workspace data (no copying)
433 // TODO: replace with HistogramX/Y/E when VectorHelper::rebin is updated
434 MantidVec &YValues_new = outputWS->dataY(wkspIndex);
435 MantidVec &YErrors_new = outputWS->dataE(wkspIndex);
436
437 // create new output X axis
438 MantidVec XValues_new;
439 const double delta = this->determineBinning(XValues_new, xmins[wkspIndex], xmaxs[wkspIndex]);
440 g_log.debug() << "delta[wkspindex=" << wkspIndex << "] = " << delta << "\n";
441
442 // output data arrays are implicitly filled by function
443 try {
444 VectorHelper::rebin(XValues, YValues, YErrors, XValues_new, YValues_new, YErrors_new, m_isDistribution);
445 } catch (std::exception &ex) {
446 g_log.error() << "Error in rebin function: " << ex.what() << '\n';
447 throw;
448 }
449
450 // Populate the output workspace X values
451 outputWS->setBinEdges(wkspIndex, XValues_new);
452
453 prog.report(name());
455 }
457 outputWS->setDistribution(m_isDistribution);
458
459 // Now propagate any masking correctly to the output workspace
460 // More efficient to have this in a separate loop because
461 // MatrixWorkspace::maskBins blocks multi-threading
462 for (int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) {
463 if (inputWS->hasMaskedBins(wkspIndex)) // Does the current spectrum have any masked bins?
464 {
465 this->propagateMasks(inputWS, outputWS, wkspIndex);
466 }
467 }
468 // Copy the units over too.
469 for (int i = 0; i < outputWS->axes(); ++i) {
470 outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit();
471 }
472
473 if (!m_isHistogram) {
474 g_log.information() << "Rebin: Converting Data back to Data Points.\n";
475 Mantid::API::Algorithm_sptr ChildAlg = createChildAlgorithm("ConvertToPointData");
476 ChildAlg->initialize();
477 ChildAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", outputWS);
478 ChildAlg->execute();
479 outputWS = ChildAlg->getProperty("OutputWorkspace");
480 }
481
482 // Assign it to the output workspace property
483 setProperty("OutputWorkspace", outputWS);
484 } // end if (inputeventWS != NULL)
485}
486
487} // namespace Mantid::Algorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
double value
The value of the point.
Definition FitMW.cpp:51
double error
#define fabs(x)
Definition Matrix.cpp:22
#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.
Helper class for reporting progress from algorithms.
Definition Progress.h:25
A property class for workspaces.
void propagateMasks(const API::MatrixWorkspace_const_sptr &inputWS, const API::MatrixWorkspace_sptr &outputWS, const int hist, const bool IgnoreBinErrors=false)
Takes the masks in the input workspace and apportions the weights into the new bins that overlap with...
Definition Rebin.cpp:437
ResampleX : TODO: DESCRIPTION.
Definition ResampleX.h:20
void setOptions(const int numBins, const bool useLogBins, const bool isDist)
MADE PUBLIC FOR TESTING ONLY - DO NOT USE.
void exec() override
Execute the algorithm.
const std::string name() const override
Algorithm's name for identification.
Definition ResampleX.cpp:34
std::map< std::string, std::string > validateInputs() override
More complicated checks of parameters and their relations.
Definition ResampleX.cpp:71
const std::string alias() const override
Algorithm's aliases.
Definition ResampleX.cpp:39
double determineBinning(MantidVec &xValues, const double xmin, const double xmax)
MADE PUBLIC FOR TESTING ONLY - DO NOT USE.
void init() override
Initialize the algorithm's properties.
Definition ResampleX.cpp:44
int version() const override
Algorithm's version for identification.
Definition ResampleX.cpp:37
A class for holding :
Definition EventList.h:57
void generateHistogram(const MantidVec &X, MantidVec &Y, MantidVec &E, bool skipError=false) const override
Generates both the Y and E (error) histograms w.r.t TOF for an EventList with or without WeightedEven...
Support for a property that holds an array of values.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
Definition Algorithm.h:52
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
Kernel::Logger g_log("DetermineSpinStateOrder")
string determineXMinMax(const MatrixWorkspace_sptr &inputWS, vector< double > &xmins, vector< double > &xmaxs)
Determine the min and max x-values for each spectrum and error check the pairs.
std::shared_ptr< const EventWorkspace > EventWorkspace_const_sptr
shared pointer to a const Workspace2D
int MANTID_KERNEL_DLL createAxisFromRebinParams(const std::vector< double > &params, std::vector< double > &xnew, const bool resize_xnew=true, const bool full_bins_only=false, const double xMinHint=std::nan(""), const double xMaxHint=std::nan(""), const bool useReverseLogarithmic=false, const double power=-1)
Creates a new output X array given a 'standard' set of rebinning parameters.
void MANTID_KERNEL_DLL rebin(const std::vector< double > &xold, const std::vector< double > &yold, const std::vector< double > &eold, const std::vector< double > &xnew, std::vector< double > &ynew, std::vector< double > &enew, bool distribution, bool addition=false)
Rebins data according to a new output X array.
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::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
Definition cow_ptr.h:172
STL namespace.
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54