Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
timeseries.h
Go to the documentation of this file.
1#ifndef TIMESERIES_H
2#define TIMESERIES_H
3#include "../init.h"
4#include "../util/utils.h"
5#include "../util/error.h"
6/* #include "../util/logger.h" */
7/* #include "../util/rnd.h" */
8
9namespace Brush::Data{
10
24template<class T>
26{
27 using Scalar = T;
28 static const size_t NumDimensions=2;
29 using EntryType = Eigen::Array<T,Dynamic,1>;
30 using ValType = std::vector<EntryType>;
31 using TimeType = std::vector<Eigen::ArrayXi>;
35 TimeSeries() = default;
36
39
41 const ValType& v): time(t), value(v) {}
42 // array<TimeSeries, 2> TimeSeries::split(const ArrayXb& mask) const ;
44 template<typename U, typename V>
45 TimeSeries operator()(const U& idx, const V& idx2=Eigen::all) const
46 {
47 TimeType t = Util::slice(this->time, idx);
48 ValType v = Util::slice(this->value, idx);
49 return TimeSeries(t, v);
50 };
51
52 inline auto size() const -> size_t { return value.size(); };
53 inline auto rows() const -> size_t { return value.size(); };
54 inline auto cols(int i = 0) const -> size_t { return value.at(i).size(); };
55 // TODO: from_json and to_json
56
57 // TODO: custom iterator that iterates over pairs of time and value vectors.
58 // for now these only iterate over values.
59
60 typename ValType::iterator begin() { return this->value.begin(); };
61 typename ValType::iterator end() { return this->value.end(); };
62 auto cbegin() const { return this->value.cbegin(); };
63 auto cend() const { return this->value.cend(); };
64 typename TimeType::iterator tbegin() { return this->time.begin(); };
65 typename TimeType::iterator tend() { return this->time.end(); };
66 auto ctbegin() const { return this->time.cbegin(); };
67 auto ctend() const { return this->time.cend(); };
68 // auto rowwise() const { return this->value.rowwise(); };
69 // auto colwise() const { return this->value.colwise(); };
70 // return std::visit([](auto v){return v.rowwise().end();}, this->value); }
71
72 //TODO: define overloaded operations?
73 /* operators on values */
74 /* transform takes a unary function, and applies it to each entry,
75 * and returns a TimeSeries.
76 */
77 template<typename ET=EntryType>
78 auto transform(std::function<ET(EntryType)> op) const
79 {
80 std::vector<ET> dest(this->value.size());
81 // std::vector<ET> dest(this->value.size());
82 std::transform(cbegin(), cend(),
83 dest.begin(),
84 op
85 );
86 return TimeSeries<typename ET::Scalar>(this->time, dest);
87 }
88 /* reduce takes a unary aggregating function, applies it to each entry, and returns an Array.*/
89 template<typename R=T>
90 auto reduce(const auto& op) const
91 {
92 using RetType = Array<R,Dynamic,1>;
93 // output "dest" has one entry for each sample.
94 RetType dest(this->value.size());
95 std::transform(cbegin(), cend(),
96 dest.begin(),
97 [&](const EntryType& i){return R(op(i));}
98 );
99 return dest;
100 };
101
102 template <typename C>
103 inline auto cast() const {
104 using NewType = Array<C, Dynamic, 1>;
105 return this->transform<NewType>([](const EntryType &i)
106 { return i.template cast<C>(); });
107 };
108
109 // transformation overloads
110 inline auto abs() const { return this->transform([](const EntryType& i){ return i.abs(); }); };
111 inline auto pow() const { return this->transform([](const EntryType& i){ return i.pow(); } ); };
112 inline auto log() const { return this->transform([](const EntryType& i){ return i.log(); } ); };
113 inline auto logabs() const { return this->transform([](const EntryType& i){ return i.abs().log(); } ); };
114 inline auto log1p() const { return this->transform([](const EntryType& i){ return i.log1p(); } ); };
115 inline auto ceil() const { return this->transform([](const EntryType& i){ return i.ceil(); } ); };
116 inline auto floor() const { return this->transform([](const EntryType& i){ return i.floor(); } ); };
117 inline auto exp() const { return this->transform([](const EntryType& i){ return i.exp(); } ); };
118 inline auto sin() const { return this->transform([](const EntryType& i){ return i.sin(); } ); };
119 inline auto cos() const { return this->transform([](const EntryType& i){ return i.cos(); } ); };
120 inline auto tan() const { return this->transform([](const EntryType& i){ return i.tan(); } ); };
121 inline auto asin() const { return this->transform([](const EntryType& i){ return i.asin(); } ); };
122 inline auto acos() const { return this->transform([](const EntryType& i){ return i.acos(); } ); };
123 inline auto atan() const { return this->transform([](const EntryType& i){ return i.atan(); } ); };
124 inline auto sinh() const { return this->transform([](const EntryType& i){ return i.sinh(); } ); };
125 inline auto cosh() const { return this->transform([](const EntryType& i){ return i.cosh(); } ); };
126 inline auto tanh() const { return this->transform([](const EntryType& i){ return i.tanh(); } ); };
127 inline auto sqrt() const { return this->transform([](const EntryType& i){ return i.sqrt(); } ); };
128 inline auto sqrtabs() const { return this->transform([](const EntryType& i){ return i.abs().sqrt(); } ); };
129 inline auto square() const { return this->transform([](const EntryType& i){ return i.square(); } ); };
130 // reduction overloads
131 inline auto median() const { return this->reduce([](const EntryType& i){ return Util::median(i); } ); };
132 inline auto mean() const { return this->reduce([](const EntryType& i){ return i.mean(); } ); };
133 inline auto std() const { return this->reduce([](const EntryType& i){ return i.std(); } ); };
134 inline auto max() const { return this->reduce([](const EntryType& i){ return i.maxCoeff(); } ); };
135 inline auto min() const { return this->reduce([](const EntryType& i){ return i.minCoeff(); } ); };
136 inline auto sum() const { return this->reduce<Scalar>([](const EntryType& i){ return i.sum(); } ); };
137 inline auto count() const { return this->reduce<float>([](const EntryType& i){ return i.size(); } ); };
138 inline auto prod() const { return this->reduce<Scalar>([](const EntryType& i){ return i.prod(); } ); };
139
140 /* template<typename V=T> */
141 /* enable_if_t<is_same_v<V,float>,TimeSeries<float>> */
142
143 template<typename T2>
144 inline auto operator*(T2 v) //requires(is_same_v<Scalar,float>)
145 {
146 return this->transform<EntryType>([=](const EntryType& i){ return i*v; } );
147 };
148
149 template<typename T2>
150 inline auto before(const TimeSeries<T2>& t2) const {
151 // return elements of this that are before elements in t2
152 // TODO
153 return (*this);
154 };
155 template<typename T2>
156 inline auto after(const TimeSeries<T2>& t2) const {
157 // return elements of this that are after elements in t2
158 // TODO
159 return (*this);
160 };
161 template<typename T2>
162 inline auto during(const TimeSeries<T2>& t2) const {
163 // return elements of this that occur within the window in which elements of t2 occur
164 // TODO
165 return (*this);
166 };
167
168 std::string print() const
169 {
177 size_t m = 40;
178 size_t max_len = std::min(m, this->value.size());
179 string output = "[";
180 for (int i = 0; i < max_len; ++i){
181 size_t max_width = std::min(m, size_t(this->value.at(i).size()));
182 string dots = max_width < m ? "" : "...";
183 auto val = this->value.at(i)(Eigen::seqN(0,max_width));
184 auto t = this->time.at(i)(Eigen::seqN(0,max_width));
185 output += fmt::format("[value: {}{},\n time: {}{}]\n", val, dots, t, dots);
186 }
187 output += "]\n";
188
189 return output;
190 };
191};
192
193
194} // namespace Brush::Data
195
196namespace Brush{
197std::ostream &operator<<( std::ostream& output, const Brush::Data::TimeSeries<bool>& ts ) ;
198std::ostream &operator<<( std::ostream& output, const Brush::Data::TimeSeries<float>& ts ) ;
199std::ostream &operator<<( std::ostream& output, const Brush::Data::TimeSeries<int>& ts ) ;
200} // namespace Brush
201
202template <> struct fmt::formatter<Brush::Data::TimeSeriesi>: formatter<string_view> {
203 // parse is inherited from formatter<string_view>.
204 template <typename FormatContext>
206 /* return formatter<string_view>::format(x.value, ctx); */
207 return formatter<string_view>::format(x.print(), ctx);
208 }
209};
210
211template <> struct fmt::formatter<Brush::Data::TimeSeriesb>: formatter<string_view> {
212 // parse is inherited from formatter<string_view>.
213 template <typename FormatContext>
215 /* return formatter<string_view>::format(print_time_series(x), ctx); */
216 return formatter<string_view>::format(x.print(), ctx);
217 }
218};
219
220template <> struct fmt::formatter<Brush::Data::TimeSeriesf>: formatter<string_view> {
221 // parse is inherited from formatter<string_view>.
222 template <typename FormatContext>
224 /* return formatter<string_view>::format(print_time_series(x), ctx); */
225 return formatter<string_view>::format(x.print(), ctx);
226 /* return formatter<string_view>::format(x.value, ctx); */
227 }
228};
229
230#endif
void bind_engine(py::module &m, string name)
namespace containing Data structures used in Brush
Definition data.cpp:49
Scalar median(const T &v)
calculate median
Definition utils.h:202
std::vector< T > slice(const vector< T > &v, const U &idx)
Definition utils.h:726
< nsga2 selection operator for getting the front
Definition data.cpp:12
ostream & operator<<(ostream &os, DataType n)
Stores time series data and implements operators over it.
Definition timeseries.h:26
ValType::iterator end()
Definition timeseries.h:61
std::string print() const
Definition timeseries.h:168
ValType::iterator begin()
Definition timeseries.h:60
auto cols(int i=0) const -> size_t
Definition timeseries.h:54
auto during(const TimeSeries< T2 > &t2) const
Definition timeseries.h:162
static const size_t NumDimensions
Definition timeseries.h:28
TimeType::iterator tend()
Definition timeseries.h:65
auto after(const TimeSeries< T2 > &t2) const
Definition timeseries.h:156
auto rows() const -> size_t
Definition timeseries.h:53
auto reduce(const auto &op) const
Definition timeseries.h:90
TimeSeries(const TimeType &t, const ValType &v)
Definition timeseries.h:40
TimeType::iterator tbegin()
Definition timeseries.h:64
Eigen::Array< T, Dynamic, 1 > EntryType
Definition timeseries.h:29
auto transform(std::function< ET(EntryType)> op) const
Definition timeseries.h:78
std::vector< Eigen::ArrayXi > TimeType
Definition timeseries.h:31
auto size() const -> size_t
Definition timeseries.h:52
auto before(const TimeSeries< T2 > &t2) const
Definition timeseries.h:150
std::vector< EntryType > ValType
Definition timeseries.h:30
TimeSeries operator()(const U &idx, const V &idx2=Eigen::all) const
return a slice of the data using indices idx
Definition timeseries.h:45
auto format(Brush::Data::TimeSeriesb x, FormatContext &ctx) const
Definition timeseries.h:214
auto format(Brush::Data::TimeSeriesf x, FormatContext &ctx) const
Definition timeseries.h:223
auto format(Brush::Data::TimeSeriesi x, FormatContext &ctx) const
Definition timeseries.h:205