Skip to content

Commit

Permalink
Streamlit improved
Browse files Browse the repository at this point in the history
  • Loading branch information
entorb committed Dec 15, 2024
1 parent 010d1b3 commit 4942264
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 94 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
## Run

* set the start date in [config.toml](src/config.toml)
* [fetch_v2.py](src/fetch_v2.py): download your Oura data to [data/](data/)
* [analyze_v2.py](src/analyze_v2.py): analyze your Oura data
* [fetch.py](src/fetch.py): download your Oura data to [data/](data/)
* [report.py](src/report.py): analyze your Oura data
* [app.py](src/app.py): Streamlit visualization

## Results
Expand Down
1 change: 1 addition & 0 deletions cspell-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
analize
autoflake
autoupdate
DataFrame
dayofweek
figsize
hypnogram
Expand Down
3 changes: 1 addition & 2 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ ignorePaths:
- ".*"
- "dist"
- "build"
- "node_modules"
- "requirements.txt"
- "api-v1"
- "src/deprecated"
89 changes: 69 additions & 20 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,70 @@
from pathlib import Path

import altair as alt
import pandas as pd
import streamlit as st

from analyze_v2 import prep_data_sleep
from report import prep_data_sleep

st.set_page_config(page_title="Oura Sleep Report", page_icon=None, layout="wide")
st.title("Oura Sleep Report")

with (Path("src/config.toml")).open("rb") as f:
config = tomllib.load(f)
date_start_default = (dt.datetime.now(tz=dt.UTC) - dt.timedelta(days=21)).date()
date_start_default = (dt.datetime.now(tz=dt.UTC) - dt.timedelta(weeks=4)).date()


df = prep_data_sleep()
df = df.reset_index()
df = df.drop(columns=["id"])
df = df.drop(columns=["bedtime_end", "bedtime_start", "sleep_phase_5_min"])
df = df.sort_values("day", ascending=False)


col1, col2, col3 = st.columns(3)
col1, col2, col3, col4 = st.columns(4)

sel_start_date = col1.date_input(
"Start", value=date_start_default, format=config["date_format"]
)
if sel_start_date:
df = df.query(f"day >= '{sel_start_date}'")
df = df.query("day >= @sel_start_date")

st.columns(1)

d = {}
for prop in ("score", "start of sleep", "end of sleep", "HR average", "HRV average"):
d[prop] = {}
d[prop]["week even"] = df.query("week_even == True")[prop].mean().round(1) # type: ignore
d[prop]["week uneven"] = df.query("week_even == False")[prop].mean().round(1) # type: ignore
lst = [0, 1, 2, 3, 4]
d[prop]["weekdays"] = df.query("dayofweek == @lst")[prop].mean().round(1) # type: ignore
lst = [5, 6]
d[prop]["weekend"] = df.query("dayofweek == @lst")[prop].mean().round(1) # type: ignore
# st.write(d)
df2 = pd.DataFrame.from_dict(d)
st.write(df2.transpose())

sel_weekend = col2.selectbox("Week or Weekend", options=("Su-Th", "Fr-Sa"), index=None)

col1, col2, col3 = st.columns(3)
sel_week_even = col1.selectbox("Week even.", options=("Even", "Uneven"), index=None)
if sel_week_even:
if sel_week_even == "Even":
df = df.query("week_even == True")
elif sel_week_even == "Uneven":
df = df.query("week_even == False")

sel_weekend = col2.selectbox("Weekend", options=("Weekday", "Weekend"), index=None)
if sel_weekend:
if sel_weekend == "Su-Th":
if sel_weekend == "Weekday": # Su-Th
lst = [0, 1, 2, 3, 4]
elif sel_weekend == "Fr-Sa":
elif sel_weekend == "Weekend": # Fr-Sa
lst = [5, 6]

df = df.query("dayofweek == @lst")


