Skip to content

Commit 34e2b79

Browse files
committed
Merge branch 'feature/risk_trajectory' into feature/cb_refactoring
2 parents 6cc217c + bc38525 commit 34e2b79

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

CHANGELOG.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ Removed:
2121

2222
- `ImpactFunc` and `ImpactFuncSet` now support equality comparisons via `==` [#1027](https://github.com/CLIMADA-project/climada_python/pull/1027)
2323

24-
- `climada.entity.impact_funcs.base.ImpactFunc.__eq__` method
25-
- `climada.entity.impact_funcs.impact_func_set.ImpactFuncSet.__eq__` method
26-
2724
### Changed
2825

2926
- `Hazard.local_exceedance_intensity`, `Hazard.local_return_period` and `Impact.local_exceedance_impact`, `Impact.local_return_period`, using the `climada.util.interpolation` module: New default (no binning), binning on decimals, and faster implementation [#1012](https://github.com/CLIMADA-project/climada_python/pull/1012)

climada/trajectories/risk_trajectory.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import itertools
2626
import logging
2727

28+
import matplotlib.dates as mdates
2829
import matplotlib.pyplot as plt
2930
import pandas as pd
3031

@@ -344,9 +345,10 @@ def eai_metrics(self, npv: bool = True, **kwargs):
344345
This computation may become quite expensive for big areas with high resolution.
345346
346347
"""
347-
return self._compute_metrics(
348+
df = self._compute_metrics(
348349
npv=npv, metric_name="eai", metric_meth="calc_eai_gdf", **kwargs
349350
)
351+
return df
350352

351353
def aai_metrics(self, npv: bool = True, **kwargs):
352354
"""Return the average annual impacts for each date.
@@ -567,6 +569,7 @@ def plot_per_date_waterfall(
567569
ax=None,
568570
start_date: datetime.date | None = None,
569571
end_date: datetime.date | None = None,
572+
figsize=(12, 6),
570573
):
571574
"""Plot a waterfall chart of risk components over a specified date range.
572575
@@ -596,7 +599,9 @@ def plot_per_date_waterfall(
596599
compared to the risk associated with future exposure and present hazard.
597600
"""
598601
if ax is None:
599-
_, ax = plt.subplots(figsize=(12, 6))
602+
fig, ax = plt.subplots(figsize=figsize)
603+
else:
604+
fig = ax.figure # get parent figure from the axis
600605
start_date = self.start_date if start_date is None else start_date
601606
end_date = self.end_date if end_date is None else end_date
602607
risk_component = self._calc_waterfall_plot_data(
@@ -611,16 +616,30 @@ def plot_per_date_waterfall(
611616
"interaction contribution",
612617
]
613618
]
614-
risk_component.plot(ax=ax, kind="bar", stacked=True)
619+
# risk_component.plot(x="date", ax=ax, kind="bar", stacked=True)
620+
ax.stackplot(
621+
risk_component.index,
622+
[risk_component[col] for col in risk_component.columns],
623+
labels=risk_component.columns,
624+
)
625+
ax.legend()
626+
# bottom = [0] * len(risk_component)
627+
# for col in risk_component.columns:
628+
# bottom = [b + v for b, v in zip(bottom, risk_component[col])]
615629
# Construct y-axis label and title based on parameters
616630
value_label = "USD"
617-
title_label = (
618-
f"Risk between {start_date} and {end_date} (Annual Average impact)"
619-
)
631+
title_label = f"Risk between {start_date} and {end_date} (Average impact)"
632+
633+
locator = mdates.AutoDateLocator()
634+
formatter = mdates.ConciseDateFormatter(locator)
635+
636+
ax.xaxis.set_major_locator(locator)
637+
ax.xaxis.set_major_formatter(formatter)
620638

621639
ax.set_title(title_label)
622640
ax.set_ylabel(value_label)
623-
return ax
641+
ax.set_ylim(0.0, 1.1 * ax.get_ylim()[1])
642+
return fig, ax
624643

625644
def plot_waterfall(
626645
self,
@@ -722,10 +741,11 @@ def plot_waterfall(
722741

723742
# Construct y-axis label and title based on parameters
724743
value_label = "USD"
725-
title_label = f"Risk at {start_date} and {end_date} (Annual Average impact)"
744+
title_label = f"Risk at {start_date} and {end_date} (Average impact)"
726745

727746
ax.set_title(title_label)
728747
ax.set_ylabel(value_label)
748+
ax.set_ylim(0.0, 1.1 * ax.get_ylim()[1])
729749
ax.tick_params(
730750
axis="x",
731751
labelrotation=90,

climada/trajectories/riskperiod.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -685,12 +685,10 @@ def calc_eai_gdf(self):
685685
df = df.reset_index().melt(
686686
id_vars="date", var_name="coord_id", value_name="risk"
687687
)
688-
eai_gdf = self.snapshot0.exposure.gdf
688+
eai_gdf = self.snapshot0.exposure.gdf[["group_id"]]
689689
eai_gdf["coord_id"] = eai_gdf.index
690690
eai_gdf = eai_gdf.merge(df, on="coord_id")
691-
eai_gdf = eai_gdf.rename(
692-
columns={"group_id": "group", "value": "exposure_value"}
693-
)
691+
eai_gdf = eai_gdf.rename(columns={"group_id": "group"})
694692
eai_gdf["metric"] = "eai"
695693
eai_gdf["measure"] = self.measure.name if self.measure else "no_measure"
696694
return eai_gdf

0 commit comments

Comments
 (0)