Mantid
Loading...
Searching...
No Matches
TimeSeriesProperty.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.hpp>
16#include <boost/python/class.hpp>
17#include <boost/python/implicit.hpp>
18#include <boost/python/init.hpp>
19#include <boost/python/make_function.hpp>
20#include <boost/python/overloads.hpp>
21#include <boost/python/register_ptr_to_python.hpp>
22#include <boost/python/return_value_policy.hpp>
23#include <boost/python/tuple.hpp>
24
27using Mantid::Types::Core::DateAndTime;
28using namespace boost::python;
29using boost::python::arg;
30
36
37namespace {
38
40
41template <typename TYPE>
42void addPyTimeValue(TimeSeriesProperty<TYPE> &self, const boost::python::api::object &datetime, const TYPE &value) {
43 const auto dateandtime = Mantid::PythonInterface::Converters::to_dateandtime(datetime);
44 self.addValue(*dateandtime, value);
45}
46
47// Call the dtype helper function
48template <typename TYPE> std::string dtype(TimeSeriesProperty<TYPE> &self) {
50}
51
52// Check for the special case of a string
53template <> std::string dtype(TimeSeriesProperty<std::string> &self) {
54 // Vector of ints to store the sizes of each of the strings
55 std::vector<size_t> stringSizes;
56
57 // Block allocate memory
58 stringSizes.reserve(std::size_t(self.size()));
59
60 // Loop for the number of strings in self
61 for (int i = 0; i < self.size(); i++) {
62 // For each string store the number of characters
63 std::string val = self.nthValue(i);
64 size_t size = val.size();
65 stringSizes.emplace_back(size);
66 }
67
68 // Find the maximum number of characters
69 size_t max = *std::max_element(std::begin(stringSizes), std::end(stringSizes));
70
71 // Create the string to return
72 std::stringstream ss;
73 ss << "S" << max;
74 std::string retVal = ss.str();
75 return retVal;
76}
77
78// Ignore -Wconversion warnings coming from boost::python
79// Seen with GCC 7.1.1 and Boost 1.63.0
80GNU_DIAG_OFF("conversion")
81BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(timeAverageValue_Overloads, timeAverageValue, 0, 1)
82
83// Macro to reduce copy-and-paste
84#define EXPORT_TIMESERIES_PROP(TYPE, Prefix) \
85 register_ptr_to_python<TimeSeriesProperty<TYPE> *>(); \
86 \
87 class_<TimeSeriesProperty<TYPE>, bases<Property>, boost::noncopyable>( \
88 #Prefix "TimeSeriesProperty", init<const std::string &>((arg("self"), arg("value")))) \
89 .add_property("value", make_function(&Mantid::Kernel::TimeSeriesProperty<TYPE>::valuesAsVector, \
90 return_value_policy<VectorToNumpy>())) \
91 .add_property("times", make_function(&Mantid::Kernel::TimeSeriesProperty<TYPE>::timesAsVector, \
92 return_value_policy<VectorToNumpy>())) \
93 .add_property("filtered_value", \
94 make_function((std::vector<TYPE> (TimeSeriesProperty<TYPE>::*)() const) & \
95 Mantid::Kernel::TimeSeriesProperty<TYPE>::filteredValuesAsVector, \
96 return_value_policy<VectorToNumpy>())) \
97 .add_property("filtered_times", \
98 make_function((std::vector<DateAndTime> (TimeSeriesProperty<TYPE>::*)() const) & \
99 Mantid::Kernel::TimeSeriesProperty<TYPE>::filteredTimesAsVector, \
100 return_value_policy<VectorToNumpy>())) \
101 .def("addValue", \
102 (void (TimeSeriesProperty<TYPE>::*)(const DateAndTime &, const TYPE &)) & \
103 TimeSeriesProperty<TYPE>::addValue, \
104 (arg("self"), arg("time"), arg("value"))) \
105 .def("addValue", \
106 (void (TimeSeriesProperty<TYPE>::*)(const std::string &, const TYPE &)) & \
107 TimeSeriesProperty<TYPE>::addValue, \
108 (arg("self"), arg("time"), arg("value"))) \
109 .def("addValue", &addPyTimeValue<TYPE>, (arg("self"), arg("time"), arg("value"))) \
110 .def("clear", &TimeSeriesProperty<TYPE>::clear, arg("self")) \
111 .def("valueAsString", &TimeSeriesProperty<TYPE>::value, arg("self")) \
112 .def("size", &TimeSeriesProperty<TYPE>::size, arg("self")) \
113 .def("firstTime", &TimeSeriesProperty<TYPE>::firstTime, arg("self"), \
114 "returns :class:`mantid.kernel.DateAndTime`") \
115 .def("firstValue", (TYPE (TimeSeriesProperty<TYPE>::*)() const) & TimeSeriesProperty<TYPE>::firstValue, \
116 arg("self")) \
117 .def("lastTime", &TimeSeriesProperty<TYPE>::lastTime, arg("self"), "returns :class:`mantid.kernel.DateAndTime`") \
118 .def("lastValue", (TYPE (TimeSeriesProperty<TYPE>::*)() const) & TimeSeriesProperty<TYPE>::lastValue, \
119 arg("self")) \
120 .def("nthValue", &TimeSeriesProperty<TYPE>::nthValue, (arg("self"), arg("index"))) \
121 .def("nthTime", &TimeSeriesProperty<TYPE>::nthTime, (arg("self"), arg("index")), \
122 "returns :class:`mantid.kernel.DateAndTime`") \
123 .def("getStatistics", &TimeSeriesProperty<TYPE>::getStatistics, getStatistics_overloads()) \
124 .def("timeAverageValue", &TimeSeriesProperty<TYPE>::timeAverageValue, \
125 timeAverageValue_Overloads((arg("self"), arg("time_roi")))) \
126 .def("dtype", &dtype<TYPE>, arg("self"));
127GNU_DIAG_ON("conversion")
128
129} // namespace
130
131// Ignore -Wconversion warnings coming from boost::python
132// Seen with GCC 7.1.1 and Boost 1.63.0
133GNU_DIAG_OFF("conversion")
134// cppcheck-suppress unknownMacro
135BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getStatistics_overloads, getStatistics, 0, 1)
136
138
140
142
144
146GNU_DIAG_ON("conversion")
148 class_<Mantid::Kernel::TimeSeriesPropertyStatistics>("TimeSeriesPropertyStatistics", no_init)
153 .add_property("standard_deviation", &Mantid::Kernel::TimeSeriesPropertyStatistics::standard_deviation)
155 .add_property("time_standard_deviation", &Mantid::Kernel::TimeSeriesPropertyStatistics::time_standard_deviation)
157}
double value
The value of the point.
Definition FitMW.cpp:51
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition GetPointer.h:17
void export_TimeSeriesProperty_Int64()
void export_TimeSeriesProperty_Int32()
void export_TimeSeriesPropertyStatistics()
void export_TimeSeriesProperty_String()
void export_TimeSeriesProperty_Double()
void export_TimeSeriesProperty_Bool()
#define EXPORT_TIMESERIES_PROP(TYPE, Prefix)
#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.
Base class for properties.
Definition Property.h:94
A specialised Property class for holding a series of time-value pairs.
int size() const override
Returns the number of values at UNIQUE time intervals in the time series.
void addValue(const Types::Core::DateAndTime &time, const TYPE &value)
Add a value to the map using a DateAndTime object.
virtual TYPE nthValue(int n) const
Returns n-th value of n-th interval in an incredibly inefficient way.
MANTID_PYTHONINTERFACE_CORE_DLL std::shared_ptr< Types::Core::DateAndTime > to_dateandtime(const boost::python::api::object &value)
std::string dtype(const Container< HeldType > &)
double time_standard_deviation
time weighted standard deviation
double standard_deviation
standard_deviation of the values
Implements a return value policy that returns a numpy array from a function returning a std::vector b...