Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Dynamic Exposure
Global Dynamic Exposure
losscalculator
Commits
504a8ca7
Commit
504a8ca7
authored
Mar 09, 2021
by
Tara Evaz Zadeh
Committed by
Tara Evaz Zadeh
Mar 17, 2021
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
Changes
2
Pipelines
4
Hide whitespace changes
Inline
Side-by-side
losscalculator/damage_calculator.py
View file @
504a8ca7
...
...
@@ -30,7 +30,7 @@ def damage_calculator(
exposure_filepath
,
fragility_pathname
,
taxonomy_conversion_filepath
,
g
round_motion
_field_filepath
,
g
m
_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
)
g
round_motion
_field
=
np
.
loadtxt
(
g
round_motion
_field_filepath
,
delimiter
=
","
,
skiprows
=
1
)
g
m
_field
=
np
.
loadtxt
(
g
m
_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_g
round_motion
_field
=
losslib
.
get_full_
GMF
(
g
round_motion
_field
,
exposure
.
lon
,
exposure
.
lat
,
interpolation_method
full_g
m
_field
=
losslib
.
get_full_
gm_field
(
g
m
_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.
g
round_motion
_type_index_map
=
{}
with
open
(
g
round_motion
_field_filepath
)
as
gmf
:
g
m
_type_index_map
=
{}
with
open
(
g
m
_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 `g
round_motion
_type_index_map` as the dictionary
# ground-motion-field file) to the `g
m
_type_index_map` as the dictionary
# key and value, respectively.
g
round_motion
_type_index_map
[
gm_types
[
i
]]
=
i
+
2
g
m
_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
(
g
round_motion
_type_index_map
,
taxonomy_to_fragility_source
,
fragility_pathname
g
m
_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_g
round_motion
_field
[
asset
,
taxonomy_to_fragility_map
[
taxonomy
][
1
]]
gm_value
=
full_g
m
_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
g
round_motion
_field_filepath
=
args
.
ground_motion_field
g
m
_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
,
g
round_motion
_field_filepath
,
g
m
_field_filepath
,
interpolation_method
,
result_filepath
,
)
...
...
losscalculator/losslib.py
View file @
504a8ca7
...
...
@@ -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:
------
- g
round_motion
_field: (Numpy nd-array of shape(m,n); m records with long,
- g
m
_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 g
round_motion
_field contains values for
Please note that if the g
m
_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:
>>> g
round_motion
_field
>>> g
m
_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_g
round_motion
_field: (numpy-ndarray of shape(a,n))
- full_g
m
_field: (numpy-ndarray of shape(a,n))
an array of logitudes and latitudes along with their interpolated values
Example extract:
>>> full_g
round_motion
_field
>>> full_g
m
_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
((
g
round_motion
_field
[:,
0
],
g
round_motion
_field
[:,
1
])).
transpose
()
points_given
=
np
.
vstack
((
g
m
_field
[:,
0
],
g
m
_field
[:,
1
])).
transpose
()
# points_todo: points to do interpolation over
points_todo
=
np
.
vstack
((
lons
,
lats
)).
transpose
()
full_g
round_motion
_field
=
np
.
vstack
((
np
.
array
(
lons
),
np
.
array
(
lats
))).
transpose
()
full_g
m
_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 g
round_motion
_field.
for
gm_type
in
range
(
2
,
g
round_motion
_field
.
shape
[
1
],
1
):
# to the last column of the g
m
_field.
for
gm_type
in
range
(
2
,
g
m
_field
.
shape
[
1
],
1
):
# gmvs_given: the input ground-motion values for interpolation.
gmvs_given
=
g
round_motion
_field
[:,
gm_type
]
gmvs_given
=
g
m
_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_g
round_motion
):
def
find_SA_frequency
(
SA_g
m
):
"""
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_g
round_motion
: (string)
- SA_g
m
: (string)
Example extract:
>>> SA_g
round_motion
>>> SA_g
m
'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_g
round_motion
)[
0
])
SA_frequency
=
float
(
re
.
findall
(
r
"\d+\.\d+|\d+"
,
SA_g
m
)[
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:
------
- g
round_motion
_type_index_map: (dictionary)
- g
m
_type_index_map: (dictionary)
key: ground-motion type; value: column number in the ground-motion
field file
Example extract:
>>> g
round_motion
_type_index_map
>>> g
m
_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 g
round_motion
_type_index_map dictionary keys which are
# Looping through the g
m
_type_index_map dictionary keys which are
# the ground-motion types of the ground-motion field file
for
g
round_motion_type
in
ground_motion
_type_index_map
:
for
g
m_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_g
round_motion
_type_index_map
==
SA_type_in_fragility_function
:
if
SA_type_in_g
m
_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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment