bettwaage-sidequest #2
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
|
import matplotlib.pyplot as plt
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import json
|
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"
|
history_url = "http://192.168.178.84:9587/bettwaage/history"
|
||||||
|
|
||||||
|
focus_on_latest_bed_data = False
|
||||||
|
|
||||||
convert_time_to_seconds = True
|
convert_time_to_seconds = True
|
||||||
|
|
||||||
# Get data
|
# Get data
|
||||||
data = None
|
data = None
|
||||||
if file_path is None:
|
if file_path is None or not os.path.exists(file_path):
|
||||||
print("Fetching data ...")
|
print("Fetching data ...")
|
||||||
data = requests.get(history_url)
|
data = requests.get(history_url)
|
||||||
data = data.json()
|
data = data.json()
|
||||||
|
|
||||||
|
print("Saving latest data ...")
|
||||||
|
with open(latest_history_path, "w", encoding="UTF-8") as fp:
|
||||||
|
json.dump(data, fp)
|
||||||
else:
|
else:
|
||||||
print("Reading data ...")
|
print("Reading data ...")
|
||||||
with open(file_path, "r") as fp:
|
with open(file_path, "r") as fp:
|
||||||
|
@ -34,36 +42,38 @@ for d in data:
|
||||||
"bl": d["bl"],
|
"bl": d["bl"],
|
||||||
"br": d["br"],
|
"br": d["br"],
|
||||||
}
|
}
|
||||||
|
total_bed_only_weight = sum(bed_only_weight.values())
|
||||||
break
|
break
|
||||||
|
|
||||||
# Collect all coherent sequences of someone being in bed
|
if focus_on_latest_bed_data:
|
||||||
in_bed_datas: list[list[dict]] = []
|
# Collect all coherent sequences of someone being in bed
|
||||||
is_in_bed_sequence = False
|
in_bed_datas: list[list[dict]] = []
|
||||||
threshhold = 100.0
|
is_in_bed_sequence = False
|
||||||
for d in data:
|
threshhold = 100.0
|
||||||
t = d["total"]
|
for d in data:
|
||||||
if t >= threshhold:
|
t = d["total"]
|
||||||
if not is_in_bed_sequence:
|
if t >= threshhold:
|
||||||
in_bed_datas.append([])
|
if not is_in_bed_sequence:
|
||||||
is_in_bed_sequence = True
|
in_bed_datas.append([])
|
||||||
in_bed_datas[-1].append(d)
|
is_in_bed_sequence = True
|
||||||
elif is_in_bed_sequence:
|
in_bed_datas[-1].append(d)
|
||||||
is_in_bed_sequence = False
|
elif is_in_bed_sequence:
|
||||||
|
is_in_bed_sequence = False
|
||||||
|
|
||||||
# Pick latest with minimum length/duration
|
# Pick latest with minimum length/duration
|
||||||
min_length = 100
|
min_length = 100
|
||||||
for sequence in in_bed_datas:
|
for sequence in in_bed_datas:
|
||||||
if len(sequence) >= min_length:
|
if len(sequence) >= min_length:
|
||||||
data = sequence
|
data = sequence
|
||||||
|
|
||||||
|
|
||||||
# Prepare data for plotting
|
# Prepare data for plotting
|
||||||
x = [d["timestamp"] for d in data]
|
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:
|
# if convert_time_to_seconds:
|
||||||
max_time = max(x)
|
# max_time = max(x)
|
||||||
x = [(d - max_time).total_seconds() for d in x]
|
# x = [(d - max_time).total_seconds() for d in x]
|
||||||
|
|
||||||
total = [d["total"] for d in data]
|
total = [d["total"] for d in data]
|
||||||
tl = [d["tl"] 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)]
|
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
|
# Experiment: Calculate position over time
|
||||||
bed_size = (140, 200)
|
bed_size = (140, 200)
|
||||||
left_bed_only = bed_only_weight["tl"] + bed_only_weight["bl"]
|
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
|
# Plot data
|
||||||
fig, (ax0, ax1) = plt.subplots(nrows=2)
|
fig, (ax0, ax1) = plt.subplots(nrows=2)
|
||||||
|
|
||||||
|
@ -112,7 +133,6 @@ ax0.legend(
|
||||||
["Total", "Top Left", "Top Right", "Bottom Left", "Bottom Right", "Top", "Bottom"]
|
["Total", "Top Left", "Top Right", "Bottom Left", "Bottom Right", "Top", "Bottom"]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# Experiment: Plot position
|
# Experiment: Plot position
|
||||||
import math
|
import math
|
||||||
import colorsys
|
import colorsys
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
matplotlib
|
matplotlib
|
||||||
requests
|
requests
|
||||||
numpy
|
numpy
|
||||||
|
PyQt5
|
||||||
|
|
Loading…
Reference in a new issue