Skip to content

Commit

Permalink
Merge pull request #236 from Deltares/212-test-the-direct-analysis-wo…
Browse files Browse the repository at this point in the history
…rkflow

212 test the direct analysis workflow
  • Loading branch information
Carsopre authored Nov 10, 2023
2 parents 14ca4a5 + 7dbdf10 commit 1d0a23e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
6 changes: 3 additions & 3 deletions ra2ce/analyses/direct/analyses_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

class DirectAnalyses: ### THIS SHOULD ONLY DO COORDINATION
"""
Coordination classs for all direct damage analysis
Coordination class for all direct damage analysis
Methods of this class are independent modules to do:
- direct damage analysis
Expand Down Expand Up @@ -91,7 +91,7 @@ def execute(self):

def road_damage(self, analysis: dict) -> gpd.GeoDataFrame:
"""
### CONTROLER FOR CALCULATING THE ROAD DAMAGE
### CONTROLLER FOR CALCULATING THE ROAD DAMAGE
Arguments:
*analysis* (dict) : contains part of the settings from the analysis ini
Expand Down Expand Up @@ -247,7 +247,7 @@ def rename_road_gdf_to_conventions(road_gdf_columns):
### Handle return period columns
new_cols = []
for c in cs:
if c.startswith("RP"):
if c.startswith("RP") or c.startswith("EV"):
new_cols.append("F_" + c)
else:
new_cols.append(c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import pandas as pd

from ra2ce.analyses.direct.direct_lookup import LookUp as lookup
from ra2ce.analyses.direct.direct_lookup import dataframe_lookup
from ra2ce.analyses.direct.direct_utils import (
clean_lane_data,
create_summary_statistics,
Expand Down Expand Up @@ -229,7 +230,7 @@ def calculate_damage_HZ(self, events):

df = self._gdf_mask
df["lanes"] = df["lanes"].astype(int)
df["max_dam_hz"] = df_max_damages_huizinga.lookup(df["lanes"], df["road_type"])
df["max_dam_hz"] = df.apply(dataframe_lookup, args=(df_max_damages_huizinga, ['lanes', 'road_type']), axis=1)

for event in events:
df["dam_{}_{}".format(event, curve_name)] = round(
Expand All @@ -249,7 +250,7 @@ def calculate_damage_HZ(self, events):

self.gdf[dam_cols] = df[dam_cols]
logging.info(
"calculate_damage_HZ(): Damage calculation with the Huizinga damage functions was succesfull"
"calculate_damage_HZ(): Damage calculation with the Huizinga damage functions was successful"
)

def calculate_damage_OSdaMage(self, events):
Expand Down
6 changes: 6 additions & 0 deletions ra2ce/analyses/direct/direct_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@
import os
from collections import OrderedDict
from pathlib import Path
from typing import Any

import numpy as np
import pandas as pd
from pandas import DataFrame
from scipy.interpolate import interp1d


def dataframe_lookup(row: pd.Series, lookup_df: DataFrame, columns: list) -> Any:
row_values = [row[column] for column in columns]
return lookup_df.loc[tuple(row_values)]

class LookUp:
""" " This namespace contains several lookup tables, used e.g. for road damage calculation."""

Expand Down
11 changes: 8 additions & 3 deletions ra2ce/graph/networks_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from tqdm import tqdm
from geopy import distance
from networkx import Graph, set_edge_attributes
from numpy.ma import MaskedArray
from osgeo import gdal
from osmnx import graph_to_gdfs, simplify_graph
from rasterio.features import shapes
Expand Down Expand Up @@ -1831,10 +1832,14 @@ def clean_memory(list_delete: list) -> None:
del to_delete


def get_valid_mean(x_value: float) -> Optional[float]:
if not isinstance(x_value, float):
def get_valid_mean(x_value: MaskedArray, **kwargs) -> Optional[float]:
# **kwargs should not be removed. properties var is passed in zonal_stats. So properties should not be removed,
# else this will not be activated
if not isinstance(x_value, MaskedArray):
return np.nan
return x_value.mean() # You know it's a valid type, so return the mean.
if x_value.mask.all():
return np.nan
return np.mean(x_value) # You know it's a valid type, so return the mean.


def buffer_geometry(
Expand Down

0 comments on commit 1d0a23e

Please sign in to comment.