Commit 504a8ca7 authored by Tara Evaz Zadeh's avatar Tara Evaz Zadeh Committed by Tara Evaz Zadeh
Browse files

Added gmType to taxonomy_to_fragility_map in losslib.py and result file

parent d7f89f51
Pipeline #26157 passed with stage
in 1 minute and 53 seconds
......@@ -30,7 +30,7 @@ def damage_calculator(
exposure_filepath,
fragility_pathname,
taxonomy_conversion_filepath,
ground_motion_field_filepath,
gm_field_filepath,
interpolation_method="linear",
result_filepath="damage_result.csv",
):
......@@ -47,36 +47,37 @@ def damage_calculator(
taxonomy_to_fragility_source = csv.reader(open(taxonomy_conversion_filepath))
# Skipping the header.
next(taxonomy_to_fragility_source)
ground_motion_field = np.loadtxt(ground_motion_field_filepath, delimiter=",", skiprows=1)
gm_field = np.loadtxt(gm_field_filepath, delimiter=",", skiprows=1)
exposure = pd.read_csv(exposure_filepath)
# Interpolate the ground-motion values for all the assets of the exposure model.
full_ground_motion_field = losslib.get_full_GMF(
ground_motion_field, exposure.lon, exposure.lat, interpolation_method
full_gm_field = losslib.get_full_gm_field(
gm_field, exposure.lon, exposure.lat, interpolation_method
)
# Creating an empty dictionary to later fill with ground-motion types and their column
# numbers (as they appear in the ground-motion-field file) as its key and value.
ground_motion_type_index_map = {}
with open(ground_motion_field_filepath) as gmf:
gm_type_index_map = {}
with open(gm_field_filepath) as gmf:
# Extracting the ground-motion types available in the ground-motion-field file. Note
# the ground-motion-field file format:
# `[lon, lat, gmValueofType1, ..., gmValueofTypeN]`
gm_types = gmf.readline().strip().split(",")[2:]
for i in range(len(gm_types)):
# Appending each `gm_type` and its column number (as it appears in the
# ground-motion-field file) to the `ground_motion_type_index_map` as the dictionary
# ground-motion-field file) to the `gm_type_index_map` as the dictionary
# key and value, respectively.
ground_motion_type_index_map[gm_types[i]] = i + 2
gm_type_index_map[gm_types[i]] = i + 2
# Calling the function "taxonomy_to_fragility" to get a dictionary with keys as the
# taxonomy and the values as both the fragility function name (excluding the ".csv" part)
# and the column number of the respective ground-motion type in `ground-motion-field` file.
taxonomy_to_fragility_map = losslib.taxonomy_to_fragility(
ground_motion_type_index_map, taxonomy_to_fragility_source, fragility_pathname
gm_type_index_map, taxonomy_to_fragility_source, fragility_pathname
)
gm_values = []
all_gm_types = []
all_PoEs = []
all_PoOs = []
damage_by_assets = []
......@@ -85,6 +86,7 @@ def damage_calculator(
for asset in range(exposure.shape[0]):
taxonomy = exposure["taxonomy"][asset]
fragilityfunction_filename = taxonomy_to_fragility_map[taxonomy][0] + ".csv"
gm_type = taxonomy_to_fragility_map[taxonomy][2]
num_buildings = exposure["number"][asset]
# Read fragility functions as numpy arrays.
......@@ -95,9 +97,11 @@ def damage_calculator(
)
# Computing the ground motions for each asset (also for duplicate locations).
gm_value = full_ground_motion_field[asset, taxonomy_to_fragility_map[taxonomy][1]]
gm_value = full_gm_field[asset, taxonomy_to_fragility_map[taxonomy][1]]
gm_values.append(gm_value)
all_gm_types.append(gm_type)
# Computing probabilities of exceedance and occurrence.
[PoEs, PoOs] = losslib.get_PoEs(fragility_function, gm_value)
......@@ -109,7 +113,8 @@ def damage_calculator(
damage_by_assets.append(damage_by_asset)
# Append results
exposure["gm_values"] = gm_values
exposure["gm_value"] = gm_values
exposure["gm_type"] = all_gm_types
exposure["PoES"] = [all_PoEs for i in exposure.index][i]
exposure["PoOS"] = [all_PoOs for i in exposure.index][i]
......@@ -197,7 +202,7 @@ if __name__ == "__main__":
interpolation_method = args.interpolation_method
fragility_pathname = args.fragilities
taxonomy_conversion_filepath = args.taxonomy_map
ground_motion_field_filepath = args.ground_motion_field
gm_field_filepath = args.ground_motion_field
exposure_filepath = args.exposure
result_filepath = args.results
overwrite_result_file = args.overwrite
......@@ -218,7 +223,7 @@ if __name__ == "__main__":
exposure_filepath,
fragility_pathname,
taxonomy_conversion_filepath,
ground_motion_field_filepath,
gm_field_filepath,
interpolation_method,
result_filepath,
)
......
......@@ -24,7 +24,7 @@ from scipy import interpolate
import re
def get_full_GMF(ground_motion_field, lons, lats, method="linear"):
def get_full_gm_field(gm_field, lons, lats, method="linear"):
"""
Returns ground-motion values using 2-dimensional interpolation for the
......@@ -32,12 +32,12 @@ def get_full_GMF(ground_motion_field, lons, lats, method="linear"):
Input:
------
- ground_motion_field: (Numpy nd-array of shape(m,n); m records with long,
- gm_field: (Numpy nd-array of shape(m,n); m records with long,
lat, and n-2 gm-types)
contains locations and the ground-motion values of each location
in the format of lon, lat, gmValueofType1, gmValueofType2, ...,
gmValueofTypeN.
Please note that if the ground_motion_field contains values for
Please note that if the gm_field contains values for
more than one ground-motion type, This function will do
interpolation multiple times and each time using one of the
gmValueTypes with the format as below:
......@@ -45,7 +45,7 @@ def get_full_GMF(ground_motion_field, lons, lats, method="linear"):
As an example:
[23.6875,38.3402777778,'PGA','SA(0.3)','SA(0.6)','SA(1.0)']
Example extract:
>>> ground_motion_field
>>> gm_field
array([
[2.289e+01, 3.631e+01, 4.822e-03, 1.255e-02, 8.736e-03, 5.612e-03],
[2.289e+01, 3.632e+01, 4.830e-03, 1.257e-02, 8.748e-03, 5.619e-03],
......@@ -74,10 +74,10 @@ def get_full_GMF(ground_motion_field, lons, lats, method="linear"):
Output:
-------
- full_ground_motion_field: (numpy-ndarray of shape(a,n))
- full_gm_field: (numpy-ndarray of shape(a,n))
an array of logitudes and latitudes along with their interpolated values
Example extract:
>>> full_ground_motion_field
>>> full_gm_field
array([
[2.36e+01, 3.83e+01, 1.79e-01, 3.62e-01, 2.52e-01, 1.52e-01],
...,
......@@ -86,29 +86,27 @@ def get_full_GMF(ground_motion_field, lons, lats, method="linear"):
"""
# points_given: the input points for interpolation
points_given = np.vstack((ground_motion_field[:, 0], ground_motion_field[:, 1])).transpose()
points_given = np.vstack((gm_field[:, 0], gm_field[:, 1])).transpose()
# points_todo: points to do interpolation over
points_todo = np.vstack((lons, lats)).transpose()
full_ground_motion_field = np.vstack((np.array(lons), np.array(lats))).transpose()
full_gm_field = np.vstack((np.array(lons), np.array(lats))).transpose()
# Griddata Interpolation
# The loop changes over the columns of the ground motion field and enables
# us to have interpolation for all different given ground-motion types.
# Please note that the columns follow the order below:
# lon,lat,gmValueofType1,gmValueofType2,etc. We want to do the interpolation
# over all the ground motion types. Thus the range begins from third column
# to the last column of the ground_motion_field.
for gm_type in range(2, ground_motion_field.shape[1], 1):
# to the last column of the gm_field.
for gm_type in range(2, gm_field.shape[1], 1):
# gmvs_given: the input ground-motion values for interpolation.
gmvs_given = ground_motion_field[:, gm_type]
gmvs_given = gm_field[:, gm_type]
# gm_value_griddata : interpolated values for each ground motion type.
gm_value_griddata = griddata(points_given, gmvs_given, points_todo, method=method)
full_ground_motion_field = np.column_stack(
(full_ground_motion_field, gm_value_griddata)
)
return full_ground_motion_field
full_gm_field = np.column_stack((full_gm_field, gm_value_griddata))
return full_gm_field
def find_SA_frequency(SA_ground_motion):
def find_SA_frequency(SA_gm):
"""
Among the ground-motion types, SA-type naming includes an additional number
in parentheses indicating the frequency. This function extracts this
......@@ -117,9 +115,9 @@ def find_SA_frequency(SA_ground_motion):
Input:
------
- SA_ground_motion: (string)
- SA_gm: (string)
Example extract:
>>> SA_ground_motion
>>> SA_gm
'SA(1)'
Output:
......@@ -129,13 +127,11 @@ def find_SA_frequency(SA_ground_motion):
>>> SA_frequency
1.0
"""
SA_frequency = float(re.findall(r"\d+\.\d+|\d+", SA_ground_motion)[0])
SA_frequency = float(re.findall(r"\d+\.\d+|\d+", SA_gm)[0])
return SA_frequency
def taxonomy_to_fragility(
ground_motion_type_index_map, taxonomy_to_fragility_source, fragility_pathname
):
def taxonomy_to_fragility(gm_type_index_map, taxonomy_to_fragility_source, fragility_pathname):
"""
Creates an extended map of taxonomies to fragility function.
The input map 'taxonomy_to_fragility_source' contains the mapping for each
......@@ -144,11 +140,11 @@ def taxonomy_to_fragility(
Input:
------
- ground_motion_type_index_map: (dictionary)
- gm_type_index_map: (dictionary)
key: ground-motion type; value: column number in the ground-motion
field file
Example extract:
>>> ground_motion_type_index_map
>>> gm_type_index_map
{'gmv_PGA': 2, 'gmv_SA(0.3)': 3, 'gmv_SA(0.6)': 4, 'gmv_SA(1)': 5}
- taxonomy_to_fragility_source: (csv.reader)
......@@ -182,15 +178,20 @@ def taxonomy_to_fragility(
# Prepare return variable
taxonomy_to_fragility_map = {}
# List of ground-motion types in the ground-motion field file
gm_types_from_gm_field = list(gm_type_index_map.keys())
# List of ground-motion types column numbers in the groun-motion field file
gm_type_column_number_from_gm_field = list(gm_type_index_map.values())
# Loop through the taxonomy-to-fragility-function map
for taxonomy_map_entry in taxonomy_to_fragility_source:
# Open the fragility-function file corresponding to the taxonomy in
# 'taxonomy_map_entry[1]'
for map_entry in taxonomy_to_fragility_source:
# Open the fragility-function file corresponding to the taxonomy in 'map_entry[1]'
fragility_function = list(
csv.reader(open(fragility_pathname + "/" + taxonomy_map_entry[1] + ".csv"))
csv.reader(open(fragility_pathname + "/" + map_entry[1] + ".csv"))
)
# Check if already one fragility function for a given GM type has been selected
if taxonomy_map_entry[0] in taxonomy_to_fragility_map:
if map_entry[0] in taxonomy_to_fragility_map:
# Ignore the additional fragility function to keep everything unambiguous
continue
fragility_function_gmt = fragility_function[0][0]
......@@ -198,29 +199,34 @@ def taxonomy_to_fragility(
if re.search("SA", fragility_function_gmt):
# Extracting the SA frequency of the fragility function).
SA_type_in_fragility_function = find_SA_frequency(fragility_function_gmt)
# Looping through the ground_motion_type_index_map dictionary keys which are
# Looping through the gm_type_index_map dictionary keys which are
# the ground-motion types of the ground-motion field file
for ground_motion_type in ground_motion_type_index_map:
for gm_type in gm_type_index_map:
# Considering only ground-motion types of SA kind.
if ground_motion_type.startswith("gmv_SA("):
SA_type_in_ground_motion_type_index_map = find_SA_frequency(
ground_motion_type
)
if gm_type.startswith("gmv_SA("):
SA_type_in_gm_type_index_map = find_SA_frequency(gm_type)
# Checking if the SA frequency is the same in both
# fragility function and the ground-motion field file
if SA_type_in_ground_motion_type_index_map == SA_type_in_fragility_function:
if SA_type_in_gm_type_index_map == SA_type_in_fragility_function:
# Taking the column number of the ground-motion type (of the fragility
# function) in the ground-motion field file.
gmtype_column_number = ground_motion_type_index_map[ground_motion_type]
gm_type_column_number = gm_type_index_map[gm_type]
fragility_function_gm_type = gm_types_from_gm_field[
gm_type_column_number_from_gm_field.index(gm_type_column_number)
]
break
else:
# The header of the ground-motion-field file contains a `gmv_` prefix which
# is kept to stay compatible with `OpenQuake`.
gmtype_column_number = ground_motion_type_index_map["gmv_" + fragility_function_gmt]
taxonomy_to_fragility_map[taxonomy_map_entry[0]] = [
taxonomy_map_entry[1],
gmtype_column_number,
gm_type_column_number = gm_type_index_map["gmv_" + fragility_function_gmt]
fragility_function_gm_type = gm_types_from_gm_field[
gm_type_column_number_from_gm_field.index(gm_type_column_number)
]
taxonomy_to_fragility_map[map_entry[0]] = [
map_entry[1],
gm_type_column_number,
fragility_function_gm_type,
]
return taxonomy_to_fragility_map
......
Markdown is supported
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