8#pragma warning(disable : 4250)
18#pragma warning(default : 4250)
23#include <boost/optional.hpp>
24#include <boost/python/bases.hpp>
25#include <boost/python/class.hpp>
26#include <boost/python/dict.hpp>
27#include <boost/python/exception_translator.hpp>
28#include <boost/python/overloads.hpp>
34#pragma warning(disable : 4267)
36#include <boost/python/raw_function.hpp>
40#include <boost/python/register_ptr_to_python.hpp>
41#include <boost/python/scope.hpp>
42#include <boost/variant.hpp>
43#include <boost/variant/static_visitor.hpp>
72using declarePropertyType2 = void (*)(boost::python::object &,
const std::string &,
const boost::python::object &,
73 const boost::python::object &,
const std::string &,
const int);
75using declarePropertyType3 = void (*)(boost::python::object &,
const std::string &,
const boost::python::object &,
76 const std::string &,
const int);
78using declarePropertyType4 = void (*)(boost::python::object &,
const std::string &,
const boost::python::object &,
86BOOST_PYTHON_FUNCTION_OVERLOADS(declarePropertyType1_Overload, PythonAlgorithm::declarePyAlgProperty, 2, 3)
87BOOST_PYTHON_FUNCTION_OVERLOADS(declarePropertyType2_Overload, PythonAlgorithm::declarePyAlgProperty, 3, 6)
88BOOST_PYTHON_FUNCTION_OVERLOADS(declarePropertyType3_Overload, PythonAlgorithm::declarePyAlgProperty, 4, 5)
98void translateCancel(const
Algorithm::CancelException &exc) {
100 PyErr_SetString(PyExc_KeyboardInterrupt,
"");
103template <
typename T> boost::optional<T> extractArg(ssize_t
index,
const tuple &args) {
104 if (
index < len(args)) {
105 return boost::optional<T>(extract<T>(args[
index]));
110template <
typename T>
void extractKwargs(
const dict &kwargs,
const std::string &keyName, boost::optional<T> &out) {
111 if (!kwargs.has_key(keyName)) {
114 if (out != boost::none) {
115 throw std::invalid_argument(
"Parameter called '" + keyName +
116 "' was specified twice."
117 " This must be either positional or a kwarg, but not both.");
119 out = boost::optional<T>(extract<T>(kwargs.get(keyName)));
125 : m_alg(alg), m_propName(propName) {}
130 void operator()(std::string
value)
const override { m_alg->setPropertyValue(m_propName,
value); }
138 using Mantid::PythonInterface::IPyTypeVisitor::operator();
141 template <
typename T>
void setProp(
const T &val)
const { m_alg->setProperty(m_propName, val); }
144 std::string
const &m_propName;
148object createChildWithProps(tuple args, dict kwargs) {
150 auto name = extractArg<std::string>(1, args);
151 auto startProgress = extractArg<double>(2, args);
152 auto endProgress = extractArg<double>(3, args);
153 auto enableLogging = extractArg<bool>(4, args);
154 auto version = extractArg<int>(5, args);
156 const std::array<std::string, 5> reservedNames = {
"name",
"startProgress",
"endProgress",
"enableLogging",
"version"};
158 extractKwargs<std::string>(kwargs, reservedNames[0], name);
159 extractKwargs<double>(kwargs, reservedNames[1], startProgress);
160 extractKwargs<double>(kwargs, reservedNames[2], endProgress);
161 extractKwargs<bool>(kwargs, reservedNames[3], enableLogging);
162 extractKwargs<int>(kwargs, reservedNames[4], version);
164 if (!name.is_initialized()) {
165 throw std::invalid_argument(
"Please specify the algorithm name");
167 auto childAlg = parentAlg->createChildAlgorithm(name.value(), startProgress.value_or(-1), endProgress.value_or(-1),
168 enableLogging.value_or(
true), version.value_or(-1));
170 const list keys = kwargs.keys();
171 for (
int i = 0; i < len(keys); ++i) {
172 const std::string propName = extract<std::string>(keys[i]);
174 if (std::find(reservedNames.cbegin(), reservedNames.cend(), propName) != reservedNames.cend())
177 object curArg = kwargs[keys[i]];
182 auto nativeObj = PyNativeTypeExtractor::convert(curArg);
184 boost::apply_visitor(SetPropertyVisitor(childAlg, propName), nativeObj);
186 return object(childAlg);
192 register_ptr_to_python<std::shared_ptr<Algorithm>>();
193 register_exception_translator<Algorithm::CancelException>(&translateCancel);
199 class_<Algorithm, bases<Mantid::API::IAlgorithm>, std::shared_ptr<PythonAlgorithm>, boost::noncopyable>(
200 "Algorithm",
"Base class for all algorithms")
202 .staticmethod(
"fromString")
203 .def(
"createChildAlgorithm", raw_function(&createChildWithProps, std::size_t(1)),
204 "Creates and intializes a named child algorithm. Output workspaces "
205 "are given a dummy name.")
206 .def(
"declareProperty", (declarePropertyType1)&PythonAlgorithm::declarePyAlgProperty,
207 declarePropertyType1_Overload((arg(
"self"), arg(
"prop"), arg(
"doc") =
"")))
209 "Turns history recording on or off for an algorithm.")
210 .def(
"declareProperty", (declarePropertyType2)&PythonAlgorithm::declarePyAlgProperty,
211 declarePropertyType2_Overload((arg(
"self"), arg(
"name"), arg(
"defaultValue"), arg(
"validator") =
object(),
213 "Declares a named property where the type is taken from "
214 "the type of the defaultValue and mapped to an appropriate C++ "
216 .def(
"declareProperty", (declarePropertyType3)&PythonAlgorithm::declarePyAlgProperty,
217 declarePropertyType3_Overload(
218 (arg(
"self"), arg(
"name"), arg(
"defaultValue"), arg(
"doc") =
"", arg(
"direction") =
Direction::Input),
219 "Declares a named property where the type is taken from the "
221 "of the defaultValue and mapped to an appropriate C++ type"))
222 .def(
"declareProperty", (declarePropertyType4)&PythonAlgorithm::declarePyAlgProperty,
223 (arg(
"self"), arg(
"name"), arg(
"defaultValue"), arg(
"direction") =
Direction::Input),
224 "Declares a named property where the type is taken from the type "
225 "of the defaultValue and mapped to an appropriate C++ type")
226 .def(
"getLogger", &PythonAlgorithm::getLogger, arg(
"self"), return_value_policy<reference_existing_object>(),
227 "Returns a reference to this algorithm's logger")
228 .def(
"log", &PythonAlgorithm::getLogger, arg(
"self"), return_value_policy<reference_existing_object>(),
229 "Returns a reference to this algorithm's logger")
232 .def(
"setWikiSummary", &PythonAlgorithm::setWikiSummary, (arg(
"self"), arg(
"summary")),
233 "(Deprecated.) Set summary for the help.");
242 scope().attr(
"PythonAlgorithm") = scope().attr(
"Algorithm");
246 register_ptr_to_python<std::shared_ptr<SerialAlgorithm>>();
247 register_exception_translator<SerialAlgorithm::CancelException>(&translateCancel);
248 class_<SerialAlgorithm, bases<Mantid::API::Algorithm>, std::shared_ptr<PythonSerialAlgorithm>, boost::noncopyable>(
249 "SerialAlgorithm",
"Base class for simple serial algorithms");
250 scope().attr(
"PythonSerialAlgorithm") = scope().attr(
"SerialAlgorithm");
254 register_ptr_to_python<std::shared_ptr<ParallelAlgorithm>>();
255 register_exception_translator<ParallelAlgorithm::CancelException>(&translateCancel);
256 class_<ParallelAlgorithm, bases<Mantid::API::Algorithm>, std::shared_ptr<PythonParallelAlgorithm>,
257 boost::noncopyable>(
"ParallelAlgorithm",
"Base class for simple parallel algorithms");
258 scope().attr(
"PythonParallelAlgorithm") = scope().attr(
"ParallelAlgorithm");
262 register_ptr_to_python<std::shared_ptr<DistributedAlgorithm>>();
263 register_exception_translator<DistributedAlgorithm::CancelException>(&translateCancel);
264 class_<DistributedAlgorithm, bases<Mantid::API::Algorithm>, std::shared_ptr<PythonDistributedAlgorithm>,
265 boost::noncopyable>(
"DistributedAlgorithm",
"Base class for simple distributed algorithms");
266 scope().attr(
"PythonDistributedAlgorithm") = scope().attr(
"DistributedAlgorithm");
double value
The value of the point.
#define GET_POINTER_SPECIALIZATION(TYPE)
std::map< DeltaEMode::Type, std::string > index
void export_ParallelAlgorithm()
void export_leaf_classes()
void export_SerialAlgorithm()
void export_DistributedAlgorithm()
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
Base class from which all concrete algorithm classes should be derived.
static IAlgorithm_sptr fromString(const std::string &input)
De-serialize an object from a string.
void enableHistoryRecordingForChild(const bool on) override
Change the state of the history recording flag.
Base class for algorithms that treat all spectra independently, i.e., we can trivially parallelize ov...
Base class for algorithms that can run in parallel on all MPI ranks but not in a distributed fashion.
Base class for algorithms that can only run serially (Parallel::ExecutionMode::MasterOnly) in an MPI ...
Base class for properties.
Provides a layer class for boost::python to allow C++ virtual functions to be overridden in a Python ...
virtual void operator()(bool value) const =0
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
Describes the direction (within an algorithm) of a Property.
@ Input
An input workspace.