Skip to content

Commit

Permalink
inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Schmid Timo committed Oct 17, 2023
1 parent c40b85a commit 05c3fe4
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 70 deletions.
75 changes: 75 additions & 0 deletions climada/util/calibrate/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pandas as pd
import numpy as np
from scipy.optimize import Bounds, LinearConstraint, NonlinearConstraint
import matplotlib.pyplot as plt
import seaborn as sns

Check warning on line 12 in climada/util/calibrate/base.py

View check run for this annotation

Jenkins - WCR / Pylint

import-error

HIGH: Unable to import 'seaborn'
Raw output
Used when pylint has been unable to import a module.

from climada.hazard import Hazard
Expand Down Expand Up @@ -157,6 +158,80 @@ def plot_impf_set(self, **plot_kwargs):
:py:meth:`~climada.entity.impact_funcs.impact_func_set.ImpactFuncSet.plot`
"""
return self.impf_set.plot(**plot_kwargs)

Check warning on line 161 in climada/util/calibrate/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
def plot_impf_variability(

Check warning on line 162 in climada/util/calibrate/base.py

View check run for this annotation

Jenkins - WCR / Pylint

too-many-locals

LOW: Too many local variables (19/15)
Raw output
Used when a function or method has too many local variables.
self,

Check warning on line 163 in climada/util/calibrate/base.py

View check run for this annotation

Jenkins - WCR / Pylint

unused-argument

NORMAL: Unused argument 'plot_kwargs'
Raw output
Used when a function or method argument is not used.
cost_func_diff: float = 0.1,
p_space_df: Optional[pd.DataFrame] = None,
plot_haz: bool = False,
haz_vals: Optional[np.array] = None,
**plot_kwargs
):
"""Plot impact function variability with parameter combinations of almost equal

Check warning on line 170 in climada/util/calibrate/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
cost function values
Args:
cost_func_diff (float, optional): Max deviation from optimal cost function value

Check warning on line 174 in climada/util/calibrate/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
(as fraction). Defaults to 0.1 (i.e. 10%).
p_space_df (pd.DataFrame, optional): parameter space. Defaults to None.
plot_haz (bool, optional): Whether or not to plot hazard intensity distibution.

Check warning on line 177 in climada/util/calibrate/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
Defaults to False.
haz_vals (np.array, optional): Hazard values at exposure points (if

Check warning on line 179 in climada/util/calibrate/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
pre-calculated). Defaults to None.
"""
haz_type = self.input.hazard.haz_type
if p_space_df is None:
p_space_df = self.output.p_space_to_dataframe()

# Ignore cost dimension
params = p_space_df.columns.tolist()
try:
params.remove('Cost Function') #Hardcoded name i.o.?
except ValueError:
pass

#determine cost function values within 'cost_func_diff' % of best estimate
max_cost_func_val = p_space_df['Cost Function'].min()*(1+cost_func_diff)
params_within_range = p_space_df.loc[p_space_df['Cost Function']<=max_cost_func_val,
params]

# Initialize figure
fig,ax = plt.subplots()

Check warning on line 199 in climada/util/calibrate/base.py

View check run for this annotation

Jenkins - WCR / Pylint

invalid-name

LOW: Variable name "ax" doesn't conform to '(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern
Raw output
Used when the name doesn't match the regular expression associated to its type(constant, variable, class...).

Check warning on line 199 in climada/util/calibrate/base.py

View check run for this annotation

Jenkins - WCR / Pylint

unused-variable

NORMAL: Unused variable 'fig'
Raw output
Used when a variable is defined but not used.

#Plot best-fit impact function
best_impf = self.impf_set.get_func(haz_type=haz_type)[0]
ax.plot(best_impf.intensity,best_impf.mdd*best_impf.paa*100,color='tab:blue',
lw=3,zorder=2,label='best fit')

#Plot all impact functions within 'cost_func_diff' % of best estimate
for row in range(params_within_range.shape[0]):
label = f'within {int(cost_func_diff*100)} percent of best fit' if row==0 else None

sel_params = params_within_range.iloc[row,:].to_dict()
temp_impf_set = self.input.impact_func_creator(**sel_params)
temp_impf = temp_impf_set.get_func(haz_type=haz_type)[0]

ax.plot(temp_impf.intensity,temp_impf.mdd*temp_impf.paa*100,color='grey',
alpha=0.4,label=label)

Check warning on line 216 in climada/util/calibrate/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
# Plot hazard intensity value distributions
if plot_haz:
if haz_vals is None:
haz_vals = self.input.hazard.intensity[:,self.input.exposure.gdf[f"centr_{haz_type}"]]

Check warning on line 220 in climada/util/calibrate/base.py

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (102/100)
Raw output
Used when a line is longer than a given number of characters.

ax2 = ax.twinx()
ax2.hist(haz_vals[haz_vals.nonzero()].getA1(),bins=40,color='tab:orange',
alpha=0.3,label='Hazard intensity\noccurence')
ax2.set(ylabel='Hazard intensity occurence (#Exposure points)')
ax.axvline(x=haz_vals.max(),label='Maximum hazard value',color='tab:orange')
ax2.legend(loc='lower right')

ax.set(xlabel=f"Intensity ({self.input.hazard.units})",
ylabel="Mean Damage Ratio (MDR) in %",
xlim=(min(best_impf.intensity),max(best_impf.intensity)))
ax.legend()
return ax


def plot_at_event(
self,
Expand Down
Loading

0 comments on commit 05c3fe4

Please sign in to comment.