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