Skip to content

Commit

Permalink
Merge pull request #45 from ioannis-vm/main
Browse files Browse the repository at this point in the history
FIX - Improve membership checks, sample size
  • Loading branch information
zsarnoczay authored May 8, 2024
2 parents bab6c24 + 5cbe940 commit 30c4d43
Showing 1 changed file with 67 additions and 32 deletions.
99 changes: 67 additions & 32 deletions pelicun/tools/DL_calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,15 +371,16 @@ def run_pelicun(
# f"{config['commonFileDir']}/CustomDLModels/"
custom_dl_file_path = custom_model_dir

if 'DL' not in config:
if ('DL' not in config) or (not config['DL']):
log_msg("Damage and Loss configuration missing from config file. ")

if auto_script_path is not None:
log_msg("Trying to auto-populate")

config_ap, CMP = auto_populate(config, auto_script_path)

if config_ap['DL'] is None:
if not config_ap['DL']:

log_msg(
"The prescribed auto-population script failed to identify "
"a valid damage and loss configuration for this asset. "
Expand All @@ -389,7 +390,9 @@ def run_pelicun(
return 0

# add the demand information
config_ap['DL']['Demands'].update({'DemandFilePath': f'{demand_file}'})
config_ap['DL']['Demands'].update(
{'DemandFilePath': f'{demand_file}', 'SampleSize': f'{realizations}'}
)

if coupled_EDP is True:
config_ap['DL']['Demands'].update({"CoupledDemands": True})
Expand All @@ -413,7 +416,7 @@ def run_pelicun(
else:
config_ap['DL'].update({'Outputs': full_out_config})
# add output settings from regional output config
if 'Settings' not in config_ap['DL']['Outputs'].keys():
if 'Settings' not in config_ap['DL']['Outputs']:
config_ap['DL']['Outputs'].update({'Settings': {}})

config_ap['DL']['Outputs']['Settings'].update(
Expand Down Expand Up @@ -454,7 +457,7 @@ def run_pelicun(
sample_size = int(sample_size_str)

# provide all outputs if the files are not specified
if 'Outputs' not in config:
if ('Outputs' not in config) or (not config['Outputs']):
config['DL']['Outputs'] = full_out_config

# provide outputs in CSV by default
Expand All @@ -476,11 +479,11 @@ def run_pelicun(
if ('Settings' in config['DL']['Outputs'].keys()) is False:
config['DL']['Outputs'].update({'Settings': pbe_settings})

if 'Asset' not in config['DL']:
if ('Asset' not in config['DL']) or (not config['DL']['Asset']):
log_msg("Asset configuration missing. Terminating analysis.")
return -1

if 'Demands' not in config['DL']:
if ('Demands' not in config['DL']) or (not config['DL']['Demands']):
log_msg("Demand configuration missing. Terminating analysis.")
return -1

Expand All @@ -502,7 +505,7 @@ def run_pelicun(
# If the user did not prescribe anything for ListAllDamageStates,
# then use True as default for DL_calculations regardless of what
# the Pelicun default is.
if "ListAllDamageStates" not in options.keys():
if "ListAllDamageStates" not in options:
options.update({"ListAllDamageStates": True})

PAL = Assessment(options)
Expand Down Expand Up @@ -632,7 +635,8 @@ def run_pelicun(
# save results
if 'Demand' in config['DL']['Outputs']:
out_reqs = [
out if val else "" for out, val in config['DL']['Outputs']['Demand'].items()
out if val else ""
for out, val in config['DL']['Outputs']['Demand'].items()
]

if np.any(np.isin(['Sample', 'Statistics'], out_reqs)):
Expand Down Expand Up @@ -719,7 +723,9 @@ def run_pelicun(
cmp_marginals.loc['excessive.coll.DEM', 'Units'] = 'ea'

locs = demand_sample[coll_DEM].columns.unique(level=0)
cmp_marginals.loc['excessive.coll.DEM', 'Location'] = ','.join(locs)
cmp_marginals.loc['excessive.coll.DEM', 'Location'] = ','.join(
locs
)

dirs = demand_sample[coll_DEM].columns.unique(level=1)
cmp_marginals.loc['excessive.coll.DEM', 'Direction'] = ','.join(
Expand Down Expand Up @@ -798,7 +804,8 @@ def run_pelicun(
)

out_reqs = [
out if val else "" for out, val in config['DL']['Outputs']['Asset'].items()
out if val else ""
for out, val in config['DL']['Outputs']['Asset'].items()
]

if np.any(np.isin(['Sample', 'Statistics'], out_reqs)):
Expand Down Expand Up @@ -867,7 +874,9 @@ def run_pelicun(
):
component_db = [
'PelicunDefault/'
+ default_DBs['fragility'][config['DL']['Asset']['ComponentDatabase']],
+ default_DBs['fragility'][
config['DL']['Asset']['ComponentDatabase']
],
]
else:
component_db = []
Expand Down Expand Up @@ -925,9 +934,9 @@ def run_pelicun(
adf.loc[coll_CMP_name, ('Demand', 'Type')] = coll_DEM_name

else:
adf.loc[
coll_CMP_name, ('Demand', 'Type')
] = f'{coll_DEM_name}|{coll_DEM_spec}'
adf.loc[coll_CMP_name, ('Demand', 'Type')] = (
f'{coll_DEM_name}|{coll_DEM_spec}'
)

coll_DEM_unit = add_units(
pd.DataFrame(
Expand Down Expand Up @@ -983,9 +992,9 @@ def run_pelicun(
# input file
adf.loc['excessiveRID', ('Demand', 'Directional')] = 1
adf.loc['excessiveRID', ('Demand', 'Offset')] = 0
adf.loc[
'excessiveRID', ('Demand', 'Type')
] = 'Residual Interstory Drift Ratio'
adf.loc['excessiveRID', ('Demand', 'Type')] = (
'Residual Interstory Drift Ratio'
)

adf.loc['excessiveRID', ('Demand', 'Unit')] = 'unitless'
adf.loc['excessiveRID', ('LS1', 'Theta_0')] = irrep_config[
Expand Down Expand Up @@ -1078,7 +1087,9 @@ def run_pelicun(

for target_val in target_vals:
for cmp_type, cmp_id in cmp_map.items():
if (cmp_type in target_val) and (cmp_id != ''):
if (cmp_type in target_val) and (
cmp_id != ''
):
target_val = target_val.replace(
cmp_type, cmp_id
)
Expand All @@ -1103,7 +1114,9 @@ def run_pelicun(
dmg_process = None

else:
log_msg(f"Prescribed Damage Process not recognized: " f"{dp_approach}")
log_msg(
f"Prescribed Damage Process not recognized: " f"{dp_approach}"
)

# calculate damages
PAL.damage.calculate(sample_size, dmg_process=dmg_process)
Expand All @@ -1119,9 +1132,13 @@ def run_pelicun(
)
is True
):
damage_units = damage_units.groupby(level=[0, 1, 2, 4], axis=1).first()
damage_units = damage_units.groupby(
level=[0, 1, 2, 4], axis=1
).first()

damage_groupby_uid = damage_sample.groupby(level=[0, 1, 2, 4], axis=1)
damage_groupby_uid = damage_sample.groupby(
level=[0, 1, 2, 4], axis=1
)

damage_sample = damage_groupby_uid.sum().mask(
damage_groupby_uid.count() == 0, np.nan
Expand All @@ -1145,7 +1162,9 @@ def run_pelicun(
damage_sample_s.to_csv(
output_path / "DMG_sample.zip",
index_label=damage_sample_s.columns.name,
compression=dict(method='zip', archive_name='DMG_sample.csv'),
compression=dict(
method='zip', archive_name='DMG_sample.csv'
),
)
output_files.append('DMG_sample.zip')

Expand All @@ -1167,14 +1186,18 @@ def run_pelicun(
)
is True
):
damage_groupby = damage_sample.groupby(level=[0, 1, 3], axis=1)
damage_groupby = damage_sample.groupby(
level=[0, 1, 3], axis=1
)

damage_units = damage_units.groupby(
level=[0, 1, 3], axis=1
).first()

else:
damage_groupby = damage_sample.groupby(level=[0, 1, 4], axis=1)
damage_groupby = damage_sample.groupby(
level=[0, 1, 4], axis=1
)

damage_units = damage_units.groupby(
level=[0, 1, 4], axis=1
Expand Down Expand Up @@ -1234,7 +1257,9 @@ def run_pelicun(
grp_damage_s.to_csv(
output_path / "DMG_grp.zip",
index_label=grp_damage_s.columns.name,
compression=dict(method='zip', archive_name='DMG_grp.csv'),
compression=dict(
method='zip', archive_name='DMG_grp.csv'
),
)
output_files.append('DMG_grp.zip')

Expand Down Expand Up @@ -1445,7 +1470,9 @@ def run_pelicun(
adf.loc[rcarb, ('DS1', 'Theta_0')] = rCarbon_config["Median"]

if pd.isna(rCarbon_config.get('Distribution', np.nan)) is False:
adf.loc[rcarb, ('DS1', 'Family')] = rCarbon_config["Distribution"]
adf.loc[rcarb, ('DS1', 'Family')] = rCarbon_config[
"Distribution"
]
adf.loc[rcarb, ('DS1', 'Theta_1')] = rCarbon_config["Theta_1"]
else:
# add a default replacement carbon value as a placeholder
Expand Down Expand Up @@ -1537,7 +1564,9 @@ def run_pelicun(
drivers.append(f'DMG-{dmg_cmp}')
loss_models.append(loss_cmp)

loss_map = pd.DataFrame(loss_models, columns=['Repair'], index=drivers)
loss_map = pd.DataFrame(
loss_models, columns=['Repair'], index=drivers
)

elif repair_config['MapApproach'] == "User Defined":
if repair_config.get('MapFilePath', False) is not False:
Expand Down Expand Up @@ -1605,7 +1634,8 @@ def run_pelicun(
)

out_reqs = [
out if val else "" for out, val in out_config_loss['Repair'].items()
out if val else ""
for out, val in out_config_loss['Repair'].items()
]

if np.any(
Expand Down Expand Up @@ -1652,7 +1682,9 @@ def run_pelicun(
if np.any(
np.isin(['GroupedSample', 'GroupedStatistics'], out_reqs)
):
repair_groupby = repair_sample.groupby(level=[0, 1, 2], axis=1)
repair_groupby = repair_sample.groupby(
level=[0, 1, 2], axis=1
)

repair_units = repair_units.groupby(
level=[0, 1, 2], axis=1
Expand All @@ -1665,7 +1697,9 @@ def run_pelicun(
if 'GroupedSample' in out_reqs:
grp_repair_s = pd.concat([grp_repair, repair_units])

grp_repair_s = convert_to_SimpleIndex(grp_repair_s, axis=1)
grp_repair_s = convert_to_SimpleIndex(
grp_repair_s, axis=1
)
grp_repair_s.to_csv(
output_path / "DV_repair_grp.zip",
index_label=grp_repair_s.columns.name,
Expand Down Expand Up @@ -1780,7 +1814,8 @@ def run_pelicun(
out_dict.update(
{
"Units": {
col: df_units.loc["Units", col] for col in df_units.columns
col: df_units.loc["Units", col]
for col in df_units.columns
}
}
)
Expand Down

0 comments on commit 30c4d43

Please sign in to comment.