%config InlineBackend.figure_formats = ['svg']
import oscovida as ov
ov.display_binder_link("tutorial-overview-graphs.ipynb")
Overview function explained¶
The most important function of oscovida package is overview. It takes the following parameters:
country— a country to analyse (mandatory,str);region— a region of the country (optional,str);subregion— a subregion of the country (optional,str);savefig— whether to save a sigure (optional,bool, default isFalse);dates— a range of dates in a format "2020-05-15:2020-10-20"weeks— how many last weeks to show (optional,int, default is zero, which means "all"),data— the external data source, a pair ofpd.Series, see below.
The function returns a triple:
(pyplot graph, a pandas series for cases, a pandas series for deaths).
This function provides six graphs:
ov.overview('Russia');
Let's see how exactly we obtain all these graphs.
Under the hood we
- retrieve the data with
get_country_data()function (see the tutorial) - then we optionally narrow the time range using either
weeks=Nfor the lastNweeks ordates="2020-05-01:2020-10-01"for the specific range of dates. Note that one cannot use bothdatesandweekstogether. - finally, we feed this data for cases and deaths to a set of plotting functions:
plot_incidence_ratefor 7-day incidence rate per 100 thousand inhabitants (plot 1)plot_daily_changefor daily changes (plots 2 and 3, see the tutorial)plot_reproduction_numberforR-value and the growth factor (plots 4 and 5, see the tutorial)plot_doubling_timefor the doubling times (plot 6, see the tutorial).
That's exactly how we fetch the data using weeks inside the overview function:
country = "Iran"
weeks = 30
cases, deaths = ov.get_country_data(country)
cases = cases[- weeks * 7:] # cut off unwanted data
deaths = deaths[- weeks * 7:] # cut off unwanted data
What we have in cases and deaths are Pandas time series: it is a sort of a two-row array with dates in one row and COVID cases / deaths in the other:
cases
And here is the example with dates:
country = "Germany"
region="Hamburg"
dates = "2020-09-15:2020-10-25"
cases, deaths = ov.get_country_data(country, region)
date_start, date_end = dates.split(':')
cases = cases[date_start:date_end]
deaths = deaths[date_start:date_end]
ov.overview(country=country, region=region, dates=dates);
External data source¶
One may pass an external data object to the overview for visualisation.
The data object should contain a pair of Pandas series (one for cases, and one for deaths).
Each series must have an index of type pd.Timestamp. See the example with an artificial data:
import numpy as np
import pandas as pd
days = 100
country = "Narnia"
dates = pd.date_range("2020-03-01", periods=days, freq='D')
data1 = np.exp(np.linspace(1,15,days)).astype(int) # don't have to be integers
data2 = np.exp(np.linspace(1,5,days)).astype(int) # don't have to be integers
c = pd.Series(data1, index=pd.DatetimeIndex(dates))
d = pd.Series(data2, index=pd.DatetimeIndex(dates))
c.name = f"{country} cases"
d.name = f"{country} deaths"
ov.overview(country=country, data=(c,d));
Other tutorials¶
You can find more tutorials here.