Skip to content

Commit

Permalink
re-black'ed
Browse files Browse the repository at this point in the history
  • Loading branch information
mchevalier2 committed Aug 7, 2024
1 parent af0a5ea commit a35c13f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
31 changes: 15 additions & 16 deletions app_streamlit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
''' The streamlit app to visualise and query the sensor data '''
""" The streamlit app to visualise and query the sensor data """

import os
from datetime import datetime as dt
Expand All @@ -9,7 +9,7 @@


def load_daily_data():
''' A wrap-up function to load the daily data '''
"""A wrap-up function to load the daily data"""
req = """
SELECT *
FROM read_parquet('./data/dat_sensors.parquet/*/*/*.parquet', hive_partitioning = true);
Expand All @@ -25,7 +25,7 @@ def load_daily_data():


def load_hourly_data():
''' A wrap-up function to load the hourly data '''
"""A wrap-up function to load the hourly data"""
req = """
SELECT *
FROM read_parquet('./data/dat_sensors_hours.parquet/*/*/*.parquet', hive_partitioning = true);
Expand All @@ -41,15 +41,15 @@ def load_hourly_data():


def filter_day(df: pd.DataFrame, cb: []) -> pd.DataFrame:
''' Filter the input dataset for days checked in cb '''
"""Filter the input dataset for days checked in cb"""
for i in range(7):
if not cb[i]:
df = df.drop(df[df.weekday == i].index)
return df


def filter_dates(df: pd.DataFrame, dates: dt, resol: str) -> pd.DataFrame:
''' Filter the input dataset for the days selected by slider'''
"""Filter the input dataset for the days selected by slider"""
date_end = dt(dates[1].year, dates[1].month, dates[1].day, 23)
if resol == "Hourly":
df["date"] = df[["date", "hour"]].apply(str_as_date_hour, axis=1)
Expand All @@ -63,23 +63,23 @@ def filter_dates(df: pd.DataFrame, dates: dt, resol: str) -> pd.DataFrame:


def filter_hours(df: pd.DataFrame, dates: dt) -> pd.DataFrame:
''' Filter the input dataset for the hours selected by slider'''
"""Filter the input dataset for the hours selected by slider"""
df = df.query(f"hour >= {dates[0]}").query(f"hour <= {dates[1]}")
return df


def str_as_date_day(s: str) -> dt:
''' Take a string and return a datetime '''
"""Take a string and return a datetime"""
return dt.strptime(s, "%Y-%m-%d")


def str_as_date_hour(s: []) -> dt:
''' Take a list of strings and return a datetime '''
"""Take a list of strings and return a datetime"""
return dt.strptime(s.iloc[0] + " " + str(int(s.iloc[1])), "%Y-%m-%d %H")


def create_slider_dates(df: pd.DataFrame) -> []:
''' Create a streamlit slider and return the min/max values '''
"""Create a streamlit slider and return the min/max values"""
values = st.slider(
"Select a range of dates",
str_as_date_day(df.date.min()),
Expand All @@ -90,7 +90,7 @@ def create_slider_dates(df: pd.DataFrame) -> []:


def create_slider_openinghours() -> []:
''' Create a streamlit slider and return the min/max values '''
"""Create a streamlit slider and return the min/max values"""
values = st.slider(
"Select a range of hours",
0,
Expand All @@ -105,11 +105,10 @@ def create_slider_openinghours() -> []:

if "dat_sensors_hours.parquet" not in os.listdir("./data"):
os.system("cp ./minidata/dat.csv ./data")
with open("process_data.py", encoding='utf-8') as f:
with open("process_data.py", encoding="utf-8") as f:
exec(f.read())



with st.sidebar:
df_shops, df_sensors = load_daily_data()

Expand All @@ -129,16 +128,16 @@ def create_slider_openinghours() -> []:
).df()

sensor = st.selectbox(
"Which sensor would you like to access? "+\
"If none are selected, will return the sum of all sensors.",
"Which sensor would you like to access? "
+ "If none are selected, will return the sum of all sensors.",
list_sensors,
index=None,
placeholder="Select a sensor...",
)
else:
sensor = st.selectbox(
"Which sensor would you like to access? "+\
"If none are selected, will return the sum of all sensors.",
"Which sensor would you like to access? "
+ "If none are selected, will return the sum of all sensors.",
"",
index=None,
placeholder="Select a shop first...",
Expand Down
6 changes: 1 addition & 5 deletions download_data_from_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ def hourly_it(start: datetime, finish: datetime) -> None:

if np.random.rand() < 0.01:
dict1.update(
{
list(dict1)[
np.random.randint(0, len(dict1) - 1)
]: "NULL"
}
{list(dict1)[np.random.randint(0, len(dict1) - 1)]: "NULL"}
)

sensor_data.append(dict1)
Expand Down
3 changes: 1 addition & 2 deletions process_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
os.chdir(__file__.split("process_data.py", maxsplit=1)[0])



os.system("rm -Rf ./data/dat_sensors_hours.parquet")
os.system("rm -Rf ./data/dat_sensors.parquet")
os.system("rm -Rf ./data/dat_shops.parquet")
os.system("rm -Rf ./data/dat_shops_hours.parquet")


def get_status(x: str) -> str:
''' A function to interpret the type of value returned by the API '''
"""A function to interpret the type of value returned by the API"""
if x == -1:
return "Measurement failed"
if x == -2:
Expand Down
3 changes: 1 addition & 2 deletions src_data_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
""" Creation of the data necessary for the API. """


from datetime import datetime

try:
Expand All @@ -10,7 +9,7 @@


def create_app() -> dict:
""" Create the data for the API """
"""Create the data for the API"""

store_name = ["Madrid", "Paris", "Berlin", "Roma", "London"]
store_n_sensors = [1, 20, 6, 4, 8]
Expand Down

0 comments on commit a35c13f

Please sign in to comment.