-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
110 lines (98 loc) · 4.61 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from flask import Flask, render_template, request
import pandas as pd
import numpy as np
from scipy import stats
import os.path
from flask import Markup
app = Flask(__name__)
app.config["DEBUG"] = True
# constructor - load once (otherwise setup a local csv copy to save on bandwidth usage)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
src = os.path.join(BASE_DIR, 'WHOSIS_000001,WHOSIS_000015.csv')
who_list = pd.read_csv(src)
who_list = who_list[['GHO (DISPLAY)', 'YEAR (CODE)' , 'COUNTRY (DISPLAY)', 'SEX (DISPLAY)', 'Numeric']]
who_list['COUNTRY (DISPLAY)'] = [ctry.title() for ctry in who_list['COUNTRY (DISPLAY)'].values]
country_list = sorted(set(who_list['COUNTRY (DISPLAY)'].values))
def get_life_expectancy(age, country, sex):
# pull latest entries for birth and 60 years
sub_set = who_list[who_list['COUNTRY (DISPLAY)'].str.startswith(country, na=False)]
sub_set = sub_set[sub_set['SEX (DISPLAY)'] == sex]
sub_set = sub_set.sort_values('YEAR (CODE)', ascending=False)
sub_set_birth = sub_set[sub_set['GHO (DISPLAY)'] == 'Life expectancy at birth (years)']
sub_set_60 = sub_set[sub_set['GHO (DISPLAY)'] == 'Life expectancy at age 60 (years)']
# not all combinations exsits so check that we have data for both
if len(sub_set_birth['Numeric']) > 0 and len(sub_set_birth['Numeric']) > 0:
# create data set with both points as shown in first example
lf_at_birth = sub_set_birth['Numeric'].values[0]
lf_at_60 = sub_set_60['Numeric'].values[0]
# model
slope, intercept, r_value, p_value, std_err = stats.linregress([0,60],[lf_at_birth, lf_at_60])
# predict for the age variable
return(np.ceil(slope * age + intercept))
else:
return None
@app.route('/', methods=['POST', 'GET'])
def interact_life_expectancy():
# select box defaults
default_age = 'Select Age'
selected_age = default_age
default_sex = 'Select Gender'
selected_sex = default_sex
default_country = 'Select Country'
selected_country = default_country
# data carriers
string_to_print = ''
healthy_image_list = []
if request.method == 'POST':
# clean up age field
selected_age = request.form["age"]
if (selected_age == default_age):
selected_age = int(29)
else:
selected_age = selected_age
# clean up sex field
selected_sex = request.form["sex"]
if (selected_sex == default_sex):
selected_sex = 'Both sexes'
# clean up country field
selected_country = request.form["country"]
if (selected_country == default_country):
selected_country = 'United States Of America'
# estimate lifespan
current_time_left = get_life_expectancy(age=int(selected_age), country=selected_country, sex=selected_sex)
if (current_time_left is not None):
# create output string
string_to_print = Markup("You have <font size='+10'>" + str(int(np.ceil(current_time_left))) + "</font> healthy years left to live!")
else:
string_to_print = Markup("Error! No data found for selected parameters")
current_time_left = 1
# healthy years
healthy_image_list = []
# squat.png, stretch.png, jog.png
healthy_years_left = int(np.ceil(current_time_left))
image_switch=0
if (healthy_years_left > 0):
for y in range(healthy_years_left):
if image_switch == 0:
healthy_image_list.append('static/images/Cycling.png')
elif image_switch == 1:
healthy_image_list.append('static/images/Jogging.png')
elif image_switch == 2:
healthy_image_list.append('static/images/JumpingJack.png')
elif image_switch == 3:
healthy_image_list.append('static/images/Stretching.png')
elif image_switch == 4:
healthy_image_list.append('static/images/WeightLifting.png')
else:
healthy_image_list.append('static/images/Yoga.png')
image_switch = -1
image_switch += 1
return render_template('time.html',
country_list = country_list,
default_country = selected_country,
default_age=selected_age,
default_sex=selected_sex,
string_to_print = string_to_print,
healthy_image_list = healthy_image_list)
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000, debug=True)