-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyapp.py
1352 lines (1090 loc) · 70.5 KB
/
myapp.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
from flask import Flask, jsonify, render_template
import plotly
import plotly.graph_objs as go
import json
from flask import Flask, jsonify
import plotly
import plotly.express as px
import json
from plotly.subplots import make_subplots
app = Flask(__name__)
import pandas as pd
import numpy as np
rawData = pd.read_csv("/home/kubilaykaratopcu/ORNT/rawData.csv")
print(rawData.columns)
rawData = rawData[[
"DisNo.",
"Historic",
"Classification Key",
"Disaster Group",
"Disaster Subgroup",
"Disaster Type",
"Disaster Subtype",
"ISO",
"Country",
"Subregion",
"Region",
"Location",
"AID Contribution ('000 US$)",
"Magnitude",
"Magnitude Scale",
"Latitude",
"Longitude",
"Start Year",
"Start Month",
"Start Day",
"End Year",
"End Month",
"End Day",
"Total Deaths",
"No. Injured",
"No. Affected",
"No. Homeless",
"Total Affected",
"Total Damage ('000 US$)",
"Total Damage, Adjusted ('000 US$)",
"CPI"
]]
rawData.columns = [
"DisNo",
"Historic",
"Classification_Key",
"Disaster_Group",
"Disaster_Subgroup",
"Disaster_Type",
"Disaster_Subtype",
"ISO",
"Country",
"Subregion",
"Region",
"Location",
"AID_Contribution",
"Magnitude",
"Magnitude_Scale",
"Latitude",
"Longitude",
"Start_Year",
"Start_Month",
"Start_Day",
"End_Year",
"End_Month",
"End_Day",
"Total_Deaths",
"No_Injured",
"No_Affected",
"No_Homeless",
"Total_Affected",
"Total_Damage",
"Total_Damage_Adjusted",
"CPI"
]
rawData["Start_Day"] = rawData["Start_Month"].fillna(1)
rawData["Start_Month"] = rawData["Start_Month"].fillna(1)
rawData["Start_Day"] = rawData["Start_Day"].astype(int).astype(str)
rawData["Start_Month"] = rawData["Start_Month"].astype(int).astype(str)
rawData["Start_Year"] = rawData["Start_Year"].astype(int).astype(str)
rawData["End_Day"] = rawData.apply(lambda row: 1 if np.isnan(row["End_Day"]) else row["End_Day"],axis = 1)
rawData["End_Month"] = rawData.apply(lambda row: 12 if np.isnan(row["End_Month"]) else row["End_Month"],axis = 1)
rawData["End_Day"] = rawData["End_Day"].astype(int).astype(str)
rawData["End_Month"] = rawData["End_Month"].astype(int).astype(str)
rawData["End_Year"] = rawData["End_Year"].astype(int).astype(str)
rawData["Start_Date"] = rawData.apply(lambda row: pd.to_datetime(f"{row['Start_Day'] + '/' + row['Start_Month'] + '/' + row['Start_Year']}", format="%d/%m/%Y"), axis = 1)
rawData["End_Date"] = rawData.apply(lambda row: pd.to_datetime(f"{row['End_Day'] + '/' + row['End_Month'] + '/' + row['End_Year']}", format="%d/%m/%Y"), axis = 1)
rawData["Start_Day"] = rawData["Start_Day"].astype(int)
rawData["Start_Month"] = rawData["Start_Month"].astype(int)
rawData["Start_Year"] = rawData["Start_Year"].astype(int)
# EDA
for elm in ["Disaster_Group", "Disaster_Subgroup", "Disaster_Type", "Disaster_Subtype"]:
print(f"{elm}:", rawData[elm].unique(), end = "\n\n")
rawData = rawData.loc[rawData["Disaster_Group"] == "Natural"] # We only take 'Natural' disasters.
disCount = rawData.shape[0]
print("Number of natural disasters", disCount)
for elm in ["Disaster_Group", "Disaster_Subgroup", "Disaster_Type", "Disaster_Subtype"]:
print(f"{elm}:", rawData[elm].unique(), end = "\n\n")
print("Number of countries in the dataset:", rawData['Country'].unique().shape[0], end = "\n\n")
print("Number of sub-regions in the dataset:", rawData['Subregion'].unique().shape[0])
print("Subregions:", rawData['Subregion'].unique(), end = "\n\n")
print("Number of regions in the dataset:", rawData['Region'].unique().shape[0])
print("Subregions:", rawData['Region'].unique(), end = "\n\n")
print("Date Range:", rawData["Start_Date"].min(), rawData["Start_Date"].max())
# BURADA HANGI KOLONU TARGET FEATURE OLARAK BELIRLIYORUZ ONU SECIYORUZ TOTAL_DAMAGE KOLONU YÜZDE 70 BOŞ OLDUĞU İÇİN DİĞER KOLONLARI KULLANCAĞIZ.
print("Percentage of None in Total_Damage", round(sum(rawData["Total_Damage"].isna()) / disCount, 2))
print("Percentage of None in Total_Affected", round(sum(rawData["Total_Affected"].isna()) / disCount, 2))
print("Percentage of None in Total_Deaths", round(sum(rawData["Total_Deaths"].isna()) / disCount, 2))
rawData["Decade"] = rawData['Start_Year'].astype(int)//10*10
# BURASINDA SADECE DAMAGE'I İFADE EDEBİLDİĞİMİZ DISASTERLARI ALIYORUZ VE EXTRA-TERRESTRIALLARI DROPLUYORUM ÇÜNKÜ SAMPLE SIZE AZ.
filteredData = rawData.copy()
filteredData = filteredData.loc[~(filteredData["Total_Damage"].isna()) | (~filteredData["Total_Deaths"].isna())]
filteredData = filteredData.loc[filteredData["Disaster_Subgroup"] != "Extra-terrestrial"]
disasterMap = {}
for disasterGroup in filteredData["Disaster_Group"].unique():
if disasterGroup not in disasterMap:
disasterMap[disasterGroup] = {}
for disasterSubgroup in filteredData[filteredData["Disaster_Group"] == disasterGroup]["Disaster_Subgroup"].unique():
if disasterSubgroup not in disasterMap[disasterGroup]:
disasterMap[disasterGroup][disasterSubgroup] = {}
for disasterType in filteredData[(filteredData["Disaster_Group"] == disasterGroup) & (filteredData["Disaster_Subgroup"] == disasterSubgroup)]["Disaster_Type"].unique():
if disasterType not in disasterMap[disasterGroup][disasterSubgroup]:
disasterMap[disasterGroup][disasterSubgroup][disasterType] = []
for disasterSubType in filteredData[(filteredData["Disaster_Group"] == disasterGroup) & (filteredData["Disaster_Subgroup"] == disasterSubgroup) & (filteredData["Disaster_Type"] == disasterType)]["Disaster_Subtype"].unique():
disasterMap[disasterGroup][disasterSubgroup][disasterType].append(disasterSubType)
# Using this map we can see unique disaster groups and types such that
print(disasterMap["Natural"]["Geophysical"]["Earthquake"])
decadeDeaths_ByDisaster_Group = filteredData.groupby(["Disaster_Group","Decade"])["Total_Deaths"].sum().reset_index()
decadeDeaths_ByDisaster_SubGroup = filteredData.groupby(["Disaster_Subgroup","Decade"])["Total_Deaths"].sum().reset_index()
decadeDeaths_ByDisaster_Type = filteredData.groupby(["Disaster_Type","Decade"])["Total_Deaths"].sum().reset_index()
yearlyDeaths_ByDisaster_Group = filteredData.groupby(["Disaster_Group","Start_Year"])["Total_Deaths"].sum().reset_index()
yearlyDeaths_ByDisaster_SubGroup = filteredData.groupby(["Disaster_Subgroup","Start_Year"])["Total_Deaths"].sum().reset_index()
yearlyDeaths_ByDisaster_Type = filteredData.groupby(["Disaster_Type","Start_Year"])["Total_Deaths"].sum().reset_index()
yearlyDeaths_ByDisaster_Type_Regional = filteredData.groupby(["Disaster_Type","Region","Decade"])["Total_Deaths"].sum().reset_index()
yearlyEvents = rawData.groupby(["Start_Year"])["DisNo"].count()
yearlyEvents = yearlyEvents.reset_index()
yearlyEvents_ByDisaster_Type = rawData.groupby(["Disaster_Type","Start_Year"])["DisNo"].count()
decadelEvents_ByDisaster_Type = rawData.groupby(["Disaster_Type","Decade"])["DisNo"].count()
yearlyEvents_ByDisaster_Type = yearlyEvents_ByDisaster_Type.reset_index()
decadelEvents_ByDisaster_Type = decadelEvents_ByDisaster_Type.reset_index()
yearlyRegionalEvents_ByDisasterGroup = filteredData.groupby(["Disaster_Group","ISO","Country"])["DisNo"].count().reset_index()
yearlyRegionalDeaths_ByDisasterGroup = filteredData.groupby(["Start_Year","Disaster_Group","ISO","Country"])["Total_Deaths"].sum().reset_index()
decadelRegionalEvents_ByDisasterGroup = filteredData.groupby(["Decade","Disaster_Group","ISO","Country"])["DisNo"].count().reset_index()
regionalDeaths_ByDisasterGroup = filteredData.groupby(["Disaster_Group","ISO","Country"])["Total_Deaths"].sum().reset_index()
decadelRegionalDeaths_ByDisasterGroup = filteredData.groupby(["Decade","Disaster_Group","ISO","Country"])["Total_Deaths"].sum().reset_index()
regionalDeaths_ByDisasterGroup["Total_Deaths"] = np.log(regionalDeaths_ByDisasterGroup["Total_Deaths"])
grouped_counts = filteredData.groupby(["Disaster_Group", "ISO", "Country", "Disaster_Type"]).size().reset_index(name='count')
sorted_counts = grouped_counts.sort_values(by=["Disaster_Type", "count"], ascending=False)
top_5_rows_per_type = sorted_counts.groupby("Disaster_Type").head(5)
subFilteredData = filteredData.loc[filteredData.Disaster_Type.isin(["Earthquake","Flood","Storm"])]
#################################### DEVELOPMENT PART ####################################
hdi = pd.read_csv("/home/kubilaykaratopcu/ORNT/hdr.csv")
melted_df = pd.melt(hdi, id_vars=['iso3', 'country', 'hdicode', 'region'],
var_name='indicator', value_name='value')
melted_df["indicator"],melted_df["year"] = zip(*melted_df["indicator"].apply(lambda x: ("_".join(x.split("_")[:-1]), x.split("_")[-1])))
hdiFormatted = melted_df.pivot(index=['iso3',"country","hdicode","region","year"], columns="indicator", values='value').reset_index()
hdiFormatted.year = hdiFormatted.year.astype(int)
subFilteredData = pd.merge(subFilteredData, hdiFormatted, left_on=["ISO","Start_Year"], right_on=["iso3","year"], suffixes=('','_GDP'), how="left")
subFilteredData = subFilteredData.loc[subFilteredData.Start_Year > 1990]
floodDataAll = subFilteredData.loc[(subFilteredData["Disaster_Type"] =="Flood")]
floodData = subFilteredData.loc[(subFilteredData["Disaster_Type"] =="Flood") & (subFilteredData["ISO"].isin(["CHN","IND","IDN","BRA","USA"]))].copy()
stormDataAll = subFilteredData.loc[(subFilteredData["Disaster_Type"] =="Storm")]
stormData = subFilteredData.loc[(subFilteredData["Disaster_Type"] =="Storm") & (subFilteredData["ISO"].isin(["USA","PHL","CHN","IND","JPN"]))].copy()
earthquakeAll = subFilteredData.loc[(subFilteredData["Disaster_Type"] =="Earthquake")]
earthquake = subFilteredData.loc[(subFilteredData["Disaster_Type"] =="Earthquake") & (subFilteredData["ISO"].isin(["CHN","IDN","IRN","TUR","JPN"]))].copy()
groupedFloodData = floodDataAll.groupby(["ISO","Country"]).agg({"Total_Deaths":"sum", "gii": "mean", "gdi":"mean", "hdi":"mean", "le":"mean","gnipc":"mean","Disaster_Group":"count"}).reset_index()
groupedFloodData.columns = ["ISO","Country","Total_Deaths","gii","gdi","hdi","le","gnipc","Total_Event_Count"]
groupedFloodData["deathPerEvent"] = groupedFloodData["Total_Deaths"] / groupedFloodData["Total_Event_Count"]
groupedFloodData['deathPerEvent_log'] = np.log(groupedFloodData['deathPerEvent'] + 1) # Adding 1 to avoid log(0) or log(negative)
groupedFloodData['Total_Deaths_Sqrt'] = np.sqrt(groupedFloodData['Total_Deaths'] + 1) # Adding 1 to avoid log(0) or log(negative)
groupedEarthquakeData = earthquakeAll.groupby(["ISO","Country","Disaster_Subtype"]).agg({"Total_Deaths":"sum", "gii": "mean", "gdi":"mean", "hdi":"mean", "le":"mean", "gnipc":"mean", "Disaster_Group":"count"}).reset_index()
groupedEarthquakeData.columns = ["ISO","Country","Disaster_Subtype","Total_Deaths","gii","gdi","hdi","le","gnipc","Total_Event_Count"]
groupedEarthquakeData["deathPerEvent"] = groupedEarthquakeData["Total_Deaths"] / groupedEarthquakeData["Total_Event_Count"]
groupedEarthquakeData['deathPerEvent_log'] = np.log(groupedEarthquakeData['deathPerEvent'] + 1) # Adding 1 to avoid log(0) or log(negative)
groupedEarthquakeData['Total_Deaths_Sqrt'] = np.sqrt(groupedEarthquakeData['Total_Deaths'] + 1) # Adding 1 to avoid log(0) or log(negative)
groupedEarthquakeDataTsunami = earthquakeAll.groupby(["ISO","Country"]).agg({"Total_Deaths":"sum", "gii": "mean", "gdi":"mean", "hdi":"mean", "le":"mean", "gnipc":"mean", "Disaster_Group":"count"}).reset_index()
groupedEarthquakeDataTsunami.columns = ["ISO","Country","Total_Deaths","gii","gdi","hdi","le","gnipc","Total_Event_Count"]
groupedEarthquakeDataTsunami["deathPerEvent"] = groupedEarthquakeDataTsunami["Total_Deaths"] / groupedEarthquakeDataTsunami["Total_Event_Count"]
groupedEarthquakeDataTsunami['deathPerEvent_log'] = np.log(groupedEarthquakeDataTsunami['deathPerEvent'] + 1) # Adding 1 to avoid log(0) or log(negative)
groupedEarthquakeDataTsunami['Total_Deaths_Sqrt'] = np.sqrt(groupedEarthquakeDataTsunami['Total_Deaths'] + 1) # Adding 1 to avoid log(0) or log(negative)
groupedStormData = stormDataAll.groupby(["ISO","Country"]).agg({"Total_Deaths":"sum", "gii": "mean", "gdi":"mean","hdi":"mean", "le":"mean","gnipc":"mean","Disaster_Group":"count"}).reset_index()
groupedStormData.columns = ["ISO","Country","Total_Deaths","gii","gdi","hdi","le","gnipc","Total_Event_Count"]
groupedStormData["deathPerEvent"] = groupedStormData["Total_Deaths"] / groupedFloodData["Total_Event_Count"]
groupedStormData['deathPerEvent_log'] = np.log(groupedStormData['deathPerEvent'] + 1) # Adding 1 to avoid log(0) or log(negative)
groupedStormData['deathPerEventSqrt'] = np.sqrt(groupedStormData['deathPerEvent'] + 1)
groupedStormData['Total_Deaths_Sqrt'] = np.sqrt(groupedStormData['Total_Deaths'] + 1) # Adding 1 to avoid log(0) or log(negative)
def ytick_formatter(x, pos):
if x >= 1e6:
return f'{x / 1e6:.1f}M'
elif x>= 1e3:
return f'{x / 1e3:.0f}K'
else:
return f'{x:.0f}'
@app.route('/')
def homepage():
return render_template('index.html')
@app.route('/get-plots')
def get_plots():
###########################################################################################################################################
fig1 = px.line(decadeDeaths_ByDisaster_Group, x='Decade', y='Total_Deaths', markers=True, title='Total Loss By Decade')
fig1.update_layout(xaxis_title='Decade', yaxis_title='Total Loss')
fig1.update_yaxes(ticklabelposition="inside", tickformat=',d')
fig1.update_layout(width=800, height=600)
plot1_json = json.dumps(fig1, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
fig2 = px.bar(decadeDeaths_ByDisaster_Type[decadeDeaths_ByDisaster_Type.Decade >= 1970], x='Decade', y='Total_Deaths', color='Disaster_Type', title='Total Loss By Disaster Type')
fig2.update_layout(xaxis_title='Year', yaxis_title='Total Loss', legend_title='Category')
fig2.update_yaxes(ticklabelposition="inside", tickformat=',d')
fig2.update_layout(width=800, height=600)
plot2_json = json.dumps(fig2, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
fig3 = px.bar(yearlyEvents[yearlyEvents.Start_Year >= 1970], x='Start_Year', y='DisNo', title='Total Number of Events By Year')
fig3.update_layout(xaxis_title='Year', yaxis_title='Total Count')
fig3.update_yaxes(ticklabelposition="inside", tickformat=',d')
fig3.update_layout(width=1080, height=600)
plot3_json = json.dumps(fig3, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
fig4 = px.pie(filteredData, names='Region', title='Proportion Of Region', hole=0.25)
fig4.update_layout(width=540, height=600)
plot4_json = json.dumps(fig4, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
fig5 = px.pie(filteredData, names='Disaster_Type', title='Proportion Of Disaster_Type', hole=0.25)
fig5.update_layout(width=540, height=600)
plot5_json = json.dumps(fig5, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
fig6 = px.choropleth(yearlyRegionalEvents_ByDisasterGroup, locations="ISO",
color="DisNo",
hover_name="Country",
color_continuous_scale=px.colors.sequential.Plasma)
fig6.update_layout(title_text='Total Regional Events by Disaster Group')
fig6.update_layout(width=1080, height=900)
plot6_json = json.dumps(fig6, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
fig7 = px.choropleth(regionalDeaths_ByDisasterGroup, locations="ISO",
color="Total_Deaths",
hover_name="Country",
color_continuous_scale=px.colors.sequential.Plasma)
fig7.update_traces(hoverinfo="location+text+z")
fig7.update_layout(title_text='Total Regional Deaths (log) by Disaster Group')
fig7.update_layout(width=1080, height=900)
plot7_json = json.dumps(fig7, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
year = 1900
scl = [[0.0, '#ffffff'],[0.2, '#b4a8ce'],[0.4, '#8573a9'],
[0.6, '#7159a3'],[0.8, '#5732a1'],[1.0, '#2c0579']] # purples
data_slider = []
for idx, year in enumerate(sorted(yearlyRegionalDeaths_ByDisasterGroup['Start_Year'].unique())):
df_segmented = yearlyRegionalDeaths_ByDisasterGroup[(yearlyRegionalDeaths_ByDisasterGroup['Start_Year'] == year)]
for col in df_segmented.columns:
df_segmented[col] = df_segmented[col].astype(str)
data_each_yr = dict(
type='choropleth',
locations=df_segmented['ISO'],
z=df_segmented['Total_Deaths'].astype(float),
colorscale=scl,
colorbar={'title':'# Total Deaths'},
name=str(year),
legendgroup=str(year),
visible=(idx == 0)) # Only the first dataset is visible
data_slider.append(data_each_yr)
steps = []
for i, year in enumerate(sorted(yearlyRegionalDeaths_ByDisasterGroup['Start_Year'].unique())):
step = dict(
method='restyle',
args=['visible', [False] * len(data_slider)],
label=f'Year {year}'
)
step['args'][1][i] = True # Make only the i-th dataset visible
steps.append(step)
sliders = [dict(active=0, pad={"t": 1}, steps=steps)]
layout = dict(title='Yearly Deaths',
geo=dict(scope='world', projection={'type': 'winkel tripel'}),
sliders=sliders,
width=1080,
height=900)
fig8 = dict(data=data_slider, layout=layout)
plot8_json = json.dumps(fig8, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
year = 1900
scl = [[0.0, '#ffffff'],[0.2, '#b4a8ce'],[0.4, '#8573a9'],
[0.6, '#7159a3'],[0.8, '#5732a1'],[1.0, '#2c0579']] # purples
data_slider = []
for idx, year in enumerate(decadelRegionalDeaths_ByDisasterGroup['Decade'].unique()):
df_segmented = decadelRegionalDeaths_ByDisasterGroup[(decadelRegionalDeaths_ByDisasterGroup['Decade']== year)].copy()
for col in df_segmented.columns:
df_segmented[col] = df_segmented[col].astype(str)
data_each_yr = dict(
type='choropleth',
locations = df_segmented['ISO'],
z=df_segmented['Total_Deaths'].astype(float),
colorscale = scl,
colorbar= {'title':'# Total Deaths'},
name=str(year),
legendgroup=str(year),
visible=(idx == 0)) # Only the first dataset is visible
data_slider.append(data_each_yr)
steps = []
for i, year in enumerate(decadelRegionalDeaths_ByDisasterGroup['Decade'].unique()):
step = dict(
method='restyle',
args=['visible', [False] * len(data_slider)],
label=f'Year {year}'
)
step['args'][1][i] = True # Make only the i-th dataset visible
steps.append(step)
sliders = [dict(active=0, pad={"t": 1}, steps=steps)]
layout = dict(title='Decadel Deaths',
geo=dict(scope='world', projection={'type': 'winkel tripel'}),
sliders=sliders,
width=1080,
height=900)
fig9 = dict(data=data_slider, layout=layout)
plot9_json = json.dumps(fig9, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
sample_df = top_5_rows_per_type.sample(20)
# Create the table figure
table_fig = go.Figure(data=[go.Table(
header=dict(values=list(sample_df.columns),
fill_color='paleturquoise',
align='left'),
cells=dict(values=[sample_df[col] for col in sample_df.columns],
fill_color='lavender',
align='left'))
])
table_fig.update_layout(width=1080, height=720)
# Convert the figure to JSON
table1_json = json.dumps(table_fig, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
fig10 = px.scatter(groupedFloodData, x='gii', y='deathPerEvent_log', trendline='ols',
title='Scatter Plot of GII vs DeathPerEventLog with Regression Line', size='Total_Event_Count',
hover_name='Country')
fig10.update_xaxes(title_text='GII')
fig10.update_yaxes(title_text='Death Per Event (log)')
fig10.update_traces(name='Regression Line') # Name the trendline for the legend
fig10.update_layout(width=1080, height=900)
plot10_json = json.dumps(fig10, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
fig11 = px.histogram(groupedFloodData, x='Total_Event_Count',
title='Histogram of Total Flood Event Count',
labels={'Total_Event_Count': 'Total Event Count', 'count': 'Frequency'})
fig11.update_layout(width=1080, height=600)
plot11_json = json.dumps(fig11, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
# Creating a 2x3 grid for subplots with 1 scatter plot on top and 3 small histograms below
fig12 = make_subplots(rows=2, cols=3,
#subplot_titles=('GII vs DeathPerEventLog', '', '', 'Event Count (Lower Third GII)', 'Event Count (Middle Third GII)', 'Event Count (Upper Third GII)'),
specs=[[{"colspan": 3, "type": "scatter"}, None, None], [{}, {}, {}]],
row_heights=[0.7, 0.3])
plotData = groupedFloodData
# Scatter plot (row 1, spans all columns)
scatter = px.scatter(groupedFloodData, x='gii', y='deathPerEvent_log', size='Total_Event_Count', hover_name='Country', trendline="ols")
for trace in scatter.data:
fig12.add_trace(trace, row=1, col=1)
# Preparing data for histograms
gii_thirds = groupedFloodData['gii'].quantile([1/3, 2/3]).values
# Histogram for lower third GII (row 2, col 1)
fig12.add_trace(go.Histogram(x=groupedFloodData[groupedFloodData['gii'] <= gii_thirds[0]]['Total_Event_Count'], nbinsx=20), row=2, col=1)
# Histogram for middle third GII (row 2, col 2)
fig12.add_trace(go.Histogram(x=groupedFloodData[(groupedFloodData['gii'] > gii_thirds[0]) & (groupedFloodData['gii'] <= gii_thirds[1])]['Total_Event_Count'], nbinsx=20), row=2, col=2)
# Histogram for upper third GII (row 2, col 3)
fig12.add_trace(go.Histogram(x=groupedFloodData[groupedFloodData['gii'] > gii_thirds[1]]['Total_Event_Count'], nbinsx=20), row=2, col=3)
# Update layout and axis labels
fig12.update_yaxes(domain=[0.20, 1], row=1, col=1)
fig12.update_yaxes(domain=[0.05, 0.15], row=2, col=1)
fig12.update_yaxes(domain=[0.05, 0.15], row=2, col=2)
fig12.update_yaxes(domain=[0.05, 0.15], row=2, col=3)
#fig.update_xaxes(title_text='GII', row=1, col=1)
fig12.update_yaxes(title_text='Death Per Event (log)', row=1, col=1)
fig12.update_xaxes(title_text='Event Count (Lower Third GII)', row=2, col=1)
fig12.update_xaxes(title_text='Event Count (Middle Third GII)', row=2, col=2)
fig12.update_xaxes(title_text='Event Count (Upper Third GII)', row=2, col=3)
fig12.update_yaxes(title_text='Frequency', row=2, col=1)
fig12.update_layout(width=1080, height=900, showlegend=False)
india_data = plotData[plotData['Country'] == 'India'].iloc[0]
usa_data = plotData[plotData['Country'] == 'United States of America'].iloc[0]
afghanhistan_data = plotData[plotData['Country'] == 'Afghanistan'].iloc[0]
china_data = plotData[plotData['Country'] == 'China'].iloc[0]
# Add annotations with arrows pointing to India and USA
fig12.add_annotation(
x=india_data['gii'], y=india_data['deathPerEvent_log'],
xref="x", yref="y",
text="India",
showarrow=True,
arrowhead=1,
ax=20, # Adjust these values to change the arrow's position
ay=-30, # Adjust these values to change the arrow's position
row=1, col=1 # Specify the subplot to add this annotation to
)
fig12.add_annotation(
x=usa_data['gii'], y=usa_data['deathPerEvent_log'],
xref="x", yref="y",
text="USA",
showarrow=True,
arrowhead=1,
ax=-20, # Adjust these values to change the arrow's position
ay=-30, # Adjust these values to change the arrow's position
row=1, col=1 # Specify the subplot to add this annotation to
)
fig12.add_annotation(
x=afghanhistan_data['gii'], y=afghanhistan_data['deathPerEvent_log'],
xref="x", yref="y",
text="Afghanistan",
showarrow=True,
arrowhead=1,
ax=-20, # Adjust these values to change the arrow's position
ay=-30, # Adjust these values to change the arrow's position
row=1, col=1 # Specify the subplot to add this annotation to
)
fig12.add_annotation(
x=china_data['gii'], y=china_data['deathPerEvent_log'],
xref="x", yref="y",
text="China",
showarrow=True,
arrowhead=1,
ax=-20, # Adjust these values to change the arrow's position
ay=-30, # Adjust these values to change the arrow's position
row=1, col=1 # Specify the subplot to add this annotation to
)
fig12.update_layout(title_text='GII vs Death Per Flood Event (All Countries)')
plot12_json = json.dumps(fig12, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
# Creating a 2x3 grid for subplots with 1 scatter plot on top and 3 small histograms below
fig13 = make_subplots(rows=2, cols=3,
#subplot_titles=('GII vs DeathPerEventLog', '', '', 'Event Count (Lower Third GII)', 'Event Count (Middle Third GII)', 'Event Count (Upper Third GII)'),
specs=[[{"colspan": 3, "type": "scatter"}, None, None], [{}, {}, {}]],
row_heights=[0.7, 0.3])
plotData = groupedFloodData.loc[(groupedFloodData.Total_Event_Count > 20)]
# Scatter plot (row 1, spans all columns)
scatter = px.scatter(plotData, x='gii', y='deathPerEvent_log', size='Total_Event_Count', hover_name='Country', trendline="ols")
for trace in scatter.data:
fig13.add_trace(trace, row=1, col=1)
# Preparing data for histograms
gii_thirds = plotData['gii'].quantile([1/3, 2/3]).values
# Histogram for lower third GII (row 2, col 1)
fig13.add_trace(go.Histogram(x=plotData[plotData['gii'] <= gii_thirds[0]]['Total_Event_Count'], nbinsx=20), row=2, col=1)
# Histogram for middle third GII (row 2, col 2)
fig13.add_trace(go.Histogram(x=plotData[(plotData['gii'] > gii_thirds[0]) & (plotData['gii'] <= gii_thirds[1])]['Total_Event_Count'], nbinsx=20), row=2, col=2)
# Histogram for upper third GII (row 2, col 3)
fig13.add_trace(go.Histogram(x=plotData[plotData['gii'] > gii_thirds[1]]['Total_Event_Count'], nbinsx=20), row=2, col=3)
# Update layout and axis labels
fig13.update_layout(width=600, height=900, showlegend=False)
fig13.update_yaxes(domain=[0.20, 1], row=1, col=1)
fig13.update_yaxes(domain=[0.05, 0.15], row=2, col=1)
fig13.update_yaxes(domain=[0.05, 0.15], row=2, col=2)
fig13.update_yaxes(domain=[0.05, 0.15], row=2, col=3)
#fig.update_xaxes(title_text='GII', row=1, col=1)
fig13.update_yaxes(title_text='Death Per Event (log)', row=1, col=1)
fig13.update_xaxes(title_text='Lower Third GII', row=2, col=1)
fig13.update_xaxes(title_text='Middle Third GII', row=2, col=2)
fig13.update_xaxes(title_text='Upper Third GII', row=2, col=3)
fig13.update_yaxes(title_text='Frequency', row=2, col=1)
india_data = plotData[plotData['Country'] == 'India'].iloc[0]
usa_data = plotData[plotData['Country'] == 'United States of America'].iloc[0]
afghanhistan_data = plotData[plotData['Country'] == 'Afghanistan'].iloc[0]
china_data = plotData[plotData['Country'] == 'China'].iloc[0]
# Add annotations with arrows pointing to India and USA
fig13.add_annotation(
x=india_data['gii'], y=india_data['deathPerEvent_log'],
xref="x", yref="y",
text="India",
showarrow=True,
arrowhead=1,
ax=20, # Adjust these values to change the arrow's position
ay=-30, # Adjust these values to change the arrow's position
row=1, col=1 # Specify the subplot to add this annotation to
)
fig13.add_annotation(
x=usa_data['gii'], y=usa_data['deathPerEvent_log'],
xref="x", yref="y",
text="USA",
showarrow=True,
arrowhead=1,
ax=-20, # Adjust these values to change the arrow's position
ay=-30, # Adjust these values to change the arrow's position
row=1, col=1 # Specify the subplot to add this annotation to
)
fig13.add_annotation(
x=afghanhistan_data['gii'], y=afghanhistan_data['deathPerEvent_log'],
xref="x", yref="y",
text="Afghanistan",
showarrow=True,
arrowhead=1,
ax=-20, # Adjust these values to change the arrow's position
ay=-30, # Adjust these values to change the arrow's position
row=1, col=1 # Specify the subplot to add this annotation to
)
fig13.add_annotation(
x=china_data['gii'], y=china_data['deathPerEvent_log'],
xref="x", yref="y",
text="China",
showarrow=True,
arrowhead=1,
ax=-20, # Adjust these values to change the arrow's position
ay=-30, # Adjust these values to change the arrow's position
row=1, col=1 # Specify the subplot to add this annotation to
)
fig13.update_layout(title_text='GII vs Death Per Flood Event (Event# > 20)')
plot13_json = json.dumps(fig13, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
fig14 = make_subplots(rows=2, cols=3,
#subplot_titles=('GII vs DeathPerEventLog', '', '', 'Event Count (Lower Third GII)', 'Event Count (Middle Third GII)', 'Event Count (Upper Third GII)'),
specs=[[{"colspan": 3, "type": "scatter"}, None, None], [{}, {}, {}]],
row_heights=[0.7, 0.3])
plotData = groupedFloodData.loc[(groupedFloodData.Total_Event_Count > 20) & (~groupedFloodData.Country.isin(["China","India"]))]
# Scatter plot (row 1, spans all columns)
scatter = px.scatter(plotData, x='gii', y='deathPerEvent_log', size='Total_Event_Count', hover_name='Country', trendline="ols")
for trace in scatter.data:
fig14.add_trace(trace, row=1, col=1)
# Preparing data for histograms
gii_thirds = plotData['gii'].quantile([1/3, 2/3]).values
# Histogram for lower third GII (row 2, col 1)
fig14.add_trace(go.Histogram(x=plotData[plotData['gii'] <= gii_thirds[0]]['Total_Event_Count'], nbinsx=20), row=2, col=1)
# Histogram for middle third GII (row 2, col 2)
fig14.add_trace(go.Histogram(x=plotData[(plotData['gii'] > gii_thirds[0]) & (plotData['gii'] <= gii_thirds[1])]['Total_Event_Count'], nbinsx=20), row=2, col=2)
# Histogram for upper third GII (row 2, col 3)
fig14.add_trace(go.Histogram(x=plotData[plotData['gii'] > gii_thirds[1]]['Total_Event_Count'], nbinsx=20), row=2, col=3)
# Update layout and axis labels
fig14.update_layout(width=1080, height=900, showlegend=False)
fig14.update_yaxes(domain=[0.20, 1], row=1, col=1)
fig14.update_yaxes(domain=[0.05, 0.15], row=2, col=1)
fig14.update_yaxes(domain=[0.05, 0.15], row=2, col=2)
fig14.update_yaxes(domain=[0.05, 0.15], row=2, col=3)
#fig14.update_xaxes(title_text='GII', row=1, col=1)
fig14.update_yaxes(title_text='Death Per Event (log)', row=1, col=1)
fig14.update_xaxes(title_text='Lower Third GII', row=2, col=1)
fig14.update_xaxes(title_text='Middle Third GII', row=2, col=2)
fig14.update_xaxes(title_text='Upper Third GII', row=2, col=3)
fig14.update_yaxes(title_text='Frequency', row=2, col=1)
countries_data = {
#"India": plotData[plotData['Country'] == 'India'].iloc[0],
"USA": plotData[plotData['Country'] == 'United States of America'].iloc[0],
"Afghanistan": plotData[plotData['Country'] == 'Afghanistan'].iloc[0],
#"China": plotData[plotData['Country'] == 'China'].iloc[0],
}
for country, data in countries_data.items():
fig14.add_annotation(
x=data['gii'], y=data['deathPerEvent_log'],
xref="x", yref="y",
text=country,
showarrow=True,
arrowhead=1,
ax=20 if country != "USA" else -20, # Adjust these values based on the country's position
ay=-30,
row=1, col=1
)
fig13.update_layout(title_text='Not Used')
plot14_json = json.dumps(fig14, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
fig15 = make_subplots(rows=2, cols=3,
specs=[[{"colspan": 3, "type": "scatter"}, None, None], [{}, {}, {}]],
row_heights=[0.7, 0.3])
plotData = groupedFloodData
# Scatter plot (row 1, spans all columns) using 'hdi' instead of 'gii'
scatter = px.scatter(plotData, x='hdi', y='deathPerEvent_log', size='Total_Event_Count', hover_name='Country', trendline="ols")
for trace in scatter.data:
fig15.add_trace(trace, row=1, col=1)
# Preparing data for histograms based on 'hdi'
hdi_thirds = plotData['hdi'].quantile([1/3, 2/3]).values
# Histogram for lower third HDI (row 2, col 1)
fig15.add_trace(go.Histogram(x=plotData[plotData['hdi'] <= hdi_thirds[0]]['Total_Event_Count'], nbinsx=20), row=2, col=1)
# Histogram for middle third HDI (row 2, col 2)
fig15.add_trace(go.Histogram(x=plotData[(plotData['hdi'] > hdi_thirds[0]) & (plotData['hdi'] <= hdi_thirds[1])]['Total_Event_Count'], nbinsx=20), row=2, col=2)
# Histogram for upper third HDI (row 2, col 3)
fig15.add_trace(go.Histogram(x=plotData[plotData['hdi'] > hdi_thirds[1]]['Total_Event_Count'], nbinsx=20), row=2, col=3)
# Update layout and axis labels
fig15.update_layout(width=1080, height=900, showlegend=False)
fig15.update_yaxes(domain=[0.20, 1], row=1, col=1)
fig15.update_yaxes(domain=[0.05, 0.15], row=2, col=1)
fig15.update_yaxes(domain=[0.05, 0.15], row=2, col=2)
fig15.update_yaxes(domain=[0.05, 0.15], row=2, col=3)
fig15.update_yaxes(title_text='Death Per Event (log)', row=1, col=1)
fig15.update_xaxes(title_text='Event Count (Lower Third HDI)', row=2, col=1)
fig15.update_xaxes(title_text='Event Count (Middle Third HDI)', row=2, col=2)
fig15.update_xaxes(title_text='Event Count (Upper Third HDI)', row=2, col=3)
fig15.update_yaxes(title_text='Frequency', row=2, col=1)
# Annotations for specific countries
countries_data = {
"India": plotData[plotData['Country'] == 'India'].iloc[0],
"USA": plotData[plotData['Country'] == 'United States of America'].iloc[0],
"Afghanistan": plotData[plotData['Country'] == 'Afghanistan'].iloc[0],
"China": plotData[plotData['Country'] == 'China'].iloc[0],
}
for country, data in countries_data.items():
fig15.add_annotation(
x=data['hdi'], y=data['deathPerEvent_log'],
xref="x", yref="y",
text=country,
showarrow=True,
arrowhead=1,
ax=20 if country != "USA" else -20, # Adjust these values based on the country's position
ay=-30,
row=1, col=1
)
fig15.update_layout(title_text='Not Used')
plot15_json = json.dumps(fig15, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
fig16 = make_subplots(rows=2, cols=3,
specs=[[{"colspan": 3, "type": "scatter"}, None, None], [{}, {}, {}]],
row_heights=[0.7, 0.3])
plotData = groupedFloodData.loc[groupedFloodData.Total_Event_Count > 20]
# Scatter plot (row 1, spans all columns) using 'hdi' instead of 'gii'
scatter = px.scatter(plotData, x='hdi', y='deathPerEvent_log', size='Total_Event_Count', hover_name='Country', trendline="ols")
for trace in scatter.data:
fig16.add_trace(trace, row=1, col=1)
# Preparing data for histograms based on 'hdi'
hdi_thirds = plotData['hdi'].quantile([1/3, 2/3]).values
# Histogram for lower third HDI (row 2, col 1)
fig16.add_trace(go.Histogram(x=plotData[plotData['hdi'] <= hdi_thirds[0]]['Total_Event_Count'], nbinsx=20), row=2, col=1)
# Histogram for middle third HDI (row 2, col 2)
fig16.add_trace(go.Histogram(x=plotData[(plotData['hdi'] > hdi_thirds[0]) & (plotData['hdi'] <= hdi_thirds[1])]['Total_Event_Count'], nbinsx=20), row=2, col=2)
# Histogram for upper third HDI (row 2, col 3)
fig16.add_trace(go.Histogram(x=plotData[plotData['hdi'] > hdi_thirds[1]]['Total_Event_Count'], nbinsx=20), row=2, col=3)
# Update layout and axis labels
fig16.update_layout(width=600, height=900, showlegend=False)
fig16.update_yaxes(domain=[0.20, 1], row=1, col=1)
fig16.update_yaxes(domain=[0.05, 0.15], row=2, col=1)
fig16.update_yaxes(domain=[0.05, 0.15], row=2, col=2)
fig16.update_yaxes(domain=[0.05, 0.15], row=2, col=3)
fig16.update_yaxes(title_text='Death Per Event (log)', row=1, col=1)
fig16.update_xaxes(title_text='Lower Third HDI', row=2, col=1)
fig16.update_xaxes(title_text='Middle Third HDI', row=2, col=2)
fig16.update_xaxes(title_text='Upper Third HDI', row=2, col=3)
fig16.update_yaxes(title_text='Frequency', row=2, col=1)
# Annotations for specific countries
countries_data = {
"India": plotData[plotData['Country'] == 'India'].iloc[0],
"USA": plotData[plotData['Country'] == 'United States of America'].iloc[0],
"Afghanistan": plotData[plotData['Country'] == 'Afghanistan'].iloc[0],
"China": plotData[plotData['Country'] == 'China'].iloc[0],
}
for country, data in countries_data.items():
fig16.add_annotation(
x=data['hdi'], y=data['deathPerEvent_log'],
xref="x", yref="y",
text=country,
showarrow=True,
arrowhead=1,
ax=20 if country != "USA" else -20, # Adjust these values based on the country's position
ay=-30,
row=1, col=1
)
fig16.update_layout(title_text='HDI vs Death Per Flood Event (Event# > 20)')
plot16_json = json.dumps(fig16, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
fig17 = make_subplots(rows=2, cols=3,
specs=[[{"colspan": 3, "type": "scatter"}, None, None], [{}, {}, {}]],
row_heights=[0.7, 0.3])
plotData = groupedFloodData.loc[(groupedFloodData.Total_Event_Count > 20)]
# Scatter plot (row 1, spans all columns) using 'gnipc'
scatter = px.scatter(plotData, x='gnipc', y='deathPerEvent_log', size='Total_Event_Count', hover_name='Country', trendline="ols")
for trace in scatter.data:
fig17.add_trace(trace, row=1, col=1)
# Preparing data for histograms based on 'gnipc'
gnipc_thirds = plotData['gnipc'].quantile([1/3, 2/3]).values
# Histogram for lower third GNIPC (row 2, col 1)
fig17.add_trace(go.Histogram(x=plotData[plotData['gnipc'] <= gnipc_thirds[0]]['Total_Event_Count'], nbinsx=20), row=2, col=1)
# Histogram for middle third GNIPC (row 2, col 2)
fig17.add_trace(go.Histogram(x=plotData[(plotData['gnipc'] > gnipc_thirds[0]) & (plotData['gnipc'] <= gnipc_thirds[1])]['Total_Event_Count'], nbinsx=20), row=2, col=2)
# Histogram for upper third GNIPC (row 2, col 3)
fig17.add_trace(go.Histogram(x=plotData[plotData['gnipc'] > gnipc_thirds[1]]['Total_Event_Count'], nbinsx=20), row=2, col=3)
# Update layout and axis labels
fig17.update_layout(width=600, height=900, showlegend=False)
fig17.update_yaxes(domain=[0.20, 1], row=1, col=1)
fig17.update_yaxes(domain=[0.05, 0.15], row=2, col=1)
fig17.update_yaxes(domain=[0.05, 0.15], row=2, col=2)
fig17.update_yaxes(domain=[0.05, 0.15], row=2, col=3)
fig17.update_yaxes(title_text='Death Per Event (log)', row=1, col=1)
fig17.update_xaxes(title_text='Lower Third GNIPC', row=2, col=1)
fig17.update_xaxes(title_text='Middle Third GNIPC', row=2, col=2)
fig17.update_xaxes(title_text='Upper Third GNIPC', row=2, col=3)
fig17.update_yaxes(title_text='Frequency', row=2, col=1)
countries_data = {
"India": plotData[plotData['Country'] == 'India'].iloc[0],
"USA": plotData[plotData['Country'] == 'United States of America'].iloc[0],
"Afghanistan": plotData[plotData['Country'] == 'Afghanistan'].iloc[0],
"China": plotData[plotData['Country'] == 'China'].iloc[0],
}
# Annotations for specific countries using 'gnipc'
for country, data in countries_data.items():
fig17.add_annotation(
x=data['gnipc'], y=data['deathPerEvent_log'],
xref="x", yref="y",
text=country,
showarrow=True,
arrowhead=1,
ax=20 if country != "Phillippines" else 90, # Adjust these values based on the country's position
ay=-30,
row=1, col=1
)
fig17.update_layout(title_text='GNIPC vs Death Per Flood Event (Event# > 20)')
plot17_json = json.dumps(fig17, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
fig18 = px.histogram(groupedEarthquakeDataTsunami, x='Total_Event_Count',
title='Histogram of Total Earthquake Event Count',
labels={'Total_Event_Count': 'Total Event Count', 'count': 'Frequency'})
fig18.update_layout(width=1080, height=600)
plot18_json = json.dumps(fig18, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
fig19 = make_subplots(rows=2, cols=3,
specs=[[{"colspan": 3, "type": "scatter"}, None, None], [{}, {}, {}]],
row_heights=[0.7, 0.3])
plotData = groupedEarthquakeDataTsunami.loc[(groupedEarthquakeDataTsunami.Total_Event_Count > 10)]
# Scatter plot (row 1, spans all columns) using 'hdi' instead of 'gii'
scatter = px.scatter(plotData, x='hdi', y='deathPerEvent_log', size='Total_Event_Count', hover_name='Country', trendline="ols")
for trace in scatter.data:
fig19.add_trace(trace, row=1, col=1)
# Preparing data for histograms based on 'hdi'
hdi_thirds = plotData['hdi'].quantile([1/3, 2/3]).values
# Histogram for lower third HDI (row 2, col 1)
fig19.add_trace(go.Histogram(x=plotData[plotData['hdi'] <= hdi_thirds[0]]['Total_Event_Count'], nbinsx=20), row=2, col=1)
# Histogram for middle third HDI (row 2, col 2)
fig19.add_trace(go.Histogram(x=plotData[(plotData['hdi'] > hdi_thirds[0]) & (plotData['hdi'] <= hdi_thirds[1])]['Total_Event_Count'], nbinsx=20), row=2, col=2)
# Histogram for upper third HDI (row 2, col 3)
fig19.add_trace(go.Histogram(x=plotData[plotData['hdi'] > hdi_thirds[1]]['Total_Event_Count'], nbinsx=20), row=2, col=3)
# Update layout and axis labels
fig19.update_layout(width=1080, height=900, showlegend=False)
fig19.update_yaxes(domain=[0.20, 1], row=1, col=1)
fig19.update_yaxes(domain=[0.05, 0.15], row=2, col=1)
fig19.update_yaxes(domain=[0.05, 0.15], row=2, col=2)
fig19.update_yaxes(domain=[0.05, 0.15], row=2, col=3)
fig19.update_yaxes(title_text='Death Per Event (log)', row=1, col=1)
fig19.update_xaxes(title_text='Lower Third HDI', row=2, col=1)
fig19.update_xaxes(title_text='Middle Third HDI', row=2, col=2)
fig19.update_xaxes(title_text='Upper Third HDI', row=2, col=3)
fig19.update_yaxes(title_text='Frequency', row=2, col=1)
# Annotations for specific countries
countries_data = {
"Indonesia": plotData[plotData['Country'] == 'Indonesia'].iloc[0],
"USA": plotData[plotData['Country'] == 'United States of America'].iloc[0],
"Japan": plotData[plotData['Country'] == 'Japan'].iloc[0],
"China": plotData[plotData['Country'] == 'China'].iloc[0],
"Türkiye": plotData[plotData['Country'] == 'Türkiye'].iloc[0],
}
for country, data in countries_data.items():
fig19.add_annotation(
x=data['hdi'], y=data['deathPerEvent_log'],
xref="x", yref="y",
text=country,
showarrow=True,
arrowhead=1,
ax=20 if country != "USA" else -20, # Adjust these values based on the country's position
ay=-30,
row=1, col=1
)
fig19.update_layout(title_text='HDI vs Death Per Earthquake Event Tsunami Included (Event# > 10)')
plot19_json = json.dumps(fig19, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################
# Creating a 2x3 grid for subplots with 1 scatter plot on top and 3 small histograms below
fig20 = make_subplots(rows=2, cols=3,
specs=[[{"colspan": 3, "type": "scatter"}, None, None], [{}, {}, {}]],
row_heights=[0.7, 0.3])
plotData = groupedEarthquakeData.loc[(groupedEarthquakeData.Total_Event_Count > 10) & (groupedEarthquakeData.Disaster_Subtype == "Ground movement")]
# Scatter plot (row 1, spans all columns) using 'hdi' instead of 'gii'
scatter = px.scatter(plotData, x='hdi', y='deathPerEvent_log', size='Total_Event_Count', hover_name='Country', trendline="ols")
for trace in scatter.data:
fig20.add_trace(trace, row=1, col=1)
# Preparing data for histograms based on 'hdi'
hdi_thirds = plotData['hdi'].quantile([1/3, 2/3]).values
# Histogram for lower third HDI (row 2, col 1)
fig20.add_trace(go.Histogram(x=plotData[plotData['hdi'] <= hdi_thirds[0]]['Total_Event_Count'], nbinsx=20), row=2, col=1)
# Histogram for middle third HDI (row 2, col 2)
fig20.add_trace(go.Histogram(x=plotData[(plotData['hdi'] > hdi_thirds[0]) & (plotData['hdi'] <= hdi_thirds[1])]['Total_Event_Count'], nbinsx=20), row=2, col=2)
# Histogram for upper third HDI (row 2, col 3)
fig20.add_trace(go.Histogram(x=plotData[plotData['hdi'] > hdi_thirds[1]]['Total_Event_Count'], nbinsx=20), row=2, col=3)
# Update layout and axis labels
fig20.update_layout(width=600, height=900, showlegend=False)
fig20.update_yaxes(domain=[0.20, 1], row=1, col=1)
fig20.update_yaxes(domain=[0.05, 0.15], row=2, col=1)
fig20.update_yaxes(domain=[0.05, 0.15], row=2, col=2)
fig20.update_yaxes(domain=[0.05, 0.15], row=2, col=3)
fig20.update_yaxes(title_text='Death Per Event (log)', row=1, col=1)
fig20.update_xaxes(title_text='Lower Third HDI', row=2, col=1)
fig20.update_xaxes(title_text='Middle Third HDI', row=2, col=2)
fig20.update_xaxes(title_text='Upper Third HDI', row=2, col=3)
fig20.update_yaxes(title_text='Frequency', row=2, col=1)
# Annotations for specific countries
countries_data = {
"Indonesia": plotData[plotData['Country'] == 'Indonesia'].iloc[0],
"USA": plotData[plotData['Country'] == 'United States of America'].iloc[0],
"Japan": plotData[plotData['Country'] == 'Japan'].iloc[0],
"China": plotData[plotData['Country'] == 'China'].iloc[0],
"Türkiye": plotData[plotData['Country'] == 'Türkiye'].iloc[0],
}
for country, data in countries_data.items():
fig20.add_annotation(
x=data['hdi'], y=data['deathPerEvent_log'],
xref="x", yref="y",
text=country,
showarrow=True,
arrowhead=1,
ax=20 if country != "USA" else -20, # Adjust these values based on the country's position
ay=-30,
row=1, col=1
)
fig20.update_layout(title_text='HDI vs Death Per Earthquake Event Only Ground Movement (Event# > 10)')
plot20_json = json.dumps(fig20, cls=plotly.utils.PlotlyJSONEncoder)
###########################################################################################################################################