Mantid
Loading...
Searching...
No Matches
CheckWorkspacesMatch.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 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
11
17#include "MantidAPI/TableRow.h"
23#include <sstream>
24
25//
26namespace Mantid::Algorithms {
27
28// Register the algorithm into the AlgorithmFactory
29DECLARE_ALGORITHM(CheckWorkspacesMatch)
30
31
33 useAlgorithm("CompareWorkspaces");
34 deprecatedDate("2015-10-27");
35}
36
37using namespace Kernel;
38using namespace API;
39using namespace DataObjects;
40using namespace Geometry;
41
42//----------------------------------------------------------------------------------------------
44 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("Workspace1", "", Direction::Input),
45 "The name of the first input workspace.");
46 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("Workspace2", "", Direction::Input),
47 "The name of the second input workspace.");
48
49 declareProperty("Tolerance", 0.0, "The maximum amount by which values may differ between the workspaces.");
50
51 declareProperty("CheckType", true,
52 "Whether to check that the data types "
53 "(Workspace2D vs EventWorkspace) match.");
54 declareProperty("CheckAxes", true, "Whether to check that the axes match.");
55 declareProperty("CheckSpectraMap", true, "Whether to check that the spectra-detector maps match. ");
56 declareProperty("CheckInstrument", true, "Whether to check that the instruments match. ");
57 declareProperty("CheckMasking", true, "Whether to check that the bin masking matches. ");
58 declareProperty("CheckSample", false,
59 "Whether to check that the sample (e.g. logs)."); // Have this one false
60 // by default - the logs
61 // are brittle
62
64
65 declareProperty("ToleranceRelErr", false,
66 "Treat tolerance as relative error rather then the absolute error.\n"
67 "This is only applicable to Matrix workspaces.");
68 declareProperty("CheckAllData", false,
69 "Usually checking data ends when first mismatch occurs. This "
70 "forces algorithm to check all data and print mismatch to "
71 "the debug log.\n"
72 "Very often such logs are huge so making it true should be "
73 "the last option.");
74 // Have this one false by default - it can be a lot of printing.
75
76 declareProperty("NumberMismatchedSpectraToPrint", 1, "Number of mismatched spectra from lowest to be listed. ");
77
78 declareProperty("DetailedPrintIndex", EMPTY_INT(), "Mismatched spectra that will be printed out in details. ");
79}
80
81//----------------------------------------------------------------------------------------------
83 // Run new algorithm
84 auto result = runCompareWorkspaces(false);
85
86 // Output as per previous behaviour
87 if (result != successString()) {
88 g_log.notice() << "The workspaces did not match: " << result << '\n';
89 }
90
91 setProperty("Result", result);
92}
93
94//----------------------------------------------------------------------------------------------
102 // Run new algorithm
103 auto result = runCompareWorkspaces(true);
104
105 // Output as per previous behaviour
106 if (result != successString()) {
107 g_log.notice() << result << "\n";
108 }
109
110 setProperty("Result", result);
111
112 return true;
113}
114
115//----------------------------------------------------------------------------------------------
125std::string CheckWorkspacesMatch::runCompareWorkspaces(bool group_compare) {
126 // This algorithm produces a single result string
127 std::string result;
128
129 // Use new CompareWorkspaces algorithm to perform comparison
130 Algorithm_sptr compare = this->createChildAlgorithm("CompareWorkspaces");
131 compare->setRethrows(true);
132 compare->setLogging(false);
133
134 // Forward workspace properties
135 Workspace_sptr ws1 = getProperty("Workspace1");
136 Workspace_sptr ws2 = getProperty("Workspace2");
137 compare->setProperty("Workspace1", ws1);
138 compare->setProperty("Workspace2", ws2);
139
140 // Copy any other non-default properties
141 const std::vector<Property *> &allProps = this->getProperties();
142 auto propCount = allProps.size();
143 for (size_t i = 0; i < propCount; ++i) {
144 Property *prop = allProps[i];
145 const std::string &pname = prop->name();
146
147 if (!prop->isDefault() && pname != "Workspace1" && pname != "Workspace2" && pname != "Result")
148 compare->setPropertyValue(pname, prop->value());
149 }
150
151 // Execute comparison
152 compare->execute();
153
154 // Generate result string
155 if (!compare->getProperty("Result")) {
156 ITableWorkspace_sptr table = compare->getProperty("Messages");
157 auto rowcount = table->rowCount();
158 for (size_t i = 0; i < rowcount; ++i) {
159 result += table->cell<std::string>(i, 0);
160
161 // Emulate special case output format when comparing groups
162 if (group_compare &&
163 table->cell<std::string>(i, 0) != "Type mismatch. One workspace is a group, the other is not." &&
164 table->cell<std::string>(i, 0) != "GroupWorkspaces size mismatch.") {
165
166 result += ". Inputs=[" + table->cell<std::string>(i, 1) + "," + table->cell<std::string>(i, 2) + "]";
167 }
168
169 if (i < (rowcount - 1))
170 result += "\n";
171 }
172 } else {
173 result = successString();
174 }
175
176 return result;
177}
178
179} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:85
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
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.
Definition: Algorithm.cpp:842
Kernel::Logger & g_log
Definition: Algorithm.h:451
const std::vector< Kernel::Property * > & getProperties() const override
Get the list of managed properties.
Definition: Algorithm.cpp:2050
Class for marking algorithms as deprecated.
A property class for workspaces.
Compares two workspaces for equality.
std::string runCompareWorkspaces(bool group_compare=false)
This algorithm is now deprecated and calls CompareWorkspaces instead.
void exec() override
Called when comparing two individual workspaces.
bool processGroups() override
Called when comparing workspace groups.
static std::string successString()
The string that is returned when comparison is successful.
void init() override
Initialisation code.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void notice(const std::string &msg)
Logs at notice level.
Definition: Logger.cpp:95
Base class for properties.
Definition: Property.h:94
virtual bool isDefault() const =0
Overriden function that returns if property has the same value that it was initialised with,...
const std::string & name() const
Get the property's name.
Definition: Property.cpp:60
virtual std::string value() const =0
Returns the value of the property as a string.
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
Definition: Algorithm.h:61
bool compare(const mypair &left, const mypair &right)
Definition: LoadSassena.cpp:91
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition: EmptyValues.h:25
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54