Skip to content

Commit

Permalink
Merge pull request #164 from Deltares/feature/107_adapt_vector_networ…
Browse files Browse the repository at this point in the history
…k_wrapper

Feature/107 adapt vector network wrapper
  • Loading branch information
Carsopre authored Jul 28, 2023
2 parents 8b0aaf9 + 07162c1 commit 7f7b512
Show file tree
Hide file tree
Showing 45 changed files with 1,274 additions and 1,214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@


from __future__ import annotations

from ra2ce.common.configuration.config_data_protocol import ConfigDataProtocol


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@


from pathlib import Path

from ra2ce.analyses.analysis_config_data.analysis_config_data import (
AnalysisConfigDataWithoutNetwork,
)

from ra2ce.common.validation.ra2ce_validator_protocol import Ra2ceIoValidator
from ra2ce.common.validation.validation_report import ValidationReport
from ra2ce.graph.network_config_data.network_config_data_validator import (
NetworkDictValues,
)


IndirectAnalysisNameList: list[str] = [
"single_link_redundancy",
"multi_link_redundancy",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
from pathlib import Path
from typing import Optional

from ra2ce.analyses.analysis_config_wrapper.analysis_config_wrapper_base import (
AnalysisConfigWrapperBase,
)
from ra2ce.analyses.analysis_config_data.readers.analysis_config_reader_base import (
AnalysisConfigReaderBase,
)
Expand All @@ -35,6 +32,9 @@
from ra2ce.analyses.analysis_config_data.readers.analysis_config_reader_without_network import (
AnalysisConfigReaderWithoutNetwork,
)
from ra2ce.analyses.analysis_config_wrapper.analysis_config_wrapper_base import (
AnalysisConfigWrapperBase,
)
from ra2ce.graph.network_config_data.network_config_data import NetworkConfigData


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@

from pathlib import Path

from ra2ce.analyses.analysis_config_wrapper.analysis_config_wrapper_base import (
AnalysisConfigWrapperBase,
)
from ra2ce.analyses.analysis_config_data.analysis_config_data import (
AnalysisConfigDataWithNetwork,
)
from ra2ce.analyses.analysis_config_data.readers.analysis_config_reader_base import (
AnalysisConfigReaderBase,
)
from ra2ce.analyses.analysis_config_wrapper.analysis_config_wrapper_base import (
AnalysisConfigWrapperBase,
)
from ra2ce.graph.network_config_wrapper import NetworkConfigWrapper


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
import logging
from pathlib import Path

from ra2ce.analyses.analysis_config_wrapper.analysis_config_wrapper_base import (
AnalysisConfigWrapperBase,
)
from ra2ce.analyses.analysis_config_data.analysis_config_data import (
AnalysisConfigDataWithoutNetwork,
)
from ra2ce.analyses.analysis_config_data.readers.analysis_config_reader_base import (
AnalysisConfigReaderBase,
)
from ra2ce.analyses.analysis_config_wrapper.analysis_config_wrapper_base import (
AnalysisConfigWrapperBase,
)
from ra2ce.graph.network_config_data.network_config_data_reader import (
NetworkConfigDataReader,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"""


from abc import abstractmethod, abstractclassmethod
from abc import abstractclassmethod, abstractmethod
from pathlib import Path
from typing import Optional

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
from pathlib import Path
from typing import Optional

from ra2ce.analyses.analysis_config_wrapper.analysis_config_wrapper_base import (
AnalysisConfigWrapperBase,
)
from ra2ce.analyses.analysis_config_data.analysis_config_data import (
AnalysisConfigData,
AnalysisConfigDataWithNetwork,
AnalysisConfigDataWithoutNetwork,
)
from ra2ce.analyses.analysis_config_wrapper.analysis_config_wrapper_base import (
AnalysisConfigWrapperBase,
)
from ra2ce.analyses.analysis_config_wrapper.analysis_config_wrapper_with_network import (
AnalysisConfigWrapperWithNetwork,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
from __future__ import annotations

from pathlib import Path
from ra2ce.analyses.analysis_config_data.analysis_config_data_validator_with_network import AnalysisConfigDataValidatorWithNetwork

from ra2ce.analyses.analysis_config_data.analysis_config_data import AnalysisConfigData
from ra2ce.analyses.analysis_config_data.analysis_config_data_validator_with_network import (
AnalysisConfigDataValidatorWithNetwork,
)
from ra2ce.analyses.analysis_config_wrapper.analysis_config_wrapper_base import (
AnalysisConfigWrapperBase,
)
from ra2ce.analyses.analysis_config_data.analysis_config_data import AnalysisConfigData
from ra2ce.graph.network_config_data.network_config_data import NetworkConfigData
from ra2ce.graph.network_config_wrapper import NetworkConfigWrapper

Expand Down Expand Up @@ -98,5 +100,7 @@ def configure(self) -> None:

def is_valid(self) -> bool:
_file_is_valid = self.ini_file.is_file() and self.ini_file.suffix == ".ini"
_validation_report = AnalysisConfigDataValidatorWithNetwork(self.config_data).validate()
_validation_report = AnalysisConfigDataValidatorWithNetwork(
self.config_data
).validate()
return _file_is_valid and _validation_report.is_valid()
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@

import logging
from pathlib import Path
from ra2ce.analyses.analysis_config_data.analysis_config_data_validator_without_network import AnalysisConfigDataValidatorWithoutNetwork

from ra2ce.analyses.analysis_config_data.analysis_config_data import AnalysisConfigData
from ra2ce.analyses.analysis_config_data.analysis_config_data_validator_without_network import (
AnalysisConfigDataValidatorWithoutNetwork,
)
from ra2ce.analyses.analysis_config_wrapper.analysis_config_wrapper_base import (
AnalysisConfigWrapperBase,
)
from ra2ce.analyses.analysis_config_data.analysis_config_data import AnalysisConfigData
from ra2ce.graph.network_config_wrapper import NetworkConfigWrapper


Expand Down Expand Up @@ -77,5 +79,7 @@ def configure(self) -> None:

def is_valid(self) -> bool:
_file_is_valid = self.ini_file.is_file() and self.ini_file.suffix == ".ini"
_validation_report = AnalysisConfigDataValidatorWithoutNetwork(self.config_data).validate()
_validation_report = AnalysisConfigDataValidatorWithoutNetwork(
self.config_data
).validate()
return _file_is_valid and _validation_report.is_valid()
2 changes: 1 addition & 1 deletion ra2ce/common/configuration/config_data_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ def to_dict(self) -> dict:
Returns:
dict: Dictionary representing the `ConfigDataProtocol` instance.
"""
pass
pass
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from pathlib import Path
from typing import Protocol, runtime_checkable

from ra2ce.common.configuration.config_data_protocol import ConfigDataProtocol
from ra2ce.common.io.readers.file_reader_protocol import FileReaderProtocol

Expand Down
8 changes: 4 additions & 4 deletions ra2ce/configuration/config_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
"""


from pathlib import Path
import shutil
from pathlib import Path
from typing import Optional

from ra2ce.analyses.analysis_config_data.readers.analysis_config_reader_factory import (
AnalysisConfigReaderFactory,
)
from ra2ce.analyses.analysis_config_wrapper.analysis_config_wrapper_base import (
AnalysisConfigWrapperBase,
)
from ra2ce.analyses.analysis_config_wrapper.analysis_config_wrapper_factory import (
AnalysisConfigWrapperFactory,
)
from ra2ce.analyses.analysis_config_data.readers.analysis_config_reader_factory import (
AnalysisConfigReaderFactory,
)
from ra2ce.configuration.config_wrapper import ConfigWrapper
from ra2ce.graph.network_config_data.network_config_data import NetworkConfigData
from ra2ce.graph.network_config_data.network_config_data_reader import (
Expand Down
18 changes: 14 additions & 4 deletions ra2ce/graph/network_config_data/network_config_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from pathlib import Path
from typing import Optional

from pyproj import CRS

from ra2ce.common.configuration.config_data_protocol import ConfigDataProtocol


Expand All @@ -37,10 +39,10 @@ class ProjectSection:
class NetworkSection:
directed: bool = False
source: str = "" # should be enum
primary_file: str = "" # TODO. Unclear whether this is `Path` or `list[Path]`
diversion_file: str = "" # TODO. Unclear whether this is `Path` or `list[Path]`
primary_file: list[Path] = field(default_factory=list)
diversion_file: list[Path] = field(default_factory=list)
file_id: str = ""
polygon: str = "" # TODO. Unclear whether this is `str`` or `Path`
polygon: Optional[Path] = None
network_type: str = "" # Should be enum
road_types: list[str] = field(default_factory=list)
save_shp: bool = False
Expand Down Expand Up @@ -90,7 +92,8 @@ class NetworkConfigData(ConfigDataProtocol):
input_path: Optional[Path] = None
output_path: Optional[Path] = None
static_path: Optional[Path] = None

# CRS is not yet supported in the ini file, it might be relocated to a subsection.
crs: CRS = field(default_factory=lambda: CRS.from_user_input(4326))
project: ProjectSection = field(default_factory=lambda: ProjectSection())
network: NetworkSection = field(default_factory=lambda: NetworkSection())
origins_destinations: OriginsDestinationsSection = field(
Expand All @@ -106,8 +109,15 @@ def output_graph_dir(self) -> Optional[Path]:
return None
return self.static_path.joinpath("output_graph")

@property
def network_dir(self) -> Optional[Path]:
if not self.static_path:
return None
return self.static_path.joinpath("network")

def to_dict(self) -> dict:
_dict = self.__dict__
_dict["crs"] = self.crs.to_epsg()
_dict["project"] = self.project.__dict__
_dict["network"] = self.network.__dict__
_dict["origins_destinations"] = self.origins_destinations.__dict__
Expand Down
53 changes: 45 additions & 8 deletions ra2ce/graph/network_config_data/network_config_data_reader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from configparser import ConfigParser
from pathlib import Path
from typing import Union
from typing import Any, Union

from ra2ce.common.configuration.ini_configuration_reader_protocol import (
ConfigDataReaderProtocol,
Expand Down Expand Up @@ -58,6 +58,15 @@ def _select_to_correct(path_value: Union[list[Path], Path]) -> bool:
return _select_to_correct(path_value[0])
return not path_value.exists()

def _correct_list(path_root: Path, path_value_list: list[Path]) -> list[Path]:
_corrected_list = []
for _path_value in path_value_list:
if not _path_value.exists():
_corrected_list.append(path_root.joinpath(_path_value))
else:
_corrected_list.append(_path_value)
return _corrected_list

# Relative to network directory.
_network_directory = config_data.static_path.joinpath("network")
if _select_to_correct(config_data.origins_destinations.origins):
Expand All @@ -70,15 +79,28 @@ def _select_to_correct(path_value: Union[list[Path], Path]) -> bool:
config_data.origins_destinations.destinations
)

if _select_to_correct(config_data.origins_destinations.region):
config_data.origins_destinations.region = _network_directory.joinpath(
config_data.origins_destinations.region
)

if _select_to_correct(config_data.network.polygon):
config_data.network.polygon = _network_directory.joinpath(
config_data.network.polygon
)

config_data.network.primary_file = _correct_list(
_network_directory, config_data.network.primary_file
)
config_data.network.diversion_file = _correct_list(
_network_directory, config_data.network.diversion_file
)

# Relative to hazard directory.
_hazard_directory = config_data.static_path.joinpath("hazard")
if _select_to_correct(config_data.hazard.hazard_map):
config_data.hazard.hazard_map = list(
map(
lambda x: _hazard_directory.joinpath(x),
config_data.hazard.hazard_map,
)
)
config_data.hazard.hazard_map = _correct_list(
_hazard_directory, config_data.hazard.hazard_map
)

def _get_str_as_path(self, str_value: Union[str, Path]) -> Path:
if str_value and not isinstance(str_value, Path):
Expand Down Expand Up @@ -107,9 +129,23 @@ def _get_sections(self) -> dict:
def get_project_section(self) -> ProjectSection:
return ProjectSection(**self._parser["project"])

def _get_path_list(
self, section_name: str, property: str, fallback_opt: Any
) -> list[Path]:
_value_list = self._parser.getlist(
section_name, property, fallback=fallback_opt
)
return list(map(self._get_str_as_path, _value_list))

def get_network_section(self) -> NetworkSection:
_section = "network"
_network_section = NetworkSection(**self._parser[_section])
_network_section.primary_file = self._get_path_list(
_section, "primary_file", _network_section.primary_file
)
_network_section.diversion_file = self._get_path_list(
_section, "diversion_file", _network_section.diversion_file
)
_network_section.directed = self._parser.getboolean(
_section, "directed", fallback=_network_section.directed
)
Expand All @@ -119,6 +155,7 @@ def get_network_section(self) -> NetworkSection:
_network_section.road_types = self._parser.getlist(
_section, "road_types", fallback=_network_section.road_types
)
_network_section.polygon = self._get_str_as_path(_network_section.polygon)
return _network_section

def get_origins_destinations_section(self) -> OriginsDestinationsSection:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""

from typing import Any

from ra2ce.common.validation.ra2ce_validator_protocol import Ra2ceIoValidator
from ra2ce.common.validation.validation_report import ValidationReport
from ra2ce.graph.network_config_data.network_config_data import (
Expand Down
1 change: 1 addition & 0 deletions ra2ce/graph/network_config_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
)
from ra2ce.graph.networks import Network


class NetworkConfigWrapper(ConfigWrapperProtocol):
files: Dict[str, Path] = {}
config_data: NetworkConfigData
Expand Down
8 changes: 8 additions & 0 deletions ra2ce/graph/network_wrappers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Network wrappers

In this module we define wrappers for reading a ra2ce network (`tuple[MultiGraph, GeoDataFrame]`). Each different class defines how a network will be created and cleaned.
Network wrappers need to instantiate the `NetworkWrapperProtocol`.

An instance of the `NetworkWrapperProtocol` defines the `get_network` method, which will return the aforementioend ra2ce network.

Any network wrapper can be created with the use of a `NetworkConfigData` instance. At the same time, if the user does not know which wrapper to use, the `NetworkWrapperFactory` will resolve this for us.
File renamed without changes.
Loading

0 comments on commit 7f7b512

Please sign in to comment.