climada.trajectories.interpolation module#

class climada.trajectories.interpolation.AllLinearStrategy[source]#

Bases: ImpactInterpolationStrategy

Linear interpolation strategy over all dimensions.

__init__() None[source]#
class climada.trajectories.interpolation.ExponentialExposureStrategy[source]#

Bases: ImpactInterpolationStrategy

Exponential interpolation strategy for exposure and linear for Hazard and Vulnerability.

__init__() None[source]#
climada.trajectories.interpolation.linear_convex_combination(arr_start: ndarray, arr_end: ndarray) ndarray[source]#

Performs a linear convex combination between two n x m NumPy arrays over their first dimension (n rows).

This function interpolates each metric (column) linearly across the time steps (rows), including both the start and end states.

Parameters:
  • arr_start (numpy.ndarray) – The starting array of metrics. The first dimension (rows) is assumed to represent the interpolation steps (e.g., dates/time points).

  • arr_end (numpy.ndarray) – The ending array of metrics. Must have the exact same shape as arr_start.

Returns:

An array with the same shape as arr_start and arr_end. The values in the first dimension transition linearly from those in arr_start to those in arr_end.

Return type:

numpy.ndarray

Raises:

ValueError – If arr_start and arr_end do not have the same shape.

Example

>>> arr_start = [ [ 1, 1], [1, 2], [10, 20] ]
>>> arr_end = [ [2, 2], [5, 6], [10, 30] ]
>>> linear_interp_arrays(arr_start, arr_end)
>>> [[1, 1], [3, 4], [10, 30]]

Notes

The interpolation is performed element-wise along the first dimension (axis 0). For each row $i$ and proportion $p_i$, the result $R_i$ is calculated as:

$$R_i = arr_start_i cdot (1 - p_i) + arr_end_i cdot p_i$$

where $p_i$ is generated by $text{np.linspace}(0, 1, n)$ and $n$ is the size of the first dimension ($text{arr_start.shape}[0]$).

climada.trajectories.interpolation.linear_interp_matrix_elemwise(mat_start: csr_matrix, mat_end: csr_matrix, number_of_interpolation_points: int) List[csr_matrix][source]#

Linearly interpolates between two sparse impact matrices.

Creates a sequence of matrices representing a linear transition from a starting matrix to an ending matrix. The interpolation includes both the start and end points.

Parameters:
  • mat_start (scipy.sparse.csr_matrix) – The starting impact matrix. Must have a shape compatible with mat_end for arithmetic operations.

  • mat_end (scipy.sparse.csr_matrix) – The ending impact matrix. Must have a shape compatible with mat_start for arithmetic operations.

  • number_of_interpolation_points (int) – The total number of matrices to return, including the start and end points. Must be $ge 2$.

Returns:

A list of matrices, where the first element is mat_start and the last element is mat_end. The total length of the list is number_of_interpolation_points.

Return type:

list of scipy.sparse.csr_matrix

Notes

The formula used for interpolation at proportion $p$ is: $$M_p = M_{start} cdot (1 - p) + M_{end} cdot p$$ The proportions $p$ range from 0 to 1, inclusive.

climada.trajectories.interpolation.exponential_convex_combination(arr_start: ndarray, arr_end: ndarray) ndarray[source]#

Performs exponential convex combination between two NumPy arrays over their first dimension.

This function achieves an exponential-like transition by performing linear interpolation in the logarithmic space.

Parameters:
  • arr_start (numpy.ndarray) – The starting array of metrics. Values must be positive.

  • arr_end (numpy.ndarray) – The ending array of metrics. Must have the exact same shape as arr_start.

Returns:

An array with the same shape as arr_start and arr_end. The values in the first dimension transition exponentially from those in arr_start to those in arr_end.

Return type:

numpy.ndarray

Raises:

ValueError – If arr_start and arr_end do not have the same shape.

See also

linear_interp_arrays

linear version of the interpolation.

Notes

The interpolation is performed by transforming the arrays to a logarithmic domain, linearly interpolating, and then transforming back.

The formula for the interpolated result $R$ at proportion $text{prop}$ is: $$ R = exp left(

ln(A_{start}) cdot (1 - text{prop}) + ln(A_{end}) cdot text{prop}

right) $$ where $A_{start}$ and $A_{end}$ are the input arrays (with $epsilon$ added to prevent $ln(0)$) and $text{prop}$ ranges from 0 to 1.

climada.trajectories.interpolation.exponential_interp_matrix_elemwise(mat_start: csr_matrix, mat_end: csr_matrix, number_of_interpolation_points: int) List[csr_matrix][source]#

Exponentially interpolates between two “impact matrices”.

This function performs interpolation in a logarithmic space, effectively achieving an exponential-like transition between mat_start and mat_end. It is designed for objects that wrap NumPy arrays and expose them via a .data attribute.

Parameters:
  • mat_start (object) – The starting matrix object. Must have a .data attribute that is a NumPy array of positive values.

  • mat_end (object) – The ending matrix object. Must have a .data attribute that is a NumPy array of positive values and have a compatible shape with mat_start.

  • number_of_interpolation_points (int) – The total number of matrix objects to return, including the start and end points. Must be $ge 2$.

Returns:

A list of interpolated matrix objects. The first element corresponds to mat_start and the last to mat_end (after the conversion/reversion). The list length is number_of_interpolation_points.

Return type:

list of object

Notes

The interpolation is achieved by:

  1. Mapping the matrix data to a transformed logarithmic space: $$M’_{i} = ln(M_{i})}$$ (where $ln$ is the natural logarithm, and $epsilon$ is added to $M_{i}$ to prevent $ln(0)$).

  2. Performing standard linear interpolation on the transformed matrices $M’_{start}$ and $M’_{end}$ to get $M’_{interp}$: $$M’_{interp} = M’_{start} cdot (1 - text{ratio}) + M’_{end} cdot text{ratio}$$

  3. Mapping the result back to the original domain: $$M_{interp} = exp(M’_{interp}$$