<jupyter_start><jupyter_text>Relating number of COVID-19 deaths and cases> Death rates, Infection-spread rates &  Infections Per million Popolation- comments: true- author: <NAME>- categories: [rate, compare, growth]- hide: false- image: images/covid-permillion-trajectories.png- permalink: /covid-compare-death-rate/<jupyter_code>#hide
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import altair as alt
%config InlineBackend.figure_format = 'retina'
import load_covid_data 
import utils
chart_width = 550
chart_height= 400
atLeast= 1000
moreThan= 50000
data = utils.getComulativeData('confirmed')
ddata = utils.getComulativeData('deaths')

#hide
def death_rate(totalDeath,totalCases):
    if totalCases!=0 :
        return (each['total_deaths'][i]/each['total_confirmed'][i])*100 
    else: return 0

country_data=[]
for i in data.location.unique():
    filter_country = data["location"]== i
    temp=data[filter_country]
    temp['total_deaths']=ddata[filter_country]['total_deaths']
    temp=temp.reset_index()
    temp=temp.drop('index',axis=1)
    country_data.append(temp)

every_country_data=pd.DataFrame(columns={'location', 'date', 'total_confirmed','total_deaths','percent_change','death_percent_change','death_rate'})
for each in country_data :
    each['percent_change']=None
    each['death_percent_change']=None
    each['death_rate']=None
    for i in range(each.total_confirmed.size):
         each['percent_change'][i]=utils.toPercent(i,each['total_confirmed'][i],each['total_confirmed'])
         each['death_percent_change'][i]=utils.toPercent(i,each['total_deaths'][i],each['total_deaths'])
         each['death_rate'][i]=death_rate(each['total_confirmed'][i],each['total_deaths'][i])
    # display(each)
    every_country_data=every_country_data.append(each)

temp_rank_data=pd.DataFrame(columns=['country','total'])#,data={'country':data.location.unique(),'total':None})
for each in country_data:
        # if each.death_rate.tail(1).to_list()[0]>0:
    if each.total_confirmed.tail(1).to_list()[0]>moreThan:
        temp_rank_data=temp_rank_data.append([{'country':each.location[0],'total':each.death_rate.tail(1).to_list()[0]}])
temp_rank_data=temp_rank_data.sort_values(by='total',ascending=False)

#hide
countries=temp_rank_data.country[0:20]
country_data=[]
for i in countries:
    filter_country = data["location"]== i
    temp=data[filter_country]
    temp['total_deaths']=ddata[filter_country]['total_deaths']
    temp=temp.reset_index()
    temp=temp.drop('index',axis=1)
    country_data.append(temp)

every_country_data=pd.DataFrame(columns={'location', 'date', 'total_confirmed','total_deaths','percent_change','death_percent_change','death_rate'})
for each in country_data :
    each['percent_change']=None
    each['death_percent_change']=None
    each['death_rate']=None
    for i in range(each.total_confirmed.size):
         each['percent_change'][i]=utils.toPercent(i,each['total_confirmed'][i],each['total_confirmed'])
         each['death_percent_change'][i]=utils.toPercent(i,each['total_deaths'][i],each['total_deaths'])
         each['death_rate'][i]=death_rate(each['total_confirmed'][i],each['total_deaths'][i])
    # display(each)
    every_country_data=every_country_data.append(each)
# pd.set_option('display.max_rows', None)
# every_country_data[every_country_data.location=='Iran']
# Death Rate
display("Countries with >"+str(moreThan)+" cases (Projected starting "+str(atLeast)+" case mark to calculate rise of deaths in terms of percentage)")
source= every_country_data[every_country_data.total_confirmed>atLeast]

scales = alt.selection_interval(bind='scales', zoom=True)
selection = alt.selection_multi(fields=['location'], bind='legend')
alt.data_transformers.disable_max_rows()

base = alt.Chart(source, title = "Death rate").encode(
    x = alt.X('date:T', title = "Days passed"),
    y = alt.Y("death_rate:Q",title = "Percentage"),
    color = alt.Color('location:N', legend=alt.Legend(title="Country",values=countries.to_list(), labelFontSize=15, titleFontSize=17),
                     scale=alt.Scale(scheme='tableau20')),
    opacity = alt.condition(selection, alt.value(1), alt.value(0.1))
)

lines = base.mark_line().add_selection(
    scales
).add_selection(
    selection
).properties(
    width=chart_width,
    height=chart_height
)

lines = base.mark_line().add_selection(
    scales
).add_selection(
    selection
).properties(
    width=chart_width,
    height=chart_height
)
lines
# Infection-spread rates
display("Countries with >"+str(moreThan)+" cases (Projected starting "+str(atLeast)+" case mark to calculate rise of infection in terms of percentage)")
source= every_country_data[every_country_data.total_confirmed>atLeast]

scales = alt.selection_interval(bind='scales', zoom=True)
selection = alt.selection_multi(fields=['location'], bind='legend')
alt.data_transformers.disable_max_rows()

