Commit b1f36533 authored by Marius Kriegerowski's avatar Marius Kriegerowski
Browse files

Removed code duplication

parent 25685dc5
Pipeline #36840 passed with stage
in 3 minutes and 7 seconds
......@@ -38,8 +38,8 @@ def damage_calculator(
exposure_format,
fragility_source,
intensity_measure_map_filepath,
taxonomy_map_filepath,
gm_field: GMField,
taxonomy_map_filepath: str = None,
interpolation_method="linear",
result_filepath="damage_result.csv",
output_format="gpkg",
......@@ -374,8 +374,8 @@ def main():
exposure_format,
fragility_source,
intensity_measure_map,
taxonomy_map_filepath,
gm_field,
taxonomy_map_filepath,
interpolation_method,
result_filepath,
output_format,
......
......@@ -25,6 +25,16 @@ import numpy as np
from scipy.stats import lognorm
DEFAULT_INTENSITY_MEASURE_MAP = {
"SA(0.3)": "gmv_SA(0.3)",
"SA(0.6)": "gmv_SA(0.6)",
"SA(1.0)": "gmv_SA(1.0)",
"SA(1)": "gmv_SA(1.0)",
"PGA": "gmv_PGA",
"MMI": "gmv_MMI",
}
class FragilityModel:
"""
General class to handle a complete fragility model as a set of fragility functions
......@@ -254,27 +264,14 @@ class FragilityModel:
self.fragility_functions[taxonomy] = parameters
# Read the intensity measure map into a dictionary
if intensity_measure_map_filepath is not None:
with open(intensity_measure_map_filepath, mode="r") as input_file:
reader = csv.reader(input_file)
next(reader)
self.intensity_measure_map = {rows[0]: rows[1] for rows in reader}
if intensity_measure_map_filepath is None:
self.intensity_measure_map = DEFAULT_INTENSITY_MEASURE_MAP
else:
self.intensity_measure_map = {
"SA(0.3)": "gmv_SA(0.3)",
"SA(0.6)": "gmv_SA(0.6)",
"SA(1.0)": "gmv_SA(1.0)",
"SA(1)": "gmv_SA(1.0)",
"PGA": "gmv_PGA",
"MMI": "gmv_MMI",
}
self.intensity_measure_map = self.read_mapping_file(intensity_measure_map_filepath)
# Read the taxonomy map if filename provided
if taxonomy_map_filepath is not None:
with open(taxonomy_map_filepath, mode="r") as input_file:
reader = csv.reader(input_file)
next(reader)
self.taxonomy_map = {rows[0]: rows[1] for rows in reader}
self.taxonomy_map = self.read_mapping_file(taxonomy_map_filepath)
im_types_dict = {}
for index, value in enumerate(im_types):
......@@ -409,3 +406,11 @@ class FragilityModel:
poos = list(-1 * np.diff(poes))
poos.append(poes[-1])
return poes[1:5], poos, im_value, im_type
@staticmethod
def read_mapping_file(filepath: str) -> dict[str, str]:
"""Reads a mapping table from `filepath` into a dictionary."""
with open(filepath, mode="r") as input_file:
reader = csv.reader(input_file)
next(reader)
return {rows[0]: rows[1] for rows in reader}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment