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"
15
16#include <boost/utility.hpp>
17#include <utility>
18
19namespace Mantid::API {
20
23
24namespace {
25Mantid::Kernel::Logger g_log("NotebookBuilder");
26}
27
28NotebookBuilder::NotebookBuilder(const std::shared_ptr<HistoryView> &view, std::string versionSpecificity)
29 : m_historyItems(view->getAlgorithmsList()), m_output(), m_versionSpecificity(std::move(versionSpecificity)),
30 m_nb_writer(new NotebookWriter()) {}
31
40const std::string NotebookBuilder::build(const std::string &ws_name, const std::string &ws_title,
41 const std::string &ws_comment) {
42 // record workspace details in notebook
43 std::string workspace_details;
44 workspace_details = "Workspace History: " + ws_name + "\n";
45 workspace_details += "------------------------\n";
46 workspace_details += ws_title + "\n";
47 workspace_details += ws_comment;
48 m_nb_writer->markdownCell(workspace_details);
49
50 auto iter = m_historyItems.begin();
51 for (; iter != m_historyItems.end(); ++iter) {
53 }
54 return m_nb_writer->writeNotebook();
55}
56
65void NotebookBuilder::writeHistoryToStream(std::vector<HistoryItem>::const_iterator &iter) {
66 auto algHistory = iter->getAlgorithmHistory();
67 if (iter->isUnrolled()) {
68
69 m_nb_writer->markdownCell(std::string("Child algorithms of ") + algHistory->name());
70
71 buildChildren(iter);
72
73 m_nb_writer->markdownCell(std::string("End of child algorithms of ") + algHistory->name());
74
75 } else {
76 // create the string for this algorithm
77 m_nb_writer->codeCell(buildAlgorithmString(algHistory));
78 }
79}
80
89void NotebookBuilder::buildChildren(std::vector<HistoryItem>::const_iterator &iter) {
90 size_t numChildren = iter->numberOfChildren();
91 ++iter; // move to first child
92 for (size_t i = 0; i < numChildren && iter != m_historyItems.end(); ++i, ++iter) {
94 }
95 --iter;
96}
97
105 std::ostringstream properties;
106 const std::string name = algHistory->name();
107 std::string prop;
108
109 auto props = algHistory->getProperties();
110 for (auto &propIter : props) {
111 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
155
156 // Create a vector of all non workspace property type names
157 std::vector<std::string> nonWorkspaceTypes{"number", "boolean", "string"};
158
159 std::string prop;
160 // No need to specify value for default properties
161 if (!propHistory->isDefault()) {
162 // Do not give values to output properties other than workspace properties
163 if (find(nonWorkspaceTypes.begin(), nonWorkspaceTypes.end(), propHistory->type()) != nonWorkspaceTypes.end() &&
164 propHistory->direction() == Direction::Output) {
165 g_log.debug() << "Ignoring property " << propHistory->name() << " of type " << propHistory->type() << '\n';
166 // Handle numerical properties
167 } else if (propHistory->type() == "number") {
168 prop = propHistory->name() + "=" + propHistory->value();
169 // Handle boolean properties
170 } else if (propHistory->type() == "boolean") {
171 std::string value = (propHistory->value() == "1" ? "True" : "False");
172 prop = propHistory->name() + "=" + value;
173 // Handle all other property types
174 } else {
175 std::string opener = "='";
176 if (propHistory->value().find('\\') != std::string::npos) {
177 opener = "=r'";
178 }
179
180 prop = propHistory->name() + opener + propHistory->value() + "'";
181 }
182 }
183
184 return prop;
185}
186
187} // namespace Mantid::API
double value
The value of the point.
Definition: FitMW.cpp:51
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:52
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
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