From ca0ce798da4514f256fc25be53dbca84821ace9a Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Thu, 5 Jan 2023 15:14:28 +0100 Subject: [PATCH] Keep min and max values of dropped data points --- ltsdb_json.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/ltsdb_json.py b/ltsdb_json.py index b70a3b5..d943642 100644 --- a/ltsdb_json.py +++ b/ltsdb_json.py @@ -43,6 +43,31 @@ class LTS: json.dump({"description": self.description, "data": self.data}, fh) self.rebuild_index() + def pop(self, i): + # Pop the element at index i and adjust the min/max values of the + # neighbours. + # We might also want to adjust the value of the neighbours to some + # (weighted) average, but I'm not sure if this is actually a good idea. + data = self.data + old = data.pop(i) # after that the neighbours are at i-1, i + min_v = old[2] if len(old) >= 4 else old[1] + max_v = old[3] if len(old) >= 4 else old[1] + if i > 0: + if len(data[i-1]) == 2: + data[i-1] = [data[i-1][0], data[i-1][1], data[i-1][1], data[i-1][1]] + if min_v < data[i-1][2]: + data[i-1][2] = min_v + if max_v > data[i-1][3]: + data[i-1][3] = max_v + if i < len(data): + if len(data[i]) == 2: + data[i] = [data[i][0], data[i][1], data[i][1], data[i][1]] + if min_v < data[i][2]: + data[i][2] = min_v + if max_v > data[i][3]: + data[i][3] = max_v + return old + def shrink(self): # Remove one element in such a way that the distributions gets closer # to an exponential curve through the first and the last few data @@ -61,7 +86,7 @@ class LTS: t_ideal = (math.exp(k * (n - i)/n) - 1) * (n * dt) if t_last - data[i][0] > t_ideal: log.debug("%s - %s > %s -> popping element %s", t_last, data[i][0], t_ideal, i) - data.pop(i) + self.pop(i) break else: # Well, it works mostly. Sometimes all the real points are below @@ -71,7 +96,7 @@ class LTS: # narrow range just before that. i = random.randrange(int(n*0.98), int(n*0.99)) log.debug("no match -> popping element %s", i) - data.pop(i) + self.pop(i) def add(self, ts, value): while len(self.data) >= self.limit: