Mantid
Loading...
Searching...
No Matches
ArrayProperty.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 +
9
15
16#include <boost/python/class.hpp>
17#include <boost/python/list.hpp>
18
19#include <boost/python/default_call_policies.hpp>
20#include <boost/python/make_constructor.hpp>
21
30using namespace boost::python;
31
32namespace {
34using return_cloned_numpy = return_value_policy<Policies::VectorRefToNumpy<Converters::Clone>>;
35
36// Call the dtype helper function
37template <typename type> std::string dtype(ArrayProperty<type> &self) { return Converters::dtype(self); }
38
39// Check for the special case of a string
40template <> std::string dtype(ArrayProperty<std::string> &self) {
41 // Get a vector of all the strings
42 std::vector<std::string> values = self();
43
44 // Vector of ints to store the sizes of each of the strings
45 std::vector<size_t> stringSizes;
46
47 // Block allocate memory
48 stringSizes.reserve(self.size());
49
50 // Loop for the number of strings
51 // For each string store the number of characters
52 for (auto val : values) {
53 auto size = val.size();
54 stringSizes.emplace_back(size);
55 }
56
57 // Find the maximum number of characters
58 size_t max = *std::max_element(std::begin(stringSizes), std::end(stringSizes));
59
60 // Create the string to return
61 std::stringstream ss;
62 ss << "S" << max;
63 std::string retVal = ss.str();
64 return retVal;
65}
66
67#define EXPORT_ARRAY_PROP(type, prefix) \
68 class_<ArrayProperty<type>, bases<PropertyWithValue<std::vector<type>>>, boost::noncopyable>( \
69 #prefix "ArrayProperty", no_init) \
70 .def(init<const std::string &, const unsigned int>( \
71 (arg("self"), arg("name"), arg("direction") = Direction::Input), \
72 "Construct an ArrayProperty of type " #type)) \
73 \
74 .def(init<const std::string &, IValidator_sptr, const unsigned int>( \
75 (arg("self"), arg("name"), arg("validator"), arg("direction") = Direction::Input), \
76 "Construct an ArrayProperty of type " #type " with a validator")) \
77 \
78 .def(init<const std::string &, const std::string &, IValidator_sptr, const unsigned int>( \
79 (arg("self"), arg("name"), arg("values"), arg("validator") = IValidator_sptr(new NullValidator), \
80 arg("direction") = Direction::Input), \
81 "Construct an ArrayProperty of type " #type " with a validator giving the values as a string")) \
82 .def("__init__", \
83 make_constructor(&createArrayPropertyFromList<type>, default_call_policies(), \
84 (arg("name"), arg("values"), arg("validator") = IValidator_sptr(new NullValidator), \
85 arg("direction") = Direction::Input))) \
86 .def("__init__", \
87 make_constructor(&createArrayPropertyFromNDArray<type>, default_call_policies(), \
88 (arg("name"), arg("values"), arg("validator") = IValidator_sptr(new NullValidator), \
89 arg("direction") = Direction::Input))) \
90 .def("dtype", &dtype<type>, arg("self")) \
91 .add_property("value", make_function(&ArrayProperty<type>::operator(), return_cloned_numpy()));
92
101template <typename T>
102ArrayProperty<T> *createArrayPropertyFromList(const std::string &name, const boost::python::list &values,
103 const IValidator_sptr &validator, const unsigned int direction) {
104 return new ArrayProperty<T>(name, Converters::PySequenceToVector<T>(values)(), validator, direction);
105}
106
116template <typename T>
117ArrayProperty<T> *createArrayPropertyFromNDArray(const std::string &name, const NDArray &values,
118 const IValidator_sptr &validator, const unsigned int direction) {
119 return new ArrayProperty<T>(name, Converters::NDArrayToVector<T>(values)(), validator, direction);
120}
121} // namespace
122
124 // Match the python names to their C types
125 EXPORT_ARRAY_PROP(double, Float);
126 EXPORT_ARRAY_PROP(long, Int);
127 EXPORT_ARRAY_PROP(std::string, String);
128
129 // Needs these declarations also to ensure that properties not created in
130 // Python can be seen also. Users shouldn't need this
131 EXPORT_ARRAY_PROP(int, CInt);
132 EXPORT_ARRAY_PROP(size_t, UnsignedInt);
133}
std::string dtype(Mantid::Kernel::PropertyWithValue< HeldType > &self)
#define EXPORT_ARRAY_PROP(type, prefix)
void export_ArrayProperty()
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
NullValidator is a validator that doesn't.
Definition: NullValidator.h:20
The concrete, templated class for properties.
int size() const override
Return the size of this property.
Thin object wrapper around a numpy array.
Definition: NDArray.h:31
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
Definition: IValidator.h:26
std::string dtype(const Container< HeldType > &)
Describes the direction (within an algorithm) of a Property.
Definition: Property.h:50
Converter taking an input numpy array and converting it to a std::vector.
Converts a Python sequence type to a C++ std::vector, where the element type is defined by the templa...