Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update from main #51

Merged
merged 19 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
24e2d7e
Moved the read and write functions to the main fiat.py script again. …
frederique-hub Mar 9, 2023
e2a75ae
Changed name susceptibility to vulnerability
frederique-hub Mar 9, 2023
32727e8
Added new script for AggregationLabels
frederique-hub Mar 9, 2023
9ae159b
Updated the docs.yml
frederique-hub Mar 9, 2023
b3bbf84
Update formatting with black, tryout testing the writer.
frederique-hub Mar 9, 2023
00e506b
Merge pull request #43 from Deltares/read_write
frederique-hub Mar 10, 2023
79e2f68
Added test_exposure, added framework of the exposure workflow
frederique-hub Mar 10, 2023
63cb9e8
Update ExposureVector class - working on setup_from_single_source
frederique-hub Mar 10, 2023
105d273
set up social vulnerability workflow
LiekeMeijer Mar 13, 2023
a4f8458
Updating SVI
Mares2022 Mar 13, 2023
2b7f920
Computing the Census dataset incl. pre-processing
LiekeMeijer Mar 13, 2023
baac82c
Removed unused variables
frederique-hub Mar 13, 2023
85f7b45
Setting up test for SVI
Mares2022 Mar 13, 2023
711817a
Merge branch 'social_vulnerability' of https://github.com/Deltares/hy…
Mares2022 Mar 13, 2023
e3af03d
Update to HydroMT workflows and update of exposure workflow. Now only…
frederique-hub Mar 15, 2023
48cd636
Social vulnerability changes Lieke up until Wednesday
LiekeMeijer Mar 15, 2023
1f091cc
Merge pull request #49 from Deltares/social_vulnerability
Mares2022 Mar 16, 2023
6071781
Merge branch 'main' into exposure_vector
Mares2022 Mar 16, 2023
a98d7ff
Merge pull request #50 from Deltares/exposure_vector
Mares2022 Mar 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ jobs:
pushd docs
make html
popd

# This overrides the version "dev" with the proper version if we're building off a
# branch that's not master (which is confined to n.nn.x above) or on a tag.
# branch that's not main (which is confined to n.nn.x above) or on a tag.
- name: Set doc version
if: ${{ github.event_name != 'push' || !contains(github.ref, 'master') }}
if: ${{ github.event_name != 'push' || !contains(github.ref, 'main') }}
run: echo "DOC_VERSION=$(python -c 'from hydromt_fiat import __version__ as v; print("dev" if "dev" in v else "v"+v.replace(".dev",""))')" >> $GITHUB_ENV

- name: Upload to GitHub Pages
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,5 @@ venv.bak/
.mypy_cache/

# dask
dask-worker-space/
dask-worker-space/
/local_test_database
185 changes: 160 additions & 25 deletions hydromt_fiat/fiat.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
"""Implement fiat model class"""

from hydromt.models.model_api import Model
from hydromt_fiat.reader import Reader
from hydromt_fiat.writer import Writer
from hydromt.models.model_grid import GridModel
from hydromt_fiat.workflows.exposure_vector import ExposureVector
import logging
from configparser import ConfigParser
import geopandas as gpd
import hydromt
from hydromt.cli.cli_utils import parse_config

from shapely.geometry import box
from typing import Union
from shutil import copy
from hydromt_fiat.workflows.social_vulnerability_index import SocialVulnerabilityIndex



from . import DATADIR
Expand All @@ -13,7 +22,7 @@
_logger = logging.getLogger(__name__)


class FiatModel(Model):
class FiatModel(GridModel):
"""General and basic API for the FIAT model in hydroMT."""

_NAME = "fiat"
Expand All @@ -30,47 +39,173 @@ def __init__(
config_fn=None,
data_libs=None,
logger=_logger,
deltares_data=False,
artifact_data=False,
):
super().__init__(
root=root,
mode=mode,
config_fn=config_fn,
data_libs=data_libs,
deltares_data=deltares_data,
artifact_data=artifact_data,
logger=logger,
)

def setup_config(self):
# TODO: check if this is required
NotImplemented
def setup_basemaps(
self,
region,
**kwargs,
):
"""Define the model domain that is used to clip the raster layers.

Adds model layer:

* **region** geom: A geometry with the nomenclature 'region'.

Parameters
----------
region: dict
Dictionary describing region of interest, e.g. {'bbox': [xmin, ymin, xmax, ymax]}. See :py:meth:`~hydromt.workflows.parse_region()` for all options.
"""

kind, region = hydromt.workflows.parse_region(region, logger=self.logger)
if kind == "bbox":
geom = gpd.GeoDataFrame(geometry=[box(*region["bbox"])], crs=4326)
elif kind == "grid":
geom = region["grid"].raster.box
elif kind == "geom":
geom = region["geom"]
else:
raise ValueError(
f"Unknown region kind {kind} for FIAT, expected one of ['bbox', 'grid', 'geom']."
)

# Set the model region geometry (to be accessed through the shortcut self.region).
self.set_geoms(geom, "region")

def setup_exposure_vector(
self,
asset_locations: str,
occupancy_type: str,
max_potential_damage: str,
ground_floor_height: Union[int, float, str, None],
ground_flood_height_unit: str,
) -> None:
ev = ExposureVector(self.data_catalog, self.region)

