Skip to content

Instantly share code, notes, and snippets.

@deniszh
Last active October 13, 2021 10:25
Show Gist options
  • Save deniszh/e9fcbbbd8026f163bbaffc6eec03b996 to your computer and use it in GitHub Desktop.
Save deniszh/e9fcbbbd8026f163bbaffc6eec03b996 to your computer and use it in GitHub Desktop.
compressPeriodicGaps.patch
--- functions.py.bak
+++ functions.py
@@ -151,6 +151,41 @@
return (seriesList)
+def _compressPeriodicGaps(series):
+ # removing periodic gaps, using summarize(seriesList, '<desired step>', 'last')
+ # but trying to auto detect step by first three existing values
+ consolidate = series.consolidationFunc
+ tags = series.tags
+ xFilesFactor = series.xFilesFactor
+ pathExpression = series.pathExpression
+ # try to detect interval
+ firstSeen = -1
+ secondSeen = -1
+ interval = None
+ for i, value in enumerate(series):
+ if value:
+ if firstSeen >= 0:
+ secondSeen = i
+ break
+ else:
+ firstSeen = i
+ stepGuess = secondSeen - firstSeen
+ thirdSeen = secondSeen + stepGuess
+ if stepGuess > 1 and thirdSeen <= len(series) - 2: # protecting list boundaries
+ if series[thirdSeen]: # if we predict value
+ if series[thirdSeen - 1] is None and series[thirdSeen + 1] is None: # ..and it surrounded by Nones
+ interval = stepGuess * series.step # we probably guessed interval.
+ if interval:
+ newStart = series.start + firstSeen * series.step # skipping initial Nones
+ (newValues, _) = _summarizeValues(series, 'last', interval, newStart, series.end)
+ newEnd = newStart + interval * (len(newValues) - 1) # calculating new end from summarized values
+ return TimeSeries(series.name, newStart, newEnd, interval, newValues,
+ consolidate=consolidate, tags=tags, xFilesFactor=xFilesFactor, pathExpression=pathExpression)
+ else:
+ # we couldn't detect interval, just return untouched series
+ return series
+
+
def formatPathExpressions(seriesList):
# remove duplicates
pathExpressions = []
@@ -2139,6 +2174,7 @@
"""
results = []
for series in seriesList:
+ series = _compressPeriodicGaps(series)
newValues = []
prev = None
for val in series:
@@ -2183,6 +2219,7 @@
"""
results = []
for series in seriesList:
+ series = _compressPeriodicGaps(series)
newValues = []
prev = None
step = series.step
@@ -2361,6 +2398,7 @@
results = []
for series in seriesList:
+ series = _compressPeriodicGaps(series)
newValues = []
prev = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment