Some more bed data science
This commit is contained in:
parent
ddf32f02b2
commit
41073fce50
3 changed files with 165 additions and 26 deletions
118
bettwaage-plotter/data_analysis.ipynb
Normal file
118
bettwaage-plotter/data_analysis.ipynb
Normal file
File diff suppressed because one or more lines are too long
|
@ -2,19 +2,27 @@ import requests
|
|||
import matplotlib.pyplot as plt
|
||||
from datetime import datetime
|
||||
import json
|
||||
import os
|
||||
|
||||
latest_history_path = "latest_history.json"
|
||||
|
||||
file_path = None
|
||||
file_path = latest_history_path
|
||||
history_url = "http://192.168.178.84:9587/bettwaage/history"
|
||||
|
||||
focus_on_latest_bed_data = False
|
||||
|
||||
convert_time_to_seconds = True
|
||||
|
||||
# Get data
|
||||
data = None
|
||||
if file_path is None:
|
||||
if file_path is None or not os.path.exists(file_path):
|
||||
print("Fetching data ...")
|
||||
data = requests.get(history_url)
|
||||
data = data.json()
|
||||
|
||||
print("Saving latest data ...")
|
||||
with open(latest_history_path, "w", encoding="UTF-8") as fp:
|
||||
json.dump(data, fp)
|
||||
else:
|
||||
print("Reading data ...")
|
||||
with open(file_path, "r") as fp:
|
||||
|
@ -34,8 +42,10 @@ for d in data:
|
|||
"bl": d["bl"],
|
||||
"br": d["br"],
|
||||
}
|
||||
total_bed_only_weight = sum(bed_only_weight.values())
|
||||
break
|
||||
|
||||
if focus_on_latest_bed_data:
|
||||
# Collect all coherent sequences of someone being in bed
|
||||
in_bed_datas: list[list[dict]] = []
|
||||
is_in_bed_sequence = False
|
||||
|
@ -59,11 +69,11 @@ for sequence in in_bed_datas:
|
|||
|
||||
# Prepare data for plotting
|
||||
x = [d["timestamp"] for d in data]
|
||||
x = [datetime.strptime(d, "%Y-%m-%d %H:%M:%S.%f") for d in x]
|
||||
# x = [datetime.strptime(d, "%Y-%m-%d %H:%M:%S.%f") for d in x]
|
||||
|
||||
if convert_time_to_seconds:
|
||||
max_time = max(x)
|
||||
x = [(d - max_time).total_seconds() for d in x]
|
||||
# if convert_time_to_seconds:
|
||||
# max_time = max(x)
|
||||
# x = [(d - max_time).total_seconds() for d in x]
|
||||
|
||||
total = [d["total"] for d in data]
|
||||
tl = [d["tl"] for d in data]
|
||||
|
@ -76,6 +86,18 @@ left = [t + b for t, b in zip(tl, bl)]
|
|||
right = [t + b for t, b in zip(tr, br)]
|
||||
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
|
||||
person_weight = [t - total_bed_only_weight for t in total]
|
||||
|
||||
ax.set_xlabel("Time (s)")
|
||||
ax.set_ylabel("Weight (kg)")
|
||||
|
||||
ax.plot(x, person_weight, color="tab:blue")
|
||||
|
||||
plt.show()
|
||||
exit()
|
||||
|
||||
# Experiment: Calculate position over time
|
||||
bed_size = (140, 200)
|
||||
left_bed_only = bed_only_weight["tl"] + bed_only_weight["bl"]
|
||||
|
@ -93,7 +115,6 @@ for t, b, l, r in zip(top, bottom, left, right):
|
|||
)
|
||||
)
|
||||
|
||||
|
||||
# Plot data
|
||||
fig, (ax0, ax1) = plt.subplots(nrows=2)
|
||||
|
||||
|
@ -112,7 +133,6 @@ ax0.legend(
|
|||
["Total", "Top Left", "Top Right", "Bottom Left", "Bottom Right", "Top", "Bottom"]
|
||||
)
|
||||
|
||||
|
||||
# Experiment: Plot position
|
||||
import math
|
||||
import colorsys
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
matplotlib
|
||||
requests
|
||||
numpy
|
||||
PyQt5
|
||||
|
|
Loading…
Reference in a new issue