Get daily Covid19 active cases worldmap JHU
Tags: #wsr #covid #active-cases #analytics #plotly #automation #naas
Author: Florent Ravenel

Input

Import libraries

1
import pandas as pd
2
from datetime import datetime
3
try:
4
from dataprep.clean import clean_country
5
except:
6
!pip install dataprep --user
7
from dataprep.clean import clean_country
8
import plotly.graph_objects as go
9
import naas
Copied!

Setup chart title

1
title = "COVID 19 - Active cases (in milions)"
Copied!

Variables

1
# Input URLs of the raw csv dataset
2
urls = [
3
'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv',
4
'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv',
5
'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv'
6
]
7
8
# Output paths
9
output_image = f"{title}.png"
10
output_html = f"{title}.html"
Copied!

Schedule your automation

1
# Schedule your job everyday at 8:00 AM (NB: you can choose the time of your scheduling bot)
2
naas.scheduler.add(cron="0 8 * * *")
3
4
#-> Uncomment the line below (by removing the hashtag) to remove your scheduler
5
# naas.scheduler.delete()
Copied!

Model

Get data from JHU

1
def get_data_url(urls):
2
df = pd.DataFrame()
3
for url in urls:
4
tmp_df = pd.read_csv(url)
5
tmp_df["Indicator"] = url.split("/time_series_covid19_")[-1].split("_global.csv")[0].capitalize()
6
df = pd.concat([df, tmp_df])
7
return df
8
9
df_init = get_data_url(urls)
10
df_init
Copied!

Get all data from JHU

1
def get_all_data(df_init):
2
df = df_init.copy()
3
# Cleaning
4
df = df.drop("Province/State", axis=1)
5
6
# Melt data
7
df = pd.melt(df,
8
id_vars=["Country/Region", "Lat", "Long", "Indicator"],
9
var_name="Date",
10
value_name="Value").fillna(0)
11
df["Date"] = pd.to_datetime(df["Date"])
12
13
# Calc active cases
14
df_active = df.copy()
15
df_active.loc[df_active["Indicator"].isin(["Deaths", "Recovered"]), "Value"] = df_active["Value"] * (-1)
16
df_active["Indicator"] = "Active cases"
17
18
# Concat data
19
df = pd.concat([df, df_active])
20
21
# Group by country/region
22
to_group = ["Country/Region", "Lat", "Long", "Indicator", "Date"]
23
df = df.groupby(to_group, as_index=False).agg({"Value": "sum"})
24
25
# Cleaning
26
df = df.rename(columns={"Country/Region": "COUNTRY"})
27
df.columns = df.columns.str.upper()
28
return df.reset_index(drop=True)
29
30
df_clean = get_all_data(df_init)
31
df_clean
Copied!

Prep data for worldmap

1
def prep_data(df_init):
2
df = df_init.copy()
3
# Filter
4
date_max = df["DATE"].max()
5
df = df[
6
(df["INDICATOR"] == "Active cases") &
7
(df["DATE"] == date_max)
8
].reset_index(drop=True)
9
10
11
# Clean country
12
df = clean_country(df, 'COUNTRY', output_format='alpha-3').dropna()
13
df = df.rename(columns={'COUNTRY_clean': 'COUNTRY_ISO'})
14
return df.reset_index(drop=True)
15
16
df_worldmap = prep_data(df_clean)
17
df_worldmap
Copied!

Create worldmap

1
def create_worldmap(df):
2
fig = go.Figure()
3
4
fig = go.Figure(data=go.Choropleth(
5
locations=df['COUNTRY_ISO'],
6
z=df['VALUE'],
7
text=df["COUNTRY"] + ": " + df['VALUE'].map("{:,.0f}".format).str.replace(",", " ") + " active cases",
8
hoverinfo="text",
9
colorscale='Blues',
10
autocolorscale=False,
11
reversescale=False,
12
marker_line_color='darkgray',
13
marker_line_width=0.5,
14
colorbar_tickprefix='',
15
colorbar_title='Active cases',
16
))
17
18
fig.update_layout(
19
title=title,
20
plot_bgcolor="#ffffff",
21
legend_x=1,
22
geo=dict(
23
showframe=False,
24
showcoastlines=False,
25
#projection_type='equirectangular'
26
),
27
dragmode= False,
28
width=1200,
29
height=800,
30
31
)
32
config = {'displayModeBar': False}
33
fig.show(config=config)
34
return fig
35
36
fig = create_worldmap(df_worldmap)
Copied!

Output

Export in PNG and HTML

1
fig.write_image(output_image, width=1200)
2
fig.write_html(output_html)
Copied!

Generate shareable assets

1
link_image = naas.asset.add(output_image)
2
link_html = naas.asset.add(output_html, {"inline":True})
3
4
#-> Uncomment the line below to remove your assets
5
# naas.asset.delete(output_image)
6
# naas.asset.delete(output_html)
Copied!
Copy link
Edit on GitHub