Skip to content

Commit

Permalink
Merge pull request #314 from Deltares/chore/310-refactor-analyses-to-…
Browse files Browse the repository at this point in the history
…common-interface

chore: 310 refactor direct analyses to common interface
  • Loading branch information
ArdtK authored Mar 11, 2024
2 parents 89d9020 + dd9b324 commit 8e6114b
Show file tree
Hide file tree
Showing 20 changed files with 713 additions and 464 deletions.
2 changes: 1 addition & 1 deletion examples/example_direct_damage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
"id_name_origin_destination = None<br>\n",
"origin_count = None<br>\n",
"origin_out_fraction = None<br>\n",
"category = categNoneory**<br>\n",
"category = category**<br>\n",
"<br>\n",
"[hazard]<br>\n",
"**hazard_map = max_flood_depth.tif<br>\n",
Expand Down
56 changes: 56 additions & 0 deletions ra2ce/analysis/analysis_collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Risk Assessment and Adaptation for Critical Infrastructure (RA2CE).
Copyright (C) 2023 Stichting Deltares
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

from __future__ import annotations

from dataclasses import dataclass, field

from ra2ce.analysis.analysis_config_wrapper import AnalysisConfigWrapper
from ra2ce.analysis.analysis_factory import AnalysisFactory
from ra2ce.analysis.analysis_protocol import AnalysisProtocol


@dataclass
class AnalysisCollection:
direct_analyses: list[AnalysisProtocol] = field(default_factory=list)
indirect_analyses: list[AnalysisProtocol] = field(default_factory=list)

@classmethod
def from_config(cls, analysis_config: AnalysisConfigWrapper) -> AnalysisCollection:
"""
Create an AnalysisCollection from an AnalysisConfigWrapper.
Args:
analysis_config (AnalysisConfigWrapper): The analysis configuration.
Returns:
AnalysisCollection: Collection of analyses to be executed.
"""
return cls(
direct_analyses=[
AnalysisFactory(analysis).get_analysis(analysis_config)
for analysis in analysis_config.config_data.direct
],
indirect_analyses=[
AnalysisFactory(analysis).get_analysis(analysis_config)
for analysis in analysis_config.config_data.indirect
],
)
70 changes: 70 additions & 0 deletions ra2ce/analysis/analysis_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Risk Assessment and Adaptation for Critical Infrastructure (RA2CE).
Copyright (C) 2023 Stichting Deltares
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

from ra2ce.analysis.analysis_config_data.analysis_config_data import (
AnalysisSectionDirect,
AnalysisSectionIndirect,
)
from ra2ce.analysis.analysis_config_data.enums.analysis_direct_enum import (
AnalysisDirectEnum,
)
from ra2ce.analysis.analysis_config_wrapper import AnalysisConfigWrapper
from ra2ce.analysis.analysis_protocol import AnalysisProtocol
from ra2ce.analysis.direct.direct_damage import DirectDamage
from ra2ce.analysis.direct.effectiveness_measures import EffectivenessMeasures


class AnalysisFactory:
analysis: AnalysisSectionIndirect | AnalysisSectionDirect

def __init__(
self, analysis: AnalysisSectionIndirect | AnalysisSectionDirect
) -> None:
self.analysis = analysis

def get_analysis(self, analysis_config: AnalysisConfigWrapper) -> AnalysisProtocol:
"""
Create an analysis based on the given analysis configuration.
Args:
analysis_config (AnalysisConfigWrapper): Analysis configuration.
Raises:
NotImplementedError: The analysis type is not implemented.
Returns:
AnalysisProtocol: The analysis to be executed.
"""
if self.analysis.analysis == AnalysisDirectEnum.DIRECT:
return DirectDamage(
analysis_config.graph_files.base_network_hazard,
self.analysis,
analysis_config.config_data.input_path,
analysis_config.config_data.output_path,
)
elif self.analysis.analysis == AnalysisDirectEnum.EFFECTIVENESS_MEASURES:
return EffectivenessMeasures(
analysis_config.graph_files.base_network_hazard,
self.analysis,
analysis_config.config_data.input_path,
analysis_config.config_data.output_path,
)
raise NotImplementedError
47 changes: 47 additions & 0 deletions ra2ce/analysis/analysis_protocol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Risk Assessment and Adaptation for Critical Infrastructure (RA2CE).
Copyright (C) 2023 Stichting Deltares
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

from pathlib import Path
from typing import Optional, Protocol

from geopandas import GeoDataFrame

from ra2ce.analysis.analysis_config_data.analysis_config_data import AnalysisSectionBase
from ra2ce.network.graph_files.graph_files_protocol import GraphFileProtocol


class AnalysisProtocol(Protocol):
graph_file: GraphFileProtocol
analysis: AnalysisSectionBase
input_path: Path
output_path: Path
result: Optional[GeoDataFrame]

def execute(self) -> GeoDataFrame:
"""
Execute the analysis on the given graph/network with the given analysis parameters.
The resulting (Geo)DataFrame of the analysis is stored in the result attribute.
TODO: Make the return type a result object #318
Returns:
GeoDataFrame: The result of the analysis.
"""
pass
Loading

0 comments on commit 8e6114b

Please sign in to comment.