Mantid
Loading...
Searching...
No Matches
NotebookBuilder.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 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
13#include "MantidKernel/Logger.h"
16
17#include <boost/utility.hpp>
18#include <utility>
19
20namespace Mantid::API {
21
24
25namespace {
26Mantid::Kernel::Logger g_log("NotebookBuilder");
27}
28
29NotebookBuilder::NotebookBuilder(const std::shared_ptr<HistoryView> &view, std::string versionSpecificity)
30 : m_historyItems(view->getAlgorithmsList()), m_output(), m_versionSpecificity(std::move(versionSpecificity)),
31 m_nb_writer(new NotebookWriter()) {}
32
41const std::string NotebookBuilder::build(const std::string &ws_name, const std::string &ws_title,
42 const std::string &ws_comment) {
43 // record workspace details in notebook
44 std::string workspace_details;
45 workspace_details = "Workspace History: " + ws_name + "\n";
46 workspace_details += "------------------------\n";
47 workspace_details += ws_title + "\n";
48 workspace_details += ws_comment;
49 m_nb_writer->markdownCell(workspace_details);
50
51 auto iter = m_historyItems.begin();
52 for (; iter != m_historyItems.end(); ++iter) {
54 }
55 return m_nb_writer->writeNotebook();
56}
57
66void NotebookBuilder::writeHistoryToStream(std::vector<HistoryItem>::const_iterator &iter) {
67 auto algHistory = iter->getAlgorithmHistory();
68 if (iter->isUnrolled()) {
69
70 m_nb_writer->markdownCell(std::string("Child algorithms of ") + algHistory->name());
71
72 buildChildren(iter);
73
74 m_nb_writer->markdownCell(std::string("End of child algorithms of ") + algHistory->name());
75
76 } else {
77 // create the string for this algorithm
78 m_nb_writer->codeCell(buildAlgorithmString(algHistory));
79 }
80}
81
90void NotebookBuilder::buildChildren(std::vector<HistoryItem>::const_iterator &iter) {
91 size_t numChildren = iter->numberOfChildren();
92 ++iter; // move to first child
93 for (size_t i = 0; i < numChildren && iter != m_historyItems.end(); ++i, ++iter) {
95 }
96 --iter;
97}
98
106 std::ostringstream properties;
107 const std::string name = algHistory->name();
108
109 auto props = algHistory->getProperties();
110 for (const auto &propIter : props) {
111 std::string prop = buildPropertyString(propIter);
112 if (prop.length() > 0) {
113 properties << prop << ", ";
114 }
115 }
116
117 // Three cases, we can either specify the version of every algorithm...
118 if (m_versionSpecificity == "all") {
119 properties << "Version=" << algHistory->version() << ", ";
120 } else if (m_versionSpecificity == "old") {
121 //...or only specify algorithm versions when they're not the newest version
122 const auto &algName = algHistory->name();
123 auto &algFactory = API::AlgorithmFactory::Instance();
124 int latestVersion = 0;
125
126 if (algFactory.exists(algName)) { // Check the alg still exists in Mantid
127 latestVersion = AlgorithmFactory::Instance().highestVersion(algName);
128 }
129 // If a newer version of this algorithm exists, then this must be an old
130 // version.
131 if (latestVersion > algHistory->version()) {
132 properties << "Version=" << algHistory->version() << ", ";
133 }
134 }
135 // Third case is we never specify the version, so do nothing.
136
137 std::string propStr = properties.str();
138 if (propStr.length() > 0) {
139 // remove trailing comma & space
140 propStr.erase(propStr.size() - 1);
141 propStr.erase(propStr.size() - 1);
142 }
143
144 return name + "(" + propStr + ")";
145}
146
147GNU_DIAG_OFF("maybe-uninitialized")
148
149
155const std::string NotebookBuilder::buildPropertyString(const PropertyHistory_const_sptr &propHistory) {
157
158 std::string prop;
159 // No need to specify value for default properties
160 if (!propHistory->isDefault()) {
161 // Create a vector of all non workspace property type names
162 std::vector<std::string> nonWorkspaceTypes{"number", "boolean", "string"};
163 // Do not give values to output properties other than workspace properties
164 if (find(nonWorkspaceTypes.begin(), nonWorkspaceTypes.end(), propHistory->type()) != nonWorkspaceTypes.end() &&
165 propHistory->direction() == Direction::Output) {
166 g_log.debug() << "Ignoring property " << propHistory->name() << " of type " << propHistory->type() << '\n';
167 // Handle numerical properties
168 } else if (propHistory->type() == "number") {
169 prop = propHistory->name() + "=" + propHistory->value();
170 // Handle boolean properties
171 } else if (propHistory->type() == "boolean") {
172 std::string value = (propHistory->value() == "1" ? "True" : "False");
173 prop = propHistory->name() + "=" + value;
174 // Handle all other property types
175 } else {
176 std::string opener = "='";
177 if (propHistory->value().find('\\') != std::string::npos) {
178 opener = "=r'";
179 }
180
181 prop = propHistory->name() + opener + propHistory->value() + "'";
182 }
183 }
184
185 return prop;
186}
187
188GNU_DIAG_ON("maybe-uninitialized")
189
190} // namespace Mantid::API
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
#define GNU_DIAG_ON(x)
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
void buildChildren(std::vector< HistoryItem >::const_iterator &iter)
Iterate over each of the items children and output them to the script.
const std::vector< HistoryItem > m_historyItems
const std::string buildAlgorithmString(const AlgorithmHistory_const_sptr &algHistory)
Build the script output for a single algorithm.
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
std::unique_ptr< NotebookWriter > m_nb_writer
NotebookBuilder(const std::shared_ptr< HistoryView > &view, std::string versionSpecificity="old")
const std::string buildPropertyString(const Mantid::Kernel::PropertyHistory_const_sptr &propHistory)
Build the script output for a single property.
void writeHistoryToStream(std::vector< HistoryItem >::const_iterator &iter)
Write out an algorithm to the notebook.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition Logger.h:51
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
std::shared_ptr< const AlgorithmHistory > AlgorithmHistory_const_sptr
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< const PropertyHistory > PropertyHistory_const_sptr
std::shared_ptr< PropertyHistory > PropertyHistory_sptr
STL namespace.
Describes the direction (within an algorithm) of a Property.
Definition Property.h:50
@ Output
An output workspace.
Definition Property.h:54