image_output_1.png
from collections import OrderedDict

from copy import deepcopy

import numpy as np # linear algebra

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import plotly.graph_objs as go

import plotly.offline as py

submission = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv')
test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
train['Province_State'].fillna('', inplace=True)
train['Date'] = pd.to_datetime(train['Date'])
train['day'] = train.Date.dt.dayofyear
train['geo'] = ['_'.join(x) for x in zip(train['Country_Region'], train['Province_State'])]
test['Province_State'].fillna('', inplace=True)
test['Date'] = pd.to_datetime(test['Date'])
test['day'] = test.Date.dt.dayofyear
test['geo'] = ['_'.join(x) for x in zip(test['Country_Region'], test['Province_State'])]
train.sort_values(by='Date', inplace=True)
test.sort_values(by='Date', inplace=True)
day_min = train['day'].min()
train['day'] -= day_min
test['day'] -= day_min
test['ConfirmedCases'] = np.nan
test['Fatalities'] = np.nan
train['ForecastId'] = np.nan
test['Id'] = np.nan
min_date_train = train['Date'].min()
min_date_test = test['Date'].min()
max_date_train = train['Date'].max()
max_date_test = test['Date'].max()
num_of_days_train = (max_date_train - min_date_train) / np.timedelta64(1, 'D') + 1
num_of_days = int((max_date_test - min_date_train) / np.timedelta64(1, 'D')) + 1
time_span0 = pd.date_range(min_date_train, max_date_test)
time_span = [str(s.month) + '/' + str(s.day) for s in time_span0]
forcast_days = int((max_date_test - max_date_train) / np.timedelta64(1, 'D'))

from collections import OrderedDict
countries_dict = OrderedDict()
countries_dict['Afghanistan'] = ['']
countries_dict['Italy'] = ['']
countries_dict['India'] = ['']
countries_dict['Germany'] = ['']
countries_dict['Spain'] = ['']
countries_dict['Taiwan*'] = ['']
countries_dict['Japan'] = ['']
countries_dict['Spain'] = ['']
countries_dict['Germany'] = ['']
countries_dict['Singapore'] = ['']
countries_dict['Korea, South'] = ['']
countries_dict['United Kingdom'] = ['']
countries_dict['US'] = ['', 'Louisiana', 'New York', 'California', 'Minnesota']

from copy import deepcopy
n = 50
N_places = sum([len(value) for key, value in countries_dict.items()])
False_mask_0 = [False] * (N_places * 2 + 1)
labels = time_span[-n - 30:-30]
x = time_span0[-n - 30:-30]
data = []
manu_list = []
data.append(go.Bar(x=x, y=[0] * len(x), name='cases'))
False_mask = deepcopy(False_mask_0)
False_mask[0] = True
manu_list.append(dict(label='Select', method='update', args=[{'visible': False_mask}, {'title': 'Select country/state'}]))
n_place = -1
for country in countries_dict:
    for state in countries_dict[country]:
        sp = ' '
        if state != '':
            sp = ', '
        n_place += 1
        data_i = train[(train['Province_State'] == state) & (train['Country_Region'] == country)].sort_values(by='Date').loc[:, ['day', 'ConfirmedCases', 'Fatalities']]
        if country in ['United Kingdom', 'Canada']:
            data_i = train[train['Country_Region'] == country].groupby('Date').sum().reset_index().sort_values(by='Date').loc[:, ['day', 'ConfirmedCases', 'Fatalities']]
        if country == 'US' and state == '':
            data_i = train[train['Country_Region'] == country].groupby('Date').sum().reset_index().sort_values(by='Date').loc[:, ['day', 'ConfirmedCases', 'Fatalities']]
        cases = country + state + ' Cases_daily'
        deaths = country + state + ' deaths_daily'
        data_i[cases] = data_i['ConfirmedCases'].diff()
        data_i[deaths] = data_i['Fatalities'].diff()
        trace1 = go.Bar(x=x, y=data_i[cases][-n:], name='cases')
        trace2 = go.Bar(x=x, y=data_i[deaths][-n:], name='deaths')
        data += [trace1, trace2]
        False_mask = deepcopy(False_mask_0)
        False_mask[2 * n_place + 1:2 * n_place + 2 + 1] = [True, True]
        manu_list.append(dict(label=country + sp + state, method='update', args=[{'visible': False_mask}, {'title': country + sp + state}]))
updatemenus = [dict(active=0, buttons=manu_list, direction='down')]
layout = dict(title='Select Countries and states', yaxis=dict(title='daily count', linecolor='rgba(255,255,255, 0.8)', showgrid=True, gridcolor='rgba(255,255,255,0.2)'), xaxis=dict(title='Date', linecolor='rgba(255,255,255, 0.8)', showgrid=True, gridcolor='rgba(255,255,255,0.2)'), margin=go.Margin(l=50, r=20), paper_bgcolor='rgb(105,105,105)', plot_bgcolor='RGB(228, 235, 234)', barmode='group', font={'color': 'RGB(179, 217, 82)'}, updatemenus=updatemenus, showlegend=True)
fig = dict(data=data, layout=layout)
py.iplot(fig, filename='relayout_option_dropdown')