Mantid
Loading...
Searching...
No Matches
AnalysisDataService.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 +
16
17#include <boost/python/enum.hpp>
18
19#include <boost/python/list.hpp>
20#include <boost/python/overloads.hpp>
21#include <boost/python/return_value_policy.hpp>
22
23using namespace Mantid::API;
24using namespace Mantid::Kernel;
25using namespace Mantid::PythonInterface;
26using namespace boost::python;
27
29
30namespace {
31std::once_flag INIT_FLAG;
32
39AnalysisDataServiceImpl &instance() {
40 // start the framework (if necessary)
41 auto &ads = AnalysisDataService::Instance();
42 std::call_once(INIT_FLAG, []() {
43 // Passing True as an argument suppresses a warning that is normally
44 // displayed when calling AnalysisDataService.clear()
45 PyRun_SimpleString("import atexit\n"
46 "def cleanup_ADS():\n"
47 " from mantid.api import AnalysisDataService\n"
48 " AnalysisDataService.clear(True)\n"
49 "atexit.register(cleanup_ADS)");
50 });
51 return ads;
52}
53
61list retrieveWorkspaces(AnalysisDataServiceImpl const *const self, const list &names, bool unrollGroups = false) {
62 using WeakPtr = std::weak_ptr<Workspace>;
63 const auto wsSharedPtrs =
65 std::vector<WeakPtr> wsWeakPtrs;
66 wsWeakPtrs.reserve(wsSharedPtrs.size());
67 std::transform(wsSharedPtrs.cbegin(), wsSharedPtrs.cend(), std::back_inserter(wsWeakPtrs),
68 [](const Workspace_sptr &wksp) -> WeakPtr { return WeakPtr(wksp); });
69 return Converters::ToPyList<WeakPtr>()(wsWeakPtrs);
70}
71
72list retrieveGroupPeaksWorkspaces(AnalysisDataServiceImpl const *const self, const list &names) {
73 using WeakPtr = std::weak_ptr<Workspace>;
74
75 auto wsSharedPtrs = self->retrieveWorkspaces(Converters::PySequenceToVector<std::string>(names)(), false);
76
77 auto isNotGroupPeakWorkspace = [](const Workspace_sptr &wksp) {
78 if (auto gws = dynamic_cast<WorkspaceGroup *>(wksp.get())) {
79 return !gws->isGroupPeaksWorkspaces();
80 }
81 return true;
82 };
83 auto end = std::remove_if(wsSharedPtrs.begin(), wsSharedPtrs.end(), isNotGroupPeakWorkspace);
84 wsSharedPtrs.erase(end, wsSharedPtrs.end());
85
86 std::vector<WeakPtr> wsWeakPtrs;
87 wsWeakPtrs.reserve(wsSharedPtrs.size());
88 std::transform(wsSharedPtrs.cbegin(), wsSharedPtrs.cend(), std::back_inserter(wsWeakPtrs),
89 [](const Workspace_sptr &wksp) -> WeakPtr { return WeakPtr(wksp); });
90
91 return Converters::ToPyList<WeakPtr>()(wsWeakPtrs);
92}
93
94GNU_DIAG_OFF("unused-local-typedef")
95// Ignore -Wconversion warnings coming from boost::python
96// Seen with GCC 7.1.1 and Boost 1.63.0
97GNU_DIAG_OFF("conversion")
98BOOST_PYTHON_FUNCTION_OVERLOADS(AdsRetrieveWorkspacesOverloads, retrieveWorkspaces, 2, 3)
99GNU_DIAG_ON("conversion")
100GNU_DIAG_ON("unused-local-typedef")
101} // namespace
102
105 auto pythonClass = ADSExporter::define("AnalysisDataServiceImpl");
106 pythonClass
107 .def("Instance", instance, return_value_policy<reference_existing_object>(),
108 "Return a reference to the singleton instance")
109 .staticmethod("Instance")
110 .def("retrieveWorkspaces", retrieveWorkspaces,
111 AdsRetrieveWorkspacesOverloads("Retrieve a list of workspaces by name",
112 (arg("self"), arg("names"), arg("unrollGroups") = false)))
113 .def("retrieveGroupPeaksWorkspaces", retrieveGroupPeaksWorkspaces, (arg("self"), arg("names")))
114 .def("addToGroup", &AnalysisDataServiceImpl::addToGroup, (arg("groupName"), arg("wsName")),
115 "Add a workspace in the ADS to a group in the ADS")
116 .def("removeFromGroup", &AnalysisDataServiceImpl::removeFromGroup, (arg("groupName"), arg("wsName")),
117 "Remove a workspace from a group in the ADS")
118 .def("unique_name", &AnalysisDataServiceImpl::uniqueName,
119 (arg("self"), arg("n") = 5, arg("prefix") = "", arg("suffix") = ""),
120 "Return a randomly generated unique name for a workspace\n"
121 "\n"
122 ":param str n: length of string of random numbers\n"
123 ":param str prefix: String to be prepended to the generated string\n"
124 ":param str suffix: String to be appended to the generated string\n"
125 ":return: prefix + n*random characters + suffix\n"
126 ":rtype: str\n")
127 .def("unique_hidden_name", &AnalysisDataServiceImpl::uniqueHiddenName, arg("self"),
128 "Return a randomly generated unique hidden workspace name.");
129}
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition GetPointer.h:17
void export_AnalysisDataService()
#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.
The Analysis data service stores instances of the Workspace objects and anything that derives from te...
const std::string uniqueName(const int n=5, const std::string &prefix="", const std::string &suffix="")
Random generated unique workspace name.
void addToGroup(const std::string &groupName, const std::string &wsName)
Add a workspace to a group.
std::vector< Workspace_sptr > retrieveWorkspaces(const std::vector< std::string > &names, bool unrollGroups=false) const
Given a list of names retrieve the corresponding workspace handles.
const std::string uniqueHiddenName()
Random generated unique hidden workspace name.
void removeFromGroup(const std::string &groupName, const std::string &wsName)
Remove a workspace from a group but not from the ADS.
Class to hold a set of workspaces.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Converts a Python sequence type to a C++ std::vector, where the element type is defined by the templa...
Converter that takes a std::vector and converts it into a python list.
Definition ToPyList.h:22
A helper struct to export templated DataService<> types to Python.