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 +
16
17#include <boost/python/class.hpp>
18#include <boost/python/copy_non_const_reference.hpp>
19#include <boost/python/iterator.hpp>
20#include <boost/python/make_constructor.hpp>
21#include <boost/python/return_value_policy.hpp>
22
23using namespace Mantid::API;
24using namespace Mantid::PythonInterface;
25using namespace boost::python;
27
28namespace {
29
30PyObject *convertWsToObj(const Workspace_sptr &ws) {
31 if (Mantid ::API::AnalysisDataService::Instance().doesExist(ws->getName())) {
32 // Decay to weak ptr so the ADS manages lifetime
33 using PtrT = std::weak_ptr<Workspace>;
34 PtrT weak_ptr = ws;
35 return boost::python::to_python_value<PtrT>()(weak_ptr);
36 } else {
37 return boost::python::to_python_value<Workspace_sptr>()(ws);
38 }
39}
40} // namespace
41
43
44
51std::vector<Workspace_sptr>::iterator group_begin(WorkspaceGroup &self) { return self.begin(); }
52
60std::vector<Workspace_sptr>::iterator group_end(WorkspaceGroup &self) { return self.end(); }
61
64 Workspace_sptr wsGroup = std::make_shared<WorkspaceGroup>();
65 return wsGroup;
66}
67
68void addItem(WorkspaceGroup &self, const std::string &name) {
70 self.add(name);
71}
72
77
78void removeItem(WorkspaceGroup &self, const std::string &name) {
80 self.remove(name);
81}
82
83PyObject *getItem(WorkspaceGroup &self, const int &index) {
84 if (index < 0) {
85 if (static_cast<size_t>(-index) > self.size())
87 return convertWsToObj(self.getItem(self.size() + index));
88 }
89 return convertWsToObj(self.getItem(index));
90}
91
92void reorder(WorkspaceGroup &self, const boost::python::object &pythonIndices) {
93 std::vector<int> indices(static_cast<size_t>(PySequence_Size(pythonIndices.ptr())));
94
95 if (NDArray::check(pythonIndices)) {
96 NDArrayToVector<int> converter(pythonIndices);
97 converter.copyTo(indices);
98 } else {
99 PySequenceToVector<int> converter(pythonIndices);
100 converter.copyTo(indices);
101 }
102
103 self.reorderMembersWithIndices(indices);
104}
105
107 class_<WorkspaceGroup, bases<Workspace>, boost::noncopyable>("WorkspaceGroup", no_init)
108 .def("__init__", make_constructor(&makeWorkspaceGroup))
109 .def("getNumberOfEntries", &WorkspaceGroup::getNumberOfEntries, arg("self"),
110 "Returns the number of entries in the group")
111 .def("getNames", &WorkspaceGroup::getNames, arg("self"), "Returns the names of the entries in the group")
112 .def("contains", (bool (WorkspaceGroup::*)(const std::string &wsName) const) & WorkspaceGroup::contains,
113 (arg("self"), arg("workspace")), "Returns true if the given name is in the group")
114 .def("sortByName", &WorkspaceGroup::sortByName, (arg("self")), "Sort members by name")
115 .def("reorder", reorder, (arg("self"), arg("indices")), "Reorder the group members using a list of indices")
116 .def("add", addItem, (arg("self"), arg("workspace_name")), "Add a name to the group")
117 .def("addWorkspace", addWorkspace, (arg("self"), arg("workspace")), "Add a workspace to the group.")
118 .def("size", &WorkspaceGroup::size, arg("self"), "Returns the number of workspaces contained in the group")
119 .def("remove", removeItem, (arg("self"), arg("workspace_name")), "Remove a name from the group")
120 .def("getItem", getItem, (arg("self"), arg("index")), "Returns the item at the given index")
121 .def("isMultiPeriod", &WorkspaceGroup::isMultiperiod, arg("self"),
122 "Returns true if the workspace group is multi-period")
123 // ------------ Operators --------------------------------
124 .def("__len__", &WorkspaceGroup::getNumberOfEntries, arg("self"),
125 "Gets the number of entries in the workspace group")
126 .def("__contains__", (bool (WorkspaceGroup::*)(const std::string &wsName) const) & WorkspaceGroup::contains,
127 (arg("self"), arg("workspace name")), "Does this group contain the named workspace?")
128 .def("__getitem__", getItem, (arg("self"), arg("index")))
129 .def("__iter__", range<return_value_policy<copy_non_const_reference>>(&group_begin, &group_end));
130
132}
std::string name
Definition Run.cpp:60
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition GetPointer.h:17
std::map< DeltaEMode::Type, std::string > index
Workspace_sptr makeWorkspaceGroup()
Constructor function for WorkspaceGroup.
PyObject * getItem(WorkspaceGroup &self, const int &index)
void removeItem(WorkspaceGroup &self, const std::string &name)
void reorder(WorkspaceGroup &self, const boost::python::object &pythonIndices)
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.
void reorderMembersWithIndices(const std::vector< int > &indices)
Reorder the group members using a list of indices (e.g the list 4,3,2,1 would reverse the order)
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
Indicates 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.
static bool check(const boost::python::object &obj)
Check if a python object points to an array type object.
Definition NDArray.cpp:49
Defines a structure for releasing the Python GIL using the RAII pattern.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
STL namespace.
Converter taking an input numpy array and converting it to a std::vector.
void copyTo(TypedVector &dest) const
Fill the container with data from the array.
Converts a Python sequence type to a C++ std::vector, where the element type is defined by the templa...
void copyTo(TypedVector &dest)
Fill the container with data from the array.
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...