# 0 -> Sunday
sel_weekday = col3.selectbox(
"Weekday", options=("0", "1", "2", "3", "4", "5", "6"), index=None
)
day_map = {"Su": 0, "Mo": 1, "Tu": 2, "Wed": 3, "Th": 4, "Fr": 5, "Sa": 6}
sel_weekday = col3.selectbox("Weekday", options=day_map.keys(), index=None)
if sel_weekday:
df = df.query(f"dayofweek == '{sel_weekday}'")
df = df.query(f"dayofweek == {day_map[sel_weekday]}")


# c = st.line_chart(data=df, x="day", y="duration of sleep", x_label=None)
Expand All @@ -65,17 +87,20 @@
base = alt.Chart(df).encode(alt.X("day", title=None))


for prop in ("duration of sleep", "HR average", "HRV average"):
c = base.mark_line().encode(
y=alt.Y(prop),
for prop in ("score", "start of sleep", "sleep total h", "HR average", "HRV average"):
ymin = df[prop].min()
ymax = df[prop].max()
c = base.mark_point(size=100).encode(
y=alt.Y(prop, scale=alt.Scale(domain=[ymin, ymax])),
)
cr = c.transform_regression("day", prop).mark_line(color="grey", strokeDash=[4, 4])
layers = alt.layer(c, cr) # .resolve_scale(y="independent")
cl = c.mark_line()
layers = alt.layer(c, cr, cl) # .resolve_scale(y="independent")
st.altair_chart(layers, use_container_width=True) # type: ignore


c1 = base.mark_line().encode(
y=alt.Y("duration of sleep"),
y=alt.Y("sleep total h"),
)
c2 = base.mark_line(color="red").encode(
y=alt.Y("HRV average"),
Expand All @@ -84,7 +109,7 @@
st.altair_chart(layers, use_container_width=True) # type: ignore

c1 = base.mark_line().encode(
y=alt.Y("duration of sleep"),
y=alt.Y("sleep total h"),
)
c2 = base.mark_line(color="red").encode(
y=alt.Y("HR average"),
Expand All @@ -94,6 +119,30 @@


st.columns(1)

st.subheader("relevant data")
st.dataframe(
data=df,
hide_index=True,
column_config={"day": st.column_config.DateColumn(format=config["date_format"])},
column_order=[
"day",
"start of sleep",
"end of sleep",
"score",
"HR mini",
"HR average",
"HRV average",
"time in bed h",
"sleep total h",
"sleep rem h",
"sleep deep h",
"temperature_deviation",
],
)


st.subheader("all data")
st.dataframe(
data=df,
hide_index=True,
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 1 addition & 9 deletions src/fetch_v2.py → src/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ def fetch_data_summaries() -> None:
"""
for data_summary_set in ("sleep",): # , "activity", "readiness"
print(f"fetching {data_summary_set} data")
# url = "https://api.ouraring.com/v1/sleep"
# -> last week
url = f"https://api.ouraring.com/v2/usercollection/{data_summary_set}?start_date={config['date_start']}"
# start=YYYY-MM-DD
# end=YYYY-MM-DD
Expand All @@ -51,13 +49,7 @@ def fetch_data_summaries() -> None:
print(f"Error fetching {data_summary_set} data: {e}")
continue

# Write raw data to file
raw_data_path = Path(f"data/data_raw_{data_summary_set}.json")
with raw_data_path.open(mode="w", encoding="utf-8", newline="\n") as fh:
fh.write(cont)

# Write formatted data to file
formatted_data_path = Path(f"data/data_formatted_{data_summary_set}.json")
formatted_data_path = Path(f"data/data_{data_summary_set}.json")
with formatted_data_path.open(mode="w", encoding="utf-8", newline="\n") as fh:
d = json.loads(cont)
json.dump(d, fh, ensure_ascii=False, sort_keys=False, indent=True)
Expand Down
Loading

0 comments on commit 4942264

Please sign in to comment.