forked from CIRED/cape_town_NEDUM_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_plots_use_case_cchange.py
More file actions
2022 lines (1761 loc) · 83.9 KB
/
3_plots_use_case_cchange.py
File metadata and controls
2022 lines (1761 loc) · 83.9 KB
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
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 29 16:49:54 2022
@author: monni
"""
# %% Preamble
# IMPORT PACKAGES
import os
import itertools
import numpy as np
import pandas as pd
import geopandas as gpd
import plotly.io as pio
import plotly.express as px
import plotly.graph_objects as go
import inputs.parameters_and_options as inpprm
import inputs.data as inpdt
# SET RELATED OPTIONS
pio.renderers.default = 'svg'
# pio.renderers.default = 'browser'
# DEFINE FILE PATHS
path_code = '..'
path_folder = path_code + '/Data/'
path_precalc_inp = path_folder + 'precalculated_inputs/'
path_data = path_folder + 'data_Cape_Town/'
path_precalc_transp = path_folder + 'precalculated_transport/'
path_scenarios = path_data + 'Scenarios/'
path_outputs = path_code + '/Output/'
path_floods = path_folder + "flood_maps/"
# WE CREATE DIRECTORIES TO STORE OUTPUTS (IF NEEDED)
path_use_case = path_outputs + 'use_case_cchange/'
path_maps = path_use_case + 'maps/'
path_charts = path_use_case + 'charts/'
path_damage_distrib = path_charts + 'damage_distrib/'
path_maps_abs_damages = path_maps + 'abs_damages/'
path_maps_rel_damages = path_maps + 'rel_damages/'
path_maps_pop_distrib = path_maps + 'pop_distrib/'
path_maps_rent_distrib = path_maps + 'rent_distrib/'
path_maps_rel_damages_formal = path_maps_rel_damages + 'formal/'
path_maps_rel_damages_subsidized = path_maps_rel_damages + 'subsidized/'
path_maps_rel_damages_informal = path_maps_rel_damages + 'informal/'
path_maps_rel_damages_backyard = path_maps_rel_damages + 'backyard/'
path_maps_rent_distrib_formal = path_maps_rent_distrib + 'formal/'
path_maps_rent_distrib_subsidized = path_maps_rent_distrib + 'subsidized/'
path_maps_rent_distrib_informal = path_maps_rent_distrib + 'informal/'
path_maps_rent_distrib_backyard = path_maps_rent_distrib + 'backyard/'
path_damage_distrib_total = path_damage_distrib + 'total/'
path_damage_distrib_content = path_damage_distrib + 'content/'
list_output_paths = [
path_use_case, path_maps, path_maps_abs_damages, path_maps_rel_damages,
path_maps_pop_distrib, path_maps_rent_distrib,
path_maps_rel_damages_formal, path_maps_rel_damages_subsidized,
path_maps_rel_damages_informal, path_maps_rel_damages_backyard,
path_maps_rent_distrib_formal, path_maps_rent_distrib_subsidized,
path_maps_rent_distrib_informal, path_maps_rent_distrib_backyard,
path_charts, path_damage_distrib,
path_damage_distrib_total, path_damage_distrib_content]
for path in list_output_paths:
try:
os.mkdir(path)
except OSError as error:
print(error)
# IMPORT PARAMETER AND OPTIONS
options = inpprm.import_options()
param = inpprm.import_param(
path_precalc_inp, options)
# LAND USE PROJECTIONS + AMENITIES (for input maps)
(interest_rate, population, housing_type_data, total_RDP
) = inpdt.import_macro_data(param, path_scenarios, path_folder)
grid, center = inpdt.import_grid(path_data)
(data_rdp, housing_types_sp, data_sp, mitchells_plain_grid_baseline,
grid_formal_density_HFA, threshold_income_distribution, income_distribution,
cape_town_limits) = inpdt.import_households_data(path_precalc_inp)
housing_types_data = pd.read_excel(path_folder + 'housing_types_grid_sal.xlsx')
housing_types_data[np.isnan(housing_types_data)] = 0
(spline_RDP, spline_estimate_RDP, spline_land_RDP,
spline_land_backyard, spline_land_informal, spline_land_constraints,
number_properties_RDP) = (
inpdt.import_land_use(grid, options, param, data_rdp, housing_types_data,
housing_type_data, path_data, path_folder)
)
coeff_land = inpdt.import_coeff_land(
spline_land_constraints, spline_land_backyard, spline_land_informal,
spline_land_RDP, param, 0)
amenities = inpdt.import_amenities(path_precalc_inp, options)
# DEFINE SCENARIOS TO IMPORT
# NB: we place climate change scenario first to serve as benchmark
list_scenarios = ['floods10_F0_P11_C11_scenario232',
'floods10_F0_P11_C10_scenario232']
# We store other dimension names
housing_types = ['formal', 'subsidized', 'informal', 'backyard']
flood_types = ['fluvialu', 'pluvial', 'coastal']
damage_types = ['structure', 'content']
# %% Imports
print("Import data")
# DAMAGE DATA
# We retrieve all available dimension combinations in the data
list_dim = [flood_types, housing_types, damage_types]
all_dim = list(itertools.product(*list_dim))
# We store them in a dictionary
dict_damage_map_nocchange = {}
for dim in all_dim:
table = pd.read_csv(
path_outputs + list_scenarios[1] + '/tables/floods/'
+ dim[0] + '_' + dim[1] + '_' + dim[2] + '_2d_sim.csv',
names=['damage'], header=None)
dict_damage_map_nocchange[dim[0] + '_' + dim[1] + '_' + dim[2]] = table
dict_damage_map_cchange = {}
for dim in all_dim:
table = pd.read_csv(
path_outputs + list_scenarios[0] + '/tables/floods/'
+ dim[0] + '_' + dim[1] + '_' + dim[2] + '_2d_sim.csv',
names=['damage'], header=None)
dict_damage_map_cchange[dim[0] + '_' + dim[1] + '_' + dim[2]] = table
dict_damage_map_nocchange_shareinc = {}
for dim in all_dim:
table = pd.read_csv(
path_outputs + list_scenarios[1] + '/tables/floods/'
+ dim[0] + '_' + dim[1] + '_' + dim[2] + '_2d_sim_shareinc.csv',
names=['damage'], header=None)
dict_damage_map_nocchange_shareinc[
dim[0] + '_' + dim[1] + '_' + dim[2]] = table
dict_damage_map_cchange_shareinc = {}
for dim in all_dim:
table = pd.read_csv(
path_outputs + list_scenarios[0] + '/tables/floods/'
+ dim[0] + '_' + dim[1] + '_' + dim[2] + '_2d_sim_shareinc.csv',
names=['damage'], header=None)
dict_damage_map_cchange_shareinc[
dim[0] + '_' + dim[1] + '_' + dim[2]] = table
dict_damage_map = {}
dict_damage_map["nocchange"] = dict_damage_map_nocchange
dict_damage_map["cchange"] = dict_damage_map_cchange
dict_damage_map["nocchange_shareinc"] = dict_damage_map_nocchange_shareinc
dict_damage_map["cchange_shareinc"] = dict_damage_map_cchange_shareinc
# OTHER EQUILIBRIUM OUTCOMES
# We import calibrated income net of commuting costs
income_net_of_commuting_costs = np.load(
path_precalc_transp + 'GRID_incomeNetOfCommuting_0.npy')
# We retrieve all available dimension combinations for calibrated fraction
# of capital destroyed by floods
housing_types_2 = [
'formal', 'backyard', 'informal', 'subsidized',
'formal_1', 'formal_2', 'subsidized_1',
'informal_settlements', 'informal_backyards']
damage_types_2 = ['structure', 'contents']
list_dim = [damage_types_2, housing_types_2]
all_dim = list(itertools.product(*list_dim))
# We store them in a dictionary
dict_fract_K_destroyed = {}
for scenario in list_scenarios:
for dim in all_dim:
try:
table = pd.read_csv(
path_outputs + scenario + '/tables/floods/'
+ dim[0] + '_' + dim[1] + '_fract_K_destroyed.csv',
usecols=[1])
if scenario == list_scenarios[0]:
type_map = "cchange"
elif scenario == list_scenarios[1]:
type_map = "nocchange"
dict_fract_K_destroyed[dim[0] + '_' + dim[1] + '_' + type_map
] = table
except FileNotFoundError:
pass
# Then we get endogenous outcomes that change across scenarios
dict_scenario_outcomes = {}
for scenario in list_scenarios:
initial_state_utility = np.load(
path_outputs + scenario + '/initial_state_utility.npy')
initial_state_households_housing_types = np.load(
path_outputs + scenario
+ '/initial_state_households_housing_types.npy')
initial_state_household_centers = np.load(
path_outputs + scenario + '/initial_state_household_centers.npy')
initial_state_households = np.load(
path_outputs + scenario + '/initial_state_households.npy')
initial_state_dwelling_size = np.load(
path_outputs + scenario + '/initial_state_dwelling_size.npy')
initial_state_housing_supply = np.load(
path_outputs + scenario + '/initial_state_housing_supply.npy')
initial_state_rent = np.load(
path_outputs + scenario + '/initial_state_rent.npy')
dict_scenario_outcomes[scenario] = {}
dict_scenario_outcomes[scenario]['utility'] = initial_state_utility
dict_scenario_outcomes[scenario]['hh_per_htype'
] = initial_state_households_housing_types
dict_scenario_outcomes[scenario]['hh_per_incgroup'
] = initial_state_household_centers
dict_scenario_outcomes[scenario]['hh_full_dist'] = initial_state_households
dict_scenario_outcomes[scenario]['dwelling_size'
] = initial_state_dwelling_size
dict_scenario_outcomes[scenario]['hsupply'] = initial_state_housing_supply
dict_scenario_outcomes[scenario]['rent'] = initial_state_rent
households_formal_inc = pd.DataFrame(initial_state_households[0, :, :].T)
households_backyard_inc = pd.DataFrame(initial_state_households[1, :, :].T)
households_informal_inc = pd.DataFrame(initial_state_households[2, :, :].T)
households_subsidized_inc = pd.DataFrame(
initial_state_households[3, :, :].T)
list_var_temp = [households_formal_inc, households_backyard_inc,
households_informal_inc, households_subsidized_inc]
for var in list_var_temp:
var.loc[var[0] != 0, 0] = 1
var.loc[var[1] != 0, 1] = 2
var.loc[var[2] != 0, 2] = 3
var.loc[var[3] != 0, 3] = 4
households_formal_inc_flat = households_formal_inc.sum(axis=1)
households_backyard_inc_flat = households_backyard_inc.sum(axis=1)
households_informal_inc_flat = households_informal_inc.sum(axis=1)
households_subsidized_inc_flat = households_subsidized_inc.sum(axis=1)
dict_scenario_outcomes[scenario]['formal_incgroup'
] = households_formal_inc_flat
dict_scenario_outcomes[scenario]['backyard_incgroup'
] = households_backyard_inc_flat
dict_scenario_outcomes[scenario]['informal_incgroup'
] = households_informal_inc_flat
dict_scenario_outcomes[scenario]['subsidized_incgroup'
] = households_subsidized_inc_flat
for var in list_var_temp:
var.loc[var[0] != 0, 0] = 1
var.loc[var[1] != 0, 1] = 1
var.loc[var[2] != 0, 2] = 1
var.loc[var[3] != 0, 3] = 1
net_income_formal = np.nansum(
income_net_of_commuting_costs * households_formal_inc.T, 0)
net_income_subsidized = np.nansum(
income_net_of_commuting_costs * households_subsidized_inc.T, 0)
net_income_informal = np.nansum(
income_net_of_commuting_costs * households_informal_inc.T, 0)
net_income_backyard = np.nansum(
income_net_of_commuting_costs * households_backyard_inc.T, 0)
dict_scenario_outcomes[scenario]['net_income_formal'
] = net_income_formal
dict_scenario_outcomes[scenario]['net_income_subsidized'
] = net_income_subsidized
dict_scenario_outcomes[scenario]['net_income_informal'
] = net_income_informal
dict_scenario_outcomes[scenario]['net_income_backyard'
] = net_income_backyard
# We also import the geography reference grid for plots
geo_grid = gpd.read_file(path_data + "grid_reference_500.shp")
geo_grid.to_crs(4326, inplace=True)
geo_grid['lon'] = geo_grid.centroid.x
geo_grid['lat'] = geo_grid.centroid.y
# NB: Should we show the distribution of exposure to flood risks by income
# group, or is it redundant with the distribution of damages? Not necessarily,
# since equivalent damages can be obtained in high and low-risk areas
# depending on the value of the underlying capital
# %% Data processing
print("Data processing")
# REARRANGE TABLES
# #For spatial distribution
# We build an aggregate damage table to show on the map, and will display all
# the additional information as hover data
list_maps = ['nocchange', 'cchange']
damage_maps = {}
list_maps_shareinc = ['nocchange_shareinc', 'cchange_shareinc']
damage_maps_shareinc = {}
for item in list_maps:
# We first concatenate all damage tables into one data frame
concat_damage_map = (
pd.concat(dict_damage_map[item], axis=1).groupby(axis=1, level=0).sum()
)
# We also define filters for column names to be used as part of processing
filter_names = ['fluvialu', 'pluvial', 'coastal', 'fluvialu_formal',
'fluvialu_subsidized', 'fluvialu_informal',
'fluvialu_backyard', 'pluvial_formal',
'pluvial_subsidized', 'pluvial_informal',
'pluvial_backyard', 'coastal_formal', 'coastal_subsidized',
'coastal_informal', 'coastal_backyard']
subfilter_names = ['fluvialu_formal', 'fluvialu_subsidized',
'fluvialu_informal', 'fluvialu_backyard',
'pluvial_formal', 'pluvial_subsidized',
'pluvial_informal', 'pluvial_backyard',
'coastal_formal', 'coastal_subsidized',
'coastal_informal', 'coastal_backyard']
# We get the columns associated with each column name and store them in a
# dictionary
dict_filters = {}
for filtre in filter_names:
dict_filters[filtre] = [
col for col in concat_damage_map if col.startswith(filtre)]
# We obtain the sum of all damage values corresponding to each data categ
for key, value in dict_filters.items():
concat_damage_map['sum_' + key] = (
concat_damage_map[dict_filters[key]].sum(axis=1))
# We take the maximum across aggregate flood type damages to display as our
# main outcome of interest (cf. bath-tub model)
concat_damage_map['max_val'] = concat_damage_map[
['sum_fluvialu', 'sum_pluvial', 'sum_coastal']].max(axis=1)
# For reference to rainwater floods
concat_damage_map['max_val_rainwater'] = concat_damage_map[
['sum_fluvialu', 'sum_pluvial']].max(axis=1)
# We also define the dominant flood type to display as hover data
concat_damage_map['flood_type'] = 'None'
concat_damage_map.loc[
(concat_damage_map['sum_fluvialu'] == concat_damage_map['max_val'])
& (concat_damage_map['max_val'] != 0), 'flood_type'] = 'fluvial'
concat_damage_map.loc[
(concat_damage_map['sum_pluvial'] == concat_damage_map['max_val'])
& (concat_damage_map['max_val'] != 0), 'flood_type'] = 'pluvial'
concat_damage_map.loc[
(concat_damage_map['sum_coastal'] == concat_damage_map['max_val'])
& (concat_damage_map['max_val'] != 0), 'flood_type'] = 'coastal'
# Then, we recover the breakdown of damages across housing types, to show
# as hover data as well
# We initialize values for each housing type
concat_damage_map['damage_formal'] = 0
concat_damage_map['damage_subsidized'] = 0
concat_damage_map['damage_informal'] = 0
concat_damage_map['damage_backyard'] = 0
# We associate each value to the corresponding sum of damages for the main
# flood type only
for housing_type in housing_types:
concat_damage_map.loc[
concat_damage_map['flood_type']
== 'fluvial', 'damage_' + housing_type
] = concat_damage_map['sum_fluvialu_' + housing_type]
concat_damage_map.loc[
concat_damage_map['flood_type']
== 'pluvial', 'damage_' + housing_type
] = concat_damage_map['sum_pluvial_' + housing_type]
concat_damage_map.loc[
concat_damage_map['flood_type']
== 'coastal', 'damage_' + housing_type
] = concat_damage_map['sum_coastal_' + housing_type]
# Finally, we also define the share of content (vs. structures) damages in
# overall housing-type-specific damages, to display as hover data
# We start by defining the ratio for all flood + housing type combinations
for subfilter in subfilter_names:
concat_damage_map[subfilter + '_content_share'] = (
concat_damage_map[subfilter + '_content']
/ concat_damage_map['sum_' + subfilter])
# We initialize values for housing types only
concat_damage_map['content_share_formal'] = 0
concat_damage_map['content_share_subsidized'] = 0
concat_damage_map['content_share_informal'] = 0
concat_damage_map['content_share_backyard'] = 0
# Then, we associate each value to the associated content ratio for the
# dominant flood type only
for housing_type in housing_types:
concat_damage_map.loc[
concat_damage_map['flood_type'] == 'fluvial',
'content_share_' + housing_type
] = concat_damage_map['fluvialu_' + housing_type +
'_content_share']
concat_damage_map.loc[
concat_damage_map['flood_type'] == 'pluvial',
'content_share_' + housing_type
] = concat_damage_map['pluvial_' + housing_type + '_content_share']
concat_damage_map.loc[
concat_damage_map['flood_type'] == 'coastal',
'content_share_' + housing_type
] = concat_damage_map['coastal_' + housing_type + '_content_share']
# We convert nans to zeros for proper rendering in output map
concat_damage_map = concat_damage_map.fillna(0)
concat_damage_map = concat_damage_map.replace([np.inf, -np.inf], 0)
# We store the output in the corresponding dictionary entry
damage_maps[item] = concat_damage_map
# We do the same expressing damages as a share of net income
for item in list_maps_shareinc:
# We first concatenate all damage tables into one data frame
concat_damage_map = (
pd.concat(dict_damage_map[item], axis=1).groupby(axis=1, level=0).sum()
)
# We also define filters for column names to be used as part of processing
subfilter_names = ['fluvialu_formal', 'fluvialu_subsidized',
'fluvialu_informal', 'fluvialu_backyard',
'pluvial_formal', 'pluvial_subsidized',
'pluvial_informal', 'pluvial_backyard',
'coastal_formal', 'coastal_subsidized',
'coastal_informal', 'coastal_backyard']
# We get the columns associated with each column name and store them in a
# dictionary
dict_filters = {}
for filtre in subfilter_names:
dict_filters[filtre] = [
col for col in concat_damage_map if col.startswith(filtre)]
# We obtain the sum of all damage values corresponding to each data categ
for key, value in dict_filters.items():
concat_damage_map['sum_' + key] = (
concat_damage_map[dict_filters[key]].sum(axis=1))
# We take the maximum across aggregate flood type damages to display as our
# main outcome of interest (cf. bath-tub model)
concat_damage_map['max_val_formal'] = concat_damage_map[
['sum_fluvialu_formal', 'sum_pluvial_formal', 'sum_coastal_formal']
].max(axis=1)
concat_damage_map['max_val_subsidized'] = concat_damage_map[
['sum_fluvialu_subsidized', 'sum_pluvial_subsidized',
'sum_coastal_subsidized']
].max(axis=1)
concat_damage_map['max_val_informal'] = concat_damage_map[
['sum_fluvialu_informal', 'sum_pluvial_informal',
'sum_coastal_informal']
].max(axis=1)
concat_damage_map['max_val_backyard'] = concat_damage_map[
['sum_fluvialu_backyard', 'sum_pluvial_backyard',
'sum_coastal_backyard']
].max(axis=1)
# We do the same for content share only (to display as hover data)
concat_damage_map['max_content_formal'] = concat_damage_map[
['fluvialu_formal_content', 'pluvial_formal_content',
'coastal_formal_content']
].max(axis=1)
concat_damage_map['max_content_subsidized'] = concat_damage_map[
['fluvialu_subsidized_content', 'pluvial_subsidized_content',
'coastal_subsidized_content']
].max(axis=1)
concat_damage_map['max_content_informal'] = concat_damage_map[
['fluvialu_informal_content', 'pluvial_informal_content',
'coastal_informal_content']
].max(axis=1)
concat_damage_map['max_content_backyard'] = concat_damage_map[
['fluvialu_backyard_content', 'pluvial_backyard_content',
'coastal_backyard_content']
].max(axis=1)
# We also define the dominant flood type to display as hover data
for housing_type in housing_types:
concat_damage_map['flood_type_' + housing_type] = 'None'
concat_damage_map.loc[
(concat_damage_map['sum_fluvialu_' + housing_type]
== concat_damage_map['max_val_' + housing_type])
& (concat_damage_map['max_val_' + housing_type] != 0),
'flood_type_' + housing_type] = 'fluvial'
concat_damage_map.loc[
(concat_damage_map['sum_pluvial_' + housing_type]
== concat_damage_map['max_val_' + housing_type])
& (concat_damage_map['max_val_' + housing_type] != 0),
'flood_type_' + housing_type] = 'pluvial'
concat_damage_map.loc[
(concat_damage_map['sum_coastal_' + housing_type]
== concat_damage_map['max_val_' + housing_type])
& (concat_damage_map['max_val_' + housing_type] != 0),
'flood_type_' + housing_type] = 'coastal'
# We also add some population info to display as hover data
if item == 'nocchange_shareinc':
concat_damage_map['nb_households_formal'] = (
dict_scenario_outcomes[list_scenarios[1]]['hh_per_htype'][0, :])
concat_damage_map['nb_households_subsidized'] = (
dict_scenario_outcomes[list_scenarios[1]]['hh_per_htype'][3, :])
concat_damage_map['nb_households_informal'] = (
dict_scenario_outcomes[list_scenarios[1]]['hh_per_htype'][2, :])
concat_damage_map['nb_households_backyard'] = (
dict_scenario_outcomes[list_scenarios[1]]['hh_per_htype'][1, :])
concat_damage_map['rent_formal'] = (
dict_scenario_outcomes[list_scenarios[1]]['rent'][0, :])
concat_damage_map.loc[
concat_damage_map['nb_households_formal'] == 0, 'rent_formal'] = 0
concat_damage_map['rent_subsidized'] = (
dict_scenario_outcomes[list_scenarios[1]]['rent'][3, :])
concat_damage_map.loc[
concat_damage_map['nb_households_subsidized'] == 0,
'rent_subsidized'] = 0
concat_damage_map['rent_informal'] = (
dict_scenario_outcomes[list_scenarios[1]]['rent'][2, :])
concat_damage_map.loc[
concat_damage_map['nb_households_informal'] == 0,
'rent_informal'] = 0
concat_damage_map['rent_backyard'] = (
dict_scenario_outcomes[list_scenarios[1]]['rent'][1, :])
concat_damage_map.loc[
concat_damage_map['nb_households_subsidized'] == 0,
'rent_subsidized'] = 0
for housing_type in housing_types:
concat_damage_map['incgroup_' + housing_type] = (
dict_scenario_outcomes[list_scenarios[1]
][housing_type + '_incgroup'])
concat_damage_map['net_income_' + housing_type] = (
dict_scenario_outcomes[list_scenarios[1]
]['net_income_' + housing_type])
elif item == 'cchange_shareinc':
concat_damage_map['nb_households_formal'] = (
dict_scenario_outcomes[list_scenarios[0]]['hh_per_htype'][0, :])
concat_damage_map['nb_households_subsidized'] = (
dict_scenario_outcomes[list_scenarios[0]]['hh_per_htype'][3, :])
concat_damage_map['nb_households_informal'] = (
dict_scenario_outcomes[list_scenarios[0]]['hh_per_htype'][2, :])
concat_damage_map['nb_households_backyard'] = (
dict_scenario_outcomes[list_scenarios[0]]['hh_per_htype'][1, :])
concat_damage_map['rent_formal'] = (
dict_scenario_outcomes[list_scenarios[0]]['rent'][0, :])
concat_damage_map.loc[
concat_damage_map['nb_households_formal'] == 0, 'rent_formal'] = 0
concat_damage_map['rent_subsidized'] = (
dict_scenario_outcomes[list_scenarios[0]]['rent'][3, :])
concat_damage_map.loc[
concat_damage_map['nb_households_subsidized'] == 0,
'rent_subsidized'] = 0
concat_damage_map['rent_informal'] = (
dict_scenario_outcomes[list_scenarios[0]]['rent'][2, :])
concat_damage_map.loc[
concat_damage_map['nb_households_informal'] == 0,
'rent_informal'] = 0
concat_damage_map['rent_backyard'] = (
dict_scenario_outcomes[list_scenarios[0]]['rent'][1, :])
concat_damage_map.loc[
concat_damage_map['nb_households_backyard'] == 0,
'rent_backyard'] = 0
for housing_type in housing_types:
concat_damage_map['incgroup_' + housing_type] = (
dict_scenario_outcomes[list_scenarios[0]
][housing_type + '_incgroup'])
concat_damage_map['net_income_' + housing_type] = (
dict_scenario_outcomes[list_scenarios[0]
]['net_income_' + housing_type])
# We convert nans to zeros for proper rendering in output map
concat_damage_map = concat_damage_map.fillna(0)
concat_damage_map = concat_damage_map.replace([np.inf, -np.inf], 0)
# We store the output in the corresponding dictionary entry
damage_maps_shareinc[item] = concat_damage_map
# We also create a data table for other equilibrium outcomes
equil_maps = {}
for item in list_scenarios:
# For overall distribution
hh_tot = np.nansum(dict_scenario_outcomes[item]['hh_per_htype'], 0)
hh_formal = dict_scenario_outcomes[item]['hh_per_htype'][0, :]
hh_backyard = dict_scenario_outcomes[item]['hh_per_htype'][1, :]
hh_informal = dict_scenario_outcomes[item]['hh_per_htype'][2, :]
hh_subsidized = dict_scenario_outcomes[item]['hh_per_htype'][3, :]
formal_incgroup = dict_scenario_outcomes[item]['formal_incgroup']
backyard_incgroup = dict_scenario_outcomes[item]['backyard_incgroup']
informal_incgroup = dict_scenario_outcomes[item]['informal_incgroup']
subsidized_incgroup = dict_scenario_outcomes[item]['subsidized_incgroup']
# + for rent distribution (per housing type)
hsupply_formal = dict_scenario_outcomes[item]['hsupply'][0, :]
hsupply_backyard = dict_scenario_outcomes[item]['hsupply'][1, :]
hsupply_informal = dict_scenario_outcomes[item]['hsupply'][2, :]
hsupply_subsidized = dict_scenario_outcomes[item]['hsupply'][3, :]
rent_formal = dict_scenario_outcomes[item]['rent'][0, :]
rent_formal[hh_formal == 0] = 0
rent_backyard = dict_scenario_outcomes[item]['rent'][1, :]
rent_backyard[hh_backyard == 0] = 0
rent_informal = dict_scenario_outcomes[item]['rent'][2, :]
rent_informal[hh_informal == 0] = 0
rent_subsidized = dict_scenario_outcomes[item]['rent'][3, :]
rent_subsidized[hh_subsidized == 0] = 0
dwelling_size_formal = dict_scenario_outcomes[item]['dwelling_size'][0, :]
dwelling_size_formal[hh_formal == 0] = 0
dwelling_size_backyard = (
dict_scenario_outcomes[item]['dwelling_size'][1, :])
dwelling_size_backyard[hh_backyard == 0] = 0
dwelling_size_informal = (
dict_scenario_outcomes[item]['dwelling_size'][2, :])
dwelling_size_informal[hh_informal == 0] = 0
dwelling_size_subsidized = (
dict_scenario_outcomes[item]['dwelling_size'][3, :])
dwelling_size_subsidized[hh_subsidized == 0] = 0
# We correct for the fact that flood depreciation varies across scenarios
# with climate change
if item == list_scenarios[0]:
type_map = 'cchange'
elif item == list_scenarios[1]:
type_map = 'nocchange'
flood_deprec_content_formal = dict_fract_K_destroyed[
'contents_formal_' + type_map]
flood_deprec_content_backyard = dict_fract_K_destroyed[
'contents_backyard_' + type_map]
flood_deprec_content_informal = dict_fract_K_destroyed[
'contents_informal_' + type_map]
flood_deprec_content_subsidized = (
dict_fract_K_destroyed['contents_subsidized_' + type_map])
flood_deprec_struct_formal = dict_fract_K_destroyed[
'structure_formal_1_' + type_map]
flood_deprec_struct_formal[dwelling_size_formal > param["threshold"]] = (
dict_fract_K_destroyed['structure_formal_2_' + type_map][
dwelling_size_formal > param["threshold"]])
flood_deprec_struct_backyard = dict_fract_K_destroyed[
'structure_informal_backyards_' + type_map]
flood_deprec_struct_informal = dict_fract_K_destroyed[
'structure_informal_settlements_' + type_map]
flood_deprec_struct_subsidized = dict_fract_K_destroyed[
'structure_subsidized_1_' + type_map]
# We store everything into one data frame
concat_equil_map = pd.DataFrame({
'hh_tot': hh_tot,
'hh_formal': hh_formal,
'hh_backyard': hh_backyard,
'hh_informal': hh_informal,
'hh_subsidized': hh_subsidized,
'formal_incgroup': formal_incgroup,
'backyard_incgroup': backyard_incgroup,
'informal_incgroup': informal_incgroup,
'subsidized_incgroup': subsidized_incgroup,
'hsupply_formal': hsupply_formal,
'hsupply_backyard': hsupply_backyard,
'hsupply_informal': hsupply_informal,
'hsupply_subsidized': hsupply_subsidized,
'rent_formal': rent_formal,
'rent_backyard': rent_backyard,
'rent_informal': rent_informal,
'rent_subsidized': rent_subsidized,
'dwelling_size_formal': dwelling_size_formal,
'dwelling_size_backyard': dwelling_size_backyard,
'dwelling_size_informal': dwelling_size_informal,
'dwelling_size_subsidized': dwelling_size_subsidized,
'flood_deprec_content_formal':
flood_deprec_content_formal.to_numpy().flatten(),
'flood_deprec_content_backyard':
flood_deprec_content_backyard.to_numpy().flatten(),
'flood_deprec_content_informal':
flood_deprec_content_informal.to_numpy().flatten(),
'flood_deprec_content_subsidized':
flood_deprec_content_subsidized.to_numpy().flatten(),
'flood_deprec_struct_formal':
flood_deprec_struct_formal.to_numpy().flatten(),
'flood_deprec_struct_backyard':
flood_deprec_struct_backyard.to_numpy().flatten(),
'flood_deprec_struct_informal':
flood_deprec_struct_informal.to_numpy().flatten(),
'flood_deprec_struct_subsidized':
flood_deprec_struct_subsidized.to_numpy().flatten()
})
# We convert nans to zeros for proper rendering in output map
concat_equil_map = concat_equil_map.fillna(0)
concat_equil_map = concat_equil_map.replace([np.inf, -np.inf], 0)
# We store the output in the corresponding dictionary entry
equil_maps[item] = concat_equil_map
# #Then, we create comparative maps across scenarios to better see changes
# We store name of variables to be created
list_num_var = ['max_val', 'damage_formal', 'damage_subsidized',
'damage_informal', 'damage_backyard']
list_num_pct = ['max_val_pct', 'damage_formal_pct', 'damage_subsidized_pct',
'damage_informal_pct', 'damage_backyard_pct']
# We create the table for changes
damage_map_compar = pd.DataFrame()
damage_map_compar[list_num_var] = (
damage_maps['cchange'][list_num_var]
- damage_maps['nocchange'][list_num_var])
damage_map_compar[list_num_pct] = (
damage_maps['cchange'][list_num_var]
/ damage_maps['nocchange'][list_num_var]
- 1)
damage_map_compar['lon'] = geo_grid.lon
damage_map_compar['lat'] = geo_grid.lat
damage_map_compar['flood_type'] = damage_maps['nocchange']['flood_type']
damage_map_compar.loc[
damage_map_compar['flood_type'] == 'None', 'flood_type'
] = damage_maps['cchange']['flood_type']
damage_map_compar = damage_map_compar.fillna(0)
damage_map_compar = damage_map_compar.replace([np.inf, -np.inf], 0)
# We do the same for damages expressed as a share of income
# We store name of variables to be created
list_num_var = [
'max_val_formal', 'max_val_subsidized', 'max_val_informal',
'max_val_backyard', 'max_content_formal', 'max_content_subsidized',
'max_content_informal', 'max_content_backyard', 'rent_formal',
'rent_subsidized', 'rent_informal', 'rent_backyard']
list_num_pct = [
'max_val_formal_pct', 'max_val_subsidized_pct', 'max_val_informal_pct',
'max_val_backyard_pct', 'max_content_formal_pct',
'max_content_subsidized_pct', 'max_content_informal_pct',
'max_content_backyard_pct', 'rent_formal_pct', 'rent_subsidized_pct',
'rent_informal_pct', 'rent_backyard_pct']
# We create the table for changes
damage_map_compar_shareinc = pd.DataFrame()
damage_map_compar_shareinc[list_num_var] = (
damage_maps_shareinc['cchange_shareinc'][list_num_var]
- damage_maps_shareinc['nocchange_shareinc'][list_num_var])
damage_map_compar_shareinc[list_num_pct] = (
damage_maps_shareinc['cchange_shareinc'][list_num_var]
/ damage_maps_shareinc['nocchange_shareinc'][list_num_var]
- 1)
damage_map_compar_shareinc['lon'] = geo_grid.lon
damage_map_compar_shareinc['lat'] = geo_grid.lat
for housing_type in housing_types:
damage_map_compar_shareinc['flood_type_' + housing_type] = (
damage_maps_shareinc['nocchange_shareinc'][
'flood_type_' + housing_type])
damage_map_compar_shareinc.loc[
damage_map_compar_shareinc['flood_type_' + housing_type] == 'None',
'flood_type_' + housing_type
] = damage_maps_shareinc['cchange_shareinc']['flood_type_' + housing_type]
# NB: we take the cchange case as a benchmark, since we want to show
# increase damages from cchange. We'll rely on other maps to show
# population moves and composition effects, and the extent to which they
# explain what we observe.
damage_map_compar_shareinc['nb_households_cc_' + housing_type] = (
damage_maps_shareinc['cchange_shareinc'][
'nb_households_' + housing_type])
damage_map_compar_shareinc['incgroup_cc_' + housing_type] = (
damage_maps_shareinc['cchange_shareinc']['incgroup_' + housing_type])
damage_map_compar_shareinc['net_income_cc_' + housing_type] = (
damage_maps_shareinc['cchange_shareinc'][
'net_income_' + housing_type])
damage_map_compar_shareinc = damage_map_compar_shareinc.fillna(0)
damage_map_compar_shareinc = damage_map_compar_shareinc.replace(
[np.inf, -np.inf], 0)
# Then for other equilibrium outcomes
# We store name of variables to be created
list_num_var = [
'hh_tot', 'hh_formal', 'hh_backyard', 'hh_informal', 'hh_subsidized',
'hsupply_formal', 'hsupply_backyard', 'hsupply_informal',
'hsupply_subsidized',
'rent_formal', 'rent_backyard', 'rent_informal', 'rent_subsidized',
'dwelling_size_formal', 'dwelling_size_backyard', 'dwelling_size_informal',
'dwelling_size_subsidized']
list_num_pct = [
'hh_tot_pct', 'hh_formal_pct', 'hh_backyard_pct', 'hh_informal_pct',
'hh_subsidized_pct',
'hsupply_formal_pct', 'hsupply_backyard_pct', 'hsupply_informal_pct',
'hsupply_subsidized_pct',
'rent_formal_pct', 'rent_backyard_pct', 'rent_informal_pct',
'rent_subsidized_pct',
'dwelling_size_formal_pct', 'dwelling_size_backyard_pct',
'dwelling_size_informal_pct', 'dwelling_size_subsidized_pct']
# We create the table for changes
equil_map_compar = pd.DataFrame()
equil_map_compar[list_num_var] = (
equil_maps[list_scenarios[0]][list_num_var]
- equil_maps[list_scenarios[1]][list_num_var])
equil_map_compar[list_num_pct] = (
equil_maps[list_scenarios[0]][list_num_var]
/ equil_maps[list_scenarios[1]][list_num_var]
- 1)
equil_map_compar['lon'] = geo_grid.lon
equil_map_compar['lat'] = geo_grid.lat
# NB: Again, we take the cchange case as a benchmark, while considering
# the income group of households who left when the area is left unoccupied
# in the benchmark case
for housing_type in housing_types:
equil_map_compar['flood_deprec_content_' + housing_type] = (
equil_maps[list_scenarios[0]]['flood_deprec_content_' + housing_type]
- equil_maps[list_scenarios[1]]['flood_deprec_content_' + housing_type]
)
equil_map_compar['flood_deprec_struct_' + housing_type] = (
equil_maps[list_scenarios[0]]['flood_deprec_struct_' + housing_type]
- equil_maps[list_scenarios[1]]['flood_deprec_struct_' + housing_type])
equil_map_compar[housing_type + '_incgroup'] = (
equil_maps[list_scenarios[0]][housing_type + '_incgroup'])
equil_map_compar.loc[
equil_map_compar[housing_type + '_incgroup'] == 0,
housing_type + '_incgroup'
] = equil_maps[list_scenarios[1]][housing_type + '_incgroup']
equil_map_compar = equil_map_compar.fillna(0)
equil_map_compar = equil_map_compar.replace([np.inf, -np.inf], 0)
# %% Output graphs
print("Output graphs")
# PLOT SPATIAL DAMAGE DISTRIBUTION
# We plot all the relevant information for a given scenario as a chloropleth
# map
# #First for damages in absolute values
for item in list_maps:
damage_maps[item]['lon'] = geo_grid.lon
damage_maps[item]['lat'] = geo_grid.lat
damage_maps[item].loc[
damage_maps[item]['max_val'] == 0, 'max_val'] = np.nan
fig = px.choropleth_mapbox(
damage_maps[item],
geojson=geo_grid.geometry,
locations=geo_grid.index,
color='max_val',
center={"lat": -33.92345542582841, "lon": 18.434424141913478},
zoom=9.25,
mapbox_style='stamen-toner',
opacity=0.75,
labels={'lon': 'Lon.', 'lat': 'Lat.',
'max_val': 'Total', 'locations': 'Pixel ID',
'flood_type': 'Flood type',
'damage_formal': 'Formal private',
'damage_subsidized': 'Formal subsidized',
'damage_informal': 'Informal settlements',
'damage_backyard': 'Informal backyards',
'content_share_formal': '% of content (formal)',
'content_share_subsidized': '% of content (subsidized)',
'content_share_informal': '% of content (informal)',
'content_share_backyard': '% of content (backyard)'},
title='Estimated annual flood damages (in rands, 2011)',
color_continuous_scale="Reds",
template='plotly_white',
hover_data={'lon': ':.2f', 'lat': ':.2f', 'flood_type': True,
'damage_formal': ':,.0f',
'content_share_formal': ':.0%',
'damage_subsidized': ':,.0f',
'content_share_subsidized': ':.0%',
'damage_informal': ':,.0f',
'content_share_informal': ':.0%',
'damage_backyard': ':,.0f',
'content_share_backyard': ':.0%'})
fig.update_layout(margin={"r": 0, "t": 30, "l": 0, "b": 0})
fig.update_traces(marker_line_width=0)
# fig.show()
fig.write_html(path_maps_abs_damages + "map_damages_" + item + ".html")
fig.write_image(path_maps_abs_damages + "map_damages_" + item + ".png",
height=650, width=1000)
print("map_damages_" + item + " done")
damage_map_compar.loc[
damage_map_compar['max_val'] == 0, 'max_val'] = np.nan
fig = px.choropleth_mapbox(
damage_map_compar,
geojson=geo_grid.geometry,
locations=geo_grid.index,
color='max_val',
center={"lat": -33.92345542582841, "lon": 18.434424141913478},
zoom=9.25,
mapbox_style='stamen-toner',
opacity=0.75,
labels={'lon': 'Lon.', 'lat': 'Lat.',
'max_val': 'Total change', 'max_val_pct': '% change',
'locations': 'Pixel ID',
'flood_type': 'Flood type',
'damage_formal': 'Formal private',
'damage_formal_pct': '% change (formal)',
'damage_subsidized': 'Formal subsidized',
'damage_subsidized_pct': '% change (subsidized)',
'damage_informal': 'Informal settlements',
'damage_informal_pct': '% change (informal)',
'damage_backyard': 'Informal backyards',
'damage_backyard_pct': '% change (backyards)'},
title='Flood damage increase from climate change (in rands, 2011)',
color_continuous_scale="Picnic",
color_continuous_midpoint=0,
template='plotly_white',
hover_data={'lon': ':.2f', 'lat': ':.2f', 'flood_type': True,
'damage_formal': ':,.0f', 'damage_formal_pct': ':+.0%',
'damage_subsidized': ':,.0f', 'damage_subsidized_pct': ':+.0%',
'damage_informal': ':,.0f', 'damage_informal_pct': ':+.0%',
'damage_backyard': ':,.0f', 'damage_backyard_pct': ':+.0%',
'max_val': True, 'max_val_pct': ':+.0%'})
fig.update_layout(margin={"r": 0, "t": 30, "l": 0, "b": 0})
fig.update_traces(marker_line_width=0)
# fig.show()
fig.write_html(path_maps_abs_damages + "map_damages_compar_cchange.html")
fig.write_image(path_maps_abs_damages + "map_damages_compar_cchange.png",
height=650, width=1000)
print("map_damages_compar_cchange done")
# #Then for damages in relative terms
for housing_type in housing_types:
for item in list_maps_shareinc:
damage_maps_shareinc[item]['lon'] = geo_grid.lon
damage_maps_shareinc[item]['lat'] = geo_grid.lat
damage_maps_shareinc[item].loc[
damage_maps_shareinc[item]['max_val_' + housing_type] == 0,
'max_val_' + housing_type] = np.nan
fig = px.choropleth_mapbox(
damage_maps_shareinc[item],
geojson=geo_grid.geometry,
locations=geo_grid.index,
color='max_val_' + housing_type,
center={"lat": -33.92345542582841, "lon": 18.434424141913478},
zoom=9.25,
mapbox_style='stamen-toner',
opacity=0.75,
labels={'lon': 'Lon.', 'lat': 'Lat.',
'max_val_' + housing_type: 'Damages (% net inc.)',
'max_content_' + housing_type: 'o.w. content',
'locations': 'Pixel ID',