Commit 0568bafd authored by Tara Evaz Zadeh's avatar Tara Evaz Zadeh
Browse files

Changed some variable and function names

parent e0870b00
Pipeline #18889 passed with stage
in 5 minutes and 19 seconds
cell_id;geometry
origin_id;geometry
cell_2423204507;POLYGON ((23.63055555555556 38.0611111111111, 23.63333333333335 38.0611111111111, 23.63333333333335 38.0638888888889, 23.63055555555556 38.0638888888889, 23.63055555555556 38.0611111111111))
......@@ -22,18 +22,20 @@ import csv
import datetime
def AddRespectiveCellIdToOBMExposureFiles(exposureFile_path, OSMSource_path, Result_path):
def append_respective_cell_id_to_building_exposure(
exposure_filepath, building_to_cell_id_filepath, result_filepath
):
startTime = datetime.datetime.now()
print(startTime)
OSMSource = csv.reader(open(OSMSource_path), delimiter=";")
next(OSMSource, None)
building_to_cell_id_source = csv.reader(open(building_to_cell_id_filepath), delimiter=";")
next(building_to_cell_id_source, None)
exposureSource = pd.read_csv(exposureFile_path)
originids = exposureSource.origin_id
exposure = pd.read_csv(exposure_filepath)
origin_ids = exposure.origin_id
with open(Result_path, "a+", newline="") as write_obj:
with open(result_filepath, "a+", newline="") as write_obj:
csv_writer = csv.writer(write_obj)
title = [
"id",
......@@ -47,21 +49,21 @@ def AddRespectiveCellIdToOBMExposureFiles(exposureFile_path, OSMSource_path, Res
"admin_name",
"admin_ID",
"origin_id",
"respectiveCellId",
"respective_cell_id",
]
csv_writer.writerow(title)
OSMIdToCellId = {}
for MappingItem in OSMSource:
if MappingItem[0] in OSMIdToCellId:
building_to_cell_id_map = {}
for mapping_item in building_to_cell_id_source:
if mapping_item[0] in building_to_cell_id_map:
# Ignore the additional geometry to keep everything unambiguous
pass
OSMIdToCellId[MappingItem[0]] = MappingItem[2]
building_to_cell_id_map[mapping_item[0]] = mapping_item[2]
for asset_from_model in range(exposureSource.shape[0]):
asset_for_calculation = list(exposureSource.loc[asset_from_model])
origin_id = originids[asset_from_model]
respectiveCellId = OSMIdToCellId[origin_id]
asset_for_calculation.append(respectiveCellId)
for asset_from_model in range(exposure.shape[0]):
asset_for_calculation = list(exposure.loc[asset_from_model])
origin_id = origin_ids[asset_from_model]
respective_cell_id = building_to_cell_id_map[origin_id]
asset_for_calculation.append(respective_cell_id)
csv_writer.writerow(asset_for_calculation)
print("Execution time of the script", (datetime.datetime.now() - startTime))
......@@ -30,9 +30,9 @@ def get_exposure_per_tile(
exposure_filepath,
fragility_pathname,
taxonomy_conversion_filepath,
shakemap_filepath,
ground_motion_filepath,
geometry_source_filepath,
cellIdSource_filepath,
cell_id_source_filepath,
exposure_type="cell",
interpolation_method="linear",
result_filepath="damage_result.csv",
......@@ -41,11 +41,11 @@ def get_exposure_per_tile(
exposure = pd.read_csv(exposure_filepath)
lons_whole_area = exposure.lon
lats_whole_area = exposure.lat
ground_motion_field = np.loadtxt(shakemap_filepath, delimiter=",", skiprows=1)
full_ground_motion_field = losslib.Get_Full_GMF(
ground_motion_field = np.loadtxt(ground_motion_filepath, delimiter=",", skiprows=1)
full_ground_motion_field = losslib.get_full_GMF(
ground_motion_field, lons_whole_area, lats_whole_area, interpolation_method
)
all_cell_ids = np.loadtxt(cellIdSource_filepath, dtype="str", skiprows=1)
cell_ids = np.loadtxt(cell_id_source_filepath, dtype="str", skiprows=1)
with open(result_filepath, "a+", newline="") as write_obj:
csv_writer = csv.writer(write_obj)
if exposure_type == "building":
......@@ -54,7 +54,7 @@ def get_exposure_per_tile(
"",
"origin_id",
"",
"RespectiveCellid",
"respective_cell_id",
"",
"asset_id",
"",
......@@ -82,7 +82,7 @@ def get_exposure_per_tile(
"",
"structural_complete",
]
tile_id = "respectiveCellId"
tile_id = "respective_cell_id"
else:
title = [
"geometry",
......@@ -118,8 +118,8 @@ def get_exposure_per_tile(
tile_id = "origin_id"
csv_writer.writerow(title)
for i in range(all_cell_ids.shape[0]):
cell_id = all_cell_ids[i]
for i in range(cell_ids.shape[0]):
cell_id = cell_ids[i]
exposure_per_tile = exposure.loc[exposure[tile_id] == cell_id]
......@@ -129,7 +129,7 @@ def get_exposure_per_tile(
fragility_pathname,
exposure_per_tile,
taxonomy_conversion_filepath,
shakemap_filepath,
ground_motion_filepath,
geometry_source_filepath,
exposure_type,
interpolation_method,
......@@ -241,8 +241,8 @@ if __name__ == "__main__":
exposure_type = args.exposure_type
fragility_pathname = args.fragilities
taxonomy_conversion_filepath = args.taxonomy_map
shakemap_filepath = args.ground_motion_field
cellIdSource_filepath = args.cell_ids
ground_motion_filepath = args.ground_motion_field
cell_id_source_filepath = args.cell_ids
exposure_filepath = args.exposure
geometry_source_filepath = args.geometry
result_filepath = args.results
......@@ -262,9 +262,9 @@ if __name__ == "__main__":
exposure_filepath,
fragility_pathname,
taxonomy_conversion_filepath,
shakemap_filepath,
ground_motion_filepath,
geometry_source_filepath,
cellIdSource_filepath,
cell_id_source_filepath,
exposure_type,
interpolation_method,
result_filepath,
......
......@@ -28,9 +28,9 @@ def damageCalculator_TileVersion(
fragility_pathname,
exposure,
taxonomy_conversion_path,
shakemap_path,
ground_motion_filepath,
geometry_source_path,
exposureType="cell",
exposure_type="cell",
method="linear",
):
......@@ -45,8 +45,8 @@ def damageCalculator_TileVersion(
contains locations and the ground-motion values of each location
in the format of lon, lat, gmValueofType1, gmValueofType2, ...,
gmValueofTypeN.
Please note that if the groundMotionField contains values for
more than one ground motion type, This function will do
Please note that if the ground_motion_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:
[lon, lat, gmValueofType1, gmValueofType2, ..., gmValueofTypeN]
......@@ -97,10 +97,10 @@ def damageCalculator_TileVersion(
>>> taxonomy_conversion_path
"/home/TileCalculations/taxonomy_mapping_Europe.csv"
- shakemap_path: (str)
- ground_motion_filepath: (str)
Address to the ground-motion values file.
Example extract:
>>> shakemap_path
>>> ground_motion_filepath
"/home/TileCalculations/shakemap1381_2.csv"
- geometry_source_path: (str)
......@@ -111,7 +111,7 @@ def damageCalculator_TileVersion(
"/home/TileCalculations
/M008_exposure_Attica_GDE_visual_v001_sat_27f_by_cell_reOrder.csv"
- exposureType: (string), optional
- exposure_type: (string), optional
{‘cell’, ‘OBM’}, optional'. cell by default.
- method: (string), optional
......@@ -141,9 +141,9 @@ def damageCalculator_TileVersion(
print(startTime)
# Read inputs as Numpy arrays or Pandas data-frames
taxonomyToFragilitySource = csv.reader(open(taxonomy_conversion_path))
taxonomy_to_fragility_source = csv.reader(open(taxonomy_conversion_path))
# Skip the header
next(taxonomyToFragilitySource, None)
next(taxonomy_to_fragility_source, None)
geometry_source = csv.reader(open(geometry_source_path), delimiter=";")
# Skip the header
next(geometry_source, None)
......@@ -160,15 +160,15 @@ def damageCalculator_TileVersion(
# Define a dictionary with keys as the ground-motion type and value as the column
# number of the ground-motion type in the shakemap file.
gmDict = {"PGA": 2, "SA(0.3)": 3, "SA(0.6)": 4, "SA(1.0)": 5, "SA(1)": 5}
# Calling the function "Taxonomy_to_Fragility" to get a dictionary with keys as the
# 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.
taxonomyToFragilityMap = losslib.Taxonomy_to_Fragility(
gmDict, taxonomyToFragilitySource, fragility_pathname
taxonomy_to_fragility_map = losslib.taxonomy_to_fragility(
gmDict, taxonomy_to_fragility_source, fragility_pathname
)
# Calling the function "origin_id_to_geometry" to get a dictionary with keys as the
# origin_id and the value as the respective polygon.
origin_id_to_geometry_map = losslib.origin_id_to_geometry(geometry_source, exposureType)
origin_id_to_geometry_map = losslib.origin_id_to_geometry(geometry_source, exposure_type)
# Define number of columns that contain the data in the fragility function files.
cls = range(1, 101)
# Just a trick to have multiple commas between each result element, since we do
......@@ -180,30 +180,32 @@ def damageCalculator_TileVersion(
csv_writer = csv.writer(write_obj)
for asset in range(exposure.shape[0]):
taxonomy = taxonomies.iloc[asset]
fragilityFileName = taxonomyToFragilityMap[taxonomy][0] + ".csv"
fragilityfunction_filename = taxonomy_to_fragility_map[taxonomy][0] + ".csv"
num_buildings = tot_num_buildings.iloc[asset]
lon = lons.iloc[asset]
lat = lats.iloc[asset]
asset_id = asset_ids.iloc[asset]
origin_id = origin_ids.iloc[asset]
# In case of `exposureType == "OBM"`, not only the geometry, but also the
# In case of `exposure_type == "OBM"`, not only the geometry, but also the
# respective cell ID (in which the asset is located), is extracted
# using `origin_id_to_geometry_map`
if exposureType == "OBM":
if exposure_type == "OBM":
[geometry, respective_cell_id] = origin_id_to_geometry_map[origin_id]
else:
geometry = origin_id_to_geometry_map[origin_id]
# Read fragility functions as numpy arrays.
fragility_function = np.loadtxt(
fragility_pathname + "/" + fragilityFileName, delimiter=",", usecols=cls
fragility_pathname + "/" + fragilityfunction_filename,
delimiter=",",
usecols=cls,
)
# Computing the ground-motion values from `full_ground_motion_field`. Please
# note that `full_ground_motion_field` contains many identical lines (because
# many of the assets have same locations and same location leads to the same
# ground-motion value). This way, `full_ground_motion_field` has the same number
# and order of assets as in the exposure model for simpler processing.
gm_value = full_ground_motion_field[asset, taxonomyToFragilityMap[taxonomy][1]]
gm_value = full_ground_motion_field[asset, taxonomy_to_fragility_map[taxonomy][1]]
# Computing probabilities of exceedance and occurrence.
[PoEs, PoOs] = losslib.get_PoEs(fragility_function, gm_value)
......@@ -212,7 +214,7 @@ def damageCalculator_TileVersion(
for h in a:
dmg_by_asset.insert(h, "")
# Append results
if exposureType == "OBM":
if exposure_type == "OBM":
asset = [
geometry,
"",
......
......@@ -23,7 +23,7 @@ import csv
from scipy import interpolate
def Get_Full_GMF(groundMotionField, lons, lats, method="linear"):
def get_full_GMF(ground_motion_field, lons, lats, method="linear"):
"""
Returns ground-motion values using 2-dimensional interpolation for the
......@@ -31,20 +31,20 @@ def Get_Full_GMF(groundMotionField, lons, lats, method="linear"):
Input:
------
- groundMotionField: (Numpy nd-array of shape(m,n); m records with long,
- ground_motion_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 groundMotionField contains values for
more than one ground motion type, This function will do
Please note that if the ground_motion_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:
[lon, lat, gmValueofType1, gmValueofType2, ..., gmValueofTypeN]
As an example:
[23.6875,38.3402777778,'PGA','SA(0.3)','SA(0.6)','SA(1.0)']
Example extract:
>>> groundMotionField
>>> ground_motion_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],
......@@ -73,10 +73,10 @@ def Get_Full_GMF(groundMotionField, lons, lats, method="linear"):
Output:
-------
- fullGroundMotionField: (numpy-ndarray of shape(a,n))
- full_ground_motion_field: (numpy-ndarray of shape(a,n))
an array of logitudes and latitudes along with their interpolated values
Example extract:
>>> fullGroundMotionField
>>> full_ground_motion_field
array([
[2.36e+01, 3.83e+01, 1.79e-01, 3.62e-01, 2.52e-01, 1.52e-01],
...,
......@@ -85,32 +85,34 @@ def Get_Full_GMF(groundMotionField, lons, lats, method="linear"):
"""
# points_given: the input points for interpolation
points_given = np.vstack((groundMotionField[:, 0], groundMotionField[:, 1])).transpose()
points_given = np.vstack((ground_motion_field[:, 0], ground_motion_field[:, 1])).transpose()
# points_todo: points to do interpolation over
points_todo = np.vstack((lons, lats)).transpose()
fullGroundMotionField = np.vstack((np.array(lons), np.array(lats))).transpose()
full_ground_motion_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 groundMotionField.
for gmType in range(2, groundMotionField.shape[1], 1):
# to the last column of the ground_motion_field.
for gm_type in range(2, ground_motion_field.shape[1], 1):
# gmvs_given: the input ground-motion values for interpolation.
gmvs_given = groundMotionField[:, gmType]
gmvs_given = ground_motion_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)
fullGroundMotionField = np.column_stack((fullGroundMotionField, gm_value_griddata))
return fullGroundMotionField
full_ground_motion_field = np.column_stack(
(full_ground_motion_field, gm_value_griddata)
)
return full_ground_motion_field
def Taxonomy_to_Fragility(gmDict, taxonomyToFragilitySource, fragilityFileDir):
def taxonomy_to_fragility(gmDict, taxonomy_to_fragility_source, fragility_pathname):
"""
Creates an extended map of taxonomies to fragility function.
The input map 'taxonomyToFragilitySource' contains the mapping for each
The input map 'taxonomy_to_fragility_source' contains the mapping for each
taxonomy to a fragility function file from which the ground-motion type is
read to be written to the extended map 'taxonomyToFragilityMap'.
read to be written to the extended map 'taxonomy_to_fragility_map'.
Input:
------
......@@ -121,7 +123,7 @@ def Taxonomy_to_Fragility(gmDict, taxonomyToFragilitySource, fragilityFileDir):
>>> gmDict
{'PGA': 2, 'SA(0.3)': 3, 'SA(0.6)': 4, 'SA(1)': 5}
- taxonomyToFragilitySource: (csv.reader)
- taxonomy_to_fragility_source: (csv.reader)
taxonomy to fragility-function file map following the format:
[taxonomy_string, fragility-function_filename, 'weight'], [...]
Example file extract:
......@@ -130,16 +132,16 @@ def Taxonomy_to_Fragility(gmDict, taxonomyToFragilitySource, fragilityFileDir):
['CR/LDUAL+CDM/HBET:6-/SOS/11.0', 'CR_LDUAL-DUL_H6', '1']
...
- fragilityFileDir: (string)
- fragility_pathname: (string)
directory of fragility-function files
Example extract:
>>> fragilityFileDir
>>> fragility_pathname
'/home/laptop/fragilities'
Output:
------
- taxonomyToFragilityMap: (Dictionary)
- taxonomy_to_fragility_map: (Dictionary)
containing the taxonomy to fragility function map considering the
ground-motion types. It follows the format:
{taxonomy_string: [fragility-function_filename, column of
......@@ -151,30 +153,30 @@ def Taxonomy_to_Fragility(gmDict, taxonomyToFragilitySource, fragilityFileDir):
"""
# Prepare return variable
taxonomyToFragilityMap = {}
taxonomy_to_fragility_map = {}
# Loop through the taxonomy-to-fragility-function map
for mappingItem in taxonomyToFragilitySource:
for mapping_item in taxonomy_to_fragility_source:
# Open the fragility-function file corresponding to the taxonomy in
# 'mappingitem[1]'
# 'mapping_item[1]'
fragilityFunction = list(
csv.reader(open(fragilityFileDir + "/" + mappingItem[1] + ".csv"))
csv.reader(open(fragility_pathname + "/" + mapping_item[1] + ".csv"))
)
# Check if already one fragility function for a given GM type has been
# selected
if mappingItem[0] in taxonomyToFragilityMap:
if mapping_item[0] in taxonomy_to_fragility_map:
# Ignore the additional fragility function to keep everything
# unambiguous
pass
# Create the entry in the extended map with adding the type of ground
# motion 'fragilityFunction[0][0]'
taxonomyToFragilityMap[mappingItem[0]] = [
mappingItem[1],
taxonomy_to_fragility_map[mapping_item[0]] = [
mapping_item[1],
gmDict[fragilityFunction[0][0]],
]
return taxonomyToFragilityMap
return taxonomy_to_fragility_map
def origin_id_to_geometry(geometry_source, exposureType):
def origin_id_to_geometry(geometry_source, exposure_type):
"""
Creates a dictionary of Origin-ids and their polygons.
The input map 'geometry_source' contains the mapping for each
......@@ -183,7 +185,7 @@ def origin_id_to_geometry(geometry_source, exposureType):
Input:
------
- exposureType: (string)
- exposure_type: (string)
Either 'OBM' or 'cell'
- geometry_source: (csv.reader)
......@@ -268,7 +270,7 @@ def origin_id_to_geometry(geometry_source, exposureType):
# to know if the geometry refers to single buildings instead of cells.
# In this case we need to also know the cell_ID that this building geometry
# is located in.
if exposureType == "OBM":
if exposure_type == "OBM":
# Loop through the origin_id_to_geometry map
for geometry_mapping_item in geometry_source:
# Check if already one geometry for a given OriginId has been
......@@ -355,14 +357,14 @@ def get_PoEs(fragility_function, gm_value):
elif gm_value > imls[-1]:
iml_min = imls[-2]
idx_iml_min = -2
index_iml_min = -2
iml_max = imls[-1]
idx_iml_max = -1
index_iml_max = -1
x = [iml_min, iml_max]
PoEs = [1]
for Poes in [PoEs_slight, PoEs_moderate, PoEs_extensive, PoEs_complete]:
y = [Poes[idx_iml_min], Poes[idx_iml_max]]
y = [Poes[index_iml_min], Poes[index_iml_max]]
f = interpolate.interp1d(x, y, fill_value="extrapolate")
PoEs.append(f(gm_value))
# Calculating PoOs
......@@ -375,11 +377,11 @@ def get_PoEs(fragility_function, gm_value):
# Largest iml that is smaller than the given ground-motion value
iml_min = imls[imls < gm_value].max()
# Finding index of the iml_min
idx_iml_min = np.searchsorted(imls, iml_min, side="left")
index_iml_min = np.searchsorted(imls, iml_min, side="left")
# Smallest iml that is larger than the given ground-motion value
iml_max = imls[imls > gm_value].min()
# Finding index of the iml_max
idx_iml_max = np.searchsorted(imls, iml_max, side="left")
index_iml_max = np.searchsorted(imls, iml_max, side="left")
# Two bounds of the interpolation
x = [iml_min, iml_max]
......@@ -387,7 +389,7 @@ def get_PoEs(fragility_function, gm_value):
PoEs = [1]
# Looping over all damage states
for Poes in [PoEs_slight, PoEs_moderate, PoEs_extensive, PoEs_complete]:
y = [Poes[idx_iml_min], Poes[idx_iml_max]]
y = [Poes[index_iml_min], Poes[index_iml_max]]
# interpolating PoE values for the given gm-value between x and y
# range (using interpolation)
PoEs.append(np.interp(gm_value, x, y))
......
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