Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dashboard using Plotly Dash. #4

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ You will find instructions to install docker engine on your system [here.](https
### Pulling and running the docker image

```commandline
docker pull tejsukhatme/influenza_estimator:random_forest
docker run -it -p 5000:5000 tejsukhatme/influenza_estimator:random_forest
docker pull tejsukhatme/influenza_estimator:latest
docker run -it -p 5000:5000 tejsukhatme/influenza_estimator:latest
```


Expand All @@ -21,7 +21,7 @@ docker run -it -p 5000:5000 tejsukhatme/influenza_estimator:random_forest
cd into the folder of choice and enter:
```commandline
git pull https://github.com/Hephaestus12/applications.git
cd applications/gsoc_application_projects/2020/influenza/web/
cd applications/gsoc_application_projects/2020/influenza/dash/
```

### Prepare the Environment
Expand All @@ -40,9 +40,7 @@ pip install -r requirements.txt

### Running the application
```commandline
export FLASK_APP=influenza_estimator
export FLASK_ENV=development
flask run
python app.py
```

Your flask application should be successfully running once this is done.
Expand All @@ -58,36 +56,18 @@ Send a GET request to the following Endpoint:
```


#### GET older influenza ESTIMATE numbers as a JSON file for all countries.
#### GET older influenza PREDICTED ESTIMATE numbers as a JSON file for all the countries.
Send a GET request to the following Endpoint:
```
/api/v1.0/all/weekly/estimate/<int:year>/<int:week>/
```


#### GET older influenza INCIDENCE numbers as a JSON file for all countries.
#### GET older influenza INCIDENCE numbers as a JSON file for all the countries.
Send a GET request to the following Endpoint:
```
/api/v1.0/all/weekly/incidence/<int:year>/<int:week>/
```


#### GET the Live influenza ESTIMATE number as a JSON file for one country.
Send a GET request to the following Endpoint:
```
/api/v1.0/specific/current/<string:country>
```


#### GET the older influenza ESTIMATE number as a JSON file for one country..
Send a GET request to the following Endpoint:
```
/api/v1.0/specific/weekly/estimate/<int:year>/<int:week>/<string:country>
```


#### GET the older influenza INCIDENCE number as a JSON file for one country.
Send a GET request to the following Endpoint:
```
/api/v1.0/specific/weekly/incidence/<int:year>/<int:week>/<string:country>
```
This frontend is inspired from one of Plotly Dash's sample projects -----> https://github.com/plotly/dash-sample-apps/tree/master/apps/dash-oil-and-gas
114 changes: 96 additions & 18 deletions gsoc_application_projects/2020/influenza/dash/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# -*- coding: utf-8 -*-
"""Module starts and runs the web-application.

Author: Tej Sukhatme

"""

# This frontend is inspired from one of Plotly Dash's sample projects - --- -> https: // github.com/plotly/dash-sample-apps/tree/master/apps/dash-oil-and-gas
# Import required libraries
import dash
import pandas as pd
Expand All @@ -6,6 +14,7 @@
import dash_html_components as html
from datetime import datetime
import util
import json

import plotly.express as px

Expand All @@ -31,7 +40,16 @@
df = {}
for country in COUNTRIES:
df[country] = pd.read_csv('data/final/'+country+'.csv')

df[country] = df[country].reset_index().set_index('week')

year_disable = [True for i in range(14)]
country_years = {
'austria': [2012+i for i in range(6)],
'belgium': [2009+i for i in range(11)],
'germany': [2007+i for i in range(13)],
'italy': [2007+i for i in range(13)],
'netherlands': [2009+i for i in range(11)],
}
data = util.DataGateway()

app.layout = html.Div(
Expand Down Expand Up @@ -138,26 +156,13 @@
[
html.Div(
[
html.P(
"Select years in histogram:",
className="control_label",
),
dcc.Checklist(
id="year_selector",
options=[
{'label': str(2008+i)+'\t', 'value': str(2008+i)} for i in range(13)
],
value = [],
labelStyle={"display": "inline-block"},
className="dcc_control",
),
html.P("Filter by Model:", className="control_label"),
dcc.RadioItems(
id="model_selector",
options=[
{"label": "Linear Ridge Regression ", "value": "lrr"},
{"label": "Random Forest Regression ", "value": "rf"},
{"label": "Poisson Regression ", "value": "p"},
{"label": "Poisson Regression ", "value": "p", "disabled":True},
],
value="rf",
className="dcc_control",
Expand All @@ -167,9 +172,9 @@
id="country_selector",
options=[
{"label": "All", "value": "all"},
{"label": "Custom ", "value": "custom"},
# {"label": "Custom ", "value": "custom"},
],
value="productive",
value="all",
labelStyle={"display": "inline-block"},
className="dcc_control",
),
Expand All @@ -180,6 +185,19 @@
value=COUNTRIES,
className="dcc_control",
),
html.P(
"Select years in histogram:\n(You can only select the years for which data is available)",
className="control_label",
),
dcc.Checklist(
id="year_selector",
options=[
{'label': str(2007+i)+'\t', 'value': str(2007+i), 'disabled':year_disable[i]} for i in range(14)
],
value = ["2015", "2016"],
labelStyle={"display": "inline-block"},
className="dcc_control",
),
],
className="pretty_container four columns",
id="cross-filter-options",
Expand Down Expand Up @@ -254,7 +272,7 @@ def update_text(years):
return estimates['total_count'], estimates['austria_count'], estimates['belgium_count'], estimates['germany_count'], estimates['italy_count'], estimates['netherlands_count']

# Nothing -> main graph
# Controls What is diaplyed on the main map
# Controls What is displyed on the main map
Hephaestus12 marked this conversation as resolved.
Show resolved Hide resolved
@app.callback(
Output("main_graph", "figure"),
[Input("year_selector", "value")],
Expand All @@ -281,6 +299,27 @@ def make_main_figure(years):

return fig


#countries -> years
# Controls the list of years available
@app.callback(
Output("year_selector", "options"),
[
Input("country_names", "value"),
],
)
def enable_years(countries):

year_disable = [True for i in range(14)]
for country in countries:
for year in country_years[country]:
year_disable[year-2007] = False

print(year_disable)
Hephaestus12 marked this conversation as resolved.
Show resolved Hide resolved
return [{'label': str(2007+i)+'\t', 'value': str(2007+i), 'disabled': year_disable[i]} for i in range(14)]



#selectors -> histogram
# Controls the histogram/bar chart
@app.callback(
Expand Down Expand Up @@ -419,6 +458,45 @@ def make_line(model, countries, years):
return fig



## REST API
server = app.server

# @server.route('/')
Hephaestus12 marked this conversation as resolved.
Show resolved Hide resolved
# def route1():
# return jsonify({'message':'this is the first route'})

# Returns the Live influenza PREDICTED ESTIMATE numbers as a JSON file for all the countries.
@server.route('/api/v1.0/all/current/', methods=['GET'])
def get_all_current():
ans = data.get_incidence()
return json.dumps(ans)


# Returns older influenza PREDICTED ESTIMATE numbers as a JSON file for all the countries.
@server.route('/api/v1.0/all/weekly/estimate/<int:year>/<int:week>/', methods=['GET'])
def get_all_weekly_estimate(year, week):
ans = {}
week = str(year) + '-' + str(week)
for country in COUNTRIES:
incidence = df[country].at[week, 'estimate_rf']
ans[country] = incidence
return json.dumps(ans)


# Returns older influenza INCIDENCE numbers as a JSON file for all the countries.
@server.route('/api/v1.0/all/weekly/incidence/<int:year>/<int:week>/', methods=['GET'])
def get_all_weekly_incidence(year, week):
ans = {}
week = str(year) + '-' + str(week)
for country in COUNTRIES:
incidence = df[country].at[week, 'incidence']
ans[country] = incidence
return json.dumps(ans)




# Main
if __name__ == "__main__":
app.run_server(host='0.0.0.0', debug=False, port=8050)
Hephaestus12 marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 5 additions & 5 deletions gsoc_application_projects/2020/influenza/dash/data/current.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
country,level_0,index,Unnamed: 0,last_checked,estimate
austria,0,0,0,2020-08-26,844.5686933370199
belgium,1,1,1,2020-08-26,277.96020465846
germany,2,2,2,2020-08-26,20.208
italy,3,3,3,2020-08-26,9.302
netherlands,4,4,4,2020-08-26,88.72925250566
austria,0,0,0,2020-08-28,757.5273658733
belgium,1,1,1,2020-08-28,599.9983690378001
germany,2,2,2,2020-08-28,20.208
italy,3,3,3,2020-08-28,8.004000000000001
netherlands,4,4,4,2020-08-28,88.72925250566
24 changes: 17 additions & 7 deletions gsoc_application_projects/2020/influenza/dash/data/saved.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
,Unnamed: 0,Unnamed: 0.1,Unnamed: 0.1.1,austria,belgium,germany,italy,netherlands,date
0,0.0,0.0,,844.5686933370198,277.96020465846,20.208,9.302,88.72925250566,
1,1.0,1.0,,844.5686933370198,277.96020465846,20.208,9.302,88.72925250566,
2,2.0,,,844.5686933370198,277.96020465846,20.208,9.302,88.72925250566,
3,3.0,,,844.5686933370198,277.96020465846,20.208,9.302,88.72925250566,
4,,,,844.5686933370198,277.96020465846,20.208,9.302,88.72925250566,2020-08-26
5,,,,844.5686933370198,277.96020465846,20.208,9.302,88.72925250566,2020-08-26
,Unnamed: 0,Unnamed: 0.1,Unnamed: 0.1.1,Unnamed: 0.1.1.1,Unnamed: 0.1.1.1.1,Unnamed: 0.1.1.1.1.1,austria,belgium,germany,italy,netherlands,date
0,0.0,0.0,0.0,0.0,0.0,,844.5686933370198,277.96020465846,20.208,9.302,88.72925250566,
1,1.0,1.0,1.0,1.0,1.0,,844.5686933370198,277.96020465846,20.208,9.302,88.72925250566,
2,2.0,2.0,2.0,2.0,,,844.5686933370198,277.96020465846,20.208,9.302,88.72925250566,
3,3.0,3.0,3.0,3.0,,,844.5686933370198,277.96020465846,20.208,9.302,88.72925250566,
4,4.0,4.0,4.0,,,,844.5686933370198,277.96020465846,20.208,9.302,88.72925250566,
5,5.0,5.0,5.0,,,,844.5686933370198,277.96020465846,20.208,9.302,88.72925250566,
6,6.0,6.0,,,,,851.0207308266597,515.6326506475001,20.208,8.030000000000001,88.72925250566,
7,7.0,7.0,,,,,851.0207308266597,515.6326506475001,20.208,8.030000000000001,88.72925250566,
8,8.0,8.0,,,,,757.5273658733,599.9983690378001,20.208,8.004000000000001,88.72925250566,
9,9.0,9.0,,,,,757.5273658733,599.9983690378001,20.208,8.004000000000001,88.72925250566,
10,10.0,10.0,,,,,757.5273658733,599.9983690378001,20.208,8.004000000000001,88.72925250566,
11,11.0,11.0,,,,,757.5273658733,599.9983690378001,20.208,8.004000000000001,88.72925250566,
12,12.0,,,,,,757.5273658733,599.9983690378001,20.208,8.004000000000001,88.72925250566,
13,13.0,,,,,,757.5273658733,599.9983690378001,20.208,8.004000000000001,88.72925250566,
14,,,,,,,757.5273658733,599.9983690378001,20.208,8.004000000000001,88.72925250566,2020-08-28
15,,,,,,,757.5273658733,599.9983690378001,20.208,8.004000000000001,88.72925250566,2020-08-28
7 changes: 7 additions & 0 deletions gsoc_application_projects/2020/influenza/dash/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# -*- coding: utf-8 -*-
"""Utility Functions for web application.

Author: Tej Sukhatme

"""

import copy
import logging
from datetime import timedelta, date, datetime
Expand Down
37 changes: 0 additions & 37 deletions gsoc_application_projects/2020/influenza/web/Dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions gsoc_application_projects/2020/influenza/web/MANIFEST.in

This file was deleted.

This file was deleted.

Loading