Mantid
Loading...
Searching...
No Matches
WorkspaceGroup.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 +
14
15#include <boost/python/class.hpp>
16#include <boost/python/copy_non_const_reference.hpp>
17#include <boost/python/iterator.hpp>
18#include <boost/python/make_constructor.hpp>
19#include <boost/python/return_value_policy.hpp>
20
21using namespace Mantid::API;
22using namespace Mantid::PythonInterface;
23using namespace boost::python;
24
25namespace {
26
27PyObject *convertWsToObj(const Workspace_sptr &ws) {
28 if (Mantid ::API::AnalysisDataService::Instance().doesExist(ws->getName())) {
29 // Decay to weak ptr so the ADS manages lifetime
30 using PtrT = std::weak_ptr<Workspace>;
31 PtrT weak_ptr = ws;
32 return boost::python::to_python_value<PtrT>()(weak_ptr);
33 } else {
34 return boost::python::to_python_value<Workspace_sptr>()(ws);
35 }
36}
37} // namespace
38
40
41
48std::vector<Workspace_sptr>::iterator group_begin(WorkspaceGroup &self) { return self.begin(); }
49
57std::vector<Workspace_sptr>::iterator group_end(WorkspaceGroup &self) { return self.end(); }
58
61 Workspace_sptr wsGroup = std::make_shared<WorkspaceGroup>();
62 return wsGroup;
63}
64
65void addItem(WorkspaceGroup &self, const std::string &name) {
67 self.add(name);
68}
69
70void addWorkspace(WorkspaceGroup &self, const boost::python::object &pyobj) {
73}
74
75void removeItem(WorkspaceGroup &self, const std::string &name) {
77 self.remove(name);
78}
79
80PyObject *getItem(WorkspaceGroup &self, const int &index) {
81 if (index < 0) {
82 if (static_cast<size_t>(-index) > self.size())
84 return convertWsToObj(self.getItem(self.size() + index));
85 }
86 return convertWsToObj(self.getItem(index));
87}
88
90 class_<WorkspaceGroup, bases<Workspace>, boost::noncopyable>("WorkspaceGroup", no_init)
91 .def("__init__", make_constructor(&makeWorkspaceGroup))
92 .def("getNumberOfEntries", &WorkspaceGroup::getNumberOfEntries, arg("self"),
93 "Returns the number of entries in the group")
94 .def("getNames", &WorkspaceGroup::getNames, arg("self"), "Returns the names of the entries in the group")
95 .def("contains", (bool (WorkspaceGroup::*)(const std::string &wsName) const) & WorkspaceGroup::contains,
96 (arg("self"), arg("workspace")), "Returns true if the given name is in the group")
97 .def("sortByName", &WorkspaceGroup::sortByName, (arg("self")), "Sort members by name")
98 .def("add", addItem, (arg("self"), arg("workspace_name")), "Add a name to the group")
99 .def("addWorkspace", addWorkspace, (arg("self"), arg("workspace")), "Add a workspace to the group.")
100 .def("size", &WorkspaceGroup::size, arg("self"), "Returns the number of workspaces contained in the group")
101 .def("remove", removeItem, (arg("self"), arg("workspace_name")), "Remove a name from the group")
102 .def("getItem", getItem, (arg("self"), arg("index")), "Returns the item at the given index")
103 .def("isMultiPeriod", &WorkspaceGroup::isMultiperiod, arg("self"),
104 "Returns true if the workspace group is multi-period")
105 // ------------ Operators --------------------------------
106 .def("__len__", &WorkspaceGroup::getNumberOfEntries, arg("self"),
107 "Gets the number of entries in the workspace group")
108 .def("__contains__", (bool (WorkspaceGroup::*)(const std::string &wsName) const) & WorkspaceGroup::contains,
109 (arg("self"), arg("workspace name")), "Does this group contain the named workspace?")
110 .def("__getitem__", getItem, (arg("self"), arg("index")))
111 .def("__iter__", range<return_value_policy<copy_non_const_reference>>(&group_begin, &group_end));
112
114}
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition: GetPointer.h:17
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
Workspace_sptr makeWorkspaceGroup()
Constructor function for WorkspaceGroup.
PyObject * getItem(WorkspaceGroup &self, const int &index)
void removeItem(WorkspaceGroup &self, const std::string &name)
void export_WorkspaceGroup()
void addItem(WorkspaceGroup &self, const std::string &name)
std::vector< Workspace_sptr >::iterator group_begin(WorkspaceGroup &self)
Returns an iterator pointing to the first element in the group.
std::vector< Workspace_sptr >::iterator group_end(WorkspaceGroup &self)
Returns an iterator pointing to the past-the-end element in the group.
void addWorkspace(WorkspaceGroup &self, const boost::python::object &pyobj)
Class to hold a set of workspaces.
void addWorkspace(const Workspace_sptr &workspace)
Adds a workspace to the group.
void remove(const std::string &wsName)
Remove a name from the group.
std::vector< Workspace_sptr >::iterator end()
Returns a non-const iterator pointing at the last element in the workspace group.
int getNumberOfEntries() const
Return the number of entries within the group.
bool contains(const std::string &wsName) const
Does a workspace exist within the group.
Workspace_sptr getItem(const size_t index) const
Return the ith workspace.
void sortByName()
Invokes the ADS to sort group members by workspace name.
std::vector< std::string > getNames() const
Returns the names of workspaces that make up this group.
void add(const std::string &wsName)
Adds a workspace to the group.
void throwIndexOutOfRangeError(int index) const
Throws an out_of_range error for an invalid index.
bool isMultiperiod() const
Inidicates that the workspace group can be treated as multiperiod.
size_t size() const
Return the size of the group, so it is more like a container.
Defines a structure for releasing the Python GIL using the RAII pattern.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
STL namespace.
A helper struct to export templated DataService<> types to Python.
Encapsulates the registration required for an interface type T that sits on top of a Kernel::DataItem...