Commit a58b2bbf authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Added feature to process only selected exposure entities

parent 5565d977
Pipeline #26364 passed with stage
in 2 minutes and 6 seconds
......@@ -38,6 +38,20 @@ necessary parameters:
gdeimporter
```
## Configuration file (config.yml)
The file `config.yml` should contain the following parameters:
- `model_name`: Name of the input aggregated exposure model (only relevant for the user).
- `exposure_format`: Format of the input aggregated exposure model. Currently supported values: esrm20.
- `data_pathname`: Path to directory that contains the model data.
- `boundaries_pathname`: Path to directory that contains the boundary geodata files.
- `occupancies_to_run`: List of occupancies for which the code will be run, separated by ", " (comma and space). They need to exist for the indicated `exposure format`. Currently supported values: residential, commercial.
- `exposure_entities_to_run`: List of names of exposure entities for which the code will be run. Currently supported options:
- "all": The list of names will be retrieved from the metadata of the input aggregated exposure model.
- A comma-space-separated list of entity names: This list of names will be used.
- A full path to a .txt or .csv file: The list of names will be retrieved from the indicated .txt/.csv file.
## Copyright and copyleft
Copyright (C) 2021
......
......@@ -2,4 +2,5 @@ model_name: esrm20
exposure_format: esrm20 # Only supported value for now
data_pathname: path_to_directory_with_model_data
boundaries_pathname: path_to_directory_with_boundary_files
occupancies_to_run: residential, commercial # From 'occupancy_cases', industrial not supported
occupancies_to_run: residential, commercial # Need to exist for the indicated `exposure format`, industrial not supported
exposure_entities_to_run: all # Either "all", a comma-space-separated list of entity names, or a name of a .txt or .csv file
......@@ -38,12 +38,15 @@ class Configuration:
self.occupancies_to_run (list of str):
List of keys of occupancy_cases of the input aggregated exposure model for which
data will be retrieved.
self.exposure_entities_to_run (list of str):
List of names of the exposure entities for which the data units will be retrieved.
"""
REQUIRES = [
"data_pathname",
"boundaries_pathname",
"occupancies_to_run",
"exposure_entities_to_run",
]
def __init__(self, filepath):
......@@ -61,6 +64,9 @@ class Configuration:
self.data_pathname = self._assign_parameter(config, "data_pathname")
self.boundaries_pathname = self._assign_parameter(config, "boundaries_pathname")
self.occupancies_to_run = self._assign_listed_parameters(config, "occupancies_to_run")
self.exposure_entities_to_run = self._assign_listed_parameters(
config, "exposure_entities_to_run"
)
# Terminate if critical parameters are missing (not all parameters are critical)
for key_parameter in self.REQUIRES:
......@@ -95,6 +101,57 @@ class Configuration:
return config
def interpret_exposure_entities_to_run(self, aggregated_exposure_model):
"""This function interprets the value assigned to self.exposure_entities_to_run from the
configuration file and updates self.exposure_entities_to_run accordingly.
Args:
aggregated_exposure_model (AggregatedExposureModel or subclass):
Instance of AggregatedExposureModel or a sub-class of AggregatedExposureModel
from which the names of the exposure entities will be read, if
self.exposure_entities_to_run is ["all"].
Returns:
The method updates self.exposure_entities_to_run as a function of its content.
Possibilities:
self.exposure_entities_to_run == ["all"]:
self.exposure_entities_to_run is updated to contain the list of names of
aggregated_exposure_model.exposure_entities.
self.exposure_entities_to_run contains a list with a path to a .txt or .csv
file:
self.exposure_entities_to_run is updated to contain the list of names of
exposure entities listed in the indicated .txt/.csv file.
Any other case:
self.exposure_entities_to_run is not updated.
"""
if len(self.exposure_entities_to_run) > 1:
# Keep the original content (several names are listed)
return
if self.exposure_entities_to_run[0].lower() == "all":
# Retrieve names of all exposure entities of the aggregated exposure model
self.exposure_entities_to_run = list(aggregated_exposure_model.exposure_entities)
elif (
self.exposure_entities_to_run[0].split(".")[-1] == "txt"
or self.exposure_entities_to_run[0].split(".")[-1] == "csv"
):
# Retrieve names of exposure entities from the indicated file
with open(self.exposure_entities_to_run[0], "r") as f:
names = []
for row in f.readlines():
raw_row = row.rstrip("\n")
raw_row = raw_row.split(",")
for element in raw_row:
names.append(element)
f.close()
self.exposure_entities_to_run = names
else:
# Keep the original content (one name listed)
pass
return
def _assign_parameter(self, config, input_parameter):
"""This function searches for the key input_parameter in the dictionary config. If
found, it returns its value (a string or a dictionary). If not found, it returns None.
......
......@@ -47,8 +47,11 @@ def main():
if len(aem.exposure_entities) < 1:
raise ValueError("no exposure entities found in %s" % (aem.model_name))
# Interpret and update config.exposure_entities_to_run
config.interpret_exposure_entities_to_run(aem)
# Retrieve data units per exposure entity and occupancy case
for exposure_entity_name in aem.exposure_entities:
for exposure_entity_name in config.exposure_entities_to_run:
for occupancy_case in config.occupancies_to_run:
aem.get_data_units(config, exposure_entity_name, occupancy_case)
......
......@@ -3,3 +3,4 @@ exposure_format: esrm20
data_pathname: /some/path/to/directory
boundaries_pathname: /some/path/to/directory
occupancies_to_run: residential, commercial, industrial
exposure_entities_to_run: all
......@@ -3,3 +3,4 @@ exposure_format: esrm20
data_pathname: /some/path/to/directory
boundaries_pathname: /some/path/to/directory
occupancies_to_run: residential
exposure_entities_to_run: Name1
model_name: esrm20
exposure_format: esrm20
data_pathname: /some/path/to/directory
boundaries_pathname: /some/path/to/directory
occupancies_to_run: residential, commercial, industrial
exposure_entities_to_run: Name1, Name2, Name3
......@@ -19,6 +19,7 @@
import os
import pytest
from gdeimporter.configuration import Configuration
from gdeimporter.aggregatedexposuremodel import ExposureModelESRM20
def test_Configuration():
......@@ -31,6 +32,7 @@ def test_Configuration():
assert returned_config.data_pathname == "/some/path/to/directory"
assert returned_config.boundaries_pathname == "/some/path/to/directory"
assert returned_config.occupancies_to_run == ["residential", "commercial", "industrial"]
assert returned_config.exposure_entities_to_run == ["all"]
# Test case in which the file is not found
with pytest.raises(OSError) as excinfo:
......@@ -44,3 +46,90 @@ def test_Configuration():
os.path.join(os.path.dirname(__file__), "data", "config_for_testing_one_list_val.yml")
)
assert returned_config.occupancies_to_run == ["residential"]
# Test interpretation of 'exposure_entities_to_run'
# Case 1: "all"
returned_config = Configuration(
os.path.join(os.path.dirname(__file__), "data", "config_for_testing_good.yml")
)
returned_config.data_pathname = os.path.join(
os.path.dirname(__file__), "data", "ExposureModelESRM20_non_trivial"
)
dummy_aem = ExposureModelESRM20(returned_config)
returned_config.interpret_exposure_entities_to_run(dummy_aem)
expected_names = [
"Entity_1",
"Entity_2",
"Entity_3",
"Entity_4",
"Entity_5",
"Entity_6",
"Entity_7",
"Entity_8",
]
assert returned_config.exposure_entities_to_run == expected_names
# Case 2: one name (different from "all")
returned_config = Configuration(
os.path.join(os.path.dirname(__file__), "data", "config_for_testing_one_list_val.yml")
)
returned_config.data_pathname = os.path.join(
os.path.dirname(__file__), "data", "ExposureModelESRM20_non_trivial"
)
dummy_aem = ExposureModelESRM20(returned_config)
returned_config.interpret_exposure_entities_to_run(dummy_aem)
assert returned_config.exposure_entities_to_run == ["Name1"]
# Case 3: several names
returned_config = Configuration(
os.path.join(
os.path.dirname(__file__), "data", "config_for_testing_several_entities.yml"
)
)
returned_config.data_pathname = os.path.join(
os.path.dirname(__file__), "data", "ExposureModelESRM20_non_trivial"
)
dummy_aem = ExposureModelESRM20(returned_config)
returned_config.interpret_exposure_entities_to_run(dummy_aem)
assert returned_config.exposure_entities_to_run == ["Name1", "Name2", "Name3"]
# Case 4: .txt file indicated
returned_config = Configuration(
os.path.join(os.path.dirname(__file__), "data", "config_for_testing_good.yml")
)
returned_config.data_pathname = os.path.join(
os.path.dirname(__file__), "data", "ExposureModelESRM20_non_trivial"
)
dummy_aem = ExposureModelESRM20(returned_config)
returned_config.exposure_entities_to_run = [
os.path.join(os.path.dirname(__file__), "data", "exposure_entities_to_run.txt")
]
returned_config.interpret_exposure_entities_to_run(dummy_aem)
assert returned_config.exposure_entities_to_run == ["Name1", "Name2", "Name3"]
# Case 5: .csv file indicated
returned_config = Configuration(
os.path.join(os.path.dirname(__file__), "data", "config_for_testing_good.yml")
)
returned_config.data_pathname = os.path.join(
os.path.dirname(__file__), "data", "ExposureModelESRM20_non_trivial"
)
dummy_aem = ExposureModelESRM20(returned_config)
returned_config.exposure_entities_to_run = [
os.path.join(os.path.dirname(__file__), "data", "exposure_entities_to_run.csv")
]
returned_config.interpret_exposure_entities_to_run(dummy_aem)
assert returned_config.exposure_entities_to_run == ["Name1", "Name2", "Name3"]
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