Basic statistics

This commit is contained in:
Maximilian Giller 2022-04-15 22:48:03 +02:00
parent a0896fc6e4
commit c5b697704f
3 changed files with 70 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
log.txt

View file

@ -7,3 +7,6 @@ homeassistant-mqtt-binding
# For Philips Hue Counter # For Philips Hue Counter
phue phue
# For statistics
matplotlib

66
statistics/statistics.py Normal file
View file

@ -0,0 +1,66 @@
from datetime import datetime
import json
from typing import Dict
import matplotlib.pyplot as plt
from numpy import where
# Config
FILE_PATH = "log.txt"
# Read file
content = None
with open(FILE_PATH, "r") as file:
content = file.readlines()
def parse_log_entry(entry: Dict) -> Dict:
if entry["countChange"] == 0:
return False
entry["dateTime"] = datetime.strptime(
str(entry["dateTime"])[:19], "%Y-%m-%d %H:%M:%S")
if entry["dateTime"] < datetime(2022, 1, 1):
return False
return entry
# Collect
log = [json.loads(line.strip("\x00")) for line in content]
print("Number of entries:", len(log))
# Parse & Filter
log = [parse_log_entry(entry) for entry in log if parse_log_entry(entry)]
print("Number of counts:", len(log))
# Render
fig, ax = plt.subplots() # Create a figure containing a single axes.
times: list[datetime] = [entry["dateTime"] for entry in log]
counts: list[int] = [entry["previousPeopleCount"] for entry in log]
ax.step(times, counts, where="pre")
plt.show()
# Calculate faults
for c, n in zip(list(range(len(log))), list(range(len(log)))[1:]):
estimated_count: int = log[c]["previousPeopleCount"] + \
log[c]["countChange"]
faulty: bool = estimated_count != log[n]["previousPeopleCount"]
log[c]["faulty"] = faulty
log[c]["faultyCount"] = log[c]["previousPeopleCount"] if faulty else None
log = log[:-1]
fault_count = sum(1 for entry in log if entry["faulty"])
print("Number of faults:", fault_count)
print("Percentage of faults:", fault_count / len(log) * 100)
print("-"*20)
faulty_off = [entry for entry in log if entry["faulty"]
and entry["faultyCount"] == 0]
faulty_on = [entry for entry in log if entry["faulty"]
and entry["faultyCount"] != 0]
print("Number of false-0:", len(faulty_off))
print("Number of false-1:", len(faulty_on))
print("Percentage of false-0:", len(faulty_off) / fault_count * 100)
print("Percentage of false-1:", len(faulty_on) / fault_count * 100)