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

Feat/622 adaptation calculate full link cost #623

Merged
merged 101 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
48326b5
chore: extend config and reader
ArdtK Nov 27, 2024
84f599d
chore: add/correct attributes of config data
ArdtK Nov 27, 2024
2954acf
chore: extend reader with options
ArdtK Nov 27, 2024
69015a8
chore: add adaptation property and extend test
ArdtK Nov 27, 2024
88af277
chore: first changes
ArdtK Nov 27, 2024
2b70a23
chore: add adaptation to enum
ArdtK Nov 27, 2024
bdbc35a
Merge branch 'feat/604-adaptation-extend-analysis-config-dataclass-an…
ArdtK Nov 27, 2024
ed85cca
chore: extend classes
ArdtK Nov 28, 2024
e84e8b2
chore: put adaptation in separate enum
ArdtK Nov 28, 2024
825d2b1
Merge branch 'feat/604-adaptation-extend-analysis-config-dataclass-an…
ArdtK Nov 28, 2024
befdfa8
chore: add get_analysis
ArdtK Nov 28, 2024
4bab16c
chore: remove no_intervention from config
ArdtK Nov 28, 2024
a4c6d24
chore: rename no_intervention to no_adaptation
ArdtK Nov 28, 2024
47c94fc
Delete ra2ce/analysis/adaptation/adaptation_option_collection.py
ArdtK Nov 28, 2024
b00ba88
chore: restore no_adaptation_option
ArdtK Nov 28, 2024
214582a
Merge branch 'feat/604-adaptation-extend-analysis-config-dataclass-an…
ArdtK Nov 28, 2024
d23492b
Merge branch 'feat/604-adaptation-extend-analysis-config-dataclass-an…
ArdtK Nov 28, 2024
c1dee8b
chore: expand logic and add tests
ArdtK Nov 28, 2024
d11c58c
chore: add losses analysis name to config
ArdtK Nov 28, 2024
e7826df
Merge branch 'feat/604-adaptation-extend-analysis-config-dataclass-an…
ArdtK Nov 28, 2024
61e09a1
chore: take losses_analysis from config
ArdtK Nov 28, 2024
60a349f
chore: fix test
ArdtK Nov 28, 2024
af07a89
test: add tests
ArdtK Nov 28, 2024
3f49320
chore: fix logic for paths
ArdtK Nov 28, 2024
75f3117
chore: small changes
ArdtK Nov 28, 2024
1fc6161
Merge branch 'master' into feat/592-adaptation-create-class-adaptatio…
ArdtK Nov 28, 2024
03be931
test: add test data
ArdtK Nov 28, 2024
4cbdd1c
chore: small change of folders
ArdtK Nov 28, 2024
cda391a
chore: small changes to paths
ArdtK Nov 28, 2024
9c06029
chore: small ini change
ArdtK Nov 28, 2024
7485c06
chore: typo
ArdtK Nov 28, 2024
5c9e0fb
chore: create class and extend factory
ArdtK Nov 28, 2024
7a0223f
test: fix failing tests
ArdtK Nov 28, 2024
26863c4
Merge branch 'feat/592-adaptation-create-class-adaptationoptioncollec…
ArdtK Nov 28, 2024
6254a5d
Merge branch 'master' into feat/584-adaptation-run-a-cost-analysis
ArdtK Nov 28, 2024
a76027f
chore: remove VAT
ArdtK Nov 29, 2024
50be217
chore: add cost calculation
ArdtK Nov 29, 2024
4c0c20b
chore: process review comments
ArdtK Nov 29, 2024
16edc1d
chore: process review comments
ArdtK Nov 29, 2024
1cd3433
merge master
ArdtK Nov 29, 2024
72c9b22
Merge branch 'feat/592-adaptation-create-class-adaptationoptioncollec…
ArdtK Nov 29, 2024
0368840
chore: fix issues
ArdtK Nov 29, 2024
1e1fb5b
chore: fix test
ArdtK Nov 29, 2024
3f42bce
test: add output_graph files
ArdtK Nov 29, 2024
4bcdc28
test: remove test
ArdtK Nov 29, 2024
f13eb06
empty commit
ArdtK Nov 29, 2024
a8c4b56
chore: process review comments
ArdtK Nov 29, 2024
de0e7ed
chore: add base_network in the factory
ArdtK Nov 29, 2024
f831e28
chore: add cost calculation
ArdtK Nov 29, 2024
8465254
chore: rename collection attribute
ArdtK Nov 29, 2024
c72fce6
empty commit
ArdtK Nov 29, 2024
4bb2c6d
chore: small changes
ArdtK Nov 29, 2024
690ff85
chore: add calculate to collectino
ArdtK Nov 30, 2024
98480a6
chore: add root_path
ArdtK Dec 2, 2024
9b34fc5
test: extend conftest
ArdtK Dec 2, 2024
84def9d
test: move test input
ArdtK Dec 2, 2024
5d0bb7e
chore: add path properties to adaptation_option
ArdtK Dec 2, 2024
76f761f
chore: first damages setup
ArdtK Dec 2, 2024
6235bb3
test: fix/extend tests
ArdtK Dec 2, 2024
7d95030
Merge branch '594-adaptation-create-and-save-result-cost-options' int…
ArdtK Dec 2, 2024
7a8ec23
chore: add aggregate_wl to config
ArdtK Dec 2, 2024
a618cf6
chore: add losses (start)
ArdtK Dec 2, 2024
9f64234
chore: big overhaul of the creation of adaptation options
ArdtK Dec 2, 2024
7aa4602
chore: add docstring
ArdtK Dec 2, 2024
5d3001c
chore: remove unused imports
ArdtK Dec 2, 2024
743cb47
empty commit to conclude merge
ArdtK Dec 2, 2024
19d749e
test: fix losses input paths
ArdtK Dec 2, 2024
20da45c
chore: fix losses run
ArdtK Dec 2, 2024
a450bd3
chore: small cleanup
ArdtK Dec 2, 2024
5dc4cd4
chore: small changes, add docstring
ArdtK Dec 3, 2024
3f86093
chore: fix tests
ArdtK Dec 3, 2024
9cc580d
chore: small changes
ArdtK Dec 3, 2024
d07a98d
test: adapt losses configs
ArdtK Dec 3, 2024
78bc2a6
chore: restore graph_file_hazard
ArdtK Dec 3, 2024
fc2bbf0
chore: copy static folder as well for avg_speed
ArdtK Dec 3, 2024
20801ce
chore: add hazard section with wl_aggregate
ArdtK Dec 3, 2024
3303e1f
test: correct name of resilience_curve csv
ArdtK Dec 3, 2024
d1ac1dc
updated network in test data
Cham8920 Dec 3, 2024
1c46891
Merge branch 'feat/595-adaptation-run-and-combine-losses-and-damages'…
ArdtK Dec 3, 2024
f6baae1
chore: calculate benefit for options
ArdtK Dec 3, 2024
c8c5eb6
chore: final changes
ArdtK Dec 3, 2024
c05cdb6
chore: add TODOs
ArdtK Dec 3, 2024
914ede8
chore: add scope to fixtures
ArdtK Dec 4, 2024
3f78f5c
chore: calculate link cost
ArdtK Dec 4, 2024
c25f7fd
chore: change scope to package
ArdtK Dec 4, 2024
716ec3d
chore: change readers into dataclasses
ArdtK Dec 4, 2024
861fb0c
chore: add expected total cost (for now based on unit cost only)
ArdtK Dec 4, 2024
dfd550d
chore: process rework
ArdtK Dec 5, 2024
f9e629e
chore: add/update tests
ArdtK Dec 5, 2024
ba83bf5
chore: rework rerooting config
ArdtK Dec 5, 2024
55a743d
chore: last changes for handling paths
ArdtK Dec 5, 2024
4bad422
chore: remove analysis.ini output file
ArdtK Dec 5, 2024
00d5c3c
Merge branch 'feat/595-adaptation-run-and-combine-losses-and-damages'…
ArdtK Dec 5, 2024
d4934b6
chore: test with "session" scope
ArdtK Dec 5, 2024
5eb5404
chore: fix expected result
ArdtK Dec 5, 2024
5e151f7
chore: remove scope for concurrency issues
ArdtK Dec 5, 2024
2acea02
chore: another test with scope=package
ArdtK Dec 5, 2024
06e9e9f
chore: test without request
ArdtK Dec 5, 2024
6d3bd11
chore: remove scope
ArdtK Dec 5, 2024
767afc2
Merge branch 'master' into feat/622-adaptation-calculate-full-link-cost
ArdtK Dec 5, 2024
cf6f0f1
fix: fix issue on master
ArdtK Dec 5, 2024
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
20 changes: 12 additions & 8 deletions ra2ce/analysis/adaptation/adaptation.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,24 @@ def execute(self) -> GeoDataFrame:

