-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #314 from Deltares/chore/310-refactor-analyses-to-…
…common-interface chore: 310 refactor direct analyses to common interface
- Loading branch information
Showing
20 changed files
with
713 additions
and
464 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.