-
Notifications
You must be signed in to change notification settings - Fork 0
/
forecasting_dashboard.py
2430 lines (2061 loc) · 118 KB
/
forecasting_dashboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/opt/anaconda3/bin/ipython python
# coding: utf-8
# # Run all the below for the dashboard
# In[1]:
import sys
print (sys.version)
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import math
import os
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import dash
import dash_table
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
from sklearn.metrics import mean_squared_error
from scipy.optimize import curve_fit
from scipy.optimize import fsolve
import matplotlib.pyplot as plt
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut
# # pull the latest john hopkins time series dataset
# In[2]:
my_path = os.path.dirname(os.path.realpath(__file__))
if os.path.isdir('./COVID-19'):
print("checkout fresh version")
pull = "git pull"
path = r"./COVID-19"
#os.system("sshpass -p your_password ssh user_name@your_localhost")
os.chdir(path) # Specifying the path where the cloned project needs to be copied
os.system(pull) # pulling
else:
print("clone the john hopkins data")
github_path = "https://github.com/CSSEGISandData/COVID-19.git"
pull = "git clone"
os.system(pull + " " + github_path) # pulling
path = r"./COVID-19"
os.chdir(path) # Specifying the path where the cloned project needs to be copied
# In[3]:
url = 'https://data.humdata.org/hxlproxy/api/data-preview.csv?url=https%3A%2F%2Fraw.githubusercontent.com%2FCSSEGISandData%2FCOVID-19%2Fmaster%2Fcsse_covid_19_data%2Fcsse_covid_19_time_series%2Ftime_series_covid19_confirmed_global.csv&filename=time_series_covid19_confirmed_global.csv'
time_series_data_confirmed = pd.read_csv(url)
url = 'https://data.humdata.org/hxlproxy/api/data-preview.csv?url=https%3A%2F%2Fraw.githubusercontent.com%2FCSSEGISandData%2FCOVID-19%2Fmaster%2Fcsse_covid_19_data%2Fcsse_covid_19_time_series%2Ftime_series_covid19_deaths_global.csv&filename=time_series_covid19_deaths_global.csv'
time_series_data_deaths = pd.read_csv(url)
url = 'https://data.humdata.org/hxlproxy/api/data-preview.csv?url=https%3A%2F%2Fraw.githubusercontent.com%2FCSSEGISandData%2FCOVID-19%2Fmaster%2Fcsse_covid_19_data%2Fcsse_covid_19_time_series%2Ftime_series_covid19_recovered_global.csv&filename=time_series_covid19_recovered_global.csv'
time_series_data_recovered = pd.read_csv(url)
# In[4]:
time_series_data_confirmed
# In[5]:
time_series_data_confirmed.columns
# In[6]:
def df_for_lineplot_diff(dfs, CaseType):
'''This is the function for construct df for line plot'''
assert type(CaseType) is str, "CaseType must be one of the following three strings Confirmed/Recovered/Deaths"
# Construct confirmed cases dataframe for line plot
DateList = []
ChinaList =[]
OtherList = []
for key, df in dfs.items():
dfTpm = df.groupby(['Country/Region'])[CaseType].agg(np.sum)
dfTpm = pd.DataFrame({'Region':dfTpm.index, CaseType:dfTpm.values})
#dfTpm = dfTpm.sort_values(by=CaseType, ascending=False).reset_index(drop=True)
DateList.append(df['Date_last_updated_AEDT'][0])
#DateList.append(df['Last Update'][0])
ChinaList.append(dfTpm.loc[dfTpm['Region'] == 'China', CaseType].iloc[0])
OtherList.append(dfTpm.loc[dfTpm['Region'] != 'China', CaseType].sum())
df = pd.DataFrame({'Date':DateList,
'Mainland China':ChinaList,
'Other locations':OtherList})
df['Total']=df['Mainland China']+df['Other locations']
# Calculate differenec in a 24-hour window
for index, _ in df.iterrows():
# Calculate the time differnece in hour
diff=(df['Date'][0] - df['Date'][index]).total_seconds()/3600
# find out the latest time after 24-hour
if diff >= 20:
break
plusNum = df['Total'][0] - df['Total'][1]
plusPercentNum = (df['Total'][0] - df['Total'][1])/df['Total'][1]
# Select the latest data from a given date
df['date_day']=[d.date() for d in df['Date']]
df=df.groupby(by=df['date_day'], sort=False).transform(max).drop_duplicates(['Date'])
df['plusNum'] = plusNum
df['plusPercentNum'] = plusPercentNum
df=df.reset_index(drop=True)
return df, plusNum, plusPercentNum
# In[7]:
###get_ipython().run_cell_magic('time', '', "################################################################################\n#### Data processing\n################################################################################\n# Method #1\n# Import csv file and store each csv in to a df list\n\nfilename = os.listdir('./csse_covid_19_data/csse_covid_19_daily_reports/')\nsheet_name = [i.replace('.csv', '') for i in filename if 'data' not in i and i.endswith('.csv')]\nsheet_name.sort(reverse=True)\n\ndfs = {sheet_name: pd.read_csv('./csse_covid_19_data/csse_covid_19_daily_reports/{}.csv'.format(sheet_name))\n for sheet_name in sheet_name}")
filename= os.listdir('./csse_covid_19_data/csse_covid_19_daily_reports/')
sheet_name = [i.replace('.csv', '') for i in filename if 'data' not in i and i.endswith('.csv')]
sheet_name.sort(reverse=True)
dfs = {sheet_name: pd.read_csv('./csse_covid_19_data/csse_covid_19_daily_reports/{}.csv'.format(sheet_name))
for sheet_name in sheet_name}
# In[8]:
# Data from each sheet can be accessed via key
keyList = list(dfs.keys())
# Data cleansing
for key, df in dfs.items():
dfs[key]=dfs[key].rename(columns={'Last_Update': 'Last Update', "Province_State":"Province/State", "Country_Region":"Country/Region", "Lat":"lat", "Long_":"lon", "Latitude":"lat", "Longitude":"lon"})
dfs[key].loc[:,'Confirmed'].fillna(value=0, inplace=True)
dfs[key].loc[:,'Deaths'].fillna(value=0, inplace=True)
dfs[key].loc[:,'Recovered'].fillna(value=0, inplace=True)
dfs[key]=dfs[key].astype({'Confirmed':'int64', 'Deaths':'int64', 'Recovered':'int64'})
# Change as China for coordinate search
dfs[key]=dfs[key].replace({'Country/Region':'Mainland China'}, 'China')
# Add a zero to the date so can be convert by datetime.strptime as 0-padded date
dfs[key]['Last Update'] = '0' + dfs[key]['Last Update']
# Convert time as Australian eastern daylight time
### Try to sort out the crazy variety of formatting of dates in the John Hopkins Data
dates_append = []
for d in dfs[key]['Last Update']:
d = str(d)
d = d.replace("T"," ")
if "-" in d:
dates_append.append(datetime.strptime(d[-19:], '%Y-%m-%d %H:%M:%S'))
#dfs[key]['Date_last_updated_AEDT'] = [datetime.strptime(d, '%Y/%m/%d %H:%M:%S') for d in dfs[key]['Last Update']]
elif d == 'nan':
dates_append.append('')
else:
try:
dates_append.append(datetime.strptime(d[-14:], '%m/%d/%Y %H:%M'))
except:
try:
dates_append.append(datetime.strptime(d[-14:], '%m/%d/%y %H:%M'))
except:
try:
dates_append.append(datetime.strptime(d[-13:], '%m/%d/%y %H:%M'))
except:
try:
dates_append.append(datetime.strptime(d, '%m/%d/%Y %H:%M'))
except:
try:
dates_append.append(datetime.strptime(d[-16:], '%m/%d/%Y %H:%M'))
except:
try:
dates_append.append(datetime.strptime(d[-16:], '%m/%d/%Y %H:%M'))
except:
try:
dates_append.append(datetime.strptime(d[-12:], '%m/%d/%y %H:%M'))
except:
print('Error could not parse date: ', d)
dates_append.append('')
#dfs[key]['Date_last_updated_AEDT'] = [datetime.strptime(d, '%m/%d/%Y %H:%M') for d in dfs[key]['Last Update']]
dfs[key]['Date_last_updated_AEDT'] = dates_append
dfs[key]['Date_last_updated_AEDT'] = dfs[key]['Date_last_updated_AEDT'] + timedelta(hours=16)
dfs[key]['Remaining'] = dfs[key]['Confirmed'] - dfs[key]['Recovered'] - dfs[key]['Deaths']
dfs[key] = dfs[key].fillna('')
#print(key)
#print(df)
# # Add coordinates for each area in the list for the latest table sheet
# # To save time, coordinates calling was done seperately
# # Import the data with coordinates
# data_sheet_df = pd.read_csv(r'../2020-03-24-06-00_data.csv'.format(keyList[0]))
# print(data_sheet_df)
# dfs[keyList[0]]=dfs[keyList[0]].astype({'Date_last_updated_AEDT':'datetime64'})
# In[9]:
# In[10]:
# In[11]:
baseURL = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/"
def loadData(fileName, columnName):
data = pd.read_csv(baseURL + fileName) .drop(['Lat', 'Long'], axis=1) .melt(id_vars=['Province/State', 'Country/Region'],
var_name='date', value_name=columnName) \
.astype({'date':'datetime64[ns]', columnName:'Int64'},
errors='ignore')
data['Province/State'].fillna('<all>', inplace=True)
data[columnName].fillna(0, inplace=True)
return data
# In[12]:
def make_country_table(countryName):
'''This is the function for building df for Province/State of a given country'''
countryTable = dfs[keyList[0]].loc[dfs[keyList[0]]['Country/Region'] == countryName]
# Suppress SettingWithCopyWarning
pd.options.mode.chained_assignment = None
countryTable['Remaining'] = countryTable['Confirmed'] - countryTable['Recovered'] - countryTable['Deaths']
countryTable = countryTable[['Province/State','Remaining','Confirmed','Recovered','Deaths','lat','lon']]
countryTable = countryTable.sort_values(by=['Remaining', 'Confirmed'], ascending=False).reset_index(drop=True)
# Set row ids pass to selected_row_ids
countryTable['id'] = countryTable['Province/State']
countryTable.set_index('id', inplace=True, drop=False)
# Turn on SettingWithCopyWarning
pd.options.mode.chained_assignment = 'warn'
return countryTable
# In[13]:
###get_ipython().run_cell_magic('time', '', "CNTable = make_country_table('China')\nAUSTable = make_country_table('Australia')\nUSTable = make_country_table('US')\nCANTable = make_country_table('Canada')")
CNTable = make_country_table('China')
AUSTable = make_country_table('Australia')
USTable = make_country_table('US')
CANTable = make_country_table('Canada')
# In[14]:
CANTable
# # Work on the UK as a special case where we try to forecast the number of free beds
# # Step 1: Build the UK COVID-19 forecase
def build_df_region(Region):
if Region == 'UK':
Region = 'United Kingdom'
df_region = pd.DataFrame(time_series_data_confirmed[time_series_data_confirmed['Country/Region'] == Region].transpose()[4:])
if len(df_region.columns) > 1:
s = df_region.sum().sort_values(ascending=False, inplace=False)
df_region = pd.DataFrame(df_region[s[:1].index[0]].astype(int))
df_region.columns = ['Confirmed']
df_region_recovered = pd.DataFrame(time_series_data_recovered[time_series_data_recovered['Country/Region'] == Region].transpose()[4:])
s = df_region_recovered.sum().sort_values(ascending=False, inplace=False)
df_region_recovered = pd.DataFrame(df_region_recovered[s[:1].index[0]].astype(int))
df_region['Recovered'] = df_region_recovered
df_region_deaths = pd.DataFrame(time_series_data_deaths[time_series_data_deaths['Country/Region'] == Region].transpose()[4:])
s = df_region_deaths.sum().sort_values(ascending=False, inplace=False)
df_region_deaths = pd.DataFrame(df_region_deaths[s[:1].index[0]].astype(int))
df_region['Deaths'] = df_region_deaths
else:
df_region.columns = ['Confirmed']
df_region['Recovered'] = time_series_data_recovered[time_series_data_recovered['Country/Region'] == Region].transpose()[4:]
df_region['Deaths'] = time_series_data_deaths[time_series_data_deaths['Country/Region'] == Region].transpose()[4:]
df_region['New'] = df_region['Confirmed'].astype(int).diff().fillna(0)
list_days = []
for i in range(1,len(df_region['Confirmed'])+1):
list_days.append(df_region['Confirmed'][:i].astype(bool).sum(axis=0))
df_region['DayElapsed'] = list_days
df_region['date_day'] = df_region.index
date_list = []
import re
for date_str in df_region['date_day']:
if re.match('./.*', date_str):
date_str='0'+date_str
if re.match('.././..', date_str):
date_str=date_str[:3]+'0'+date_str[-4:]
if re.match('.././....', date_str):
date_str=date_str[:3]+'0'+date_str[-6:]
if re.match('../../....', date_str):
date_list.append(datetime.strptime(date_str, '%m/%d/%Y'))
else:
date_list.append(datetime.strptime(date_str, '%m/%d/%y'))
df_region['Date_last_updated_AEDT'] = date_list
df_region['Date_last_updated_AEDT'] = df_region['Date_last_updated_AEDT'] + timedelta(hours=16)
df_region=df_region.astype({'Date_last_updated_AEDT':'datetime64', 'date_day':'datetime64'})
return df_region
# In[15]:
Region = 'United Kingdom'
df_region = build_df_region(Region)
df = df_region.loc[:,['date_day','Confirmed']]
df.index = df['date_day']
df = df.sort_index()
FMT = '%Y-%m-%d %H:%M:%S'
date = df['date_day']
df['data'] = date.map(lambda x : (datetime.strptime(str(x), FMT) - datetime.strptime("2020-01-01 00:00:00", FMT)).days )
#The logistic model
def logistic_model(x,a,b,c):
return c/(1+np.exp(-(x-b)/a))
#We can use the curve_fit function of scipy library to estimate the parameter values and errors starting from the original data.
x = list(df['data'])
y = list(df['Confirmed'])
fit = curve_fit(logistic_model,x,y,p0=[2,100,20000])
a = fit[0][0]
b = fit[0][1]
c = fit[0][2]
errors = [np.sqrt(fit[1][i][i]) for i in [0,1,2]]
sol = int(fsolve(lambda x : logistic_model(x,a,b,c) - int(c),b))
#Exponential model
def exponential_model(x,a,b,c):
return a*np.exp(b*(x-c))
exp_fit = curve_fit(exponential_model,x,y,p0=[1,1,1],maxfev=5000)
base = datetime.strptime(str(date[-1:].values[0]).replace("T"," ")[:19],"%Y-%m-%d %H:%M:%S")
dates = []
for i_date in date:
dates.append(datetime.strptime(str(i_date).replace("T"," ")[:19],"%Y-%m-%d %H:%M:%S"))
date_list_pred = [base + timedelta(days=i_x) for i_x in range(1,sol)]
date_list = []
for i_date in dates:
date_list.append(i_date)
for i_date in date_list_pred:
date_list.append(i_date)
fig = plt.figure(figsize=(20,10))
pred_x = list(range(max(x),sol))
plt.rcParams['figure.figsize'] = [7, 7]
plt.rc('font', size=14)
# Real data
plt.scatter(dates,y,label="Real data",color="red")
# Predicted logistic curve
plt.plot(date_list, [logistic_model(i,fit[0][0],fit[0][1],fit[0][2]) for i in range(0+min(x),len(date_list)+min(x))], label="Logistic model" )
# Predicted exponential curve
plt.plot(date_list, [exponential_model(i,exp_fit[0][0],exp_fit[0][1],exp_fit[0][2]) for i in range(0+min(x),len(date_list)+min(x))], label="Exponential model" )
plt.legend()
plt.xlabel("Days since 1 January 2020")
plt.ylabel("Total number of infected people")
plt.ylim((min(y)*0.9,c*1.1))
plt.show()
# # Step 2: Download the NHS England free beds hisorical data for a baseline of free beds at different NHS Trusts in England
# In[16]:
lm_predicted = [logistic_model(i,fit[0][0],fit[0][1],fit[0][2]) for i in range(0+min(x),len(date_list)+min(x))]
from datetime import date, datetime
today = date.today()
today_with_time = datetime(
year=today.year,
month=today.month,
day=today.day,
hour=0,
minute=0
)
try:
location = date_list.index(today_with_time)
print(today, " was found in the list.")
except:
print(today, " was not found in the list.")
total_cases_predicted_in_a_week = lm_predicted[location+7]
current_cases = lm_predicted[location]
growth_factor_in_a_week = total_cases_predicted_in_a_week / current_cases
print(growth_factor_in_a_week)
#Read in the beds data: Using the beds data for England in Q1 2019 as a baseline (current Quarter in 2020) before the COVID-19 cases
### Using Q1 beds data from 2019 as a baseline
df_england_beds = pd.read_excel('https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2020/02/Beds-Timeseries-2010-11-onwards-Q3-2019-20-ADJ-for-missings-j8hyu.xls', header=13)
df_england_beds[['Year','Total ', 'General & Acute']].plot()
df_england_beds[['Year','Total .1', 'General & Acute.1']].plot()
df_england_beds[['Year','Total .2', 'General & Acute.2']].plot()
#read in the free beds via hospital
df_england_beds_region = pd.read_excel("https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2019/11/Beds-Open-Overnight-Web_File-Final-Q1-201920.xlsx", header=14)
df_england_beds_region['Free General & Acute'] = df_england_beds_region['General & Acute'] - df_england_beds_region['General & Acute.1']
#20200503#df_england_beds_region.index = df_england_beds_region['Org Name']
df_england_beds_region['Free General & Acute'][2:].plot(figsize=(50,5), kind='bar', ylim=(0,300))
df_england_beds_region.dropna(how='all',inplace=True)
df_england_beds_region=df_england_beds_region[df_england_beds_region['Org Name'] != 'England']
df_england_beds_region=df_england_beds_region.reset_index(drop=True)
# # Get the England regions COVID-19 data
# In[17]:
#read in the latest UK COVID-19 data for England regions
# download UK regional cases
##url = "https://www.arcgis.com/sharing/rest/content/items/b684319181f94875a6879bbc833ca3a6/data"
##excel_url = 'https://fingertips.phe.org.uk/documents/Historic%20COVID-19%20Dashboard%20Data.xlsx'
new_csv = 'https://raw.githubusercontent.com/tomwhite/covid-19-uk-data/master/data/covid-19-cases-uk.csv'
##new_csv='../covid-19-cases-uk.csv'
url="https://c19downloads.azureedge.net/downloads/csv/coronavirus-cases_latest.csv"
excel_sheet_name = 'UTLAs'
## Get latest data from new_csv
df_UK_tmp = pd.read_csv(new_csv, error_bad_lines=False)
latest_date = max(df_UK_tmp['Date'])
df_UK_latest = df_UK_tmp[df_UK_tmp['Date']== str(datetime.strptime(latest_date,'%Y-%m-%d') - timedelta(days=1))[0:10]]
df_UK = df_UK_latest[['Area', 'TotalCases']]
df_UK.columns = ['GSS_NM', 'TotalCases']
## Get series data (not quite up to date)
df_UK_tmp = pd.read_csv(url, error_bad_lines=False)
## Should probably pick out 'Upper tier local authority' only?
df_UK_tmp.drop(axis=1,labels='Area type',inplace=True)
df_UK_tmp.drop_duplicates(inplace=True)
df_UK_series = df_UK_tmp[['Area name','Area code', 'Specimen date', 'Daily lab-confirmed cases', 'Cumulative lab-confirmed cases']]
df_UK_series.columns = ['Area','GSS_NM','Date','NewCases','TotalCases']
df_UK_series.NewCases.fillna(value=0.0,inplace=True)
df_UK_series.TotalCases.fillna(value=0.0,inplace=True)
##dates_append = pd.DataFrame(columns=['day_date'])
dates_append = []
for index, _ in df_UK_series.iterrows():
date=df_UK_series['Date'][index]
dates_append.append(datetime.strptime(date, '%Y-%m-%d'))
df_UK_series['day_date']=dates_append
##df_UK_series.merge(dates_append,how='inner',left_index=True,right_index=True)
#2020/05/01#print("df_UK_series")
#2020/05/01#print(df_UK_series)
###Scotland cases by region
excel_url="https://www.gov.scot/binaries/content/documents/govscot/publications/statistics/2020/04/trends-in-number-of-people-in-hospital-with-confirmed-or-suspected-covid-19/documents/covid-19-data-by-nhs-board/covid-19-data-by-nhs-board/govscot%3Adocument/COVID-19%2Bdata%2Bby%2BNHS%2BBoard-300420-1.xlsx"
excel_url="https://www.gov.scot/binaries/content/documents/govscot/publications/statistics/2020/04/coronavirus-covid-19-trends-in-daily-data/documents/covid-19-data-by-nhs-board/covid-19-data-by-nhs-board/govscot%3Adocument/COVID-19%2Bdata%2Bby%2BNHS%2BBoard%2B110520.xlsx"
## Need to update the date (also what if it is moved?)
###Sheet 3 "Table 1 - Cumulative cases"
# 2020/05/12 This spreadsheet no longer exists
#20200512#df_UK_tmp=pd.read_excel(excel_url, header=2, sheet_name='Table 1 - Cumulative cases')
#20200512#df_UK_tmp.replace(to_replace='*',value=0,inplace=True)
#2020/05/10#print('df_UK_tmp')
#2020/05/10#print(df_UK_tmp)
#20200512#dates_append = []
#20200512#for index, _ in df_UK_tmp.iterrows():
#20200512#date=df_UK_tmp['Date'][index]
#20200512#datestr=date.strftime('%Y-%m-%d')
#20200512#dates_append.append(datestr)
#20200512#df_UK_tmp.columns=['Date','Ayrshire and Arran','Borders','Dumfries and Galloway','Fife','Forth Valley','Grampian','Greater Glasgow and Clyde','Highland','Lanarkshire','Lothian','Orkney','Shetland','Tayside','Western Isles','Scotland']
#2020/05/10#print('df_UK_tmp again')
#2020/05/10#print(df_UK_tmp)
#20200512#df_region_tmp=df_UK_tmp[['Date','Ayrshire and Arran']]
#20200512#df_region_tmp.columns=['Date','TotalCases']
#20200512#df_region_tmp['Area']='Ayrshire and Arran'
#20200512#df_region_tmp['GSS_NM']='S08000015'
#20200512#df_region_tmp['Type']='Region'
#20200512#df_region_tmp['NewCases']=0.0
#20200512#print('Ayrshire and Arran')
#20200512#print(df_region_tmp)
#20200512#df_UK_series=df_UK_series.append(df_region_tmp,ignore_index=True)
df_UK_lat_lon = pd.read_csv('../df_UK_lat_lon_all.csv')
del df_UK_lat_lon['Unnamed: 0']
df_UK = df_UK.merge(df_UK_lat_lon, how='outer', left_on=["GSS_NM"], right_on=["GSS_NM"])
df_UK = df_UK.dropna()
#from geopy.geocoders import Nominatim
#from geopy.exc import GeocoderTimedOut
#geolocator = Nominatim(user_agent="covid_shahinrostami.com")
#
#for index, row in df_UK.iterrows():
# location = geolocator.geocode(row.GSS_NM+", UK",timeout=100)
# df_UK.loc[index,'lat'] = location.latitude
# df_UK.loc[index,'lon'] = location.longitude
#
#print("Done!")
# In[18]:
tmp_total_cases = []
# eunsure we have total cases as floats and remove commas to do so
for item in df_UK['TotalCases']:
if isinstance(item, str):
tmp_total_cases.append(float(item.replace(',','')))
elif isinstance(item, int):
tmp_total_cases.append(float(item))
elif isinstance(item, float):
tmp_total_cases.append(float(item))
else:
tmp_total_cases.append(0)
print('item type error: ', item)
df_UK['TotalCases'] = tmp_total_cases
df_UK_tmp = df_UK.copy()
# # Match the UK local data to the NHS beds data for England NHS trusts
# In[19]:
import pgeocode
#match the local data to the beds regions
#20200501#df_nhs_trusts = pd.read_csv('trusts_lat_lon.csv', header=0)
#20200503#df_nhs_trusts = pd.read_csv('https://nhsenglandfilestore.s3.amazonaws.com/ods/etr.csv', header=None)
df_nhs_trusts = pd.read_csv('../trusts_lat_lon_unitary.csv',header=0)
matches = []
for org_name in df_england_beds_region['Org Name']:
for trust in df_nhs_trusts['Org Name']:
if trust == org_name:
matches.append(trust + ' matches ' + org_name)
df_england_beds_region.reset_index(drop=True)
#20200503#merged_df = df_england_beds_region.reset_index(drop=True).merge(df_nhs_trusts[['Org Name','Postcode','lat','lon','uni_code','Unitary Authority','uni_lat','uni_lon']], how='inner', left_on=["Org Name"], right_on=["Org Name"])
merged_df = df_nhs_trusts.reset_index(drop=True).merge(df_england_beds_region[['Org Name',"Total ", "General & Acute", "Total .1", "General & Acute.1",'Free General & Acute']], how='outer', left_on=["Org Name"], right_on=["Org Name"])
#match the UK death data to the trusts
###df_UK_deaths = pd.read_excel('https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2020/04/COVID-19-total-announced-deaths-20-April-2020.xlsx', sheet_name='COVID19 total deaths by trust', header=15)
nomi = pgeocode.Nominatim('gb')
#find the closest lat and lon to that trust from the geospatial data and join the datasets to the beds dataset
from math import cos, asin, sqrt
def distance(lat1, lon1, lat2, lon2):
p = 0.017453292519943295
a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p)*cos(lat2*p) * (1-cos((lon2-lon1)*p)) / 2
return 12742 * asin(sqrt(a))
def closest(data, v):
return min(data, key=lambda p: distance(v['lat'],v['lon'],p['lat'],p['lon']))
#20200503#hospital_lat = []
#20200503#hospital_lon = []
#20200503#for index, row in df_UK.iterrows():
#20200503#v = {'lat': row['lat'], 'lon': row['lon']}
#20200503#match = closest(list(merged_df[['lat','lon']].T.to_dict().values()),v)
#20200420#print(distance(v['lat'],v['lon'],match['lat'],match['lon']))
#20200503#if distance(v['lat'],v['lon'],match['lat'],match['lon']) < 50: #ensures the match is sensible
#20200503#hospital_lat.append(match['lat'])
#20200503#hospital_lon.append(match['lon'])
#20200503#else:
#20200503#hospital_lat.append(np.nan)
#20200503#hospital_lon.append(np.nan)
#20200503#df_UK['hospital_lat'] = hospital_lat
#20200503#df_UK['hospital_lon'] = hospital_lon
#20200503#df_UK_merged = df_UK.merge(merged_df[["Org Name", "Total ", "General & Acute", "Total .1", "General & Acute.1","lat","lon"]], how='inner', left_on=["hospital_lon", "hospital_lat"], right_on=["lon","lat"])
#20200503#df_UK_merged = df_UK.merge(merged_df[["Org Name", "Total ", "General & Acute", "Total .1", "General & Acute.1","Unitary Authority","uni_lat","uni_lon"]], how='inner', left_on=["GSS_NM"], right_on=["Unitary Authority"])
df_UK_merged = merged_df.merge(df_UK, how='inner', left_on=["Unitary Authority"], right_on=["GSS_NM"])
df_UK_merged['Free Beds Without COVID-19'] = df_UK_merged['Total '] - df_UK_merged['Total .1']
#Same as above for deaths by trust
#20200501#merged_UK_deaths = df_UK_deaths.reset_index(drop=True).merge(df_nhs_trusts[["Org Name","Postcode","lat","lon"]], how='inner', left_on=["Org Name"], right_on=["Org Name"])
#20200501#df_UK_merged_deaths = df_UK.merge(merged_UK_deaths[["Date", "Org Name", "Deaths","lat","lon"]], how='inner', left_on=["hospital_lon", "hospital_lat"], right_on=["lon","lat"])
Free_Beds_INCLUDING_COVID = []
counts_list = df_UK_merged.groupby(by=['Unitary Authority'])['Unitary Authority'].count()
print("Grouping free beds")
for item in df_UK_merged['Unitary Authority']:
print(item)
print(counts_list[item])
Free_Beds_INCLUDING_COVID.append(counts_list[item])
df_UK_merged['count'] = Free_Beds_INCLUDING_COVID
percentage_cases_open = 1-(277643/1044167)
#8.2% of patients require hospitalisation according to Imperial College study
percentage_hospital = percentage_cases_open * 0.082
percentage_serious = percentage_cases_open * 0.05
print(percentage_cases_open, percentage_serious, percentage_hospital)
print("percentage_hospital: ",percentage_hospital)
print("Total Cases: ",df_UK_merged['TotalCases'][0])
print("Ratio: ",df_UK_merged['TotalCases']/df_UK_merged['count'])
df_UK_merged['Free Beds INCLUDING COVID-19'] = df_UK_merged['Free Beds Without COVID-19'] - (percentage_hospital * df_UK_merged['TotalCases']/df_UK_merged['count'].astype(float))
df_UK_merged.index = df_UK_merged['Org Name']
# # Print out and plot the expectation for current free beds in the NHS trusts in England
# In[20]:
#20200516#minimum = min(df_UK_merged['Free Beds INCLUDING COVID-19'][2:].fillna(0))
#20200516#maximum = max(df_UK_merged['Free Beds INCLUDING COVID-19'][2:].fillna(0))
#Plot a chart representing the current hospital bed vacancy by trust by incrementing over and above 2019 Q1 total free beds with COVID-19 estimated current cases requiring hospitalisation in the UK
#20200516#df_UK_merged.sort_values(by=['Free Beds INCLUDING COVID-19'])['Free Beds INCLUDING COVID-19'].dropna()[2:].plot(figsize=(100,50), kind='bar', ylim=(minimum,maximum),color=(df_UK_merged.sort_values(by=['Free Beds INCLUDING COVID-19'])['Free Beds INCLUDING COVID-19'][2:].dropna() > 0).map({True: 'g',False: 'r'}),title="Estimated current number of free beds after COVID-19 by hospital trust in the UK",fontsize = 30)
#20200516#df_UK_merged.sort_values(by=['Free Beds INCLUDING COVID-19'])
# # Print out and plot the forecasted free beds in 1 WEEK for NHS Enland Trusts
# In[21]:
#Plot a chart representing the NEXT WEEKS hospital bed vacancy by trust by incrementing over and above 2019 Q1 total free beds with COVID-19 estimated cases 1 WEEK FROM TODAY requiring hospitalisation in the UK
#20200516#df_UK_merged['Predicted Free Beds INCLUDING COVID-19 in 1 WEEK'] = df_UK_merged['Free Beds Without COVID-19'] - (percentage_hospital * growth_factor_in_a_week * df_UK_merged['TotalCases']/df_UK_merged['count'])
#20200516#minimum = min(df_UK_merged['Predicted Free Beds INCLUDING COVID-19 in 1 WEEK'][2:].fillna(0))
#20200516#maximum = max(df_UK_merged['Predicted Free Beds INCLUDING COVID-19 in 1 WEEK'][2:].fillna(0))
#20200516#df_UK_merged.sort_values(by=['Predicted Free Beds INCLUDING COVID-19 in 1 WEEK'])['Predicted Free Beds INCLUDING COVID-19 in 1 WEEK'].dropna()[2:].plot(figsize=(100,50), kind='bar', ylim=(minimum,maximum),color=(df_UK_merged.sort_values(by=['Predicted Free Beds INCLUDING COVID-19 in 1 WEEK'])['Predicted Free Beds INCLUDING COVID-19 in 1 WEEK'][2:].dropna() > 0).map({True: 'g',False: 'r'}),title="Estimated current number of free beds after COVID-19 by hospital trust in the UK",fontsize = 30)
#20200516#df_UK_merged.sort_values(by=['Predicted Free Beds INCLUDING COVID-19 in 1 WEEK']).dropna()
# # Go back to building the UK regions data for the dashboard
# In[22]:
df_UK = df_UK_tmp
UKTable= pd.DataFrame(columns=CANTable.columns)
UKTable['Province/State'] = df_UK['GSS_NM']
UKTable['Confirmed'] = df_UK['TotalCases']
UKTable['lat'] = df_UK['lat']
UKTable['lon'] = df_UK['lon']
UKTable['id'] = df_UK['GSS_NM']
UKTable.index = UKTable['id']
UKTable
# In[25]:
UKTable_append = UKTable.copy()
# In[26]:
UKTable_append['Country/Region']='UK'
###UKTable_append['Last Update'] = '2020-03-24 22:00:00'
###UKTable_append['Date_last_updated_AEDT'] = '2020-03-24 22:00:00'
UKTable_append['Recovered'] = 0
UKTable_append['Deaths'] = 0
UKTable_append['Remaining'] = 0
del UKTable_append['id']
UKTable_append
print(UKTable_append['Country/Region'])
print(UKTable_append['Deaths'][0])
# In[27]:
###get_ipython().run_cell_magic('time', '', "# Save numbers into variables to use in the app\nconfirmedCases=int(dfs[keyList[0]]['Confirmed'].sum())\ndeathsCases=int(dfs[keyList[0]]['Deaths'].sum())\nrecoveredCases=int(dfs[keyList[0]]['Recovered'].sum())\n\n# Construct confirmed cases dataframe for line plot and 24-hour window case difference\ndf_confirmed, plusConfirmedNum, plusPercentNum1 = df_for_lineplot_diff(dfs, 'Confirmed')\n\n\n# Construct recovered cases dataframe for line plot and 24-hour window case difference\ndf_recovered, plusRecoveredNum, plusPercentNum2 = df_for_lineplot_diff(dfs, 'Recovered')\n\n\n# Construct death case dataframe for line plot and 24-hour window case difference\ndf_deaths, plusDeathNum, plusPercentNum3 = df_for_lineplot_diff(dfs, 'Deaths')\n\n# Construct remaining case dataframe for line plot and 24-hour window case difference\ndf_remaining, plusRemainNum, plusRemainNum3 = df_for_lineplot_diff(dfs, 'Remaining')")
confirmedCases=int(dfs[keyList[0]]['Confirmed'].sum())
deathsCases=int(dfs[keyList[0]]['Deaths'].sum())
recoveredCases=int(dfs[keyList[0]]['Recovered'].sum())
# Construct confirmed cases dataframe for line plot and 24-hour window case difference
df_confirmed, plusConfirmedNum, plusPercentNum1 = df_for_lineplot_diff(dfs, 'Confirmed')
# Construct recovered cases dataframe for line plot and 24-hour window case difference
df_recovered, plusRecoveredNum, plusPercentNum2 = df_for_lineplot_diff(dfs, 'Recovered')
# Construct death case dataframe for line plot and 24-hour window case difference
df_deaths, plusDeathNum, plusPercentNum3 = df_for_lineplot_diff(dfs, 'Deaths')
# Construct remaining case dataframe for line plot and 24-hour window case difference
df_remaining, plusRemainNum, plusRemainNum3 = df_for_lineplot_diff(dfs, 'Remaining')
# In[29]:
confirmedCases=dfs[keyList[0]]['Confirmed'].sum()
confirmedCases
# In[30]:
# Create data table to show in app
# Generate sum values for Country/Region level
dfCase = dfs[keyList[0]].groupby(by='Country/Region', sort=False).sum().reset_index()
dfCase = dfCase.sort_values(by=['Confirmed'], ascending=False).reset_index(drop=True)
# As lat and lon also underwent sum(), which is not desired, remove from this table.
#dfCase = dfCase.drop(columns=['lat','lon'])
# In[31]:
dfs[keyList[0]].sort_values('Confirmed',ascending=False).groupby(by=['Country/Region']).first().reset_index()
# In[32]:
# Grep lat and lon by the first instance to represent its Country/Region
#dfGPS = dfs[keyList[0]].groupby(by=['Country/Region'], sort=True).first().reset_index()
dfGPS = dfs[keyList[0]].sort_values('Confirmed',ascending=False).groupby(by=['Country/Region']).first().reset_index()
dfGPS = dfGPS[['Country/Region','lat','lon']]
# Merge two dataframes
dfSum = pd.merge(dfCase, dfGPS, how='inner', on='Country/Region')
dfSum = dfSum.replace({'Country/Region':'China'}, 'Mainland China')
dfSum['Remaining'] = dfSum['Confirmed'] - dfSum['Recovered'] - dfSum['Deaths']
# Rearrange columns to correspond to the number plate order
dfSum = dfSum[['Country/Region','Remaining','Confirmed','Recovered','Deaths','lat','lon']]
# Sort value based on Remaining cases and then Confirmed cases
dfSum = dfSum.sort_values(by=['Remaining', 'Confirmed'], ascending=False).reset_index(drop=True)
# Set row ids pass to selected_row_ids
dfSum['id'] = dfSum['Country/Region']
dfSum.set_index('id', inplace=True, drop=False)
# Save numbers into variables to use in the app
latestDate=datetime.strftime(df_confirmed['Date'][0], '%b %d, %Y %H:%M AEDT')
secondLastDate=datetime.strftime(df_confirmed['Date'][1], '%b %d')
daysOutbreak=(df_confirmed['Date'][0] - datetime.strptime('12/31/2019', '%m/%d/%Y')).days
# In[33]:
#############################################################################################
#### Start to make plots
#############################################################################################
# Line plot for confirmed cases
# Set up tick scale based on confirmed case number
tickList = list(np.arange(0, df_confirmed['Other locations'].max()+1000, 100000))
# Create empty figure canvas
fig_confirmed = go.Figure()
# Add trace to the figure
fig_confirmed.add_trace(go.Scatter(x=df_confirmed['Date'].fillna(''), y=df_confirmed['Mainland China'],
mode='lines+markers',
line_shape='spline',
name='Mainland China',
line=dict(color='#921113', width=4),
marker=dict(size=4, color='#f4f4f2',
line=dict(width=1,color='#921113')),
text=[datetime.strftime(d, '%b %d %Y AEDT') for d in df_confirmed['Date'].fillna(pd.Timestamp('20200101'))],
hovertext=['Mainland China confirmed<br>{:,d} cases<br>'.format(int(i)) for i in df_confirmed['Mainland China'].fillna(0)],
hovertemplate='<b>%{text}</b><br>'+
'%{hovertext}'+
'<extra></extra>'))
fig_confirmed.add_trace(go.Scatter(x=df_confirmed['Date'].fillna(''), y=df_confirmed['Other locations'],
mode='lines+markers',
line_shape='spline',
name='Other Region',
line=dict(color='#eb5254', width=4),
marker=dict(size=4, color='#f4f4f2',
line=dict(width=1,color='#eb5254')),
text=[datetime.strftime(d, '%b %d %Y AEDT') for d in df_confirmed['Date'].fillna(pd.Timestamp('20200101'))],
hovertext=['Other region confirmed<br>{:,d} cases<br>'.format(int(i)) for i in df_confirmed['Other locations'].fillna(0)],
hovertemplate='<b>%{text}</b><br>'+
'%{hovertext}'+
'<extra></extra>'))
# Customise layout
fig_confirmed.update_layout(
# title=dict(
# text="<b>Confirmed Cases Timeline<b>",
# y=0.96, x=0.5, xanchor='center', yanchor='top',
# font=dict(size=20, color="#292929", family="Playfair Display")
# ),
margin=go.layout.Margin(
l=10,
r=10,
b=10,
t=5,
pad=0
),
yaxis=dict(
showline=False, linecolor='#272e3e',
zeroline=False,
#showgrid=False,
gridcolor='rgba(203, 210, 211,.3)',
gridwidth = .1,
tickmode='array',
# Set tick range based on the maximum number
tickvals=tickList,
# Set tick label accordingly
ticktext=["{:.0f}k".format(i/1000) for i in tickList]
),
# yaxis_title="Total Confirmed Case Number",
xaxis=dict(
showline=False, linecolor='#272e3e',
showgrid=False,
gridcolor='rgba(203, 210, 211,.3)',
gridwidth = .1,
zeroline=False
),
xaxis_tickformat='%b %d',
hovermode = 'x',
legend_orientation="h",
# legend=dict(x=.35, y=-.05),
plot_bgcolor='#f4f4f2',
paper_bgcolor='#cbd2d3',
font=dict(color='#292929')
)
# In[34]:
dfs[keyList[0]]=dfs[keyList[0]].append(UKTable_append, ignore_index=True)
# In[ ]:
UKTable_append[UKTable_append['Province/State']=='UK']
# In[35]:
# Line plot for combine cases
# Set up tick scale based on confirmed case number
tickList = list(np.arange(0, df_remaining['Total'].max()+2000, 100000))
# Create empty figure canvas
fig_combine = go.Figure()
# Add trace to the figure
fig_combine.add_trace(go.Scatter(x=df_recovered['Date'], y=df_recovered['Total'],
mode='lines+markers',
line_shape='spline',
name='Total Recovered Cases',
line=dict(color='#168038', width=4),
marker=dict(size=4, color='#f4f4f2',
line=dict(width=1,color='#168038')),
text=[datetime.strftime(d, '%b %d %Y AEDT') for d in df_recovered['Date'].fillna(pd.Timestamp('20200101'))],
hovertext=['Total recovered<br>{:,d} cases<br>'.format(int(i)) for i in df_recovered['Total'].fillna(0)],
hovertemplate='<b>%{text}</b><br>'+
'%{hovertext}'+
'<extra></extra>'))
fig_combine.add_trace(go.Scatter(x=df_deaths['Date'].fillna(pd.Timestamp('20200101')), y=df_deaths['Total'].fillna(0),
mode='lines+markers',
line_shape='spline',
name='Total Death Cases',
line=dict(color='#626262', width=4),
marker=dict(size=4, color='#f4f4f2',
line=dict(width=1,color='#626262')),
text=[datetime.strftime(d, '%b %d %Y AEDT') for d in df_deaths['Date'].fillna(pd.Timestamp('20200101'))],
hovertext=['Total death<br>{:,d} cases<br>'.format(int(i)) for i in df_deaths['Total'].fillna(0)],
hovertemplate='<b>%{text}</b><br>'+
'%{hovertext}'+
'<extra></extra>'))
fig_combine.add_trace(go.Scatter(x=df_remaining['Date'].fillna(pd.Timestamp('20200101')), y=df_remaining['Total'].fillna(0),
mode='lines+markers',
line_shape='spline',
name='Total Remaining Cases',
line=dict(color='#e36209', width=4),
marker=dict(size=4, color='#f4f4f2',
line=dict(width=1,color='#e36209')),
text=[datetime.strftime(d, '%b %d %Y AEDT') for d in df_deaths['Date'].fillna(pd.Timestamp('20200101'))],
hovertext=['Total remaining<br>{:,d} cases<br>'.format(int(i)) for i in df_remaining['Total'].fillna(0)],
hovertemplate='<b>%{text}</b><br>'+
'%{hovertext}'+
'<extra></extra>'))
# Customise layout
fig_combine.update_layout(
# title=dict(
# text="<b>Confirmed Cases Timeline<b>",
# y=0.96, x=0.5, xanchor='center', yanchor='top',
# font=dict(size=20, color="#292929", family="Playfair Display")
# ),
margin=go.layout.Margin(
l=10,
r=10,
b=10,
t=5,
pad=0
),
yaxis=dict(
showline=False, linecolor='#272e3e',
zeroline=False,
#showgrid=False,
gridcolor='rgba(203, 210, 211,.3)',
gridwidth = .1,
tickmode='array',
# Set tick range based on the maximum number
tickvals=tickList,
# Set tick label accordingly
ticktext=["{:.0f}k".format(i/1000) for i in tickList]
),
# yaxis_title="Total Confirmed Case Number",
xaxis=dict(
showline=False, linecolor='#272e3e',
showgrid=False,
gridcolor='rgba(203, 210, 211,.3)',
gridwidth = .1,
zeroline=False
),
xaxis_tickformat='%b %d',
hovermode = 'x',
legend_orientation="h",
# legend=dict(x=.35, y=-.05),
plot_bgcolor='#f4f4f2',
paper_bgcolor='#cbd2d3',
font=dict(color='#292929')
)
# Line plot for death rate cases
# Set up tick scale based on confirmed case number
tickList = list(np.arange(0, (df_deaths['Other locations']/df_confirmed['Other locations']*100).max(), 0.5))
# Create empty figure canvas
fig_rate = go.Figure()
# Add trace to the figure
fig_rate.add_trace(go.Scatter(x=df_deaths['Date'], y=df_deaths['Mainland China']/df_confirmed['Mainland China']*100,
mode='lines+markers',
line_shape='spline',
name='Mainland China',
line=dict(color='#626262', width=4),
marker=dict(size=4, color='#f4f4f2',
line=dict(width=1,color='#626262')),
text=[datetime.strftime(d, '%b %d %Y AEDT') for d in df_deaths['Date'].fillna(pd.Timestamp('20200101'))],
hovertext=['Mainland China death rate<br>{:.2f}%'.format(float(i)) for i in (df_deaths['Mainland China']/df_confirmed['Mainland China']).fillna(0)*100],
hovertemplate='<b>%{text}</b><br>'+
'%{hovertext}'+
'<extra></extra>'))
fig_rate.add_trace(go.Scatter(x=df_deaths['Date'], y=df_deaths['Other locations']/df_confirmed['Other locations']*100,
mode='lines+markers',
line_shape='spline',
name='Other Region',
line=dict(color='#a7a7a7', width=4),
marker=dict(size=4, color='#f4f4f2',
line=dict(width=1,color='#a7a7a7')),
text=[datetime.strftime(d, '%b %d %Y AEDT') for d in df_deaths['Date'].fillna(pd.Timestamp('20200101'))],
hovertext=['Other region death rate<br>{:.2f}%'.format(float(i)) for i in (df_deaths['Other locations']/df_confirmed['Other locations']).fillna(0)*100],
hovertemplate='<b>%{text}</b><br>'+
'%{hovertext}'+
'<extra></extra>'))
# Customise layout
fig_rate.update_layout(
margin=go.layout.Margin(
l=10,
r=10,
b=10,
t=5,
pad=0
),
yaxis=dict(
showline=False, linecolor='#272e3e',
zeroline=False,
#showgrid=False,
gridcolor='rgba(203, 210, 211,.3)',