Mantid
Loading...
Searching...
No Matches
GenerateIPythonNotebook.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 +
12#include "MantidAPI/Workspace.h"
14#include "MantidKernel/System.h"
15
16#include <fstream>
17
18using namespace Mantid::Kernel;
19using namespace Mantid::API;
20using Mantid::Types::Core::DateAndTime;
21
22namespace {
23Mantid::Kernel::Logger g_log("GenerateIPythonNotebook");
24}
25
26namespace Mantid::Algorithms {
27
28// Register the algorithm into the AlgorithmFactory
29DECLARE_ALGORITHM(GenerateIPythonNotebook)
30
31
34 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("InputWorkspace", "", Direction::Input),
35 "An input workspace.");
36
37 std::vector<std::string> exts{".ipynb"};
38
39 declareProperty(std::make_unique<API::FileProperty>("Filename", "", API::FileProperty::OptionalSave, exts),
40 "The name of the file into which the workspace history will "
41 "be generated.");
42 declareProperty("NotebookText", std::string(""), "Saves the history of the workspace to a variable.",
44 getPointerToProperty("NotebookText")->setAutoTrim(false);
45
46 declareProperty("UnrollAll", false, "Unroll all algorithms to show just their child algorithms.", Direction::Input);
47
48 declareProperty("StartTimestamp", std::string(""), "The filter start time in the format YYYY-MM-DD HH:mm:ss",
50 declareProperty("EndTimestamp", std::string(""), "The filter end time in the format YYYY-MM-DD HH:mm:ss",
52
53 std::vector<std::string> saveVersions{"Specify Old", "Specify All", "Specify None"};
54 declareProperty("SpecifyAlgorithmVersions", "Specify Old", std::make_shared<StringListValidator>(saveVersions),
55 "When to specify which algorithm version was used by Mantid.");
56}
57
61 const Workspace_const_sptr ws = getProperty("InputWorkspace");
62 const bool unrollAll = getProperty("UnrollAll");
63 const std::string startTime = getProperty("StartTimestamp");
64 const std::string endTime = getProperty("EndTimestamp");
65 const std::string saveVersions = getProperty("SpecifyAlgorithmVersions");
66
67 // Get the algorithm histories of the workspace.
68 const WorkspaceHistory wsHistory = ws->getHistory();
69 g_log.information() << "Number of history items: " << wsHistory.size() << '\n';
70
71 auto view = wsHistory.createView();
72
73 if (unrollAll) {
74 view->unrollAll();
75 }
76
77 // Need at least a start time to do time filter
78 if (!startTime.empty()) {
79 if (endTime.empty()) {
80 // If no end time was given then filter up to now
81 view->filterBetweenExecDate(DateAndTime(startTime));
82 } else {
83 view->filterBetweenExecDate(DateAndTime(startTime), DateAndTime(endTime));
84 }
85 }
86
87 std::string versionSpecificity;
88 if (saveVersions == "Specify Old")
89 versionSpecificity = "old";
90 else if (saveVersions == "Specify None")
91 versionSpecificity = "none";
92 else
93 versionSpecificity = "all";
94
95 NotebookBuilder builder(view, versionSpecificity);
96 std::string generatedNotebook;
97 generatedNotebook += builder.build(ws->getName(), ws->getTitle(), ws->getComment());
98
99 setPropertyValue("NotebookText", generatedNotebook);
100
101 const std::string filename = getPropertyValue("Filename");
102
103 if (!filename.empty()) {
104 std::ofstream file(filename.c_str(), std::ofstream::trunc);
105 file << generatedNotebook;
106 file.flush();
107 file.close();
108 }
109}
110
111} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
void setPropertyValue(const std::string &name, const std::string &value) override
Set the value of a property by string N.B.
Definition: Algorithm.cpp:1975
@ OptionalSave
to specify a file to write to but an empty string is
Definition: FileProperty.h:50
const std::string build(const std::string &ws_name, const std::string &ws_title, const std::string &ws_comment)
build an ipython notebook from the history view
This class stores information about the Workspace History used by algorithms on a workspace and the e...
std::shared_ptr< HistoryView > createView() const
Create a flat view of the workspaces algorithm history.
size_t size() const
How many entries are there.
A property class for workspaces.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< const Workspace > Workspace_const_sptr
shared pointer to Mantid::API::Workspace (const version)
Definition: Workspace_fwd.h:22
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54