Mantid
Loading...
Searching...
No Matches
CalculateZscore.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 +
12#include "MantidHistogramData/Histogram.h"
13#include "MantidHistogramData/HistogramBuilder.h"
16
17#include <sstream>
18
19using namespace Mantid;
20using namespace Mantid::API;
21using namespace Mantid::Kernel;
22using namespace Mantid::DataObjects;
23using namespace Mantid::HistogramData;
24using namespace std;
25
26namespace Mantid::Algorithms {
27
28DECLARE_ALGORITHM(CalculateZscore)
29
30//----------------------------------------------------------------------------------------------
33void CalculateZscore::init() {
34 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "Anonymous", Direction::Input),
35 "Name of input MatrixWorkspace to have Z-score calculated.");
36
37 declareProperty(std::make_unique<WorkspaceProperty<Workspace2D>>("OutputWorkspace", "", Direction::Output),
38 "Name of the output Workspace2D containing the Z-scores.");
39
40 declareProperty("WorkspaceIndex", EMPTY_INT(),
41 "Index of the spetrum to have Z-score calculated. "
42 "Default is to calculate for all spectra.");
43}
44
45//----------------------------------------------------------------------------------------------
49 // 1. Get input and validate
50 MatrixWorkspace_const_sptr inpWS = getProperty("InputWorkspace");
51 int inpwsindex = getProperty("WorkspaceIndex");
52
53 bool zscoreforall = false;
54 if (inpwsindex == EMPTY_INT()) {
55 zscoreforall = true;
56 }
57
58 // 2. Generate output
59 size_t numspec;
60 if (zscoreforall) {
61 numspec = inpWS->getNumberHistograms();
62 } else {
63 numspec = 1;
64 }
65 size_t sizex = inpWS->x(0).size();
66 size_t sizey = inpWS->y(0).size();
67
68 HistogramBuilder builder;
69 builder.setX(sizex);
70 builder.setY(sizey);
71 builder.setDistribution(inpWS->isDistribution());
72 Workspace2D_sptr outWS = create<Workspace2D>(numspec, builder.build());
73
74 Progress progress(this, 0.0, 1.0, numspec);
75
76 // 3. Get Z values
77 for (size_t i = 0; i < numspec; ++i) {
78 // a) figure out wsindex
79 size_t wsindex;
80 if (zscoreforall) {
81 // Update wsindex to index in input workspace
82 wsindex = i;
83 } else {
84 // Use the wsindex as the input
85 wsindex = static_cast<size_t>(inpwsindex);
86 if (wsindex >= inpWS->getNumberHistograms()) {
87 stringstream errmsg;
88 errmsg << "Input workspace index " << inpwsindex
89 << " is out of input workspace range = " << inpWS->getNumberHistograms() << '\n';
90 }
91 }
92
93 // b) Calculate Zscore
94 auto &inpY = inpWS->y(wsindex).rawData();
95 auto &inpE = inpWS->e(wsindex).rawData();
96
97 auto &histY = outWS->mutableY(i);
98 auto &histE = outWS->mutableE(i);
99
100 vector<double> yzscores = getZscore(inpY);
101 vector<double> ezscores = getZscore(inpE);
102
103 outWS->setSharedX(i, inpWS->sharedX(wsindex));
104 histY = yzscores;
105 histE = ezscores;
106
107 progress.report("Calculating Z Score");
108 } // ENDFOR
109
110 // 4. Set the output
111 setProperty("OutputWorkspace", outWS);
112}
113
114} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
CalculateZscore : Calculate Zscore for a Matrix Workspace.
void exec() override
Implement abstract Algorithm methods.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< Workspace2D > Workspace2D_sptr
shared pointer to Mantid::DataObjects::Workspace2D
std::vector< double > getZscore(const std::vector< TYPE > &data)
Return the Z score values for a dataset.
Definition: Statistics.cpp:81
Helper class which provides the Collimation Length for SANS instruments.
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition: EmptyValues.h:25
STL namespace.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54