-
Notifications
You must be signed in to change notification settings - Fork 122
/
CompareMDWorkspaces.cpp
364 lines (325 loc) · 14.2 KB
/
CompareMDWorkspaces.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// 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/CompareMDWorkspaces.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidAPI/IMDWorkspace.h"
#include "MantidDataObjects/MDEventFactory.h"
#include "MantidDataObjects/MDHistoWorkspace.h"
#include "MantidGeometry/MDGeometry/IMDDimension.h"
#include "MantidKernel/Strings.h"
#include "MantidKernel/System.h"
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
using namespace Mantid::Geometry;
//=============================================================================
/** Custom exception class to signal a failure
* in the comparison
*/
class CompareFailsException : public std::runtime_error {
public:
explicit CompareFailsException(const std::string &msg)
: std::runtime_error(msg) {}
std::string getMessage() const { return this->what(); }
};
namespace Mantid {
namespace MDAlgorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(CompareMDWorkspaces)
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string CompareMDWorkspaces::name() const {
return "CompareMDWorkspaces";
}
/// Algorithm's version for identification. @see Algorithm::version
int CompareMDWorkspaces::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string CompareMDWorkspaces::category() const {
return "MDAlgorithms\\Utility\\Workspaces";
}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void CompareMDWorkspaces::init() {
declareProperty(std::make_unique<WorkspaceProperty<IMDWorkspace>>(
"Workspace1", "", Direction::Input),
"First MDWorkspace to compare.");
declareProperty(std::make_unique<WorkspaceProperty<IMDWorkspace>>(
"Workspace2", "", Direction::Input),
"Second MDWorkspace to compare.");
declareProperty(
"Tolerance", 0.0,
"The maximum amount by which values may differ between the workspaces.");
declareProperty("CheckEvents", true,
"Whether to compare each MDEvent. If "
"False, will only look at the box "
"structure.");
declareProperty(std::make_unique<PropertyWithValue<bool>>("Equals", false,
Direction::Output),
"Boolean set to true if the workspaces match.");
declareProperty(
std::make_unique<PropertyWithValue<std::string>>("Result", "",
Direction::Output),
"String describing the difference found between the workspaces");
declareProperty("IgnoreBoxID", false,
"To ignore box ID-s when comparing MD "
"boxes as Multithreaded splitting "
"assigns box id-s randomly");
}
//----------------------------------------------------------------------------------------------
/** Return a string "(a vs b)". */
template <typename T> std::string versus(T a, T b) {
return "(" + Strings::toString(a) + " vs " + Strings::toString(b) + ")";
}
//----------------------------------------------------------------------------------------------
/** Compare a and b. Throw if they dont match
*
* @param a :: any type
* @param b :: any type
* @param message :: message for result
* @throw CompareFailsException if the two DONT match
*/
template <typename T>
void CompareMDWorkspaces::compare(T a, T b, const std::string &message) {
if (a != b)
throw CompareFailsException(message + " " + versus(a, b));
}
//----------------------------------------------------------------------------------------------
/** Compare a and b. Throw if they dont match within Tolerance
*
* @param a :: double
* @param b :: double
* @param message :: message for result
* @throw CompareFailsException if the two DONT match
*/
template <class T>
void CompareMDWorkspaces::compareTol(T a, T b, const std::string &message) {
double diff = fabs(a - b);
if (diff > m_tolerance) {
double pa = fabs(a);
double pb = fabs(b);
if ((pa > 2 * m_tolerance) || (pb > 2 * m_tolerance)) {
diff = 0.5 * diff / (pa + pb);
if (diff > m_tolerance)
throw CompareFailsException(message + " " + versus(a, b));
} else
throw CompareFailsException(message + " " + versus(a, b));
}
}
//----------------------------------------------------------------------------------------------
/** Compare the dimensions etc. of two MDWorkspaces
*/
void CompareMDWorkspaces::compareMDGeometry(
const Mantid::API::IMDWorkspace_sptr &ws1,
const Mantid::API::IMDWorkspace_sptr &ws2) {
compare(ws1->getNumDims(), ws2->getNumDims(),
"Workspaces have a different number of dimensions");
for (size_t d = 0; d < ws1->getNumDims(); d++) {
IMDDimension_const_sptr dim1 = ws1->getDimension(d);
IMDDimension_const_sptr dim2 = ws2->getDimension(d);
compare(dim1->getName(), dim2->getName(),
"Dimension #" + Strings::toString(d) + " has a different name");
compare(dim1->getUnits(), dim2->getUnits(),
"Dimension #" + Strings::toString(d) + " has different units");
compare(dim1->getNBins(), dim2->getNBins(),
"Dimension #" + Strings::toString(d) +
" has a different number of bins");
compareTol(dim1->getMinimum(), dim2->getMinimum(),
"Dimension #" + Strings::toString(d) +
" has a different minimum");
compareTol(dim1->getMaximum(), dim2->getMaximum(),
"Dimension #" + Strings::toString(d) +
" has a different maximum");
}
}
//----------------------------------------------------------------------------------------------
/** Compare the dimensions etc. of two MDWorkspaces
*/
void CompareMDWorkspaces::compareMDHistoWorkspaces(
const Mantid::DataObjects::MDHistoWorkspace_sptr &ws1,
const Mantid::DataObjects::MDHistoWorkspace_sptr &ws2) {
compare(ws1->getNumDims(), ws2->getNumDims(),
"Workspaces have a different number of dimensions");
compare(ws1->getNPoints(), ws2->getNPoints(),
"Workspaces have a different number of points");
for (size_t i = 0; i < ws1->getNPoints(); i++) {
double diff = fabs(ws1->getSignalAt(i) - ws2->getSignalAt(i));
if (diff > m_tolerance)
throw CompareFailsException(
"MDHistoWorkspaces have a different signal at index " +
Strings::toString(i) + " " +
versus(ws1->getSignalAt(i), ws2->getSignalAt(i)));
double diffErr = fabs(ws1->getErrorAt(i) - ws2->getErrorAt(i));
if (diffErr > m_tolerance)
throw CompareFailsException(
"MDHistoWorkspaces have a different error at index " +
Strings::toString(i) + " " +
versus(ws1->getErrorAt(i), ws2->getErrorAt(i)));
}
}
//----------------------------------------------------------------------------------------------
/** Perform the comparison on MDEventWorkspaces
*
* @param ws :: MDEventWorkspace to compare
*/
template <typename MDE, size_t nd>
void CompareMDWorkspaces::compareMDWorkspaces(
typename MDEventWorkspace<MDE, nd>::sptr ws1) {
typename MDEventWorkspace<MDE, nd>::sptr ws2 =
std::dynamic_pointer_cast<MDEventWorkspace<MDE, nd>>(inWS2);
if (!ws1 || !ws2)
throw std::runtime_error("Incompatible workspace types passed to PlusMD.");
std::vector<API::IMDNode *> boxes1;
std::vector<API::IMDNode *> boxes2;
ws1->getBox()->getBoxes(boxes1, 1000, false);
ws2->getBox()->getBoxes(boxes2, 1000, false);
this->compare(boxes1.size(), boxes2.size(),
"Workspaces do not have the same number of boxes");
for (size_t j = 0; j < boxes1.size(); j++) {
API::IMDNode *box1 = boxes1[j];
API::IMDNode *box2 = boxes2[j];
if (m_CompareBoxID)
this->compare(box1->getID(), box2->getID(), "Boxes have different ID");
else {
if (box1->getID() != box2->getID())
g_log.debug() << " Boxes N: " << j << " have box ID: " << box1->getID()
<< " and " << box2->getID() << " correspondingly\n";
}
this->compare(size_t(box1->getDepth()), size_t(box2->getDepth()),
"Boxes are at a different depth");
this->compare(box1->getNumChildren(), box2->getNumChildren(),
"Boxes do not have the same number of children");
for (size_t i = 0; i < box1->getNumChildren(); i++) {
if (m_CompareBoxID)
this->compare(box1->getChild(i)->getID(), box2->getChild(i)->getID(),
"Child of boxes do not match IDs");
else {
if (box1->getID() != box2->getID())
g_log.debug() << " Boxes N: " << j << " children N: " << i
<< " have box ID: " << box1->getChild(i)->getID()
<< " and " << box2->getChild(i)->getID()
<< " correspondingly\n";
}
}
for (size_t d = 0; d < nd; d++) {
this->compareTol(box1->getExtents(d).getMin(),
box2->getExtents(d).getMin(),
"Extents of box do not match");
this->compareTol(box1->getExtents(d).getMax(),
box2->getExtents(d).getMax(),
"Extents of box do not match");
}
this->compareTol(box1->getInverseVolume(), box2->getInverseVolume(),
"Box inverse volume does not match");
this->compareTol(box1->getSignal(), box2->getSignal(),
"Box signal does not match");
this->compareTol(box1->getErrorSquared(), box2->getErrorSquared(),
"Box error squared does not match");
if (m_CheckEvents)
this->compare(box1->getNPoints(), box2->getNPoints(),
"Number of points in box does not match");
// Are both MDGridBoxes ?
auto *gridbox1 = dynamic_cast<MDGridBox<MDE, nd> *>(box1);
auto *gridbox2 = dynamic_cast<MDGridBox<MDE, nd> *>(box2);
if (gridbox1 && gridbox2) {
for (size_t d = 0; d < nd; d++)
this->compareTol(gridbox1->getBoxSize(d), gridbox2->getBoxSize(d),
"Box sizes do not match");
}
// Are both MDBoxes (with events)
auto *mdbox1 = dynamic_cast<MDBox<MDE, nd> *>(box1);
auto *mdbox2 = dynamic_cast<MDBox<MDE, nd> *>(box2);
if (mdbox1 && mdbox2) {
if (m_CheckEvents) {
const std::vector<MDE> &events1 = mdbox1->getConstEvents();
const std::vector<MDE> &events2 = mdbox2->getConstEvents();
try {
this->compare(events1.size(), events2.size(),
"Box event vectors are not the same length");
if (events1.size() == events2.size() && events1.size() > 2) {
// Check first and last event
for (size_t i = 0; i < events1.size(); i++) {
for (size_t d = 0; d < nd; d++) {
this->compareTol(events1[i].getCenter(d),
events2[i].getCenter(d),
"Event center does not match");
}
this->compareTol(events1[i].getSignal(), events2[i].getSignal(),
"Event signal does not match");
this->compareTol(events1[i].getErrorSquared(),
events2[i].getErrorSquared(),
"Event error does not match");
}
}
} catch (CompareFailsException &) {
// Boxes must release events if the check fails
mdbox1->releaseEvents();
mdbox2->releaseEvents();
throw;
}
mdbox1->releaseEvents();
mdbox2->releaseEvents();
} // Don't compare if BoxStructureOnly
} // is mdbox1
}
}
//----------------------------------------------------------------------------------------------
/** Perform comparison, set m_result if not matching.
*/
void CompareMDWorkspaces::doComparison() {
m_tolerance = getProperty("Tolerance");
m_CheckEvents = getProperty("CheckEvents");
IMDWorkspace_sptr ws1 = getProperty("Workspace1");
IMDWorkspace_sptr ws2 = getProperty("Workspace2");
inWS2 = ws2;
if (!ws1 || !ws2)
throw std::invalid_argument("Invalid workspace given.");
MatrixWorkspace_sptr mws1 = std::dynamic_pointer_cast<MatrixWorkspace>(ws1);
MatrixWorkspace_sptr mws2 = std::dynamic_pointer_cast<MatrixWorkspace>(ws2);
if (mws1 || mws2)
throw std::invalid_argument("Cannot compare MatrixWorkspaces. Please use "
"CompareWorkspaces algorithm instead.");
MDHistoWorkspace_sptr histo1 =
std::dynamic_pointer_cast<MDHistoWorkspace>(ws1);
MDHistoWorkspace_sptr histo2 =
std::dynamic_pointer_cast<MDHistoWorkspace>(ws2);
IMDEventWorkspace_sptr event1 =
std::dynamic_pointer_cast<IMDEventWorkspace>(ws1);
IMDEventWorkspace_sptr event2 =
std::dynamic_pointer_cast<IMDEventWorkspace>(ws2);
try {
compare(ws1->id(), ws2->id(), "Workspaces are of different types");
this->compareMDGeometry(ws1, ws2);
if (histo1 && histo2) {
this->compareMDHistoWorkspaces(histo1, histo2);
} else if (event1 && event2) {
CALL_MDEVENT_FUNCTION(this->compareMDWorkspaces, event1);
} else
m_result = "Workspaces are of different types.";
} catch (CompareFailsException &e) {
m_result = e.getMessage();
}
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void CompareMDWorkspaces::exec() {
m_result.clear();
m_CompareBoxID = !getProperty("IgnoreBoxID");
this->doComparison();
if (!m_result.empty()) {
g_log.notice() << "The workspaces did not match: " << m_result << '\n';
this->setProperty("Equals", false);
} else {
m_result = "Success!";
this->setProperty("Equals", true);
}
setProperty("Result", m_result);
}
} // namespace MDAlgorithms
} // namespace Mantid