From c5b697704f43f041c01c40bfa03799223b360009 Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 15 Apr 2022 22:48:03 +0200 Subject: [PATCH] Basic statistics --- .gitignore | 1 + requirements.txt | 3 ++ statistics/statistics.py | 66 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 .gitignore create mode 100644 statistics/statistics.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6bd6746 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +log.txt diff --git a/requirements.txt b/requirements.txt index 7711555..6deb9c8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,6 @@ homeassistant-mqtt-binding # For Philips Hue Counter phue + +# For statistics +matplotlib diff --git a/statistics/statistics.py b/statistics/statistics.py new file mode 100644 index 0000000..bda2912 --- /dev/null +++ b/statistics/statistics.py @@ -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)