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 +
12
13#include <boost/python/class.hpp>
14#include <boost/python/implicit.hpp>
15#include <boost/python/init.hpp>
16#include <boost/python/make_function.hpp>
17#include <boost/python/register_ptr_to_python.hpp>
18#include <boost/python/return_value_policy.hpp>
19#include <boost/python/tuple.hpp>
20
23using Mantid::Types::Core::DateAndTime;
24using namespace boost::python;
25using boost::python::arg;
26
32
33namespace {
34
36
37template <typename TYPE>
38void addPyTimeValue(TimeSeriesProperty<TYPE> &self, const boost::python::api::object &datetime, const TYPE &value) {
39 const auto dateandtime = Mantid::PythonInterface::Converters::to_dateandtime(datetime);
40 self.addValue(*dateandtime, value);
41}
42
43// Call the dtype helper function
44template <typename TYPE> std::string dtype(TimeSeriesProperty<TYPE> &self) {
46}
47
48// Check for the special case of a string
49template <> std::string dtype(TimeSeriesProperty<std::string> &self) {
50 // Vector of ints to store the sizes of each of the strings
51 std::vector<size_t> stringSizes;
52
53 // Block allocate memory
54 stringSizes.reserve(self.size());
55
56 // Loop for the number of strings in self
57 for (int i = 0; i < self.size(); i++) {
58 // For each string store the number of characters
59 std::string val = self.nthValue(i);
60 size_t size = val.size();
61 stringSizes.emplace_back(size);
62 }
63
64 // Find the maximum number of characters
65 size_t max = *std::max_element(std::begin(stringSizes), std::end(stringSizes));
66
67 // Create the string to return
68 std::stringstream ss;
69 ss << "S" << max;
70 std::string retVal = ss.str();
71 return retVal;
72}
73
74// Macro to reduce copy-and-paste
75#define EXPORT_TIMESERIES_PROP(TYPE, Prefix) \
76 register_ptr_to_python<TimeSeriesProperty<TYPE> *>(); \
77 \
78 class_<TimeSeriesProperty<TYPE>, bases<Property>, boost::noncopyable>( \
79 #Prefix "TimeSeriesProperty", init<const std::string &>((arg("self"), arg("value")))) \
80 .add_property("value", make_function(&Mantid::Kernel::TimeSeriesProperty<TYPE>::valuesAsVector, \
81 return_value_policy<VectorToNumpy>())) \
82 .add_property("times", make_function(&Mantid::Kernel::TimeSeriesProperty<TYPE>::timesAsVector, \
83 return_value_policy<VectorToNumpy>())) \
84 .add_property("filtered_value", make_function(&Mantid::Kernel::TimeSeriesProperty<TYPE>::filteredValuesAsVector, \
85 return_value_policy<VectorToNumpy>())) \
86 .add_property("filtered_times", make_function(&Mantid::Kernel::TimeSeriesProperty<TYPE>::filteredTimesAsVector, \
87 return_value_policy<VectorToNumpy>())) \
88 .def("addValue", \
89 (void (TimeSeriesProperty<TYPE>::*)(const DateAndTime &, const TYPE &)) & \
90 TimeSeriesProperty<TYPE>::addValue, \
91 (arg("self"), arg("time"), arg("value"))) \
92 .def("addValue", \
93 (void (TimeSeriesProperty<TYPE>::*)(const std::string &, const TYPE &)) & \
94 TimeSeriesProperty<TYPE>::addValue, \
95 (arg("self"), arg("time"), arg("value"))) \
96 .def("addValue", &addPyTimeValue<TYPE>, (arg("self"), arg("time"), arg("value"))) \
97 .def("clear", &TimeSeriesProperty<TYPE>::clear, arg("self")) \
98 .def("valueAsString", &TimeSeriesProperty<TYPE>::value, arg("self")) \
99 .def("size", &TimeSeriesProperty<TYPE>::size, arg("self")) \
100 .def("filterByTime", \
101 (void (TimeSeriesProperty<TYPE>::*)(const DateAndTime &, const DateAndTime &)) & \
102 TimeSeriesProperty<TYPE>::filterByTime, \
103 (arg("self"), arg("start"), arg("stop"))) \
104 .def("firstTime", &TimeSeriesProperty<TYPE>::firstTime, arg("self"), \
105 "returns :class:`mantid.kernel.DateAndTime`") \
106 .def("firstValue", &TimeSeriesProperty<TYPE>::firstValue, arg("self")) \
107 .def("lastTime", &TimeSeriesProperty<TYPE>::lastTime, arg("self"), "returns :class:`mantid.kernel.DateAndTime`") \
108 .def("lastValue", &TimeSeriesProperty<TYPE>::lastValue, arg("self")) \
109 .def("nthValue", &TimeSeriesProperty<TYPE>::nthValue, (arg("self"), arg("index"))) \
110 .def("nthTime", &TimeSeriesProperty<TYPE>::nthTime, (arg("self"), arg("index")), \
111 "returns :class:`mantid.kernel.DateAndTime`") \
112 .def("getStatistics", &TimeSeriesProperty<TYPE>::getStatistics, arg("self"), \
113 "returns :class:`mantid.kernel.TimeSeriesPropertyStatistics`") \
114 .def("timeAverageValue", &TimeSeriesProperty<TYPE>::timeAverageValue, arg("self")) \
115 .def("dtype", &dtype<TYPE>, arg("self"));
116
117} // namespace
118
120
122
124
126
128
130 class_<Mantid::Kernel::TimeSeriesPropertyStatistics>("TimeSeriesPropertyStatistics", no_init)
135 .add_property("standard_deviation", &Mantid::Kernel::TimeSeriesPropertyStatistics::standard_deviation)
137 .add_property("time_standard_deviation", &Mantid::Kernel::TimeSeriesPropertyStatistics::time_standard_deviation)
139}
double value
The value of the point.
Definition: FitMW.cpp:51
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition: GetPointer.h:17
std::string dtype(Mantid::Kernel::PropertyWithValue< HeldType > &self)
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)
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.
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)
Definition: DateAndTime.cpp:81
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...