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 for images containing NaN values #383

Closed ortk95 closed 1 month ago

ortk95 commented 1 month ago

Improved mapping with BodyXY.map_img when using spline interpolation for images containing NaN pixels. The spline interpolator does not accept NaN values, so any NaN pixels must be replaced with finite values before performing the interpolation.

Although we propagate NaN values to the map (by default at least), the replacement values can still affect the interpolated values elsewhere if the replacement values are very different to the surrounding values. Previously, we just used np.nan_to_num to replaced any NaNs with 0, which could introduce large artefacts when the data values were large, due to the jump between 0 and the data (see below).

In the updated code, we now replace NaN (or infinite) values with the mean of surrounding non-NaN pixels (with a 3x3 footprint). All other NaN pixels (which don't have neighbouring non-NaN pixels) are replaced with the median of all the pixels in the original image. This generally minimises the delta between replaced NaN pixels, and the surrounding pixels, massively reducing (or even preventing) any artefacts around the NaN regions.

Other minor mapping changes:

Closes #382

Example

This is a very extreme example of the artefacts that could be introduced around NaN regions with the old behaviour with cubic interpolation. The circles show the locations and values for the image pixels. The old behaviour had artefacts introduced along the top edge of the mapped area and the NaN pixel on the left, caused by the large jump from the filled-in 0 values and the data values. The new behaviour prevents any large jumps between the pixels, preventing any artefacts.

image image

Pull request checklist

See CONTRIBUTING.md for more details.

coveralls commented 1 month ago

Pull Request Test Coverage Report for Build 9991348244

Details


Totals Coverage Status
Change from base Build 9714025732: 0.003%
Covered Lines: 3135
Relevant Lines: 3147

đź’› - Coveralls