ortk95 / planetmapper

PlanetMapper: An open source Python package for visualising, navigating and mapping Solar System observations
https://planetmapper.readthedocs.io
MIT License
10 stars 1 forks source link

Improve mapping interpolation with k>1 when NaN values are present #382

Closed ortk95 closed 1 month ago

ortk95 commented 2 months ago

Currently, we use RectBivariateSpline, so have to replace NaN pixels with some value before interpolating (currently just set to 0). Even though we later mask any projected NaN pixels (with _should_propagate_nan_to_map), this can produce edge effects near NaN pixels when using e.g. cubic splines.

It would make more sense therefore to either:

ortk95 commented 2 months ago

Could do locally derived value fixing with something along the lines of

def clean(img: np.ndarray) -> np.ndarray:
    nans = np.isnan(img)
    cleaned = img.copy()
    cleaned[nans] = np.nanmedian(img)
    to_fix = nans & ~scipy.ndimage.uniform_filter(nans, size=3)
    for i, j in np.argwhere(to_fix):
        cleaned[i, j] = np.nanmean(img[max(i - 1, 0) : i + 2, max(j - 1, 0) : j + 2])
    return cleaned
ortk95 commented 1 month ago

griddata clearly performs best at keeping original variation with no influence from NaNs, so think we should use that as the default method. Can keep RectBivariateSpline as an option for e.g. if smoothing is wanted.

image image image image image

ortk95 commented 1 month ago

From futher testing, griddata doesn't seem as great:

I think this probably therefore rules out griddata, as e.g. we don't want unstable mapping results to be interpreted as changes in the target etc.

Scipy's RegularGridInterpolator seems to perform really nicely (and quickly) when paired with our custom _replace_nans_with_interpolated_values, so this is probably the way to go.

image

image