hkayabilisim commited on
Commit
7ca9c4d
·
1 Parent(s): 78c5667

engine: added monte-carlo for bld + earthquakes

Browse files
tomorrowcities/backend/engine.py CHANGED
@@ -445,6 +445,7 @@ def compute(gdf_landuse, gdf_buildings, df_household, df_individual,gdf_intensit
445
  cdf_median_increase_in_percent = 0.20,
446
  flood_depth_reduction = 0.20,
447
  damage_curve_suppress_factor = 0.9,
 
448
  ):
449
  print('threshold_flood', threshold_flood)
450
  print('cdf_median_increase_in_percent',cdf_median_increase_in_percent)
@@ -456,7 +457,11 @@ def compute(gdf_landuse, gdf_buildings, df_household, df_individual,gdf_intensit
456
 
457
  gem_fragility = True if isinstance(df_hazard, dict) else False
458
  print('gem_fragility mode', gem_fragility)
459
- if hazard_type != "landslide":
 
 
 
 
460
  np.random.seed(seed=0)
461
 
462
  column_names = {'zoneID':'zoneid','bldID':'bldid','nHouse':'nhouse',
@@ -722,11 +727,16 @@ def compute(gdf_landuse, gdf_buildings, df_household, df_individual,gdf_intensit
722
  return prob_ds1, prob_ds2, prob_ds3, prob_ds4
723
  bld_eq[['prob_ds1','prob_ds2','prob_ds3','prob_ds4']] = bld_eq.apply(computer_damage_state, axis=1,result_type='expand')
724
 
725
- bld_eq[['prob_ds0','prob_ds5']] = [1,0]
726
- for i in [1,2,3,4,5]:
727
- bld_eq[f'ds_{i}'] = np.abs(bld_eq[f'prob_ds{i-1}'] - bld_eq[f'prob_ds{i}'])
728
- df_ds = bld_eq[['ds_1','ds_2','ds_3','ds_4','ds_5']]
729
- bld_eq['eq_ds'] = df_ds.idxmax(axis='columns').str.extract(r'ds_([0-9]+)').astype('int') - 1
 
 
 
 
 
730
  casualty_rates = np.array([0, 0.05, 0.28, 1.152, 74.41]) # percent
731
  bld_eq['casualy'] = 0
732
  bld_eq = bld_eq.assign(casualty=lambda x: casualty_rates[x['eq_ds']] * x['residents'] / 100)
 
445
  cdf_median_increase_in_percent = 0.20,
446
  flood_depth_reduction = 0.20,
447
  damage_curve_suppress_factor = 0.9,
448
+ earthquake_simulation_method = 'legacy'
449
  ):
450
  print('threshold_flood', threshold_flood)
451
  print('cdf_median_increase_in_percent',cdf_median_increase_in_percent)
 
457
 
458
  gem_fragility = True if isinstance(df_hazard, dict) else False
459
  print('gem_fragility mode', gem_fragility)
460
+ if hazard_type == 'landslide' or \
461
+ (hazard_type == 'earthquake' and earthquake_simulation_method == 'monte carlo'):
462
+ # do not assign a fixed seed, we are in a simulation zone
463
+ pass
464
+ else:
465
  np.random.seed(seed=0)
466
 
467
  column_names = {'zoneID':'zoneid','bldID':'bldid','nHouse':'nhouse',
 
727
  return prob_ds1, prob_ds2, prob_ds3, prob_ds4
728
  bld_eq[['prob_ds1','prob_ds2','prob_ds3','prob_ds4']] = bld_eq.apply(computer_damage_state, axis=1,result_type='expand')
729
 
730
+ # legacy (most common approach)
731
+ if earthquake_simulation_method == 'legacy':
732
+ bld_eq[['prob_ds0','prob_ds5']] = [1,0]
733
+ for i in [1,2,3,4,5]:
734
+ bld_eq[f'ds_{i}'] = np.abs(bld_eq[f'prob_ds{i-1}'] - bld_eq[f'prob_ds{i}'])
735
+ df_ds = bld_eq[['ds_1','ds_2','ds_3','ds_4','ds_5']]
736
+ bld_eq['eq_ds'] = df_ds.idxmax(axis='columns').str.extract(r'ds_([0-9]+)').astype('int') - 1
737
+ elif earthquake_simulation_method == 'monte carlo':
738
+ bld_eq['eq_ds'] = pd.concat([(bld_eq['rnd'] < bld_eq[f'prob_ds{i}']).astype(int) for i in [1,2,3,4]], axis=1).sum(axis=1)
739
+
740
  casualty_rates = np.array([0, 0.05, 0.28, 1.152, 74.41]) # percent
741
  bld_eq['casualy'] = 0
742
  bld_eq = bld_eq.assign(casualty=lambda x: casualty_rates[x['eq_ds']] * x['residents'] / 100)
tomorrowcities/pages/engine.py CHANGED
@@ -1404,6 +1404,7 @@ def ExecutePanel():
1404
  flood_depth_reduction = layers.value['flood_depth_reduction'].value
1405
  cdf_median_increase_in_percent = layers.value['cdf_median_increase_in_percent'].value
1406
  damage_curve_suppress_factor = layers.value['damage_curve_suppress_factor'].value
 
1407
  threshold_flood = [threshold_flood_ds2.value, threshold_flood_ds3.value, threshold_flood_ds4.value]
1408
 
1409
  policies = [p['id'] for _, p in layers.value['policies'].items() if f"{p['description']} ({p['label']})" in layers.value['selected_policies'].value]
@@ -1433,7 +1434,8 @@ def ExecutePanel():
1433
  earthquake_intensity_unit=earthquake_intensity_unit,
1434
  cdf_median_increase_in_percent=cdf_median_increase_in_percent,
1435
  flood_depth_reduction=flood_depth_reduction,
1436
- damage_curve_suppress_factor=damage_curve_suppress_factor
 
1437
  )
1438
  else:
1439
  if fragility is None:
@@ -1452,6 +1454,7 @@ def ExecutePanel():
1452
  cdf_median_increase_in_percent=cdf_median_increase_in_percent,
1453
  flood_depth_reduction=flood_depth_reduction,
1454
  damage_curve_suppress_factor=damage_curve_suppress_factor,
 
1455
  )
1456
  buildings['ds'] = list(df_bld_hazard['ds'])
1457
  buildings['casualty'] = list(df_bld_hazard['casualty'])
 
1404
  flood_depth_reduction = layers.value['flood_depth_reduction'].value
1405
  cdf_median_increase_in_percent = layers.value['cdf_median_increase_in_percent'].value
1406
  damage_curve_suppress_factor = layers.value['damage_curve_suppress_factor'].value
1407
+ earthquake_simulation_method = layers.value['earthquake_simulation_method_selected'].value
1408
  threshold_flood = [threshold_flood_ds2.value, threshold_flood_ds3.value, threshold_flood_ds4.value]
1409
 
1410
  policies = [p['id'] for _, p in layers.value['policies'].items() if f"{p['description']} ({p['label']})" in layers.value['selected_policies'].value]
 
1434
  earthquake_intensity_unit=earthquake_intensity_unit,
1435
  cdf_median_increase_in_percent=cdf_median_increase_in_percent,
1436
  flood_depth_reduction=flood_depth_reduction,
1437
+ damage_curve_suppress_factor=damage_curve_suppress_factor,
1438
+ earthquake_simulation_method=earthquake_simulation_method,
1439
  )
1440
  else:
1441
  if fragility is None:
 
1454
  cdf_median_increase_in_percent=cdf_median_increase_in_percent,
1455
  flood_depth_reduction=flood_depth_reduction,
1456
  damage_curve_suppress_factor=damage_curve_suppress_factor,
1457
+ earthquake_simulation_method=earthquake_simulation_method,
1458
  )
1459
  buildings['ds'] = list(df_bld_hazard['ds'])
1460
  buildings['casualty'] = list(df_bld_hazard['casualty'])