44 _time.reserve(time_dim);
45 _values.reserve(time_dim * value_dim);
50 if (values.empty())
return true;
58 _time.push_back(time);
73 _time.push_back(time);
81 if (values_matrix.cols() != time.size())
83 PRINT_ERROR(
"TimeSeries::set(): time.size() != values_matrix.cols()");
90 _time.assign(time.data(), time.data() + time.size());
91 if (values_matrix.IsRowMajor)
96 target = values_matrix;
100 const int size = values_matrix.rows() * values_matrix.cols();
101 _values.assign(values_matrix.data(), values_matrix.data() +
size);
106 bool TimeSeries::set(
const std::vector<double>& time,
const std::vector<Eigen::VectorXd>& values_vector,
double time_from_start)
108 if (values_vector.size() != time.size())
129 for (
const Eigen::VectorXd& vec : values_vector)
132 _values.insert(
_values.end(), vec.data(), vec.data() + vec.size());
137 PRINT_ERROR_NAMED(
"Vectors in values_vector must be of equal size. Clearing time series object.");
153 assert(time_idx <
_time.size());
154 std::vector<double> temp;
155 const int value_idx_begin =
_value_dim * time_idx;
156 const int value_idx_end = value_idx_begin +
_value_dim;
157 temp.assign(
_values.begin() + value_idx_begin,
_values.begin() + value_idx_end);
163 assert(time_idx <
_time.size());
164 const int value_idx_begin =
_value_dim * time_idx;
169 double tolerance)
const 171 if (
_time.empty())
return false;
173 auto it = std::find_if(
_time.begin(),
_time.end(), [time](
double val) {
return val >= time; });
174 if (it ==
_time.end())
176 switch (extrapolation)
189 PRINT_ERROR(
"TimeSeries::valuesInterpolate(): desired extrapolation method not implemented.");
196 int idx = (
int)std::distance(
_time.begin(), it);
206 PRINT_ERROR(
"accessing a time idx in the past which is not this time series");
209 double dt = time -
_time[idx - 1];
212 switch (interpolation)
216 if (idx < 1) idx = 1;
228 double dt_data = _time[idx] - _time[idx - 1];
229 double dt_frac = dt / dt_data;
236 PRINT_ERROR(
"TimeSeries::valuesInterpolate(): desired interpolation method not implemented.");
249 PRINT_ERROR(
"TimeSeries::computeMeanCwise(): provided mean_values vector does not match value dimension");
269 double first_value =
_values.front();
270 if (first_value != 0)
305 PRINT_ERROR(
"TimeSeries::normalize(): selected method not implemented.");
316 PRINT_ERROR(
"TimeSeries::normalize(): specified value_idx does not match getValueDimension().");
324 double first_value =
getValues(0)[value_idx];
325 if (first_value != 0)
360 PRINT_ERROR(
"TimeSeries::normalize(): selected method not implemented.");
366 #ifdef MESSAGE_SUPPORT 373 google::protobuf::RepeatedField<double> time(
_time.begin(),
_time.end());
374 message.mutable_time()->Swap(&time);
378 message.mutable_values()->set_rows(
_value_dim);
379 message.mutable_values()->set_row_major(
false);
380 google::protobuf::RepeatedField<double> values(
_values.begin(),
_values.end());
381 message.mutable_values()->mutable_data()->Swap(&values);
387 message.clear_value_labels();
388 for (
const std::string& label :
_value_labels) message.add_value_labels(label);
394 _time.assign(message.time().begin(), message.time().end());
400 PRINT_ERROR(
"TimeSeries::fromMessage(): Dimension mismatch: values raw data does not match rows*cols: " 402 if (issues) *issues <<
"Dimension mismatch: values raw data does not match rows*cols.\n";
406 if (message.values().row_major())
417 _values.assign(message.values().data().begin(), message.values().data().end());
425 for (
int i = 0; i < message.value_labels_size(); ++i)
_value_labels.push_back(message.value_labels(i));
436 out <<
"TimeSeries is empty." << std::endl;
442 out <<
"time: " << ts.
getTime()[i] <<
"\t values: " << ts.
getValuesMap(i).transpose() << std::endl;
458 if (!ts)
return false;
460 if (_ts_sequence.empty())
464 else if (ts->getValueDimension() !=
_value_dim)
466 PRINT_ERROR(
"TimeSeriesSequence::add(): cannot add TimeSeries since its dimension differs with previously added ones.");
469 _ts_sequence.push_back(ts);
475 _ts_sequence.clear();
481 std::sort(_ts_sequence.begin(), _ts_sequence.end(),
485 #ifdef MESSAGE_SUPPORT 489 message.clear_ts_sequence();
492 ts->toMessage(*message.add_ts_sequence());
510 for (
int i = 0; i < message.ts_sequence_size(); ++i)
513 new_ts->fromMessage(message.ts_sequence(i));
514 if (new_ts->getValueDimension() !=
_value_dim)
517 *issues <<
"Imported TimeSeries object has a value dimension other than specified: " << new_ts->getValueDimension() <<
"/" 522 _ts_sequence.push_back(new_ts);
#define PRINT_ERROR_NAMED(msg)
std::vector< double > _time
virtual double computeMeanOverall()
Compute and return the mean value of all values among all dimensions.
Time Series (trajectory resp. sequence of values w.r.t. time)
bool set(const Eigen::Ref< const Eigen::VectorXd > &time, const Eigen::Ref< const Eigen::MatrixXd > &values_matrix, double time_from_start=0.0)
Set time vector and values matrix.
#define PRINT_ERROR_COND_NAMED(cond, msg)
A matrix or vector expression mapping an existing array of data.
Interpolation
List of available interpolation methods.
double _time_from_start
Speciy an offset for the time sequence _time;.
static constexpr size_t size(Tuple< Args... > &)
Provides access to the number of elements in a tuple as a compile-time constant expression.
virtual bool getValuesInterpolate(double time, Eigen::Ref< Eigen::VectorXd > values, Interpolation interpolation=Interpolation::Linear, Extrapolation extrapolate=Extrapolation::NoExtrapolation, double tolerance=1e-6) const
Retrieve value vector at a desired time stamp (seconds) via interpolation.
int getValueDimension() const
Return dimension of the value vector.
void sortByTimeFromStart()
Ascending sort of the time series objects according to their TimeSeries::timeFromStart() ...
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const AbsReturnType abs() const
void clear()
Clear time series (except valueLabels())
void reserve(int time_dim, int value_dim)
Allocate memory for a given time and value vector dimension.
int getTimeDimension() const
Return dimension of the time vector.
void print(int precision=2) const
Print the current time series content to console (with a desired precision for decimal numbers) ...
virtual void normalize(Normalization norm_method, bool value_cwise=true)
Normalize values according to a specified method.
Normalization
List of available normalizations.
ValuesMatConstMap getValuesMatrixView() const
Read access to the complete values matrix in Eigen matrix format [getValueDimension() x getTimeDimens...
bool add(double time, const std::vector< double > &values)
Add time and value vector pair (STL version)
virtual void computeMeanCwise(Eigen::Ref< Eigen::VectorXd > mean_values)
Compute and return the component-wise mean values.
std::vector< double > _values
value x time [column major], column: set of values corresponding to the given time idx ...
constexpr const double CORBO_INF_DBL
Representation for infinity (double version)
void setValueDimension(int value_dim)
Change value dimension (warning: clears existing values)
bool isEmpty() const
Determine if time series is empty.
const std::vector< double > & getTime() const
Read access to the underlying time values [getTimeDimension() x 1].
void clear()
Erase all time series.
friend std::ostream & operator<<(std::ostream &out, const TimeSeries &ts)
Print TimeSeries object.
A matrix or vector expression mapping an existing expression.
bool add(TimeSeries::Ptr ts)
Add shared instance of a time series (without copying)
Extrapolation
List of available interpolation methods.
Eigen::Map< const Eigen::VectorXd > getValuesMap(int time_idx) const
Read access to a value vector at a given time idx (< getTimeDimension) without copying.
void setValueDimension(int value_dim)
Change value dimension (warning: clears existing values)
const std::vector< double > & getValues() const
Read access to the complete values matrix in column-major format [getValueDimension() x getTimeDimens...
std::vector< std::string > _value_labels
label values should be empty or valueDimension()
std::shared_ptr< TimeSeries > Ptr
#define PRINT_INFO(msg)
Print msg-stream.
#define PRINT_ERROR(msg)
Print msg-stream as error msg.