Skip to content

Commit

Permalink
Determine whether running on server from environment variable; Remove…
Browse files Browse the repository at this point in the history
…d main_server.py; Updated Procfile accordingly; Added app url to README
  • Loading branch information
WeixuanZ committed Mar 26, 2020
1 parent 8a7886e commit 984706e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 470 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: bokeh serve --port=$PORT --address=0.0.0.0 --allow-websocket-origin=flood-warning.herokuapp.com --use-xheaders main_server.py
web: bokeh serve --port=$PORT --address=0.0.0.0 --allow-websocket-origin=flood-warning.herokuapp.com --use-xheaders main.py
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ The documentation can be found [here](/docs/flood-warning-system.pdf).

## Web Interface

To run the web interface,
A version of the web interface without neural network is available at https://flood-warning.herokuapp.com.

To run the web interface locally,
1. Set your Google Maps API key as an environment variable through
```bash
export API_KEY=<api_key>
Expand Down
84 changes: 45 additions & 39 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from floodsystem.predictor import predict
from floodsystem.stationdata import build_station_list, update_water_levels

ON_SERVER = environ.get('ON_SERVER') == 'true' # if the app is running on server, if so disable nn prediction

logger = logging.getLogger('main')
logger.setLevel(logging.INFO)

Expand Down Expand Up @@ -185,7 +187,12 @@ def update_map_select(attr, old, new):
text="""<p>Choose one of the high risk stations to see the prediction by a recurrent neural network
and least-squares polynomial fit.</p>"""
)
predicting_text = Div(text="""<p><i>Prediction is running...</i></p>""")

if not ON_SERVER:
predicting_text = Div(text="""<p><i>Prediction is running...</i></p>""")
else:
predicting_text = Div(text="""<i>Please note that due to limited processing power
and lack of GPUs on the server, this part is suppressed.</i>""")

# Warning

Expand Down Expand Up @@ -383,46 +390,45 @@ def update_map_select(attr, old, new):
doc.add_root(page_layout)
doc.title = "Flood Warning System"


# Run prediction in a new thread

def update_layout(old, new):
"""Function that updates the interface layout."""
old.children = new.children


def prediction_func():
"""Function that wraps the prediction code."""
predict_plots = []
for i, station in enumerate(highrisk_stations):
try:
date, level = predict(station.name, dataset_size=1000, lookback=200, iteration=100, display=300,
use_pretrained=True, batch_size=256, epoch=20)
except Exception:
logger.error('NN prediction failed')
date, level = ([], []), ([], [], [])
predict_plot = plot_prediction(date, level)
try:
poly, d0 = polyfit(date[0], level[0], 4)
predict_plot.line(date[0] + date[1], [poly(date - d0) for date in date2num(date[0] + date[1])],
line_width=2,
line_color='gray', legend_label='Polynomial Fit', line_dash='dashed')
except TypeError:
logger.error('No data for polyfit')
predict_plot.plot_width = 400
predict_plot.plot_height = 400
predict_plot.sizing_mode = 'scale_width'
predict_plots.append(Panel(child=predict_plot, title=station.name))
predicting_text = Div(text='<p><i>Prediction is running... {:.0%}</i></p>'.format(i / 6))
if not ON_SERVER:
def update_layout(old, new):
"""Function that updates the interface layout."""
old.children = new.children


def prediction_func():
"""Function that wraps the prediction code."""
predict_plots = []
for i, station in enumerate(highrisk_stations):
try:
date, level = predict(station.name, dataset_size=1000, lookback=200, iteration=100, display=300,
use_pretrained=True, batch_size=256, epoch=20)
except Exception:
logger.error('NN prediction failed')
date, level = ([], []), ([], [], [])
predict_plot = plot_prediction(date, level)
try:
poly, d0 = polyfit(date[0], level[0], 4)
predict_plot.line(date[0] + date[1], [poly(date - d0) for date in date2num(date[0] + date[1])],
line_width=2,
line_color='gray', legend_label='Polynomial Fit', line_dash='dashed')
except TypeError:
logger.error('No data for polyfit')
predict_plot.plot_width = 400
predict_plot.plot_height = 400
predict_plot.sizing_mode = 'scale_width'
predict_plots.append(Panel(child=predict_plot, title=station.name))
predicting_text = Div(text='<p><i>Prediction is running... {:.0%}</i></p>'.format(i / 6))
doc.add_next_tick_callback(partial(update_layout,
old=predict_column,
new=column(predict_text, predicting_text, width=500, height=650)))

predict_tabs = Tabs(tabs=predict_plots)
doc.add_next_tick_callback(partial(update_layout,
old=predict_column,
new=column(predict_text, predicting_text, width=500, height=650)))

predict_tabs = Tabs(tabs=predict_plots)
doc.add_next_tick_callback(partial(update_layout,
old=predict_column,
new=column(predict_text, predict_tabs, width=500, height=650)))
new=column(predict_text, predict_tabs, width=500, height=650)))


thread = Thread(target=prediction_func)
thread.start()
thread = Thread(target=prediction_func)
thread.start()

0 comments on commit 984706e

Please sign in to comment.