-
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.
Fix/512 read year from config for control risk calculation in cut fro…
…m year (#514) * chore: renaming representative_damage_percentile * chore: year is read from config, default arguments are deleted * test: test is updated where year is not input * 513 resolve_zero_division_in_triangle_to_null (#516) * chore: if year is 0 then turned it into 1. * feat: function created to aggregate link damages and risk * chore: extra comment lines removed. (previous scipt part replaced with a function) * feat: to_integrate_shaper_factory is created. hz_to_integrate_shaper is completed. osd_to_integrate_shaper under construction * feat: osd_to_integrate_shaper is completed. * feat: man_to_integrate_shaper is completed. * chore: pattern added to find risk_<vulnerability_curve> * chore: skip adding np.nan of the segment_value to the link_value * chore: risk_result_columns was referenced before being defined * chore: tests updated to refer to the correct risk column * chore: _rps = list(_to_integrate.columns) assigned once
- Loading branch information
1 parent
88a4350
commit 66d1961
Showing
10 changed files
with
411 additions
and
102 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
Empty file.
30 changes: 30 additions & 0 deletions
30
ra2ce/analysis/damages/shape_to_integrate_object/hz_to_integrate_shaper.py
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,30 @@ | ||
import geopandas as gpd | ||
|
||
from ra2ce.analysis.analysis_config_data.enums.damage_curve_enum import DamageCurveEnum | ||
from ra2ce.analysis.damages.shape_to_integrate_object.to_Integrate_shaper_protocol import ( | ||
ToIntegrateShaperProtocol, | ||
) | ||
|
||
|
||
class HzToIntegrateShaper(ToIntegrateShaperProtocol): | ||
gdf: gpd.GeoDataFrame | ||
|
||
def __init__(self, gdf): | ||
self.gdf = gdf | ||
|
||
def get_return_periods(self) -> list: | ||
return sorted(c for c in self.gdf.columns if c.startswith("dam")) | ||
|
||
def shape_to_integrate_object( | ||
self, return_periods: list | ||
) -> dict[str : gpd.GeoDataFrame]: | ||
_to_integrate = self.gdf[return_periods] | ||
|
||
_to_integrate.columns = [ | ||
float(c.split("_")[1].replace("RP", "")) for c in _to_integrate.columns | ||
] | ||
return { | ||
DamageCurveEnum.HZ.name: _to_integrate.sort_index( | ||
axis="columns", ascending=False | ||
) | ||
} |
71 changes: 71 additions & 0 deletions
71
ra2ce/analysis/damages/shape_to_integrate_object/man_to_integrate_shaper.py
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,71 @@ | ||
import geopandas as gpd | ||
import pandas as pd | ||
import re | ||
|
||
from ra2ce.analysis.damages.shape_to_integrate_object.to_Integrate_shaper_protocol import ( | ||
ToIntegrateShaperProtocol, | ||
) | ||
|
||
|
||
class ManToIntegrateShaper(ToIntegrateShaperProtocol): | ||
gdf: gpd.GeoDataFrame | ||
|
||
def __init__(self, gdf): | ||
self.gdf = gdf | ||
|
||
@staticmethod | ||
def _extract_columns_by_pattern( | ||
pattern_text: str, gdf: gpd.GeoDataFrame | ||
) -> set[str]: | ||
""" | ||
Extract column names based on the provided regex pattern. | ||
Args: | ||
pattern_text (Pattern[str]): The compiled regex pattern to match the column names. | ||
df (pd.DataFrame): The DataFrame from which to extract the RP values. | ||
Returns: | ||
Set[str]: A set of RP values extracted from the column names. | ||
""" | ||
pattern = re.compile(pattern_text) | ||
columns = {pattern.search(c).group(1) for c in gdf.columns if pattern.search(c)} | ||
return columns | ||
|
||
def get_return_periods(self) -> list: | ||
# Extract the RP values from the columns | ||
rp_values = ManToIntegrateShaper._extract_columns_by_pattern( | ||
pattern_text=r"RP(\d+)", gdf=self.gdf | ||
) | ||
if not rp_values: | ||
raise ValueError("No damage column with RP found") | ||
|
||
return sorted([float(rp) for rp in rp_values]) | ||
|
||
def shape_to_integrate_object( | ||
self, return_periods: list | ||
) -> dict[str : gpd.GeoDataFrame]: | ||
_to_integrate_dict = {} | ||
# finds vulnerability curves shown with Ci, where 1<= i <=6 | ||
vulnerability_curves = sorted( | ||
ManToIntegrateShaper._extract_columns_by_pattern( | ||
pattern_text=r"dam_RP\d+_(.*)", gdf=self.gdf | ||
) | ||
) | ||
for vulnerability_curve in vulnerability_curves: | ||
|
||
filtered_columns = sorted( | ||
ManToIntegrateShaper._extract_columns_by_pattern( | ||
pattern_text=rf"(dam_RP\d+.*{vulnerability_curve})", | ||
gdf=self.gdf, | ||
) | ||
) | ||
_to_integrate = self.gdf[filtered_columns] | ||
|
||
_to_integrate.columns = [ | ||
float(c.split("_")[1].replace("RP", "")) for c in _to_integrate.columns | ||
] | ||
_to_integrate_dict[f"{vulnerability_curve}"] = _to_integrate.sort_index( | ||
axis="columns", ascending=False | ||
) | ||
|
||
return _to_integrate_dict |
71 changes: 71 additions & 0 deletions
71
ra2ce/analysis/damages/shape_to_integrate_object/osd_to_integrate_shaper.py
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,71 @@ | ||
import geopandas as gpd | ||
import pandas as pd | ||
import re | ||
|
||
from ra2ce.analysis.damages.shape_to_integrate_object.to_Integrate_shaper_protocol import ( | ||
ToIntegrateShaperProtocol, | ||
) | ||
|
||
|
||
class OsdToIntegrateShaper(ToIntegrateShaperProtocol): | ||
gdf: gpd.GeoDataFrame | ||
|
||
def __init__(self, gdf): | ||
self.gdf = gdf | ||
|
||
@staticmethod | ||
def _extract_columns_by_pattern( | ||
pattern_text: str, gdf: gpd.GeoDataFrame | ||
) -> set[str]: | ||
""" | ||
Extract column names based on the provided regex pattern. | ||
Args: | ||
pattern_text (Pattern[str]): The compiled regex pattern to match the column names. | ||
df (pd.DataFrame): The DataFrame from which to extract the RP values. | ||
Returns: | ||
Set[str]: A set of RP values extracted from the column names. | ||
""" | ||
pattern = re.compile(pattern_text) | ||
columns = {pattern.search(c).group(1) for c in gdf.columns if pattern.search(c)} | ||
return columns | ||
|
||
def get_return_periods(self) -> list: | ||
# Extract the RP values from the columns | ||
rp_values = OsdToIntegrateShaper._extract_columns_by_pattern( | ||
pattern_text=r"RP(\d+)", gdf=self.gdf | ||
) | ||
if not rp_values: | ||
raise ValueError("No damage column with RP found") | ||
|
||
return sorted([float(rp) for rp in rp_values]) | ||
|
||
def shape_to_integrate_object( | ||
self, return_periods: list | ||
) -> dict[str : gpd.GeoDataFrame]: | ||
_to_integrate_dict = {} | ||
# finds vulnerability curves shown with Ci, where 1<= i <=6 | ||
vulnerability_curves = sorted( | ||
OsdToIntegrateShaper._extract_columns_by_pattern( | ||
pattern_text=r"(C\d+)", gdf=self.gdf | ||
) | ||
) | ||
for vulnerability_curve in vulnerability_curves: | ||
|
||
filtered_columns = sorted( | ||
OsdToIntegrateShaper._extract_columns_by_pattern( | ||
pattern_text=rf"(.*{vulnerability_curve}.*representative.*)", | ||
gdf=self.gdf, | ||
) | ||
) | ||
_to_integrate = self.gdf[filtered_columns] | ||
|
||
_to_integrate.columns = [ | ||
float(c.split("_")[2].replace("RP", "")) for c in _to_integrate.columns | ||
] | ||
_to_integrate_dict[f"{vulnerability_curve}"] = _to_integrate.sort_index( | ||
axis="columns", ascending=False | ||
) | ||
|
||
return _to_integrate_dict |
27 changes: 27 additions & 0 deletions
27
ra2ce/analysis/damages/shape_to_integrate_object/to_Integrate_shaper_protocol.py
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,27 @@ | ||
import geopandas as gpd | ||
from typing import Any, Protocol, runtime_checkable | ||
|
||
from ra2ce.analysis.analysis_config_data.enums.damage_curve_enum import DamageCurveEnum | ||
|
||
|
||
@runtime_checkable | ||
class ToIntegrateShaperProtocol(Protocol): | ||
gdf: gpd.GeoDataFrame | ||
|
||
def get_return_periods(self) -> list: | ||
""" | ||
Gets the columns' names that have damages calculated | ||
Returns: | ||
list: columns' name that have damages calculated. | ||
""" | ||
|
||
def shape_to_integrate_object( | ||
self, return_periods: list | ||
) -> dict[str : gpd.GeoDataFrame]: | ||
""" | ||
Gets the return periods and create columns for risk calculation for each damage curve and return period | ||
Returns: | ||
list[GeoDataFrame]: | ||
to_integrate objects each corresponding to each vulnerability curve to be used for risk calculation | ||
""" |
Oops, something went wrong.