From 14fc52359371172d9b637b7e5af828ce3c0a767a Mon Sep 17 00:00:00 2001 From: Torben <59419684+entorb@users.noreply.github.com> Date: Tue, 23 Jan 2024 20:34:20 +0100 Subject: [PATCH] fix ruff findings --- 1fetch_v2.py | 16 +++++++--------- 2analyze_v2.py | 27 +++++++++++++-------------- api-v1/1fetch_v1.py | 6 +++++- api-v1/2analyze_v1.py | 4 ++++ 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/1fetch_v2.py b/1fetch_v2.py index 673f27d..ca2edde 100755 --- a/1fetch_v2.py +++ b/1fetch_v2.py @@ -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 @@ -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 = { @@ -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", diff --git a/2analyze_v2.py b/2analyze_v2.py index 9ecf2d9..c8e8832 100755 --- a/2analyze_v2.py +++ b/2analyze_v2.py @@ -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 @@ -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", @@ -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 @@ -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" @@ -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( @@ -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. """ @@ -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. """ @@ -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() diff --git a/api-v1/1fetch_v1.py b/api-v1/1fetch_v1.py index a12c0ff..034bd4c 100755 --- a/api-v1/1fetch_v1.py +++ b/api-v1/1fetch_v1.py @@ -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. @@ -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 = { diff --git a/api-v1/2analyze_v1.py b/api-v1/2analyze_v1.py index aa0ebcd..a27c564 100755 --- a/api-v1/2analyze_v1.py +++ b/api-v1/2analyze_v1.py @@ -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.