solarwindpy.plotting.tools.nan_gaussian_filter

nan_gaussian_filter(array, sigma, **kwargs)[source]

Apply Gaussian filter with proper NaN handling via normalized convolution.

Unlike scipy.ndimage.gaussian_filter() which propagates NaN values to all neighboring cells, this function:

  1. Smooths valid data correctly near NaN regions

  2. Preserves NaN locations (no interpolation into NaN cells)

The algorithm uses normalized convolution: both the data (with NaN replaced by 0) and a weight mask (1 for valid, 0 for NaN) are filtered. The result is the ratio of filtered data to filtered weights, ensuring proper normalization near boundaries.

Parameters:
  • array (np.ndarray) – 2D array possibly containing NaN values.

  • sigma (float) – Standard deviation for the Gaussian kernel, in pixels.

  • **kwargs – Additional keyword arguments passed to scipy.ndimage.gaussian_filter().

Returns:

Filtered array with original NaN locations preserved.

Return type:

np.ndarray

See also

scipy.ndimage.gaussian_filter

Underlying filter implementation.

Notes

This implementation follows the normalized convolution approach described in [1]. The key insight is that filtering a weight mask alongside the data allows proper normalization at boundaries and near missing values.

References

[1]

Knutsson, H., & Westin, C. F. (1993). Normalized and differential convolution. In Proceedings of IEEE Conference on Computer Vision and Pattern Recognition (pp. 515-523).

Examples

>>> import numpy as np
>>> arr = np.array([[1, 2, np.nan], [4, 5, 6], [7, 8, 9]])
>>> result = nan_gaussian_filter(arr, sigma=1.0)
>>> bool(np.isnan(result[0, 2]))  # NaN preserved
True
>>> bool(np.isfinite(result[0, 1]))  # Neighbor is valid
True