base = alt.Chart(source, title = "Infection-spread rate").encode(
    x = alt.X('date:T', title = "Days passed"),
    y = alt.Y("percent_change:Q",title = "Percentage"),
    color = alt.Color('location:N', legend=alt.Legend(title="Country",values=countries.to_list(), labelFontSize=15, titleFontSize=17),
                     scale=alt.Scale(scheme='tableau20')),
    opacity = alt.condition(selection, alt.value(1), alt.value(0.1))
)

lines = base.mark_line().add_selection(
    scales
).add_selection(
    selection
).properties(
    width=chart_width,
    height=chart_height
)

lines = base.mark_line().add_selection(
    scales
).add_selection(
    selection
).properties(
    width=chart_width,
    height=chart_height
)
lines

#hide
ddata = ddata.rename(columns={"total_deaths": "total_cases"})
ddata.columns

# Population data
data_pop=pd.read_csv("data_files/world_population.csv")[["country","population"]]
data_pop=data_pop[data_pop.country.isin(countries)]
# To list the countries who's population is not found in world bank
# pd.set_option('display.max_rows', None)
# countries=countries[~countries.isin(data_pop.country)]
countries=countries[countries.isin(data_pop.country)]


# display(data_pop.country.unique().size , countries.size)
# (countries.isin(data_pop.country))[countries == False]
data_pc=ddata.copy()
data_pc=data_pc[data_pc.location.isin(countries)]
#hide
for i in countries:
    data_pc.loc[data_pc.location == i,"total_cases"] = data_pc.loc[data_pc.location == i,"total_cases"]/(data_pop.loc[data_pop.country == i, "population"].astype(float).to_list()[0]/1000000)

data_countries_pc=[]

ranked_data=pd.DataFrame(columns=['country','total_cases_per_milliion_pop'])#country, total_cases_per_milliion_pop
# ranked_data.append({'a','b'})
for i in countries:
    filter_country = data_pc["location"]== i
    temp=data_pc[filter_country]
    # display(temp)
    # if temp['total_cases'].tail(1).to_list()[0]> 500:
    if temp['total_cases'].sum() > 0 :
        ranked_data=ranked_data.append([{'country':i,'total_cases_per_milliion_pop':temp['total_cases'].tail(1).to_list()[0]}])

ranked_data=ranked_data.sort_values(by='total_cases_per_milliion_pop',ignore_index=True,ascending=False)
countries=ranked_data['country']
countries=countries[0:20]
for i in countries:
    filter_country = data_pc["location"]== i
    # temp=data_pc[filter_country]
    data_countries_pc.append(data_pc[filter_country])
# pd.set_option('display.max_rows', None)
# display(ranked_data)
for i in range(0,len(countries)):
    data_countries_pc[i]=data_countries_pc[i].reset_index()
    data_countries_pc[i]['x_day'] = data_countries_pc[i].index
len(data_countries_pc)

data_plot=pd.DataFrame()
for i in range(0,len(countries)):
    data_plot = pd.concat([data_plot, data_countries_pc[i]], axis=0)
# data_plot

data_plot_death_log=data_plot.copy()
def a(x):
    if x==0:
        return 0
    return np.log(x)
pd.set_option('display.max_rows', 100)

# data_plot['total_log_cases']=data_plot['total_cases'].apply(a)
data_plotdata_plot=pd.DataFrame()
for i in range(0,len(countries)):
    data_plot = pd.concat([data_plot, data_countries_pc[i]], axis=0)
# data_plot

data_plot_death_log=data_plot.copy()
def a(x):
    if x==0:
        return 0
    return np.log(x)
pd.set_option('display.max_rows', 100)

# data_plot['total_log_cases']=data_plot['total_cases'].apply(a)
# data_plot<jupyter_output><empty_output><jupyter_text>Deaths Per Million Population<jupyter_code># Plot it using Altair
source = data_plot

scales = alt.selection_interval(bind='scales', zoom=True)
selection = alt.selection_multi(fields=['location'], bind='legend')
alt.data_transformers.disable_max_rows()

base = alt.Chart(source, title = "20 Highest COVID-19 Deaths Per Million of Inhabitants").encode(
    x = alt.X('date:T', title = "Days passed"),
    y = alt.Y("total_cases:Q",title = "Deaths per million"),
    color = alt.Color('location:N', legend=alt.Legend(title="Country",values=countries.to_list(), labelFontSize=15, titleFontSize=17),
                     scale=alt.Scale(scheme='tableau20')),
    opacity = alt.condition(selection, alt.value(1), alt.value(0.1))
)

lines = base.mark_line().add_selection(
    scales
).add_selection(
    selection
).properties(
    width=chart_width,
    height=chart_height
)

lines = base.mark_line().add_selection(
    scales
).add_selection(
    selection
).properties(
    width=chart_width,
    height=chart_height
)
lines<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15189333333333332, 'fraction_numerical': 0.010666666666666666, 'mean_word_length': 6.234567901234568, 'pattern_counts': {'":': 1, '<': 10, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '98650', 'license': 'Apache-2.0', 'n_tokens_mistral': 3312, 'n_tokens_neox': 3193, 'n_words': 486, 'path': '_notebooks/.ipynb_checkpoints/2020-05-15-relation-in-deaths-and-number-of-cases-checkpoint.ipynb', 'repo_name': 'Guneetgstar/covid19', 'size': '9381'}