Go to the documentation of this file.
24 namespace time_series {
31 template <
class TType>
37 template <
class TType>
39 { getStamp<TType>(m) } -> sophus::concepts::ConvertibleTo<double>;
42 template <
class TType>
44 TType
const& foo_from_bar, TType
const& foo_from_daz,
double p = 0.5)
48 template <
class TType>
50 requires(TType m,
double p) {
51 { interpolate<TType>(m, m, p) } -> sophus::concepts::ConvertibleTo<TType>;
81 template <time_series::StampedValue TValueWithStamp>
89 bool empty()
const {
return time_ordered_series_.empty(); }
96 if (std::isnan(stamp)) {
100 auto it = findSmallestUpperBound(stamp);
102 time_ordered_series_.insert(it, value);
106 size_t size()
const {
return time_ordered_series_.size(); }
112 return time_ordered_series_[idx];
121 auto it_smallest_upper_bound = findSmallestUpperBound(query_stamp);
122 if (it_smallest_upper_bound ==
cbegin() &&
123 it_smallest_upper_bound ==
cend()) {
127 if (it_smallest_upper_bound ==
cbegin()) {
130 return *it_smallest_upper_bound;
132 auto it_greatest_lower_bound = std::prev(it_smallest_upper_bound);
133 if (it_smallest_upper_bound ==
cend()) {
136 return *it_greatest_lower_bound;
144 return lb_abs_diff < ub_abs_diff ? *it_smallest_upper_bound
145 : *it_greatest_lower_bound;
154 auto it_smallest_upper_bound = findSmallestUpperBound(query_stamp);
155 if (it_smallest_upper_bound ==
cbegin() &&
156 it_smallest_upper_bound ==
cend()) {
160 if (it_smallest_upper_bound ==
cbegin()) {
162 "No element with stamp < query_stamp ({}). Min/Max {}/{}",
167 auto it_greatest_lower_bound = std::prev(it_smallest_upper_bound);
168 if (it_smallest_upper_bound ==
cend()) {
170 "No element with stamp > query_stamp ({}). Min/Max {}/{}",
178 .smallest_upper_bound = *it_smallest_upper_bound};
187 double stamp,
double threshold)
const {
190 if (abs_diff > threshold) {
192 "|value.stamp ({}) - stamp ({})| ({}) > threshold ({})",
198 return nearest_value;
205 return std::is_sorted(
206 time_ordered_series_.begin(),
207 time_ordered_series_.end(),
209 return time_series::getStamp(left) < time_series::getStamp(right);
229 time_series::InterpolativeValue<TValueWithStamp>,
236 auto expect_nearest =
238 if (expect_nearest) {
242 return this->interpolatedValueImpl(
243 query_stamp, maximal_time_gap.time_duration);
250 void clear() {
return time_ordered_series_.clear(); }
252 void pop_front() {
return time_ordered_series_.pop_front(); }
254 void pop_back() {
return time_ordered_series_.pop_back(); }
270 return std::lower_bound(
271 time_ordered_series_.begin(),
272 time_ordered_series_.end(),
275 return time_series::getStamp<ValueWithStamp>(lhs) < rhs;
280 time_series::InterpolativeValue<ValueWithStamp>,
281 Expected<ValueWithStamp>>
282 interpolatedValueImpl(
double timestamp,
double max_delta_t = 1.0)
const {
285 auto lower = bounds.largest_lower_bound;
286 auto upper = bounds.smallest_upper_bound;
291 double delta_t = std::abs(lower_stamp - upper_stamp);
293 if (!(delta_t < max_delta_t)) {
295 "Delta of [{}, {}] = {}, which is too large (> {})",
304 double p = 1.0 - (upper_stamp - timestamp) / delta_t;
305 FARM_ASSERT(p >= 0.0 && p <= 1.0,
"p ({}) must in [0, 1].", p);
Definition: time_series.h:55
Definition: backtrace.cpp:102
void clear()
Definition: time_series.h:250
double time_thr
Definition: time_series.h:66
auto getStamp(TType const &value) -> double
Non-intrusive way to add getStamp function to a type needed for StampedValue concept.
Definition: time_series.h:32
#define FARM_TRY(Type, var, expression)
Assigns *expression to var of Type, but returns error if there is one.
Definition: expected.h:91
ValueWithStamp smallest_upper_bound
Definition: time_series.h:150
Expected< ValueWithStamp > findNearest(double query_stamp) const
Find nearest value given stamp.
Definition: time_series.h:120
#define FARM_ASSERT(condition,...)
If condition is false, Print formatted error message and then panic.
Definition: logger.h:264
ValueWithStamp largest_lower_bound
Definition: time_series.h:149
bool empty() const
Returns true if there are no Values in series.
Definition: time_series.h:89
NearnessThreshold(double time_thr)
Definition: time_series.h:64
void insert(ValueWithStamp const &value)
Adds ValueWithStamp to series.
Definition: time_series.h:94
std::enable_if_t< time_series::InterpolativeValue< TValueWithStamp >, Expected< TValueWithStamp > > interpolatedValue(double query_stamp, time_series::MaxGap maximal_time_gap=time_series::MaxGap(1.0), time_series::NearnessThreshold nearness_threshold=time_series::NearnessThreshold(1e-5)) const
Returns interpolated value from time series at query_stamp.
Definition: time_series.h:231
Definition: time_series.h:63
ConstIterator end() const
Definition: time_series.h:261
TimeSeries is a container of StampedValue that are sorted by their timestamp.
Definition: time_series.h:82
double time_duration
Definition: time_series.h:58
Definition: time_series.h:148
#define FARM_WARN(...)
More significant information with high signal to noise. Typically to communicate an usual but valid s...
Definition: logger.h:161
bool testIsSortedInvariant() const
Just for unit testing.
Definition: time_series.h:204
size_t size() const
Number of values in series.
Definition: time_series.h:106
#define FARM_ASSERT_LE(lhs, rhs,...)
If it is false that lhs <= rhs, print formatted error message and then panic.
Definition: logger.h:289
ValueWithStamp const & back() const
Definition: time_series.h:248
TValueWithStamp ValueWithStamp
Definition: time_series.h:84
Expected< Bounds > findBounds(double query_stamp) const
Definition: time_series.h:153
#define FARM_UNEXPECTED(cstr,...)
Definition: expected.h:86
auto interpolate(TType const &foo_from_bar, TType const &foo_from_daz, double p=0.5) -> TType
ConstIterator cend() const
Definition: time_series.h:262
concept StampedValue
StampedValue is a type that has a timestamp.
Definition: time_series.h:38
Expected< ValueWithStamp > findNearestWithin(double stamp, double threshold) const
Find nearest value given stamp within search radius threshold.
Definition: time_series.h:186
std::deque< ValueWithStamp > Container
Definition: time_series.h:85
ValueWithStamp const & operator[](size_t idx) const
access ValueWithStamp by index
Definition: time_series.h:111
void pop_front()
Definition: time_series.h:252
ConstIterator begin() const
Definition: time_series.h:257
MaxGap(double time_duration)
Definition: time_series.h:56
typename Container::const_iterator ConstIterator
Definition: time_series.h:86
ValueWithStamp const & front() const
Definition: time_series.h:246
tl::expected< TT, TE > Expected
Definition: expected.h:37
#define FARM_UNWRAP(wrapper,...)
Returns *wrapper, but panics if wrapper is nullopt or null.
Definition: logger.h:576
void pop_back()
Definition: time_series.h:254
concept InterpolativeValue
Interpolative is a type which can be interpolated.
Definition: time_series.h:49
ConstIterator cbegin() const
Definition: time_series.h:258