-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataFilter.py
43 lines (34 loc) · 1.11 KB
/
DataFilter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import logging
import statistics
import sys
log = logging.getLogger(__name__)
def median(t_t_h: list) -> dict[str, float] or None:
y_list = []
for time_temp in t_t_h:
y_list.append(time_temp['temperature'])
if len(y_list) < 1:
return None
median = statistics.median(y_list)
try:
pstdev = statistics.pstdev(y_list)
except OverflowError as ex:
pstdev = 0
log.error(str(ex))
mean = statistics.mean(y_list)
return {'mean': mean, 'median': median, 'p_stand_dev': pstdev}
def linear(t_t_h: list) -> dict or None:
# Linear regression added in Python 3.10.x, not on Pi as of April 2023
vers = sys.version_info
major = vers[0]
minor = vers[1]
vers = major + minor / 100
if len(t_t_h) > 1 and vers >= 3.1:
x_list = []
y_list = []
for time_temp in t_t_h:
x_list.append(time_temp['time_ms'])
y_list.append(time_temp['temperature'])
result = statistics.linear_regression(x_list, y_list)
return {'slope': result.slope, 'intercept': result.intercept}
else:
return None