/
FindClusterFaces.cpp
398 lines (334 loc) · 14.1 KB
/
FindClusterFaces.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "MantidCrystal/FindClusterFaces.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/IMDIterator.h"
#include "MantidAPI/IPeaksWorkspace.h"
#include "MantidAPI/IMDHistoWorkspace.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidGeometry/Crystal/IPeak.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/EnabledWhenProperty.h"
#include "MantidKernel/Utils.h"
#include "MantidCrystal/PeakClusterProjection.h"
using namespace Mantid::Kernel;
using namespace Mantid::Geometry;
using namespace Mantid::API;
namespace {
using namespace Mantid::Crystal;
// Map of label ids to peak index in peaks workspace.
typedef std::map<int, int> LabelMap;
// Optional set of labels
typedef boost::optional<LabelMap> OptionalLabelPeakIndexMap;
/**
* Create an optional label set for filtering.
* @param dimensionality : Dimensionality of the workspace.
* @param emptyLabelId : Label id corresponding to empty.
* @param filterWorkspace : Peaks workspace to act as filter.
* @param clusterImage : Image
* @return (optional) Map of labels to inspect for to the Peaks Index in the
* peaks workspace which matches the cluster id.
*/
OptionalLabelPeakIndexMap
createOptionalLabelFilter(size_t dimensionality, int emptyLabelId,
IPeaksWorkspace_sptr filterWorkspace,
IMDHistoWorkspace_sptr &clusterImage) {
OptionalLabelPeakIndexMap optionalAllowedLabels;
if (filterWorkspace) {
if (dimensionality < 3) {
throw std::invalid_argument("A FilterWorkspace has been given, but the "
"dimensionality of the labeled workspace is "
"< 3.");
}
LabelMap allowedLabels;
PeakClusterProjection projection(clusterImage);
for (int i = 0; i < filterWorkspace->getNumberPeaks(); ++i) {
Mantid::Geometry::IPeak &peak = filterWorkspace->getPeak(i);
const int labelIdAtPeakCenter =
static_cast<int>(projection.signalAtPeakCenter(peak));
if (labelIdAtPeakCenter > emptyLabelId) {
allowedLabels.emplace(labelIdAtPeakCenter, i);
}
}
optionalAllowedLabels = allowedLabels;
}
return optionalAllowedLabels;
}
/**
Type to represent cluster face (a.k.a a row in the output table)
*/
struct ClusterFace {
int clusterId;
size_t workspaceIndex;
int faceNormalDimension;
bool maxEdge;
double radius;
};
typedef std::deque<ClusterFace> ClusterFaces;
typedef std::vector<ClusterFaces> VecClusterFaces;
/**
Check that the data point signal value is an integer.
@param linearIndex : MDHistoWorkspace linear index
@param signalValue : signalValue at linearIndex
@throws runtime_error if signalValue is not an integer
*/
void checkDataPoint(const size_t &linearIndex, const double signalValue) {
double intPart;
// Check that the signal value looks like a label id.
if (std::modf(signalValue, &intPart) != 0.0) {
std::stringstream buffer;
buffer << "Problem at linear index: " << linearIndex
<< " SignalValue is not an integer: " << signalValue
<< " Suggests wrong input IMDHistoWorkspace passed to "
"FindClusterFaces.";
throw std::runtime_error(buffer.str());
}
}
/**
Find faces at the workspace index and write them to the localClusterFaces
container
@param linearIndex : IMDHistoWorkspace linear index
@param mdIterator : workspace iterator
@param clusterImage : IMDHistoWorkspace image
@param radius : radius from peak centre
@param id : label id
@param emptyLabelId : definition of empty label id
@param imageShape : shape of IMDHistoWorkspace
@param localClusterFaces : collection of cluster faces to add faces to
(writable)
*/
void findFacesAtIndex(const size_t linearIndex, IMDIterator *mdIterator,
IMDHistoWorkspace_sptr &clusterImage,
const double &radius, const int &id,
const int &emptyLabelId,
const std::vector<size_t> &imageShape,
ClusterFaces &localClusterFaces) {
auto indexes =
Mantid::Kernel::Utils::getIndicesFromLinearIndex(linearIndex, imageShape);
const auto neighbours = mdIterator->findNeighbourIndexesFaceTouching();
for (auto neighbourLinearIndex : neighbours) {
const int neighbourId =
static_cast<int>(clusterImage->getSignalAt(neighbourLinearIndex));
if (neighbourId <= emptyLabelId) {
// We have an edge!
// In which dimension is the edge?
auto neighbourIndexes = Mantid::Kernel::Utils::getIndicesFromLinearIndex(
neighbourLinearIndex, imageShape);
for (size_t j = 0; j < imageShape.size(); ++j) {
if (indexes[j] != neighbourIndexes[j]) {
const bool maxEdge = neighbourLinearIndex > linearIndex;
ClusterFace face;
face.clusterId = id;
face.workspaceIndex = linearIndex;
face.faceNormalDimension = static_cast<int>(j);
face.maxEdge = maxEdge;
face.radius = radius;
localClusterFaces.push_back(face);
}
}
}
}
}
/**
Process without peak filtering
@param mdIterator : workspace iterator
@param localClusterFaces : collection of cluster faces to add faces to
(writable)
@param progress : progress reporting object
@param clusterImage : IMDHistoWorkspace image
@param imageShape : shape of IMDHistoWorkspace
*/
void executeUnFiltered(IMDIterator *mdIterator, ClusterFaces &localClusterFaces,
Progress &progress, IMDHistoWorkspace_sptr &clusterImage,
const std::vector<size_t> &imageShape) {
const int emptyLabelId = 0;
const double radius = -1;
do {
const Mantid::signal_t signalValue = mdIterator->getSignal();
const int id = static_cast<int>(signalValue);
if (id > emptyLabelId) {
const size_t linearIndex = mdIterator->getLinearIndex();
// Sanity check the signal value.
checkDataPoint(linearIndex, signalValue);
progress.report();
// Find faces
findFacesAtIndex(linearIndex, mdIterator, clusterImage, radius, id,
emptyLabelId, imageShape, localClusterFaces);
}
} while (mdIterator->next());
}
/**
Process with peak filtering
@param mdIterator : workspace iterator
@param localClusterFaces : collection of cluster faces to add faces to
(writable)
@param progress : progress reporting object
@param clusterImage : IMDHistoWorkspace image
@param imageShape : shape of IMDHistoWorkspace
@param filterWorkspace : Peaks workspace to use as a filter
@param optionalAllowedLabels : Labels to consider (ignoring any others) in
processing.
*/
void executeFiltered(IMDIterator *mdIterator, ClusterFaces &localClusterFaces,
Progress &progress, IMDHistoWorkspace_sptr &clusterImage,
const std::vector<size_t> &imageShape,
IPeaksWorkspace_sptr &filterWorkspace,
const OptionalLabelPeakIndexMap &optionalAllowedLabels) {
const int emptyLabelId = 0;
PeakClusterProjection projection(clusterImage);
do {
const Mantid::signal_t signalValue = mdIterator->getSignal();
const int id = static_cast<int>(signalValue);
auto it = optionalAllowedLabels->find(id);
if (it != optionalAllowedLabels->end()) {
if (id > emptyLabelId) {
const size_t linearIndex = mdIterator->getLinearIndex();
// Sanity check data.
checkDataPoint(linearIndex, signalValue);
// Find the peak center
const int &peakIndex = it->second;
const IPeak &peak = filterWorkspace->getPeak(peakIndex);
V3D peakCenter = projection.peakCenter(peak);
// Calculate the radius
VMD positionND = clusterImage->getCenter(mdIterator->getLinearIndex());
V3D cellPosition(positionND[0], positionND[1], positionND[2]);
double radius = cellPosition.distance(peakCenter);
progress.report();
// Find faces
findFacesAtIndex(linearIndex, mdIterator, clusterImage, radius, id,
emptyLabelId, imageShape, localClusterFaces);
}
}
} while (mdIterator->next());
}
}
namespace Mantid {
namespace Crystal {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(FindClusterFaces)
//----------------------------------------------------------------------------------------------
/** Constructor
*/
FindClusterFaces::FindClusterFaces() {}
//----------------------------------------------------------------------------------------------
/** Destructor
*/
FindClusterFaces::~FindClusterFaces() {}
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string FindClusterFaces::name() const { return "FindClusterFaces"; }
/// Algorithm's version for identification. @see Algorithm::version
int FindClusterFaces::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string FindClusterFaces::category() const {
return "Crystal\\Integration";
}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void FindClusterFaces::init() {
declareProperty(make_unique<WorkspaceProperty<IMDHistoWorkspace>>(
"InputWorkspace", "", Direction::Input),
"An input image workspace consisting of cluster ids.");
declareProperty(
make_unique<WorkspaceProperty<IPeaksWorkspace>>(
"FilterWorkspace", "", Direction::Input, PropertyMode::Optional),
"Optional filtering peaks workspace. Used to restrict face finding to "
"clusters in image which correspond to peaks in the workspace.");
declareProperty("LimitRows", true,
"Limit the report output to a maximum number of rows");
declareProperty(make_unique<PropertyWithValue<int>>(
"MaximumRows", 100000,
boost::make_shared<BoundedValidator<int>>(),
Direction::Input),
"The number of neighbours to utilise. Defaults to 100000.");
setPropertySettings(
"MaximumRows", make_unique<EnabledWhenProperty>("LimitRows", IS_DEFAULT));
declareProperty(
make_unique<WorkspaceProperty<ITableWorkspace>>("OutputWorkspace", "",
Direction::Output),
"An output table workspace containing cluster face information.");
declareProperty(make_unique<PropertyWithValue<bool>>("TruncatedOutput", false,
Direction::Output),
"Indicates that the output results were truncated if True");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void FindClusterFaces::exec() {
IMDHistoWorkspace_sptr clusterImage = getProperty("InputWorkspace");
const int emptyLabelId = 0;
std::vector<size_t> imageShape;
const size_t dimensionality = clusterImage->getNumDims();
for (size_t i = 0; i < dimensionality; ++i) {
imageShape.push_back(clusterImage->getDimension(i)->getNBins());
}
// Get the peaks workspace
IPeaksWorkspace_sptr filterWorkspace = this->getProperty("FilterWorkspace");
// Use the peaks workspace to filter to labels of interest
OptionalLabelPeakIndexMap optionalAllowedLabels = createOptionalLabelFilter(
dimensionality, emptyLabelId, filterWorkspace, clusterImage);
const int nThreads = Mantid::API::FrameworkManager::Instance()
.getNumOMPThreads(); // NThreads to Request
auto mdIterators = clusterImage->createIterators(nThreads); // Iterators
const int nIterators =
static_cast<int>(mdIterators.size()); // Number of iterators yielded.
VecClusterFaces clusterFaces(nIterators);
size_t nSteps = 1000;
if (optionalAllowedLabels.is_initialized()) {
nSteps = optionalAllowedLabels->size();
}
const bool usingFiltering = optionalAllowedLabels.is_initialized();
Progress progress(this, 0, 1, nSteps);
PARALLEL_FOR_NO_WSP_CHECK()
for (int it = 0; it < nIterators; ++it) {
PARALLEL_START_INTERUPT_REGION
ClusterFaces &localClusterFaces = clusterFaces[it];
auto mdIterator = mdIterators[it];
if (usingFiltering) {
executeFiltered(mdIterator, localClusterFaces, progress, clusterImage,
imageShape, filterWorkspace, optionalAllowedLabels);
} else {
executeUnFiltered(mdIterator, localClusterFaces, progress, clusterImage,
imageShape);
}
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
const bool limitRows = getProperty("LimitRows");
const int maxRows = getProperty("MaximumRows");
// Create an output table workspace now that all local cluster faces have been
// found in parallel.
auto out = WorkspaceFactory::Instance().createTable("TableWorkspace");
out->addColumn("int", "ClusterId");
out->addColumn("double", "MDWorkspaceIndex");
out->addColumn("int", "FaceNormalDimension");
out->addColumn("bool", "MaxEdge");
out->addColumn("double", "Radius");
size_t totalFaces = 0;
for (int i = 0; i < nIterators; ++i) {
const ClusterFaces &localClusterFaces = clusterFaces[i];
for (const auto &clusterFace : localClusterFaces) {
if (!limitRows || (out->rowCount() < size_t(maxRows))) {
TableRow row = out->appendRow();
row << clusterFace.clusterId << double(clusterFace.workspaceIndex)
<< clusterFace.faceNormalDimension << clusterFace.maxEdge
<< clusterFace.radius;
}
++totalFaces;
}
}
bool truncatedOutput = false;
if (limitRows && out->rowCount() == size_t(maxRows)) {
truncatedOutput = true;
std::stringstream buffer;
buffer << "More faces found than can be reported given the MaximumRows "
"limit. Row limit at: " << maxRows
<< " Total faces available: " << totalFaces;
g_log.warning(buffer.str());
}
setProperty("OutputWorkspace", out);
setProperty("TruncatedOutput", truncatedOutput);
}
} // namespace Crystal
} // namespace Mantid