def run_cost(self) -> GeoDataFrame:
"""
Calculate the unit cost for all adaptation options.

Returns:
GeoDataFrame: The result of the cost calculation.
<<<<<<< HEAD
Calculate the link cost for all adaptation options.
=======
Calculate the unit cost for all adaptation options.
>>>>>>> master

Returns:
GeoDataFrame: The result of the cost calculation.
"""
_cost_gdf = deepcopy(self.graph_file.get_graph())

for (
_option,
_cost,
) in self.adaptation_collection.calculate_options_cost().items():
_cost_gdf[f"{_option.id}_cost"] = _cost

# TODO: calculate link cost instead of unit cost
) in self.adaptation_collection.calculate_options_unit_cost().items():
_cost_gdf[f"{_option.id}_cost"] = _cost_gdf.apply(
lambda x, cost=_cost: x["length"] * cost, axis=1
)

return _cost_gdf

Expand Down
2 changes: 1 addition & 1 deletion ra2ce/analysis/adaptation/adaptation_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def from_config(
analysis_config=analysis_config,
)

def calculate_cost(self, time_horizon: float, discount_rate: float) -> float:
def calculate_unit_cost(self, time_horizon: float, discount_rate: float) -> float:
"""
Calculate the net present value unit cost (per meter) of the adaptation option.

Expand Down
4 changes: 2 additions & 2 deletions ra2ce/analysis/adaptation/adaptation_option_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ def from_config(

return _collection

def calculate_options_cost(self) -> dict[AdaptationOption, float]:
def calculate_options_unit_cost(self) -> dict[AdaptationOption, float]:
"""
Calculate the unit cost for all adaptation options.
Returns:
dict[AdaptationOption, float]: The calculated cost for all adaptation options.
"""
return {
_option: _option.calculate_cost(
_option: _option.calculate_unit_cost(
self.time_horizon,
self.discount_rate,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/analysis/adaptation/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class AdaptationOptionCases:
),
]
unit_cost: list[float] = [0.0, 2693.684211, 5231.908660]
total_cost: list[float] = [0.0, 633015.789583, 1229498.535112]
total_cost: list[float] = [0.0, 97411702.122141, 189201512.873560]
cases: list[tuple[AnalysisSectionAdaptationOption, float, float]] = list(
zip(config_cases, unit_cost, total_cost)
)
Expand Down
4 changes: 2 additions & 2 deletions tests/analysis/adaptation/test_adaptation_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_from_config_no_damages_losses_raises(self):
"adaptation_option",
AdaptationOptionCases.cases,
)
def test_calculate_option_cost_returns_float(
def test_calculate_unit_cost_returns_float(
self,
valid_adaptation_config: tuple[AnalysisInputWrapper, AnalysisConfigWrapper],
adaptation_option: tuple[AnalysisSectionAdaptation, float, float],
Expand All @@ -82,7 +82,7 @@ def test_calculate_option_cost_returns_float(
_discount_rate = valid_adaptation_config[1].config_data.adaptation.discount_rate

# 2. Run test.
_cost = _option.calculate_cost(_time_horizon, _discount_rate)
_cost = _option.calculate_unit_cost(_time_horizon, _discount_rate)

# 3. Verify expectations.
assert isinstance(_cost, float)
Expand Down
14 changes: 14 additions & 0 deletions tests/analysis/adaptation/test_adaptation_option_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ def test_from_config_no_adaptation_raises(self):

# 3. Verify expectations.
assert _exc.match("No adaptation section found in the analysis config data.")

def test_calculate_options_unit_cost(
self,
valid_adaptation_config: tuple[AnalysisInputWrapper, AnalysisConfigWrapper],
):
# 1. Define test data.
_collection = AdaptationOptionCollection.from_config(valid_adaptation_config[1])

# 2. Run test.
_result = _collection.calculate_options_unit_cost()

# 3. Verify expectations.
assert isinstance(_result, dict)
assert all(_option in _result for _option in _collection.adaptation_options)
4 changes: 2 additions & 2 deletions tests/analysis/test_analysis_result_wrapper_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from shapely import Point

from ra2ce.analysis.analysis_config_data.analysis_config_data import AnalysisConfigData
from ra2ce.analysis.analysis_config_data.analysis_config_data import AnalysisSectionBase
from ra2ce.analysis.analysis_config_data.enums.analysis_losses_enum import (
AnalysisLossesEnum,
)
Expand Down Expand Up @@ -36,7 +36,7 @@ def valid_result_wrapper(
) -> AnalysisResultWrapper:
class MockedAnalysis(AnalysisProtocol):
def __init__(self) -> None:
_analysis = AnalysisConfigData.ANALYSIS_SECTION(
_analysis = AnalysisSectionBase(
name="Mocked Analysis", save_csv=False, save_gpkg=False
)
_analysis.analysis = AnalysisLossesEnum.SINGLE_LINK_LOSSES
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tests import acceptance_test_data


@pytest.fixture(name="valid_analysis_ini")
@pytest.fixture(name="valid_analysis_ini", scope="session")
def _get_valid_analysis_ini_fixture() -> Iterator[Path]:
_ini_file = acceptance_test_data.joinpath("analyses.ini")
assert _ini_file.exists()
Expand Down