Issue: Misalignment in Time Series Data Due to Differing Trajectory Sizes with reactive scripts
Problem Description
Currently, PJ uses the nearest time index for data retrieval (TimeseriesRef::atTime(double t)), which can lead to inaccuracies when analyzing time series data of different sizes. This method may inadvertently use outdated data towards the end of a data set, affecting analysis reliability.
I have exposed the internal index through TimeseriesRef::getIndexAtTime(double t) to ensure that indices match when correlating data points across different series. This ensures that comparisons and analyses use data points from the same measurement intervals.
function CreateSeriesFromArray(new_series, prefix, suffix_X, suffix_Y, timestamp)
new_series:clear()
local index = 0, internal_index, internal_prev_index = 0, -1
while true do
local series_x = TimeseriesView.find(string.format("%s[%d]/%s", prefix, index, suffix_X))
if not series_x then break end
internal_index = series_x:getIndexAtTime(timestamp)
if internal_index ~= internal_prev_index then break end
local x = series_x:atTime(timestamp)
local y = (TimeseriesView.find(string.format("%s[%d]/%s", prefix, index, suffix_Y))):atTime(timestamp)
new_series:push_back(x, y)
internal_prev_index = internal_index
index = index + 1
end
end
Proposed Solution
To address this issue more robustly, I propose enhancing the TimeseriesRef class to support both exact and nearest match strategies for time-based data retrieval:
enum class MatchType {
Exact, // Returns an index only if the exact time is found
Nearest // Returns the nearest time index (current behavior)
};
std::optional<unsigned> getIndexAtTime(double t, MatchType match_type) const {
if (match_type == MatchType::Exact) {
auto it = std::find_if(_plot_data->begin(), _plot_data->end(),
[t](const auto& point) { return point.x == t; });
if (it != _plot_data->end()) {
return std::distance(_plot_data->begin(), it);
}
return std::nullopt; // Exact time not found
} else {
return _plot_data->getIndexFromX(t); // Nearest match
}
}
double atTime(double t, MatchType match_type) const {
auto index = getIndexAtTime(t, match_type);
if (!index) {
throw std::runtime_error("Time point not found for exact match requirement");
}
return _plot_data->at(*index).y;
}
However, I think that @facontidavide could have better ideas.
Issue: Misalignment in Time Series Data Due to Differing Trajectory Sizes with reactive scripts
Problem Description
Currently, PJ uses the nearest time index for data retrieval (
TimeseriesRef::atTime(double t)
), which can lead to inaccuracies when analyzing time series data of different sizes. This method may inadvertently use outdated data towards the end of a data set, affecting analysis reliability.Example of the Problem
Please found some dummy data here: DummyData_2024-03-19.csv
Implemented Workaround
I have exposed the internal index through
TimeseriesRef::getIndexAtTime(double t)
to ensure that indices match when correlating data points across different series. This ensures that comparisons and analyses use data points from the same measurement intervals.Code Snippet Illustrating the Workaround
Proposed Solution
To address this issue more robustly, I propose enhancing the
TimeseriesRef
class to support both exact and nearest match strategies for time-based data retrieval:However, I think that @facontidavide could have better ideas.