/
AccumulateMD.cpp
524 lines (458 loc) · 19.8 KB
/
AccumulateMD.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#include "MantidMDAlgorithms/AccumulateMD.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/ArrayBoundedValidator.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/PropertyWithValue.h"
#include "MantidAPI/FileFinder.h"
#include "MantidAPI/HistoryView.h"
#include "MantidDataObjects/MDHistoWorkspaceIterator.h"
#include "MantidAPI/FileProperty.h"
#include "MantidKernel/EnabledWhenProperty.h"
#include <Poco/File.h>
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
namespace Mantid {
namespace MDAlgorithms {
/*
* Reduce the vector of input data to only data files and workspaces which can
* be found
* @param input_data :: Vector of input data sources
* @param psi :: Vector of goniometer angle psi containing a value for each data
* source
* @param gl :: Vector of goniometer anglegl containing a value for each data
* source
* @param gs :: Vector of goniometer angle gs containing a value for each data
* source
* @param efix :: Vector of data source energy values in meV
* @returns names of data sources which cannot be found
*/
std::string filterToExistingSources(std::vector<std::string> &input_data,
std::vector<double> &psi,
std::vector<double> &gl,
std::vector<double> &gs,
std::vector<double> &efix) {
std::ostringstream nonexistent;
for (size_t i = input_data.size(); i > 0; i--) {
if (!dataExists(input_data[i - 1])) {
nonexistent << input_data[i - 1] << ",";
input_data.erase(input_data.begin() + i - 1);
psi.erase(psi.begin() + i - 1);
gl.erase(gl.begin() + i - 1);
gs.erase(gs.begin() + i - 1);
efix.erase(efix.begin() + i - 1);
}
}
return nonexistent.str();
}
/*
* Return true if dataName is an existing workspace or file
* @param data_name :: Workspace name or file name
* @returns true if a workspace or file with given name exists
*/
bool dataExists(const std::string &data_name) {
const std::string filepath =
Mantid::API::FileFinder::Instance().getFullPath(data_name);
// Calls to the ADS in algorithms like this should ordinarily
// be avoided, unfortunately we have little choice in this case.
// If we gave FileFinder an absolute path it just returns it (whether or not
// the file exists) so we must also check the full path returned with
// fileExists()
return (AnalysisDataService::Instance().doesExist(data_name) ||
fileExists(filepath));
}
/*
* Test if a file with this full path exists
* @param filename :: full path of a file to test existence of
* @returns true if the file exists
*/
bool fileExists(const std::string &filename) {
if (filename.empty())
return false;
Poco::File test_file(filename);
return test_file.exists();
}
/*
* Remove anything from input_data which is already in current_data
* @param input_data :: Vector of input data sources
* @param current_data :: Vector of data sources previously appended to
* workspace
* @param psi :: Vector of goniometer angle psi containing a value for each data
* source
* @param gl :: Vector of goniometer anglegl containing a value for each data
* source
* @param gs :: Vector of goniometer angle gs containing a value for each data
* source
* @param efix :: Vector of data source energy values in meV
* @returns data sources which are already in the workspace
*/
std::string filterToNew(std::vector<std::string> &input_data,
std::vector<std::string> ¤t_data,
std::vector<double> &psi, std::vector<double> &gl,
std::vector<double> &gs, std::vector<double> &efix) {
std::ostringstream old_sources;
for (size_t i = input_data.size(); i > 0; i--) {
if (appearsInCurrentData(input_data[i - 1], current_data)) {
old_sources << input_data[i - 1] << ",";
input_data.erase(input_data.begin() + i - 1);
psi.erase(psi.begin() + i - 1);
gl.erase(gl.begin() + i - 1);
gs.erase(gs.begin() + i - 1);
efix.erase(efix.begin() + i - 1);
}
}
return old_sources.str();
}
/*
* Check if the named data source is in the vector of data currently in the
* workspace
* @param data_source :: Name of a data source
* @param current_data :: Vector of data sources previously appended to
* workspace
* @returns true if the named data source appears in the vector of current data
*/
bool appearsInCurrentData(const std::string &data_source,
std::vector<std::string> ¤t_data) {
for (auto reverse_iter = current_data.rbegin();
reverse_iter != current_data.rend(); ++reverse_iter) {
if (data_source == *reverse_iter) {
return true;
}
}
return false;
}
/*
* Return a vector of the names of files and workspaces which have been
* previously added to the workspace
* @param ws_history :: History of the workspace
* @returns a vector of the names of data_sources which have previously been
* appended to the workspace
*/
std::vector<std::string>
getHistoricalDataSources(const WorkspaceHistory &ws_history,
const std::string &create_alg_name,
const std::string &accumulate_alg_name) {
// Using a set so we only insert unique names
std::unordered_set<std::string> historical_data_sources;
// Get previously added data sources from DataSources property of the original
// call of CreateMD and any subsequent calls of AccumulateMD
auto view = ws_history.createView();
view->unrollAll();
const std::vector<HistoryItem> history_items = view->getAlgorithmsList();
for (const auto &history_item : history_items) {
auto alg_history = history_item.getAlgorithmHistory();
if (alg_history->name() == create_alg_name ||
alg_history->name() == accumulate_alg_name) {
auto props = alg_history->getProperties();
for (auto &prop : props) {
PropertyHistory_const_sptr prop_history = prop;
if (prop_history->name() == "DataSources") {
insertDataSources(prop_history->value(), historical_data_sources);
}
}
}
}
std::vector<std::string> result(historical_data_sources.begin(),
historical_data_sources.end());
return result;
}
/*
* Split string of data sources from workspace history and insert them into
* complete set of historical data sources
* @param data_sources :: string from workspace history containing list of data
* sources
* @param historical_data_sources :: set of data sources
*/
void insertDataSources(
const std::string &data_sources,
std::unordered_set<std::string> &historical_data_sources) {
// Split the property string into a vector of data sources
std::vector<std::string> data_split;
boost::split(data_split, data_sources, boost::is_any_of(","));
// Trim any whitespace from ends of each data source string
std::for_each(
data_split.begin(), data_split.end(),
boost::bind(boost::algorithm::trim<std::string>, _1, std::locale()));
// Insert each data source into our complete set of historical data sources
historical_data_sources.insert(data_split.begin(), data_split.end());
}
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(AccumulateMD)
/*
* Constructor
*/
AccumulateMD::AccumulateMD() {}
/*
* Destructor
*/
AccumulateMD::~AccumulateMD() {}
/// Algorithms name for identification. @see Algorithm::name
const std::string AccumulateMD::name() const { return "AccumulateMD"; }
/// Algorithm's version for identification. @see Algorithm::version
int AccumulateMD::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string AccumulateMD::category() const { return "MDAlgorithms"; }
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string AccumulateMD::summary() const {
return "Add new data to an existing MDHistoWorkspace";
}
/*
* Initialize the algorithm's properties.
*/
void AccumulateMD::init() {
declareProperty(make_unique<WorkspaceProperty<IMDEventWorkspace>>(
"InputWorkspace", "", Direction::Input),
"An input MDEventWorkspace to append data to.");
declareProperty(make_unique<WorkspaceProperty<IMDEventWorkspace>>(
"OutputWorkspace", "", Direction::Output),
"MDEventWorkspace with new data appended.");
declareProperty(
Kernel::make_unique<ArrayProperty<std::string>>(
"DataSources",
boost::make_shared<MandatoryValidator<std::vector<std::string>>>(),
Direction::Input),
"Input workspaces to process, or filenames to load and process");
declareProperty(make_unique<ArrayProperty<double>>("EFix", Direction::Input),
"datasource energy values in meV");
std::vector<std::string> e_mode_options{"Elastic", "Direct", "Indirect"};
declareProperty("Emode", "Direct",
boost::make_shared<StringListValidator>(e_mode_options),
"Analysis mode ['Elastic', 'Direct', 'Indirect'].");
declareProperty(
Kernel::make_unique<ArrayProperty<double>>(
"Alatt",
boost::make_shared<MandatoryValidator<std::vector<double>>>(),
Direction::Input),
"Lattice parameters");
declareProperty(
Kernel::make_unique<ArrayProperty<double>>(
"Angdeg",
boost::make_shared<MandatoryValidator<std::vector<double>>>(),
Direction::Input),
"Lattice angles");
declareProperty(
Kernel::make_unique<ArrayProperty<double>>(
"u", boost::make_shared<MandatoryValidator<std::vector<double>>>(),
Direction::Input),
"Lattice vector parallel to neutron beam");
declareProperty(
Kernel::make_unique<ArrayProperty<double>>(
"v", boost::make_shared<MandatoryValidator<std::vector<double>>>(),
Direction::Input),
"Lattice vector perpendicular to neutron beam in the horizontal plane");
declareProperty(make_unique<ArrayProperty<double>>("Psi", Direction::Input),
"Psi rotation in degrees. Optional or one entry per run.");
declareProperty(make_unique<ArrayProperty<double>>("Gl", Direction::Input),
"gl rotation in degrees. Optional or one entry per run.");
declareProperty(make_unique<ArrayProperty<double>>("Gs", Direction::Input),
"gs rotation in degrees. Optional or one entry per run.");
declareProperty(
make_unique<PropertyWithValue<bool>>("InPlace", true, Direction::Input),
"Execute conversions to MD and Merge in one-step. Less "
"memory overhead.");
declareProperty(
make_unique<PropertyWithValue<bool>>("Clean", false, Direction::Input),
"Create workspace from fresh rather than appending to "
"existing workspace data.");
declareProperty(
make_unique<FileProperty>("Filename", "", FileProperty::OptionalSave,
".nxs"),
"The name of the Nexus file to write, as a full or relative path.\n"
"Only used if FileBackEnd is true.");
setPropertySettings("Filename", make_unique<EnabledWhenProperty>(
"CreateFileBackEnd", IS_EQUAL_TO, "1"));
declareProperty("FileBackEnd", false,
"If true, Filename must also be specified. The algorithm "
"will create the specified file in addition to an output "
"workspace. The workspace will load data from the file on "
"demand in order to reduce memory use.");
}
/*
* Execute the algorithm.
*/
void AccumulateMD::exec() {
IMDEventWorkspace_sptr input_ws = this->getProperty("InputWorkspace");
std::vector<std::string> input_data = this->getProperty("DataSources");
const std::string out_filename = this->getProperty("Filename");
const bool filebackend = this->getProperty("FileBackEnd");
std::vector<double> psi = this->getProperty("Psi");
padParameterVector(psi, input_data.size());
std::vector<double> gl = this->getProperty("Gl");
padParameterVector(gl, input_data.size());
std::vector<double> gs = this->getProperty("Gs");
padParameterVector(gs, input_data.size());
std::vector<double> efix = this->getProperty("EFix");
padParameterVector(efix, input_data.size());
// Create progress reporting object
// Progress prog = Progress(this, 0.0, 1.0, 2);
this->progress(0);
const std::string nonexistent =
filterToExistingSources(input_data, psi, gl, gs, efix);
g_log.notice() << "These data sources were not found: " << nonexistent
<< std::endl;
// If we can't find any data, we can't do anything
if (input_data.empty()) {
g_log.warning() << "No data found matching input in " << this->name()
<< std::endl;
this->setProperty("OutputWorkspace", input_ws);
return; // POSSIBLE EXIT POINT
}
this->interruption_point();
// If Clean=True then just call CreateMD to create a fresh workspace and
// delete the old one, note this means we don't retain workspace history...
bool do_clean = this->getProperty("Clean");
if (do_clean) {
this->progress(0.5);
IMDEventWorkspace_sptr out_ws = createMDWorkspace(
input_data, psi, gl, gs, efix, out_filename, filebackend);
this->setProperty("OutputWorkspace", out_ws);
g_log.notice() << this->name() << " successfully created a clean workspace"
<< std::endl;
this->progress(1.0);
return; // POSSIBLE EXIT POINT
}
this->interruption_point();
// Find what files and workspaces have already been included in the workspace.
const WorkspaceHistory ws_history = input_ws->getHistory();
// Get name from algorithm like this so that an error is thrown if the
// name of the algorithm is changed
Algorithm_sptr create_alg = createChildAlgorithm("CreateMD");
std::vector<std::string> current_data =
getHistoricalDataSources(ws_history, create_alg->name(), this->name());
// If there's no new data, we don't have anything to do
const std::string old_sources =
filterToNew(input_data, current_data, psi, gl, gs, efix);
g_log.notice() << "Data from these sources are already in the workspace: "
<< old_sources << std::endl;
if (input_data.empty()) {
g_log.notice() << "No new data to append to workspace in " << this->name()
<< std::endl;
this->setProperty("OutputWorkspace", input_ws);
return; // POSSIBLE EXIT POINT
}
this->interruption_point();
// If we reach here then new data exists to append to the input workspace
// Use CreateMD with the new data to make a temp workspace
// Merge the temp workspace with the input workspace using MergeMD
IMDEventWorkspace_sptr tmp_ws =
createMDWorkspace(input_data, psi, gl, gs, efix, "", false);
this->interruption_point();
this->progress(0.5); // Report as CreateMD is complete
const std::string temp_ws_name = "TEMP_WORKSPACE_ACCUMULATEMD";
// Currently have to use ADS here as list of workspaces can only be passed as
// a list of workspace names as a string
AnalysisDataService::Instance().add(temp_ws_name, tmp_ws);
std::string ws_names_to_merge = input_ws->getName();
ws_names_to_merge.append(",");
ws_names_to_merge.append(temp_ws_name);
Algorithm_sptr merge_alg = createChildAlgorithm("MergeMD");
merge_alg->setProperty("InputWorkspaces", ws_names_to_merge);
merge_alg->executeAsChildAlg();
API::IMDEventWorkspace_sptr out_ws =
merge_alg->getProperty("OutputWorkspace");
this->setProperty("OutputWorkspace", out_ws);
g_log.notice() << this->name() << " successfully appended data" << std::endl;
this->progress(1.0); // Report as MergeMD is complete
// Clean up temporary workspace
AnalysisDataService::Instance().remove(temp_ws_name);
}
/*
* Use the CreateMD algorithm to create an MD workspace
* @param data_sources :: Vector of input data sources
* @param psi :: Vector of goniometer angle psi containing a value for each data
* source
* @param gl :: Vector of goniometer anglegl containing a value for each data
* source
* @param gs :: Vector of goniometer angle gs containing a value for each data
* source
* @param efix :: Vector of data source energy values in meV
* @returns the newly created workspace
*/
IMDEventWorkspace_sptr AccumulateMD::createMDWorkspace(
const std::vector<std::string> &data_sources,
const std::vector<double> &psi, const std::vector<double> &gl,
const std::vector<double> &gs, const std::vector<double> &efix,
const std::string &filename, const bool filebackend) {
Algorithm_sptr create_alg = createChildAlgorithm("CreateMD");
create_alg->setProperty("DataSources", data_sources);
create_alg->setProperty("EFix", efix);
create_alg->setPropertyValue("EMode", this->getPropertyValue("EMode"));
create_alg->setPropertyValue("Alatt", this->getPropertyValue("Alatt"));
create_alg->setPropertyValue("Angdeg", this->getPropertyValue("Angdeg"));
create_alg->setPropertyValue("u", this->getPropertyValue("u"));
create_alg->setPropertyValue("v", this->getPropertyValue("v"));
create_alg->setProperty("Psi", psi);
create_alg->setProperty("Gl", gl);
create_alg->setProperty("Gs", gs);
create_alg->setPropertyValue("InPlace", this->getPropertyValue("InPlace"));
if (filebackend) {
create_alg->setProperty("Filename", filename);
create_alg->setProperty("FileBackEnd", filebackend);
}
create_alg->executeAsChildAlg();
return create_alg->getProperty("OutputWorkspace");
}
/*
* Validate the input properties
* @returns a map of properties names with errors
*/
std::map<std::string, std::string> AccumulateMD::validateInputs() {
// Create the map
std::map<std::string, std::string> validation_output;
// Get properties to validate
const std::vector<std::string> data_sources =
this->getProperty("DataSources");
const std::vector<double> u = this->getProperty("u");
const std::vector<double> v = this->getProperty("v");
const std::vector<double> alatt = this->getProperty("Alatt");
const std::vector<double> angdeg = this->getProperty("Angdeg");
const std::vector<double> psi = this->getProperty("Psi");
const std::vector<double> gl = this->getProperty("Gl");
const std::vector<double> gs = this->getProperty("Gs");
const std::vector<double> efix = this->getProperty("Efix");
const std::string filename = this->getProperty("Filename");
const bool fileBackEnd = this->getProperty("FileBackEnd");
if (fileBackEnd && filename.empty()) {
validation_output["Filename"] =
"Filename must be given if FileBackEnd is required.";
}
const size_t ws_entries = data_sources.size();
if (u.size() < 3) {
validation_output["u"] = "u must have 3 components";
}
if (v.size() < 3) {
validation_output["v"] = "v must have 3 components";
}
if (alatt.size() < 3) {
validation_output["Alatt"] = "Lattice parameters must have 3 components";
}
if (angdeg.size() < 3) {
validation_output["Angdeg"] = "Angle must have 3 components";
}
if (!psi.empty() && psi.size() != ws_entries) {
validation_output["Psi"] = "If Psi is given an entry "
"should be provided for "
"every input datasource";
}
if (!gl.empty() && gl.size() != ws_entries) {
validation_output["Gl"] = "If Gl is given an entry "
"should be provided for "
"every input datasource";
}
if (!gs.empty() && gs.size() != ws_entries) {
validation_output["Gs"] = "If Gs is given an entry "
"should be provided for "
"every input datasource";
}
if (efix.size() > 1 && efix.size() != ws_entries) {
validation_output["EFix"] =
"Either specify a single EFix value, or as many "
"as there are input datasources";
}
return validation_output;
}
} // namespace MDAlgorithms
} // namespace Mantid