Justetc Social Services (non-profit)Jan 31 · 5 min read
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inlinefrom __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgetsquality_of_care = pd.read_excel('../data/quality-of-care.xls')
quality_of_care.head()
# find all indicators
quality_of_care.set_index(['Indicator'])
indicators = pd.Index(quality_of_care['Indicator']).unique()
# find all years
years = quality_of_care['Data year'].dropna().unique()
indicators[0], years('30-Day In-Hospital Fatality: AMI',
array([2015, 2014, 2013, 'Not applicable', '2013', '2014', '2015',
'2012 to 2014', '2013 to 2015', '2010 to 2014', '2003 to 2008',
'2006 to 2011', '2008 to 2013', '2007 to 2012', '2004 to 2009',
2016, '2016'], dtype=object))# sort years
years = [ int(aYear) for aYear in years if (aYear != 'Not applicable') and len( str(aYear).split(' ')) <= 1 ]
years = sorted(years)
years
all_years = [0] + years
all_years[0, 2013, 2013, 2014, 2014, 2015, 2015, 2016, 2016]plt.rcParams['figure.figsize'] = [10, 10]
def plot_quality_of_care(year, indicator, bubble_scale, all_years=all_years):
# print('year, indicator', year, indicator)
plt.ion()
f = plt.figure()
ax = f.gca()
f.show()
#indicator_data = health_status.loc[ (health_status['Indicator'] == indicators[0] ) ]
if ( year > 1 ):
indicator_data = quality_of_care.loc[ (quality_of_care['Indicator'] == indicator) & (quality_of_care['Data year'] == year) ]
ax.scatter(indicator_data['Data year'], indicator_data['Region'], s=indicator_data['Value'] * bubble_scale )
plt.show()
else:
for aYear in all_years:
indicator_data = quality_of_care.loc[ (quality_of_care['Indicator'] == indicator) & (quality_of_care['Data year'] == aYear) ]
ax.scatter(indicator_data['Data year'], indicator_data['Region'], s = indicator_data['Value'] * bubble_scale )
f.canvas.draw()# create the interactive interface
def f(x):
return x
indicator = interactive(f, x=indicators);
display(indicator)
indicator.result
year = interactive(f, x=all_years);
display(year)
year.result
bubble_scale = interactive(f, x=(0, 100, 1));
display(bubble_scale)
bubble_scale.resultinteractive(children=(Dropdown(description='x', options=('30-Day In-Hospital Fatality: AMI', '30-Day In-Hospit…
interactive(children=(Dropdown(description='x', options=(0, 2013, 2013, 2014, 2014, 2015, 2015, 2016, 2016), v…
interactive(children=(IntSlider(value=50, description='x'), Output()), _dom_classes=('widget-interact',))
50plot_quality_of_care(year.result, indicator.result, bubble_scale.result)C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\figure.py:445: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.
% get_backend())
plt.rcParams['figure.figsize'] = [20, 10]
def plot_quality_of_care_by_regions(year, indicator, bubble_scale, all_years=all_years):
#print('year, indicator', year, indicator)
plt.ion()
f = plt.figure()
ax = f.gca()
f.show()
#indicator_data = health_status.loc[ (health_status['Indicator'] == indicators[0] ) ]
if ( year > 1 ):
#indicator_data = indicator_data [ indicator_data['Data year'] == year]
indicator_data = quality_of_care.loc[ (quality_of_care['Indicator'] == indicator) & (quality_of_care['Data year'] == year) ]
#print(indicator_data)
ax.scatter(indicator_data['Region'], indicator_data['Value'], s=indicator_data['Value'] * bubble_scale )
plt.show()
else:
for aYear in all_years:
indicator_data = quality_of_care.loc[ (quality_of_care['Indicator'] == indicator) & (quality_of_care['Data year'] == aYear) ]
ax.scatter(indicator_data['Region'], indicator_data['Value'], s = indicator_data['Value'] * bubble_scale )
f.canvas.draw()print('Select parameter to visualize across countries over years')
# create the interactive interface
def f(x):
return x
indicator = interactive(f, x=indicators);
display(indicator)
indicator.result
year = interactive(f, x=all_years);
display(year)
year.result
bubble_scale = interactive(f, x=(0, 100, 1));
display(bubble_scale)
bubble_scale.resultSelect parameter to visualize across countries over years
interactive(children=(Dropdown(description='x', options=('30-Day In-Hospital Fatality: AMI', '30-Day In-Hospit…
interactive(children=(Dropdown(description='x', options=(0, 2013, 2013, 2014, 2014, 2015, 2015, 2016, 2016), v…
interactive(children=(IntSlider(value=50, description='x'), Output()), _dom_classes=('widget-interact',))
50plot_quality_of_care_by_regions(year.result, indicator.result, bubble_scale.result)
plt.rcParams['figure.figsize'] = [20, 10]
def plot_quality_of_care_by_countries(year, indicator, bubble_scale, selected_countries, all_years=all_years):
#print('year, indicator', year, indicator)
plt.ion()
f = plt.figure()
ax = f.gca()
f.show()
#indicator_data = health_status.loc[ (health_status['Indicator'] == indicators[0] ) ]
if ( year > 1 ):
#indicator_data = indicator_data [ indicator_data['Data year'] == year]
indicator_data = quality_of_care.loc[ (quality_of_care['Indicator'] == indicator) & (quality_of_care['Data year'] == str(year) ) & (quality_of_care['Type of region'] == 'Country' ) ]
#print(indicator_data)
if ( len(selected_countries) > 1 ):
indicator_data = indicator_data[ indicator_data['Region'].isin(selected_countries)]
ax.scatter(indicator_data['Region'], indicator_data['Value'], s=indicator_data['Value'] * bubble_scale )
plt.show()
else:
for aYear in all_years:
indicator_data = quality_of_care.loc[ (quality_of_care['Indicator'] == indicator) & (quality_of_care['Data year'] == str(aYear) ) & (quality_of_care['Type of region'] == 'Country' ) ]
if ( len(selected_countries) > 1 ):
indicator_data = indicator_data[ indicator_data['Region'].isin(selected_countries)]
#print(indicator_data)
ax.scatter(indicator_data['Region'], indicator_data['Value'], s = indicator_data['Value'] * bubble_scale )
f.canvas.draw()countries = quality_of_care[ quality_of_care['Type of region'] == 'Country' ]['Region']
countries = sorted (countries.unique())
#countries = ['Canada'] + countries
if 'Canada' in countries:
countries = ['Canada'] + countries
#countriesprint('Select parameters to visualize across countries over years\n')
# create the interactive interface
def f(x):
return x
print('Select chart type:')
chart_types = ['Bubble', 'Bar', 'Pie', 'Line']
chart = interactive(f, x=chart_types);
display(chart)
chart.result
print('Select Indicator')
indicator_country = interactive(f, x=indicators);
display(indicator_country)
indicator_country.result
print('Select Year: 0 = all years')
year_country = interactive(f, x=all_years);
display(year_country)
year_country.result
print('Select bubble size')
bubble_scale_country = interactive(f, x=(0, 100, 1));
display(bubble_scale_country)
bubble_scale_country.result
print('Max country count to compare with')
country_count = interactive(f, x=range(1, 20));
display(country_count)
country_count.result
print('Select countries')
print('Compare with Canada')
all_countries = interactive(f, x=True);
display(all_countries)
all_countries.result
country_0 = interactive(f, x = countries);
country_1 = interactive(f, x = countries);
#country_str = "var%d = interactive(f, x = countries)"
display(country_0)
display(country_1)
country_0.result
print('Compare with Canada')
compare_canada = interactive(f, x=True);
display(compare_canada)
compare_canada.resultSelect parameters to visualize across countries over years
Select chart type:
interactive(children=(Dropdown(description='x', options=('Bubble', 'Bar', 'Pie', 'Line'), value='Bubble'), Out…
Select Indicator
interactive(children=(Dropdown(description='x', options=('30-Day In-Hospital Fatality: AMI', '30-Day In-Hospit…
Select Year: 0 = all years
interactive(children=(Dropdown(description='x', options=(0, 2013, 2013, 2014, 2014, 2015, 2015, 2016, 2016), v…
Select bubble size
interactive(children=(IntSlider(value=50, description='x'), Output()), _dom_classes=('widget-interact',))
Max country count to compare with
interactive(children=(Dropdown(description='x', options=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16…
Select countries
Compare with Canada
interactive(children=(Checkbox(value=True, description='x'), Output()), _dom_classes=('widget-interact',))
interactive(children=(Dropdown(description='x', options=('Austria', 'Belgium', 'Chile', 'Czech Republic', 'Den…
interactive(children=(Dropdown(description='x', options=('Austria', 'Belgium', 'Chile', 'Czech Republic', 'Den…
Compare with Canada
interactive(children=(Checkbox(value=True, description='x'), Output()), _dom_classes=('widget-interact',))
Truecountry_0.result'Austria'selected_countries = []
if (all_countries.result == False):
selected_countries = [country_0.result, country_1.result]
#print(year_country.result, indicator_country.result, bubble_scale_country.result)
plot_quality_of_care_by_countries(year_country.result, indicator_country.result, bubble_scale_country.result, selected_countries)
#print(country_0, country_1)
quality_of_care.loc[ (quality_of_care['Indicator'] == indicators[0]) & (quality_of_care['Data year'] == '2013') & (quality_of_care['Type of region'] == 'Country') ]
Appendix
"""
print('Select countries')
for i in range(country_count.result):
#country_str = interactive(f, x = countries);
country_str = "var%d = interactive(f, x = countries)"
display(country)
country.result
"""
for i in range(country_count.result):
country_str = "var%d = interactive(f, x = countries)" %(i)
exec(country_str)
country_str
for i in [0, 2]:
'var' + str(i)