-
Notifications
You must be signed in to change notification settings - Fork 122
/
GenerateIPythonNotebook.cpp
118 lines (96 loc) · 3.83 KB
/
GenerateIPythonNotebook.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
#include "MantidAlgorithms/GenerateIPythonNotebook.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/System.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/AlgorithmHistory.h"
#include "MantidAPI/NotebookBuilder.h"
#include "MantidAPI/Workspace.h"
#include <fstream>
using namespace Mantid::Kernel;
using namespace Mantid::API;
namespace {
Mantid::Kernel::Logger g_log("GenerateIPythonNotebook");
}
namespace Mantid {
namespace Algorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(GenerateIPythonNotebook)
/** Initialize the algorithm's properties.
*/
void GenerateIPythonNotebook::init() {
declareProperty(make_unique<WorkspaceProperty<Workspace>>(
"InputWorkspace", "", Direction::Input),
"An input workspace.");
std::vector<std::string> exts{".ipynb"};
declareProperty(Kernel::make_unique<API::FileProperty>(
"Filename", "", API::FileProperty::OptionalSave, exts),
"The name of the file into which the workspace history will "
"be generated.");
declareProperty("NotebookText", std::string(""),
"Saves the history of the workspace to a variable.",
Direction::Output);
getPointerToProperty("NotebookText")->setAutoTrim(false);
declareProperty("UnrollAll", false,
"Unroll all algorithms to show just their child algorithms.",
Direction::Input);
declareProperty("StartTimestamp", std::string(""),
"The filter start time in the format YYYY-MM-DD HH:mm:ss",
Direction::Input);
declareProperty("EndTimestamp", std::string(""),
"The filter end time in the format YYYY-MM-DD HH:mm:ss",
Direction::Input);
std::vector<std::string> saveVersions{"Specify Old", "Specify All",
"Specify None"};
declareProperty(
"SpecifyAlgorithmVersions", "Specify Old",
boost::make_shared<StringListValidator>(saveVersions),
"When to specify which algorithm version was used by Mantid.");
}
/** Execute the algorithm.
*/
void GenerateIPythonNotebook::exec() {
const Workspace_const_sptr ws = getProperty("InputWorkspace");
const bool unrollAll = getProperty("UnrollAll");
const std::string startTime = getProperty("StartTimestamp");
const std::string endTime = getProperty("EndTimestamp");
const std::string saveVersions = getProperty("SpecifyAlgorithmVersions");
// Get the algorithm histories of the workspace.
const WorkspaceHistory wsHistory = ws->getHistory();
g_log.information() << "Number of history items: " << wsHistory.size()
<< '\n';
auto view = wsHistory.createView();
if (unrollAll) {
view->unrollAll();
}
// Need at least a start time to do time filter
if (startTime != "") {
if (endTime == "") {
// If no end time was given then filter up to now
view->filterBetweenExecDate(DateAndTime(startTime));
} else {
view->filterBetweenExecDate(DateAndTime(startTime), DateAndTime(endTime));
}
}
std::string versionSpecificity;
if (saveVersions == "Specify Old")
versionSpecificity = "old";
else if (saveVersions == "Specify None")
versionSpecificity = "none";
else
versionSpecificity = "all";
NotebookBuilder builder(view, versionSpecificity);
std::string generatedNotebook;
generatedNotebook +=
builder.build(ws->getName(), ws->getTitle(), ws->getComment());
setPropertyValue("NotebookText", generatedNotebook);
const std::string filename = getPropertyValue("Filename");
if (!filename.empty()) {
std::ofstream file(filename.c_str(), std::ofstream::trunc);
file << generatedNotebook;
file.flush();
file.close();
}
}
} // namespace Algorithms
} // namespace Mantid