Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions glances/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ def subsample(data, sampling):
"""
if len(data) <= sampling:
return data
sampling_length = int(round(len(data) / float(sampling)))
return [mean(data[s * sampling_length : (s + 1) * sampling_length]) for s in range(0, sampling)]
# Slice bounds spread over the whole list: no slice is empty, none is left out
bounds = [s * len(data) // sampling for s in range(sampling + 1)]
return [mean(data[bounds[s] : bounds[s + 1]]) for s in range(0, sampling)]


def time_series_subsample(data, sampling):
Expand All @@ -196,9 +197,9 @@ def time_series_subsample(data, sampling):
return data
t = [t[0] for t in data]
v = [t[1] for t in data]
sampling_length = int(round(len(data) / float(sampling)))
t_subsampled = [t[s * sampling_length : (s + 1) * sampling_length][0] for s in range(0, sampling)]
v_subsampled = [mean(v[s * sampling_length : (s + 1) * sampling_length]) for s in range(0, sampling)]
bounds = [s * len(data) // sampling for s in range(sampling + 1)]
t_subsampled = [t[bounds[s]] for s in range(0, sampling)]
v_subsampled = [mean(v[bounds[s] : bounds[s + 1]]) for s in range(0, sampling)]
return list(zip(t_subsampled, v_subsampled))


Expand Down
24 changes: 23 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
UTC = timezone.utc

from glances.filter import GlancesFilter, GlancesFilterList
from glances.globals import auto_unit, pretty_date, split_esc, string_value_to_float, subsample
from glances.globals import (
auto_unit,
pretty_date,
split_esc,
string_value_to_float,
subsample,
time_series_subsample,
)
from glances.outputs.glances_bars import Bar


Expand All @@ -42,6 +49,21 @@ def test_016_subsample(self):
]:
l_subsample = subsample(l_test[0], l_test[1])
self.assertLessEqual(len(l_subsample), l_test[1])
# 1200 values in 800 points (graph export defaults): no empty slice
self.assertEqual(len(subsample(list(range(1200)), 800)), 800)
# 1000 values in 800 points: the last values are kept (mean of 998 and 999)
self.assertEqual(subsample(list(range(1000)), 800)[-1], 998.5)

def test_017_time_series_subsample(self):
"""Test time series subsampling function."""
print('INFO: [TEST_017] Time series subsampling')
for length, sampling in [(3, 4), (8, 4), (15, 10), (1000, 800), (1200, 800), (8000, 800)]:
data = [(t, float(t)) for t in range(length)]
l_subsample = time_series_subsample(data, sampling)
self.assertEqual(len(l_subsample), min(length, sampling))
# The first point starts the series, the last points end it
self.assertEqual(l_subsample[0][0], 0)
self.assertGreater(l_subsample[-1][1], length - 1 - length / sampling)

def test_018_string_value_to_float(self):
"""Check string_value_to_float function"""
Expand Down