def setup_exposure_vector(self, region, **kwargs):
NotImplemented
# workflows.exposure_vector.Exposure
if asset_locations == occupancy_type == max_potential_damage:
# The source for the asset locations, occupancy type and maximum potential
# damage is the same, use one source to create the exposure data.
ev.setup_from_single_source(asset_locations)

# Add: linking damage functions to assets

def setup_exposure_raster(self):
NotImplemented

def setup_vulnerability(self):
NotImplemented

def setup_hazard(self):
def setup_hazard(self, map_fn):
NotImplemented

def setup_social_vulnerability_index(self):
NotImplemented
def setup_social_vulnerability_index(self, census_key: str, path:str, state_abbreviation:str):

#Create SVI object
svi = SocialVulnerabilityIndex(self.data_catalog, self.config)

#Call functionalities of SVI
svi.set_up_census_key(census_key)
svi.variable_code_csv_to_pd_df(path)
svi.set_up_download_codes()
svi.set_up_state_code(state_abbreviation)
svi.download_census_data()
svi.rename_census_data("Census_code_withE", "Census_variable_name")
svi.create_indicator_groups("Census_variable_name", "Indicator_code")
svi.processing_svi_data()
svi.normalization_svi_data()
svi.domain_scores()
svi.composite_scores()

#TO DO: JOIN WITH GEOMETRIES. FOR MAPPING.
#this link can be used: https://github.com/datamade/census

def read(self):
reader = Reader()
reader.read_config()
reader.read_staticmaps()
reader.read_staticgeoms()
"""Method to read the complete model schematization and configuration from file."""
self.logger.info(f"Reading model data from {self.root}")
self.read_config()
self.read_grid()
self.read_geoms()

def _configread(self, fn):
"""Parse fiat_configuration.ini to dict."""

# Read and parse the fiat_configuration.ini.
opt = parse_config(fn)

# Store the general information.
config = opt["setup_config"]

# Set the paths. # FIXME: how to do this more elegantly?
# config["hazard_dp"] = self.root.joinpath("hazard")
# config["exposure_dp"] = self.root.joinpath("exposure")
# config["vulnerability_dp"] = self.root.joinpath("vulnerability")
# config["output_dp"] = self.root.joinpath("output")

# Store the hazard information.
config["hazard"] = {}
for hazard_dict in [opt[key] for key in opt.keys() if "hazard" in key]:
hazard_dict.update(
{"map_fn": config["hazard_dp"].joinpath(hazard_dict["map_fn"])}
)
if hazard_dict["map_type"] not in config["hazard"].keys():
config["hazard"][hazard_dict["map_type"]] = {
hazard_dict["map_fn"].stem: hazard_dict,
}
else:
config["hazard"][hazard_dict["map_type"]].update(
{
hazard_dict["map_fn"].stem: hazard_dict,
}
)

# Store the exposure information.
config["exposure"] = opt["setup_exposure"]

return config

def write(self):
writer = Writer()
writer.write_staticmaps()
writer.write_staticgeoms()
writer.write_config()
"""Method to write the complete model schematization and configuration to file."""

self.logger.info(f"Writing model data to {self.root}")
if self.config: # try to read default if not yet set
self.write_config()
if self._staticmaps:
self.write_grid()
if self._staticgeoms:
self.write_geoms()

def _configwrite(self, fn):
"""Write config to Delft-FIAT configuration toml file."""
# TODO: change function to new Delft-FIAT configuration toml file.
parser = ConfigParser()

# Store the general information.
parser["setup_config"] = {
"case": str(self.config.get("case")),
"strategy": str(self.config.get("strategy")),
"scenario": str(self.config.get("scenario")),
"year": str(self.config.get("year")),
"country": str(self.get_config("country")),
"hazard_type": str(self.config.get("hazard_type")),
"output_unit": str(self.config.get("output_unit")),
# "hazard_dp": str(self.config.get("hazard_dp").name),
# "exposure_dp": str(self.config.get("exposure_dp").name),
# "vulnerability_dp": str(self.config.get("vulnerability_dp").name),
# "output_dp": str(self.config.get("output_dp").name),
"category_output": str(self.config.get("category_output")),
"total_output": str(self.config.get("total_output")),
"risk_output": str(self.config.get("risk_output")),
"map_output": str(self.config.get("map_output")),
}

# # Save the configuration file.
# with open(self.root.joinpath(self._CONF), "w") as config:
# parser.write(config)
102 changes: 0 additions & 102 deletions hydromt_fiat/reader.py

This file was deleted.

4 changes: 2 additions & 2 deletions hydromt_fiat/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### TO BE UPDATED ###
class Validation:
""" CONTROL FUNCTIONS """
"""CONTROL FUNCTIONS"""

def check_dir_exist(self, dir, name=None):
""" """
Expand Down Expand Up @@ -84,4 +84,4 @@ def check_uniqueness(self, *args, file_type=None, filename=None):
self.get_config(args[0], args[1], key)
== list(branch[args[0]][args[1]].values())[0]
):
raise ValueError(f"Each model input layers must be unique.")
raise ValueError("Each model input layers must be unique.")
7 changes: 7 additions & 0 deletions hydromt_fiat/workflows/aggregation_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from hydromt_fiat.workflows.exposure_vector import ExposureVector


class AggregationLabels(ExposureVector):
"""This will be the class to generate aggregation labels for Delft-FIAT."""

NotImplemented
Loading