diff --git a/core/include/Data/DataSeriesUtils.h b/core/include/Data/DataSeriesUtils.h index dca5382..a8a4785 100644 --- a/core/include/Data/DataSeriesUtils.h +++ b/core/include/Data/DataSeriesUtils.h @@ -3,10 +3,47 @@ #include "CoreGlobal.h" +#include + +Q_DECLARE_LOGGING_CATEGORY(LOG_DataSeriesUtils) + /** * Utility class with methods for data series */ struct SCIQLOP_CORE_EXPORT DataSeriesUtils { + /** + * Processes data from a data series to complete the data holes with a fill value. + * + * A data hole is determined by the resolution passed in parameter: if, between two continuous + * data on the x-axis, the difference between these data is greater than the resolution, then + * there is one or more holes between them. The holes are filled by adding: + * - for the x-axis, new data corresponding to the 'step resolution' starting from the first + * data; + * - for values, a default value (fill value) for each new data added on the x-axis. + * + * For example, with : + * - xAxisData = [0, 1, 5, 7, 14 ] + * - valuesData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (two components per x-axis data) + * - fillValue = NaN + * - and resolution = 2; + * + * For the x axis, we calculate as data holes: [3, 9, 11, 13]. These holes are added to the + * x-axis data, and NaNs (two per x-axis data) are added to the values: + * => xAxisData = [0, 1, 3, 5, 7, 9, 11, 13, 14 ] + * => valuesData = [0, 1, 2, 3, NaN, NaN, 4, 5, 6, 7, NaN, NaN, NaN, NaN, NaN, NaN, 8, 9] + * + * @param xAxisData the x-axis data of the data series + * @param valuesData the values data of the data series + * @param resolution the resoultion (on x-axis) used to determinate data holes + * @param fillValue the fill value used for data holes in the values data + * + * @remarks There is no control over the consistency between x-axis data and values data. The + * method considers that the data is well formed (the total number of values data is a multiple + * of the number of x-axis data) + */ + static void fillDataHoles(std::vector &xAxisData, std::vector &valuesData, + double resolution, + double fillValue = std::numeric_limits::quiet_NaN()); }; #endif // SCIQLOP_DATASERIESUTILS_H diff --git a/core/src/Data/DataSeriesUtils.cpp b/core/src/Data/DataSeriesUtils.cpp index 63e20af..c0de3ff 100644 --- a/core/src/Data/DataSeriesUtils.cpp +++ b/core/src/Data/DataSeriesUtils.cpp @@ -1 +1,59 @@ #include "Data/DataSeriesUtils.h" + +Q_LOGGING_CATEGORY(LOG_DataSeriesUtils, "DataSeriesUtils") + +void DataSeriesUtils::fillDataHoles(std::vector &xAxisData, std::vector &valuesData, + double resolution, double fillValue) +{ + if (resolution == 0. || std::isnan(resolution)) { + qCWarning(LOG_DataSeriesUtils()) + << "Can't fill data holes with a null resolution, no changes will be made"; + return; + } + + if (xAxisData.empty()) { + qCWarning(LOG_DataSeriesUtils()) + << "Can't fill data holes for empty data, no changes will be made"; + return; + } + + // Gets the number of values per x-axis data + auto nbComponents = valuesData.size() / xAxisData.size(); + + // Generates fill values that will be used to complete values data + std::vector fillValues(nbComponents, fillValue); + + // Traverses the x-axis data + auto xAxisIt = xAxisData.begin(); + while (xAxisIt != xAxisData.end()) { + // Stops at first value which has a gap greater than resolution with the value next to it + xAxisIt = std::adjacent_find( + xAxisIt, xAxisData.end(), + [resolution](const auto &a, const auto &b) { return (b - a) > resolution; }); + + if (xAxisIt != xAxisData.end()) { + auto nextXAxisIt = xAxisIt + 1; + + // Gets the values that has a gap greater than resolution between them + auto lowValue = *xAxisIt; + auto highValue = *nextXAxisIt; + + // Completes holes between the two values by creating new values (according to the + // resolution) + for (auto i = lowValue + resolution; i < highValue; i += resolution) { + // Gets the iterator of values data from which to insert fill values + auto nextValuesIt = valuesData.begin() + + std::distance(xAxisData.begin(), nextXAxisIt) * nbComponents; + + // New value is inserted before nextXAxisIt + nextXAxisIt = xAxisData.insert(nextXAxisIt, i) + 1; + + // New values are inserted before nextValuesIt + valuesData.insert(nextValuesIt, fillValues.begin(), fillValues.end()); + } + + // Moves to the next value to continue loop on the x-axis data + xAxisIt = nextXAxisIt; + } + } +}