Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions flagging_site/blueprints/flagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,18 @@ def add_to_dict(models, df, reach) -> None:
@bp.route('/')
def index() -> str:
"""
Retrieves data from hobolink and usgs and processes data, then displays data
on `index_model.html`
Retrieves data from database,
then displays data on `index_model.html`

returns: render model on index.html
"""

df = latest_model_outputs()
df = df.set_index('reach')
flags = {
key: val['safe']
for key, val
in latest_model_outputs().items()
in df.to_dict(orient='index').items()
}
return render_template('index.html', flags=flags)

Expand Down Expand Up @@ -113,27 +116,20 @@ def output_model() -> str:
# Look at no more than 72 hours.
hours = min(max(hours, 1), 72)

df = get_data()
df = latest_model_outputs(hours)

reach_model_mapping = {
2: reach_2_model,
3: reach_3_model,
4: reach_4_model,
5: reach_5_model
}

if reach in reach_model_mapping:
reach_func = reach_model_mapping[int(reach)]
reach_html_tables = {
reach: stylize_model_output(reach_func(df, rows=hours))
}
else:
reach_html_tables = {
reach: stylize_model_output(reach_func(df, rows=hours))
for reach, reach_func
in reach_model_mapping.items()
}
# reach_html_tables is a dict where the index is the reach number
# and the values are HTML code for the table of data to display for
# that particular reach
reach_html_tables = {}

# loop through each reach in df
# extract the subset from the df for that reach
# then convert that df subset to HTML code
# and then add that HTML subset to reach_html_tables
for i in df.reach.unique():
reach_html_tables[i] = stylize_model_output( df.loc[df['reach'] == i ] )

return render_template('output_model.html', tables=reach_html_tables)


Expand Down Expand Up @@ -174,4 +170,4 @@ def get(self):
return self.model_api()


api.add_resource(ReachApi, '/api/v1/model')
api.add_resource(ReachApi, '/api/v1/model')
2 changes: 1 addition & 1 deletion flagging_site/data/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def update_database():

# Populate the `hobolink` table.
from .hobolink import get_live_hobolink_data
df_hobolink = get_live_hobolink_data()
df_hobolink = get_live_hobolink_data('code_for_boston_export_21d')
df_hobolink.to_sql('hobolink', **options)

from .model import process_data
Expand Down
26 changes: 17 additions & 9 deletions flagging_site/data/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def process_data(
return df


def reach_2_model(df: pd.DataFrame, rows: int = 24) -> pd.DataFrame:
def reach_2_model(df: pd.DataFrame, rows: int = 48) -> pd.DataFrame:
"""Model params:
a- rainfall sum 0-24 hrs
d- Days since last rain
Expand All @@ -172,7 +172,7 @@ def reach_2_model(df: pd.DataFrame, rows: int = 24) -> pd.DataFrame:
return df[['reach', 'time', 'log_odds', 'probability', 'safe']]


def reach_3_model(df: pd.DataFrame, rows: int = 24) -> pd.DataFrame:
def reach_3_model(df: pd.DataFrame, rows: int = 48) -> pd.DataFrame:
"""
a- rainfall sum 0-24 hrs
b- rainfall sum 24-48 hr
Expand Down Expand Up @@ -201,7 +201,7 @@ def reach_3_model(df: pd.DataFrame, rows: int = 24) -> pd.DataFrame:
return df[['reach', 'time', 'log_odds', 'probability', 'safe']]


def reach_4_model(df: pd.DataFrame, rows: int = 24) -> pd.DataFrame:
def reach_4_model(df: pd.DataFrame, rows: int = 48) -> pd.DataFrame:
"""
a- rainfall sum 0-24 hrs
b- rainfall sum 24-48 hr
Expand Down Expand Up @@ -230,7 +230,7 @@ def reach_4_model(df: pd.DataFrame, rows: int = 24) -> pd.DataFrame:
return df[['reach', 'time', 'log_odds', 'probability', 'safe']]


def reach_5_model(df: pd.DataFrame, rows: int = 24) -> pd.DataFrame:
def reach_5_model(df: pd.DataFrame, rows: int = 48) -> pd.DataFrame:
"""
c- rainfall sum 0-48 hr
d- Days since last rain
Expand Down Expand Up @@ -269,9 +269,17 @@ def all_models(df: pd.DataFrame, *args, **kwargs):


def latest_model_outputs(hours: int = 1) -> dict:
if hours != 1:
raise NotImplementedError('Need to work on this!')
from .database import execute_sql_from_file
df = execute_sql_from_file('latest_model.sql')
df = df.set_index('reach')
return df.to_dict(orient='index')

if hours == 1:
df = execute_sql_from_file('return_1_hour_of_model_outputs.sql')
elif hours > 1:
df = execute_sql_from_file('return_48_hours_of_model_outputs.sql') # pull out 48 hours of model outputs
latest_time = max(df['time']) # find most recent timestamp
time_interval = pd.Timedelta(str(hours) + ' hours') # create pandas Timedelta, based on input parameter hours
df = df[ latest_time - df['time']< time_interval ] # reset df to exclude anything from before time_interval ago

else:
raise ValueError('hours of data to pull cannot be less than one')

return df
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- This query returns up to 48 hours of the latest data

SELECT *
FROM model_outputs
WHERE time BETWEEN
(SELECT MAX(time) - interval '47 hours' FROM model_outputs)
AND
(SELECT MAX(time) FROM model_outputs)
Binary file modified requirements.txt
Binary file not shown.