@@ -50,6 +50,9 @@ def exponential_interp_imp_mat(mat_start, mat_end, interpolation_range, rate) ->
5050 `mat_start` and `mat_end`.
5151 """
5252 # Convert matrices to logarithmic domain
53+ if rate <= 0 :
54+ raise ValueError ("Rate for exponential interpolation must be positive" )
55+
5356 mat_start = mat_start .copy ()
5457 mat_end = mat_end .copy ()
5558 mat_start .data = np .log (mat_start .data + np .finfo (float ).eps ) / np .log (rate )
@@ -65,8 +68,16 @@ def exponential_interp_imp_mat(mat_start, mat_end, interpolation_range, rate) ->
6568 return res
6669
6770
68- def linear_interp_arrays (arr_start , arr_end , interpolation_range ):
69- """Perform linear interpolation between two arrays (of a scalar metric) over an interpolation range."""
71+ def linear_interp_arrays (arr_start , arr_end ):
72+ """Perform linear interpolation between two arrays of `n` dates of one or multiple scalar metrics.
73+
74+ Returns a `n` sized arrays where the values linearly change from `arr_start` to `arr_end` over the `n` dates.
75+ """
76+ if arr_start .shape != arr_end .shape :
77+ raise ValueError (
78+ f"Cannot interpolate arrays of different shapes: { arr_start .shape } and { arr_end .shape } ."
79+ )
80+ interpolation_range = arr_start .shape [0 ]
7081 prop1 = np .linspace (0 , 1 , interpolation_range )
7182 prop0 = 1 - prop1
7283 if arr_start .ndim > 1 :
@@ -75,32 +86,35 @@ def linear_interp_arrays(arr_start, arr_end, interpolation_range):
7586 return np .multiply (arr_start , prop0 ) + np .multiply (arr_end , prop1 )
7687
7788
78- def exponential_interp_arrays (arr_start , arr_end , interpolation_range , rate ):
79- """Perform exponential interpolation between two arrays (of a scalar metric) over an interpolation range with a growth rate `rate`."""
89+ def exponential_interp_arrays (arr_start , arr_end , rate ):
90+ """Perform exponential interpolation between two arrays of `n` dates of one or multiple scalar metrics.
91+
92+ Returns a `n` sized arrays where the values exponentially change from `arr_start` to `arr_end` over the `n` dates.
93+ """
94+
95+ if rate <= 0 :
96+ raise ValueError ("Rate for exponential interpolation must be positive" )
97+
98+ if arr_start .shape != arr_end .shape :
99+ raise ValueError (
100+ f"Cannot interpolate arrays of different shapes: { arr_start .shape } and { arr_end .shape } ."
101+ )
102+ interpolation_range = arr_start .shape [0 ]
103+
80104 prop1 = np .linspace (0 , 1 , interpolation_range )
81105 prop0 = 1 - prop1
82106 if arr_start .ndim > 1 :
83107 prop0 , prop1 = prop0 .reshape (- 1 , 1 ), prop1 .reshape (- 1 , 1 )
84108
85109 return np .exp (
86110 (
87- np .multiply (np .log (arr_start ) / np .log (rate ), prop0 )
88- + np .multiply (np .log (arr_end ) / np .log (rate ), prop1 )
111+ np .multiply (np .log (arr_start + np . finfo ( float ). eps ) / np .log (rate ), prop0 )
112+ + np .multiply (np .log (arr_end + np . finfo ( float ). eps ) / np .log (rate ), prop1 )
89113 )
90114 * np .log (rate )
91115 )
92116
93117
94- def logarithmic_interp_arrays (arr_start , arr_end , interpolation_range ):
95- """Perform logarithmic (natural logarithm) interpolation between two arrays (of a scalar metric) over an interpolation range."""
96- prop1 = np .logspace (0 , 1 , interpolation_range )
97- prop0 = 1 - prop1
98- if arr_start .ndim > 1 :
99- prop0 , prop1 = prop0 .reshape (- 1 , 1 ), prop1 .reshape (- 1 , 1 )
100-
101- return np .multiply (arr_start , prop0 ) + np .multiply (arr_end , prop1 )
102-
103-
104118class InterpolationStrategyBase (ABC ):
105119 exposure_interp : Callable
106120 hazard_interp : Callable
@@ -126,17 +140,11 @@ def interp_exposure_dim(
126140
127141 return res
128142
129- def interp_hazard_dim (
130- self , metric_0 , metric_1 , interpolation_range : int , ** kwargs
131- ) -> np .ndarray :
132- return self .hazard_interp (metric_0 , metric_1 , interpolation_range , ** kwargs )
143+ def interp_hazard_dim (self , metric_0 , metric_1 , ** kwargs ) -> np .ndarray :
144+ return self .hazard_interp (metric_0 , metric_1 , ** kwargs )
133145
134- def interp_vulnerability_dim (
135- self , metric_0 , metric_1 , interpolation_range : int , ** kwargs
136- ) -> np .ndarray :
137- return self .vulnerability_interp (
138- metric_0 , metric_1 , interpolation_range , ** kwargs
139- )
146+ def interp_vulnerability_dim (self , metric_0 , metric_1 , ** kwargs ) -> np .ndarray :
147+ return self .vulnerability_interp (metric_0 , metric_1 , ** kwargs )
140148
141149
142150class InterpolationStrategy (InterpolationStrategyBase ):
0 commit comments