fillMissingDays function
Use this function to fill missing days (the function adds NaNs) both in the future and in the
past (up to the specified number of days in the past). Since the SDK does not return a value
for the score in a day where the confidence it too low, this function can be useful to fill
in blanks for UI/UX purposes.
timeSeries the current TimeSeries
rewindDays how many days to fill in in the past
Implementation
TimeSeries<double> fillMissingDays(
TimeSeries<double> timeSeries, int rewindDays) {
final List<ZonedDateTime> newTimestamps = <ZonedDateTime>[];
final List<double> newValues = <double>[];
final List<double> newConfidenceIntervalLow = <double>[];
final List<double> newConfidenceIntervalHigh = <double>[];
final List<double> newConfidence = <double>[];
final ZonedDateTime currentDay = ZonedDateTime.now();
ZonedDateTime prevDate = timeSeries.timestamps.isNotEmpty
? timeSeries.timestamps[0].minus(days: rewindDays)
: currentDay.minus(days: rewindDays);
if (timeSeries.timestamps.isEmpty) {
final int nMissingDays = currentDay.difference(prevDate).inDays - 1;
for (int j = 0; j < nMissingDays; j++) {
newTimestamps.add(prevDate.add(Duration(days: j + 1)));
newValues.add(double.nan);
newConfidenceIntervalLow.add(double.nan);
newConfidenceIntervalHigh.add(double.nan);
newConfidence.add(double.nan);
}
}
for (int i = 0; i < timeSeries.values.length; i++) {
final int nMissingDays =
timeSeries.timestamps[i].difference(prevDate).inDays - 1;
for (int j = 0; j < nMissingDays; j++) {
newTimestamps.add(prevDate.add(Duration(days: j + 1)));
newValues.add(double.nan);
newConfidenceIntervalLow.add(double.nan);
newConfidenceIntervalHigh.add(double.nan);
newConfidence.add(double.nan);
}
newTimestamps.add(timeSeries.timestamps[i]);
newValues.add(timeSeries.values[i]);
newConfidenceIntervalLow.add(timeSeries.confidenceIntervalLow[i]);
newConfidenceIntervalHigh.add(timeSeries.confidenceIntervalHigh[i]);
newConfidence.add(timeSeries.confidence[i]);
prevDate = timeSeries.timestamps[i];
}
// Adding missing days up to today
final int nMissingFutureDays = newTimestamps.isNotEmpty
? currentDay.difference(newTimestamps.last).inDays
: 0;
final ZonedDateTime lastValue = newTimestamps.last;
for (int j = 0; j < nMissingFutureDays; j++) {
newTimestamps.add(lastValue.add(Duration(days: j + 1)));
newValues.add(double.nan);
newConfidenceIntervalLow.add(double.nan);
newConfidenceIntervalHigh.add(double.nan);
newConfidence.add(double.nan);
}
return TimeSeries<double>(
values: newValues,
timestamps: newTimestamps,
confidenceIntervalLow: newConfidenceIntervalLow,
confidenceIntervalHigh: newConfidenceIntervalHigh,
confidence: newConfidence,
);
}