Commit fac4c77d authored by Tara Evaz Zadeh's avatar Tara Evaz Zadeh
Browse files

Changed the calculator to adapt the augmented exposure files as inputs

parent 9aec54b1
Pipeline #20123 passed with stage
in 1 minute and 33 seconds
......@@ -17,13 +17,13 @@
# along with this program. If not, see http://www.gnu.org/licenses/.
import os
import argparse
import csv
import numpy as np
import pandas as pd
import losslib
import os
import argparse
from damage_calculator_tile_version import damageCalculator_TileVersion
import csv
def get_exposure_per_tile(
......@@ -31,9 +31,6 @@ def get_exposure_per_tile(
fragility_pathname,
taxonomy_conversion_filepath,
ground_motion_field_filepath,
geometry_source_filepath,
cell_id_source_filepath,
exposure_type="cell",
interpolation_method="linear",
result_filepath="damage_result.csv",
):
......@@ -59,55 +56,39 @@ def get_exposure_per_tile(
# ground-motion-field file) to the `ground_motion_type_index_map` as the dictionary
# key and value, respectively.
ground_motion_type_index_map[gm_types[i]] = i + 2
cell_ids = pd.read_csv(cell_id_source_filepath)
cell_ids = exposure.tile_id.unique()
with open(result_filepath, mode="w") as write_obj:
if exposure_type == "building":
field_names = [
"geometry",
"origin_id",
"respective_cell_id",
"asset_id",
"lon",
"lat",
"taxonomy",
"gm_value",
"PoEs",
"PoOs",
"num_buildings",
"structural_no_damage",
"structural_slight",
"structural_moderate",
"structural_extensive",
"structural_complete",
]
tile_id = "respective_cell_id"
else:
field_names = [
"geometry",
"origin_id",
"asset_id",
"lon",
"lat",
"taxonomy",
"gm_value",
"PoEs",
"PoOs",
"num_buildings",
"structural_no_damage",
"structural_slight",
"structural_moderate",
"structural_extensive",
"structural_complete",
]
tile_id = "origin_id"
field_names = [
"asset_id",
"lon",
"lat",
"taxonomy",
"number",
"structural",
"night",
"occupancy",
"admin_name",
"admin_ID",
"tile_id",
"tile_geometry",
"building_id",
"building_geometry",
"gm_value",
"gm_type",
"PoEs",
"PoOs",
"structural_no_damage",
"structural_slight",
"structural_moderate",
"structural_extensive",
"structural_complete",
]
writer = csv.DictWriter(write_obj, fieldnames=field_names, delimiter=";")
writer.writeheader()
for i in range(cell_ids.shape[0]):
cell_id = cell_ids.loc[i][0]
exposure_per_tile = exposure.loc[exposure[tile_id] == cell_id]
for i in range(len(cell_ids)):
exposure_per_tile = exposure.loc[exposure["tile_id"] == cell_ids[i]]
damageCalculator_TileVersion(
full_ground_motion_field,
......@@ -116,9 +97,7 @@ def get_exposure_per_tile(
exposure_per_tile,
taxonomy_conversion_filepath,
ground_motion_field_filepath,
geometry_source_filepath,
ground_motion_type_index_map,
exposure_type,
interpolation_method,
)
......@@ -143,16 +122,6 @@ if __name__ == "__main__":
+ "the exposure locations (asset locations). "
+ "Options: [‘linear’, ‘nearest’, ‘cubic’] (default set to linear)",
)
parser.add_argument(
"-x",
"--exposure-type",
required=False,
type=str,
default="cell",
choices=["cell", "building"],
help="exposure type. 'cell' if your assets belong to a tile and 'building' if "
+ "your assets are buildings. Options = [‘cell’, ‘building’] (default set to cell)",
)
parser.add_argument(
"-f",
"--fragilities",
......@@ -177,15 +146,6 @@ if __name__ == "__main__":
help="path to the ground-motion values file. This file should include all the "
+ " intensity measure types that you have in your fragility functions (Required)",
)
parser.add_argument(
"-c",
"--cell-ids",
required=True,
type=str,
help="path to the file that includes all the unique cell-ids you have in your "
+ "exposure file (same as the origin-ids of the cell parts of the exposure "
+ "files or simpy cell-ids) (Required)",
)
parser.add_argument(
"-e",
"--exposure",
......@@ -193,17 +153,6 @@ if __name__ == "__main__":
type=str,
help="path to the file that includes the exposure assets (Required)",
)
parser.add_argument(
"-p",
"--geometry",
required=True,
type=str,
help="path to the file that includes the origin-ids of either your tiles "
+ "(in case exposure-type = 'cell'. As an example of the origin-id: "
+ "cell_92258412) "
+ "or the buildings (in case exposure-type = 'building'. As an example of the "
+ "origin-id: OSM_529613252) and their respective polygons. (Required)",
)
parser.add_argument(
"-r",
"--results",
......@@ -218,20 +167,17 @@ if __name__ == "__main__":
"--overwrite",
required=False,
action="store_true",
help="to overwrite the existing result file if this option is mentioned in "
+ "the command line",
help="Result file exists. Choose another name or set --overwrite"
+ "to overwrite the existing file.",
)
args = parser.parse_args()
# read arguments from command line
interpolation_method = args.interpolation_method
exposure_type = args.exposure_type
fragility_pathname = args.fragilities
taxonomy_conversion_filepath = args.taxonomy_map
ground_motion_field_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
overwrite_result_file = args.overwrite
......@@ -250,9 +196,6 @@ if __name__ == "__main__":
fragility_pathname,
taxonomy_conversion_filepath,
ground_motion_field_filepath,
geometry_source_filepath,
cell_id_source_filepath,
exposure_type,
interpolation_method,
result_filepath,
)
......@@ -29,10 +29,8 @@ def damageCalculator_TileVersion(
exposure,
taxonomy_conversion_path,
ground_motion_field_filepath,
geometry_source_path,
ground_motion_type_index_map,
exposure_type="cell",
method="linear",
interpolation_method="linear",
):
"""
......@@ -145,9 +143,6 @@ def damageCalculator_TileVersion(
taxonomy_to_fragility_source = csv.reader(open(taxonomy_conversion_path))
# Skip the header
next(taxonomy_to_fragility_source, None)
geometry_source = csv.reader(open(geometry_source_path), delimiter=";")
# Skip the header
next(geometry_source, None)
# Read each column of the exposure model
taxonomies = exposure.taxonomy
......@@ -155,7 +150,10 @@ def damageCalculator_TileVersion(
lons = exposure.lon
lats = exposure.lat
asset_ids = exposure.id
origin_ids = exposure.origin_id
tile_geometries = exposure.tile_geometry
tile_ids = exposure.tile_id
building_geometries = exposure.building_geometry
building_ids = exposure.building_id
# Begin Computation
# Calling the function "taxonomy_to_fragility" to get a dictionary with keys as the
......@@ -164,9 +162,7 @@ def damageCalculator_TileVersion(
taxonomy_to_fragility_map = losslib.taxonomy_to_fragility(
ground_motion_type_index_map, 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, exposure_type)
# Define number of columns that contain the data in the fragility function files.
cls = range(1, 101)
......@@ -180,14 +176,10 @@ def damageCalculator_TileVersion(
lon = lons.iloc[asset]
lat = lats.iloc[asset]
asset_id = asset_ids.iloc[asset]
origin_id = origin_ids.iloc[asset]
# In case of `exposure_type == "building"`, 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 exposure_type == "building":
[geometry, respective_cell_id] = origin_id_to_geometry_map[origin_id]
else:
geometry = origin_id_to_geometry_map[origin_id]
tile_geometry = tile_geometries.iloc[asset]
tile_id = tile_ids.iloc[asset]
building_geometry = building_geometries.iloc[asset]
building_id = building_ids.iloc[asset]
# Read fragility functions as numpy arrays.
fragility_function = np.loadtxt(
......@@ -207,33 +199,20 @@ def damageCalculator_TileVersion(
# Compute damage by assets
damage_by_asset = [i * num_buildings for i in PoOs]
# Append results
if exposure_type == "building":
result_asset_info = [
geometry,
origin_id,
respective_cell_id,
asset_id,
lon,
lat,
taxonomy,
gm_value,
PoEs,
PoOs,
num_buildings,
]
else:
result_asset_info = [
geometry,
origin_id,
asset_id,
lon,
lat,
taxonomy,
gm_value,
PoEs,
PoOs,
num_buildings,
]
result_asset_info = [
asset_id,
lon,
lat,
taxonomy,
gm_value,
PoEs,
PoOs,
num_buildings,
tile_id,
tile_geometry,
building_id,
building_geometry,
]
for i in range(len(damage_by_asset)):
result_asset_info.append(damage_by_asset[i])
csv_writer.writerow(result_asset_info)
......
......@@ -225,123 +225,6 @@ def taxonomy_to_fragility(
return taxonomy_to_fragility_map
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
Origin-id to a polygon along with its cell-id if it's a building geometry source
as the 'origin_id_to_geometry_map'.
Input:
------
- exposure_type: (string)
Either 'building' or 'cell'
- geometry_source: (csv.reader)
Origin-id to geometry file map with semicolon as the delimiter,
following the format:
['OriginID'; 'geometry'; 'cell_ID']. 'cell_ID' would only be available if
the input geometry belongs to the building data(single buildings). 'cell_ID'
here points to the cell that contains the geometry.
Example extract:
if you have cell geometry source:
>>> geometry_source
cell_2410244527;POLYGON ((23.6861 38.3389,
23.6889 38.3389, 23.6889 38.34167,
23.6861 38.3417, 23.6861 38.3389))
cell_2410244528;POLYGON ((23.6889 38.3389,
23.6916 38.3389, 23.6916 38.3416,
23.6889 38.3416, 23.6889 38.3389))
...
if you have building geometry source:
>>> geometry_source
OSM_517924352;POLYGON((2641289.2688 4582010.1248,2641299.6772
4582007.1292,2641304.1745 4582019.8887,2641294.4229
4582023.4778,2641291.5175 4582013.4736,2641290.5490
4582013.8551,2641289.2688 4582010.1248));cell_2425278141
OSM_452860266;POLYGON((2647123.74 4559674.98,2647130.88 4559680.21,
2647136 4559673.23,2647138.57 4559675.12,2647143.21 4559668.8,2647133.5
4559661.68,2647123.74 4559674.98));cell_2432665360
...
please note that it doesn't matter if your input geometry_source has more
columns than needed. But the first columns should be as sited above.
The first column does not necessarily have to start with OSM or cell. It
only needs to have the same format as this column has in the exposure
file.
Output:
------
- origin_id_to_geometry_map: (Dictionary)
contains the origin-id to polygons items.
(If the geometry show single buildings, the dictionary also considers
their cell-IDs ) following the format below:
When geometry defines cells:
{OriginID_string: [geometry]}
Example extract:
>>> origin_id_to_geometry_map
{'cell_2410244527': 'POLYGON ((23.6861 38.33889,
23.6889 38.3389, 23.6889 38.3416,
23.6861 38.3416, 23.6861 38.3389))'
,'cell_2410244528': 'POLYGON ((23.6889 38.3389,
23.6916 38.3389, 23.6916 38.3416,
23.6889 38.3416, 23.6889 38.3389))',
... }
In case the geometry defines buildings:
{OriginID_string: [geometry, CellID]}
Example extract:
>>> origin_id_to_geometry_map
{'OSM_517924352': ['POLYGON((2641289.2688 4582010.1248,
2641299.6772 4582007.1292,2641304.1745 4582019.8887,
2641294.4229 4582023.4778,2641291.5175 4582013.4736,
2641290.5490 4582013.8551,2641289.2688 4582010.1248))',
'cell_2425278141'],
'OSM_452860266': ['POLYGON((2647123.74 4559674.98,2647130.88 4559680.21,
2647136 4559673.23,2647138.57 4559675.12,2647143.21 4559668.8,
2647133.5 4559661.68 ,2647123.74 4559674.98))', 'cell_2432665360'], ...}
Please Note that the first columns of the geometry_source does not have to
have the same format as either "OSM_517924352" or "cell_2410244527", but
the id have to have the same format as "origin_id" column of the exposure
input file.
"""
# Prepare return variable
origin_id_to_geometry_map = {}
# 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 exposure_type == "building":
# 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
# selected.
if geometry_mapping_item[0] in origin_id_to_geometry_map:
# Ignore the additional geometry to keep everything unambiguous
continue
# Create the entry in the extended map with adding the cell-ID that
# The building belongs to ('geometry_mapping_item[2]')
origin_id_to_geometry_map[geometry_mapping_item[0]] = [
geometry_mapping_item[1],
geometry_mapping_item[2],
]
else:
for geometry_mapping_item in geometry_source:
if geometry_mapping_item[0] in origin_id_to_geometry_map:
# Implement your duplicate row(geometry_mapping_item) handling here
continue
origin_id_to_geometry_map[geometry_mapping_item[0]] = geometry_mapping_item[1]
return origin_id_to_geometry_map
def get_PoEs(fragility_function, gm_value):
"""
This function interpolates PoE values (for slight,moderate,extensive and
......
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