Huseyin Kaya commited on
Commit
ad3a782
·
unverified ·
2 Parent(s): 83426af 06f2c34

Merge pull request #43 from TomorrowsCities/redefine_population_displacement

Browse files
tomorrowcities/backend/engine.py CHANGED
@@ -747,7 +747,12 @@ def compute(gdf_landuse, gdf_buildings, df_household, df_individual,gdf_intensit
747
  return bld_hazard
748
 
749
 
750
- def calculate_metrics(gdf_buildings, df_household, df_individual, infra, hazard_type, policies=[],capacity=1.0):
 
 
 
 
 
751
  # only use necessary columns
752
  bld_hazard = gdf_buildings[['bldid','ds','expstr','occupancy','storeys',
753
  'code_level','material','nhouse','residents','hospital_access','has_power','casualty']]
@@ -783,7 +788,7 @@ def calculate_metrics(gdf_buildings, df_household, df_individual, infra, hazard_
783
  .merge(df_workers[['individ','ds','has_power']].rename(columns={'ds':'ds_workplace','has_power':'workplace_power'}),on='individ', how='left')\
784
  .merge(df_students[['individ','ds','has_power']].rename(columns={'ds':'ds_school','has_power':'school_power'}), on='individ', how='left')\
785
  .merge(df_indiv_household[['individ','ds','hospital_access','hospital_has_power']].rename(columns={'ds':'ds_household'}), on='individ',how='left')\
786
- .merge(df_household[['hhid','bldid']],on='hhid',how='left')
787
 
788
  DS_NO = 0
789
  DS_SLIGHT = 1
@@ -816,7 +821,7 @@ def calculate_metrics(gdf_buildings, df_household, df_individual, infra, hazard_
816
  if 'power' in infra:
817
  metric1_index = (metric1_index) | (df_workers['has_power'] == False)
818
  if 'road' in infra:
819
- metric1_index = (metric1_index) | (df_workers['facility_access'] == False)
820
  df_workers_per_building = df_workers[metric1_index][['individ','hhid','ds']].merge(
821
  df_household[['hhid','bldid']],on='hhid',how='left').groupby(
822
  'bldid',as_index=False).agg({'individ':'count'})
@@ -832,7 +837,7 @@ def calculate_metrics(gdf_buildings, df_household, df_individual, infra, hazard_
832
  if 'power' in infra:
833
  metric2_index = (metric2_index) | (df_students['has_power'] == False)
834
  if 'road' in infra:
835
- metric2_index = (metric2_index) | (df_students['facility_access'] == False)
836
  df_students_per_building = df_students[metric2_index][['individ','hhid','ds']].merge(
837
  df_household[['hhid','bldid']],on='hhid',how='left').groupby(
838
  'bldid',as_index=False).agg({'individ':'count'})
@@ -895,17 +900,20 @@ def calculate_metrics(gdf_buildings, df_household, df_individual, infra, hazard_
895
 
896
  # metric 7 the number of displaced individuals in each building
897
  # more info: an individual is displaced if at least of the conditions below hold
898
- metric7_index = (df_displaced_indiv['ds_household'] > thresholds['metric6']) |\
899
- (df_displaced_indiv['ds_school'] > thresholds['metric2']) |\
900
- (df_displaced_indiv['ds_workplace'] > thresholds['metric1']) |\
901
- (df_displaced_indiv['ds_hospital'] > thresholds['metric4'])
 
902
  if 'road' in infra:
903
- metric7_index = (metric7_index) | (df_displaced_indiv['hospital_access'] == False)
904
- metric7_index = (metric7_index) | (df_displaced_indiv['facility_access'] == False)
 
 
905
  if 'power' in infra:
906
- metric7_index = (metric7_index) | (df_displaced_indiv['hospital_has_power'] == False)
907
- metric7_index = (metric7_index) | (df_displaced_indiv['workplace_power'] == False)
908
- metric7_index = (metric7_index) | (df_displaced_indiv['school_power'] == False)
909
  df_disp_per_bld = df_displaced_indiv[metric7_index]\
910
  .groupby('bldid',as_index=False)\
911
  .agg({'individ':'count'})
 
747
  return bld_hazard
748
 
749
 
750
+ def calculate_metrics(gdf_buildings, df_household, df_individual, infra, hazard_type, population_displacement_consensus, policies=[],capacity=1.0):
751
+ # Very handy temporary attributes showin if an individual is associated with a facility
752
+ # and lost access to facility
753
+ df_individual['has_facility'] = df_individual['indivfacid'].apply(lambda x: x > -1)
754
+ df_individual['lost_facility_access'] = df_individual.apply(lambda x: x['has_facility'] and not x['facility_access'], axis=1)
755
+
756
  # only use necessary columns
757
  bld_hazard = gdf_buildings[['bldid','ds','expstr','occupancy','storeys',
758
  'code_level','material','nhouse','residents','hospital_access','has_power','casualty']]
 
788
  .merge(df_workers[['individ','ds','has_power']].rename(columns={'ds':'ds_workplace','has_power':'workplace_power'}),on='individ', how='left')\
789
  .merge(df_students[['individ','ds','has_power']].rename(columns={'ds':'ds_school','has_power':'school_power'}), on='individ', how='left')\
790
  .merge(df_indiv_household[['individ','ds','hospital_access','hospital_has_power']].rename(columns={'ds':'ds_household'}), on='individ',how='left')\
791
+ .merge(df_household[['hhid','bldid','has_power']].rename(columns={'has_power':'household_power'}),on='hhid',how='left')
792
 
793
  DS_NO = 0
794
  DS_SLIGHT = 1
 
821
  if 'power' in infra:
822
  metric1_index = (metric1_index) | (df_workers['has_power'] == False)
823
  if 'road' in infra:
824
+ metric1_index = (metric1_index) | (df_workers['lost_facility_access'] == True)
825
  df_workers_per_building = df_workers[metric1_index][['individ','hhid','ds']].merge(
826
  df_household[['hhid','bldid']],on='hhid',how='left').groupby(
827
  'bldid',as_index=False).agg({'individ':'count'})
 
837
  if 'power' in infra:
838
  metric2_index = (metric2_index) | (df_students['has_power'] == False)
839
  if 'road' in infra:
840
+ metric2_index = (metric2_index) | (df_students['lost_facility_access'] == True)
841
  df_students_per_building = df_students[metric2_index][['individ','hhid','ds']].merge(
842
  df_household[['hhid','bldid']],on='hhid',how='left').groupby(
843
  'bldid',as_index=False).agg({'individ':'count'})
 
900
 
901
  # metric 7 the number of displaced individuals in each building
902
  # more info: an individual is displaced if at least of the conditions below hold
903
+ print('population_displacement_consensus', population_displacement_consensus)
904
+ # direct damage
905
+ metric7_index = (df_displaced_indiv[['ds_household','ds_school','ds_workplace','ds_hospital']] \
906
+ > [thresholds['metric6'],thresholds['metric2'],thresholds['metric1'],thresholds['metric4']]).sum(axis=1) >= population_displacement_consensus
907
+ # inaccebility
908
  if 'road' in infra:
909
+ inaccesibility_index = (df_displaced_indiv[['hospital_access','lost_facility_access']] \
910
+ == [False,True]).sum(axis=1) >= population_displacement_consensus
911
+ metric7_index = (metric7_index) | (inaccesibility_index)
912
+ # power loss
913
  if 'power' in infra:
914
+ power_loss_index = (df_displaced_indiv[['household_power','hospital_has_power','workplace_power','school_power']] \
915
+ == [False,False,False,False]).sum(axis=1) >= population_displacement_consensus
916
+ metric7_index = (metric7_index) | (power_loss_index)
917
  df_disp_per_bld = df_displaced_indiv[metric7_index]\
918
  .groupby('bldid',as_index=False)\
919
  .agg({'individ':'count'})
tomorrowcities/pages/engine.py CHANGED
@@ -22,7 +22,8 @@ import logging, sys
22
  import pickle
23
  import datetime
24
  from .settings import storage, landslide_max_trials, revive_storage
25
- from .settings import threshold_flood, threshold_flood_distance, threshold_road_water_height, threshold_culvert_water_height, preserve_edge_directions
 
26
  from ..backend.engine import compute, compute_power_infra, compute_road_infra, calculate_metrics, generate_exposure
27
  from ..backend.utils import building_preprocess, identity_preprocess, ParameterFile
28
  from .utilities import S3FileBrowser, extension_list, extension_list_w_dots
@@ -1229,7 +1230,8 @@ def ExecutePanel():
1229
  # capacity = 1
1230
  computed_metrics, df_metrics = calculate_metrics(buildings, household,
1231
  individual, layers.value['infra'].value,
1232
- layers.value['hazard'].value, policies=policies,capacity=1)
 
1233
  print(computed_metrics)
1234
  for metric in df_metrics.keys():
1235
  buildings[metric] = list(df_metrics[metric][metric])
 
22
  import pickle
23
  import datetime
24
  from .settings import storage, landslide_max_trials, revive_storage
25
+ from .settings import threshold_flood, threshold_flood_distance, threshold_road_water_height, threshold_culvert_water_height, preserve_edge_directions,\
26
+ population_displacement_consensus
27
  from ..backend.engine import compute, compute_power_infra, compute_road_infra, calculate_metrics, generate_exposure
28
  from ..backend.utils import building_preprocess, identity_preprocess, ParameterFile
29
  from .utilities import S3FileBrowser, extension_list, extension_list_w_dots
 
1230
  # capacity = 1
1231
  computed_metrics, df_metrics = calculate_metrics(buildings, household,
1232
  individual, layers.value['infra'].value,
1233
+ layers.value['hazard'].value, population_displacement_consensus.value,
1234
+ policies=policies,capacity=1)
1235
  print(computed_metrics)
1236
  for metric in df_metrics.keys():
1237
  buildings[metric] = list(df_metrics[metric][metric])
tomorrowcities/pages/settings.py CHANGED
@@ -24,6 +24,7 @@ threshold_flood_distance = solara.reactive(10)
24
  threshold_road_water_height = solara.reactive(0.3)
25
  threshold_culvert_water_height = solara.reactive(1.5)
26
  preserve_edge_directions = solara.reactive(False)
 
27
 
28
  def storage_control(aws_access_key_id: str, aws_secret_access_key: str, region_name: str, bucket_name: str):
29
  storage.value = S3Storage(aws_access_key_id, aws_secret_access_key, region_name, bucket_name)
@@ -106,6 +107,10 @@ def Page(name: Optional[str] = None, page: int = 0, page_size=100):
106
  if err_message != '':
107
  solara.Error(err_message)
108
 
 
 
 
 
109
  with solara.Card(title='Connectivity Parameters',subtitle='Parameters effecting connectivity analysis in road/power networks'):
110
  solara.Checkbox(label='Preserve directions in graph edges', value=preserve_edge_directions)
111
 
 
24
  threshold_road_water_height = solara.reactive(0.3)
25
  threshold_culvert_water_height = solara.reactive(1.5)
26
  preserve_edge_directions = solara.reactive(False)
27
+ population_displacement_consensus = solara.reactive(2)
28
 
29
  def storage_control(aws_access_key_id: str, aws_secret_access_key: str, region_name: str, bucket_name: str):
30
  storage.value = S3Storage(aws_access_key_id, aws_secret_access_key, region_name, bucket_name)
 
107
  if err_message != '':
108
  solara.Error(err_message)
109
 
110
+ with solara.Card(title='Metric-related Parameters',
111
+ subtitle='Minimum number of direct damage, inaccessibility or power lost should be observed to claim a population displacement.'):
112
+ solara.Select(label='population displacement consensus', values=[1,2,3,4], value=population_displacement_consensus)
113
+
114
  with solara.Card(title='Connectivity Parameters',subtitle='Parameters effecting connectivity analysis in road/power networks'):
115
  solara.Checkbox(label='Preserve directions in graph edges', value=preserve_edge_directions)
116