Interpolation#

Summary#

Fast linear interpolation for 1D, 2D, and 3D data.

This module provides functions for efficient linear interpolation of 1D, 2D, and 3D data using NumPy and Numba. It includes both a high-level interface (interp()) and lower-level functions for specific dimensionalities.

Key Features:
  • Fast linear interpolation for 1D, 2D, and 3D data

  • Support for both grid-based and point-based interpolation

  • Numba-accelerated core interpolation functions

  • Input validation and error checking

  • Visualization tool for comparing original and interpolated data

Main Functions:
Dependencies:
  • NumPy

  • Numba

  • Matplotlib

  • SciPy (for comparison function)

Example

>>> import numpy as np
>>> from pymedphys.interpolate import interp
>>>
>>> # Define known data
>>> x = np.linspace(0, 10, 11)
>>> y = np.linspace(0, 10, 11)
>>> z = np.linspace(0, 10, 11)
>>> values = np.random.rand(11, 11, 11)
>>>
>>> # Define interpolation points
>>> x_interp = np.linspace(0, 10, 21)
>>> y_interp = np.linspace(0, 10, 21)
>>> z_interp = np.linspace(0, 10, 21)
>>>
>>> # Perform interpolation
>>> interpolated_values = interp(
...     [x, y, z], values, axes_interp=[x_interp, y_interp, z_interp]
... )

For more detailed information, refer to the individual function docstrings.

Performance Comparison Between Implementations#

../../../_images/interp_comparison.png

(Right-click and open in a new tab for better viewing)

API#

Interpolation - Low Level Interfaces#

It generally shouldn’t be necessary to use these in lieu of pymedphys.interpolate.interp(), since running interp() with skip_checks=True will produce similar speed performance. See Performance Comparison Between Implementations

pymedphys.interpolate.interp_linear_1d(axis_known, values, points_interp, extrap_fill_value=None)[source]#
pymedphys.interpolate.interp_linear_2d(axes_known, values, points_interp, extrap_fill_value=None)[source]#
pymedphys.interpolate.interp_linear_3d(axes_known, values, points_interp, extrap_fill_value=None)[source]#

Interpolation - Visualisation#

pymedphys.interpolate.plot_interp_comparison_heatmap(values, values_interp, slice_axis: int, slice_number: int, slice_number_interp: int)[source]#

Plot a comparison heatmap of original and interpolated 3D data slices.

This function creates a side-by-side heatmap comparison of a slice from the original data and a corresponding slice from the interpolated data.

Parameters:
  • values (array-like) – The original 3D data array.

  • values_interp (array-like) – The interpolated 3D data array.

  • slice_axis (int) – The axis along which to take the slice (0, 1, or 2).

  • slice_number (int) – The index of the slice to display from the original data.

  • slice_number_interp (int) – The index of the slice to display from the interpolated data. Note that auto matching between the original and interpolated slices is not implemented. The user must ensure the slices correspond.

Returns:

This function displays the plot directly and does not return any value.

Return type:

None

Notes

  • The function creates a figure with two subplots side by side.

  • The left subplot shows the slice from the original data.

  • The right subplot shows the slice from the interpolated data.

  • It is up to the user to ensure that

  • Both heatmaps use the same color scale, determined by the minimum and maximum values across both datasets.

  • A shared colorbar is displayed on the right side of the figure.

  • The plot is automatically displayed using plt.show().