Skip to content

Commit

Permalink
fix ruff findings
Browse files Browse the repository at this point in the history
  • Loading branch information
entorb committed Jan 23, 2024
1 parent f77093a commit 14fc523
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
16 changes: 7 additions & 9 deletions 1fetch_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
"""
# standard modules
import json
import os
from pathlib import Path

import requests

# external modules

os.makedirs("data", exist_ok=True)
Path("data").mkdir(exist_ok=True)

with open(file="config.json", encoding="utf-8") as fh:
with Path("config.json").open(encoding="utf-8") as fh:
config = json.load(fh)

with open(file="token.txt") as fh:
with Path("token.txt").open() as fh:
token = fh.read()
token = token.strip() # trim spaces

Expand All @@ -35,7 +35,7 @@ def fetch_data_summaries() -> None:
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']}" # noqa: E501
url = f"https://api.ouraring.com/v2/usercollection/{data_summary_set}?start_date={config['date_start']}"
# start=YYYY-MM-DD
# end=YYYY-MM-DD
headers = {
Expand All @@ -45,15 +45,13 @@ def fetch_data_summaries() -> None:
cont = requests.get(url, headers=headers, timeout=3).content
cont = cont.decode("utf-8")

with open(
file=f"data/data_raw_{data_summary_set}.json",
with Path(f"data/data_raw_{data_summary_set}.json").open(
mode="w",
encoding="utf-8",
newline="\n",
) as fh:
fh.write(cont)
with open(
f"data/data_formatted_{data_summary_set}.json",
with Path(f"data/data_formatted_{data_summary_set}.json").open(
mode="w",
encoding="utf-8",
newline="\n",
Expand Down
27 changes: 13 additions & 14 deletions 2analyze_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
some charts of correlating data are generated in plot/
"""
import json
import os
from pathlib import Path

import matplotlib.pyplot as plt
import pandas as pd
Expand All @@ -19,11 +19,10 @@
# import numpy as np
# import matplotlib.ticker as mtick

os.makedirs("plot", exist_ok=True)
Path("plot").mkdir(exist_ok=True)

# empty file
fh_report = open( # noqa: SIM115
file="sleep_report.txt",
fh_report = Path("sleep_report.txt").open( # noqa: SIM115
mode="w",
encoding="utf-8",
newline="\n",
Expand All @@ -43,7 +42,7 @@ def prep_data_sleep() -> pd.DataFrame:
"""
Prepare sleep data.
"""
with open(file="data/data_raw_sleep.json", encoding="utf-8") as fh:
with Path("data/data_raw_sleep.json").open(encoding="utf-8") as fh:
d_json = json.load(fh)
d_json = d_json["data"] # drop first level

Expand All @@ -56,7 +55,7 @@ def prep_data_sleep() -> pd.DataFrame:
df = df[df["time_in_bed"] > 4 * 3600]

# remove 5min-interval time series
df.drop(columns=["heart_rate", "hrv", "movement_30_sec"], inplace=True)
df = df.drop(columns=["heart_rate", "hrv", "movement_30_sec"])

# DateTime parsing
df["day"] = pd.to_datetime(df["day"]) # , format="ISO8601"
Expand Down Expand Up @@ -114,24 +113,22 @@ def prep_data_sleep() -> pd.DataFrame:

df["time awake"] = df["awake_time"] / 60

df.drop(
df = df.drop(
columns=[
"total_sleep_duration",
"efficiency",
"latency",
"awake_time",
],
inplace=True,
)

# rename some columns
df.rename(
df = df.rename(
columns={
"average_hrv": "HRV average",
"average_heart_rate": "HR average",
"lowest_heart_rate": "HR min",
},
inplace=True,
)

df.to_csv(
Expand All @@ -142,7 +139,9 @@ def prep_data_sleep() -> pd.DataFrame:
return df


def corrlation_tester(df, was, interesting_properties):
def corrlation_tester(
df: pd.DataFrame, was: str, interesting_properties: str
) -> tuple[dict, list, list]:
"""
Tester for Correlations.
"""
Expand Down Expand Up @@ -183,7 +182,9 @@ def corrlation_tester(df, was, interesting_properties):
return d_results, l_corr_pos, l_corr_neg


def plotit(df, was, d_results, l_corr_pos, l_corr_neg) -> None:
def plotit(
df: pd.DataFrame, was: str, d_results: dict, l_corr_pos: list, l_corr_neg: list
) -> None:
"""
Plot the data.
"""
Expand Down Expand Up @@ -366,5 +367,3 @@ def plotit(df, was, d_results, l_corr_pos, l_corr_neg) -> None:
)
axes.grid(zorder=0)
plt.savefig(fname="plot/scatter1.png", format="png")

exit()
6 changes: 5 additions & 1 deletion api-v1/1fetch_v1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env python3
# by Dr. Torben Menke https://entorb.net
# https://github.com/entorb/analyze-oura

# TODO:
# ruff: noqa

"""
Fetch Oura day-summary data from Oura Cloud API.
Expand Down Expand Up @@ -35,7 +39,7 @@ def fetch_data_summaries() -> None:
print(f"fetching {data_summary_set} data")
# url = "https://api.ouraring.com/v1/sleep"
# -> last week
url = f"https://api.ouraring.com/v1/{data_summary_set}?start={config['date_start']}" # noqa: E501
url = f"https://api.ouraring.com/v1/{data_summary_set}?start={config['date_start']}"
# start=YYYY-MM-DD
# end=YYYY-MM-DD
headers = {
Expand Down
4 changes: 4 additions & 0 deletions api-v1/2analyze_v1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env python3
# by Dr. Torben Menke https://entorb.net
# https://github.com/entorb/analyze-oura

# TODO:
# ruff: noqa

"""
Analyze data of Oura daily summaries fetched from Oura Cloud API.
Expand Down

0 comments on commit 14fc523

Please sign in to comment.