Skip to content

Commit

Permalink
ENH: Set y-axis range using min/max from pressure profile window
Browse files Browse the repository at this point in the history
  • Loading branch information
cortadocodes committed Aug 11, 2022
1 parent bb42700 commit 7013229
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
14 changes: 9 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from dashboard.components.y_axis_select import YAxisSelect
from dashboard.graphs import plot_connections_statistics, plot_pressure_bar_chart, plot_sensors
from dashboard.queries import ROW_LIMIT, BigQuery
from dashboard.utils import get_cleaned_sensor_column_names


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -291,13 +292,14 @@ def plot_sensors_graph(

@cache.memoize(timeout=0)
def get_pressure_profiles_for_time_window(installation_reference, node_id, start_datetime, finish_datetime):
"""Get pressure profiles for the given node during the given time window.
"""Get pressure profiles for the given node during the given time window along with the minimum and maximum
pressures over all the sensors over that window.
:param str installation_reference:
:param str node_id:
:param datetime.datetime start_datetime:
:param datetime.datetime finish_datetime:
:return pandas.DataFrame:
:return (pandas.DataFrame, float, float): the pressure profiles, minimum pressure, and maximum pressure for the time window
"""
df, _ = BigQuery().get_sensor_data(
installation_reference=installation_reference,
Expand All @@ -314,7 +316,9 @@ def get_pressure_profiles_for_time_window(installation_reference, node_id, start
finish_datetime.isoformat(),
)

return df
sensor_column_names, _ = get_cleaned_sensor_column_names(df)
df_with_sensors_only = df[sensor_column_names]
return (df, df_with_sensors_only.min().min(), df_with_sensors_only.max().max())


@app.callback(
Expand All @@ -334,7 +338,7 @@ def plot_pressure_profile_graph(installation_reference, node_id, date, hour, min

initial_datetime = dt.datetime.combine(date=dt.date.fromisoformat(date), time=dt.time(hour, minute, second))

df = get_pressure_profiles_for_time_window(
df, minimum, maximum = get_pressure_profiles_for_time_window(
installation_reference=installation_reference,
node_id=node_id,
start_datetime=initial_datetime,
Expand All @@ -349,7 +353,7 @@ def plot_pressure_profile_graph(installation_reference, node_id, date, hour, min
]

logger.debug("Filtered pressure profile time window for single datetime.")
return plot_pressure_bar_chart(df)
return plot_pressure_bar_chart(df, minimum, maximum)


@app.callback(
Expand Down
24 changes: 5 additions & 19 deletions dashboard/graphs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import re

from plotly import express as px

from dashboard.queries import BigQuery
from dashboard.utils import generate_time_range
from dashboard.utils import generate_time_range, get_cleaned_sensor_column_names


def plot_connections_statistics(
Expand Down Expand Up @@ -39,7 +37,7 @@ def plot_sensors(installation_reference, node_id, sensor_name, time_range, custo
finish=finish,
)

original_sensor_names, cleaned_sensor_names = _get_cleaned_sensor_column_names(df)
original_sensor_names, cleaned_sensor_names = get_cleaned_sensor_column_names(df)

df.rename(
columns={
Expand All @@ -54,8 +52,8 @@ def plot_sensors(installation_reference, node_id, sensor_name, time_range, custo
return (figure, data_limit_applied)


def plot_pressure_bar_chart(df):
original_sensor_names, cleaned_sensor_names = _get_cleaned_sensor_column_names(df)
def plot_pressure_bar_chart(df, minimum, maximum):
original_sensor_names, cleaned_sensor_names = get_cleaned_sensor_column_names(df)
df_transposed = df[original_sensor_names].transpose()
df_transposed["Barometer number"] = cleaned_sensor_names

Expand All @@ -66,17 +64,5 @@ def plot_pressure_bar_chart(df):

figure = px.line(df_transposed, x="Barometer number", y="Raw value")
figure.add_bar(x=df_transposed["Barometer number"], y=df_transposed["Raw value"])
figure.update_layout(showlegend=False)
figure.update_layout(showlegend=False, yaxis_range=[minimum, maximum])
return figure


def _get_cleaned_sensor_column_names(dataframe):
"""Get cleaned sensor column names for a dataframe when the columns are named like "f0_", "f1_"... "fn_" for `n`
sensors.
:param pandas.DataFrame dataframe: a dataframe containing columns of sensor data named like "f0_", "f1_"...
:return (list, list): the uncleaned and cleaned sensor names
"""
original_names = [column for column in dataframe.columns if column.startswith("f") and column.endswith("_")]
cleaned_names = [re.findall(r"f(\d+)_", sensor_name)[0] for sensor_name in original_names]
return original_names, cleaned_names
13 changes: 13 additions & 0 deletions dashboard/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import re


TIME_RANGE_OPTIONS = {
Expand Down Expand Up @@ -36,3 +37,15 @@ def generate_time_range(time_range, custom_start_date=None, custom_end_date=None
finish = datetime.datetime.now()
start = finish - TIME_RANGE_OPTIONS[time_range]
return start, finish


def get_cleaned_sensor_column_names(dataframe):
"""Get cleaned sensor column names for a dataframe when the columns are named like "f0_", "f1_"... "fn_" for `n`
sensors.
:param pandas.DataFrame dataframe: a dataframe containing columns of sensor data named like "f0_", "f1_"...
:return (list, list): the uncleaned and cleaned sensor names
"""
original_names = [column for column in dataframe.columns if column.startswith("f") and column.endswith("_")]
cleaned_names = [re.findall(r"f(\d+)_", sensor_name)[0] for sensor_name in original_names]
return original_names, cleaned_names

0 comments on commit 7013229

Please sign in to comment.