scipy: BUG: interpolate.griddata: NaNs inside convex hull (despite no NaNs or duplicates in data)
Describe your issue.
Hi, thanks for taking the time to look at this bug report.
Short summary
scipy.interpolate.griddata is returning NaNs with method = 'linear' and method = 'cubic'. By construction, the data points used in the interpolation correspond to the convex hull of the data. Also, there are no duplicate data points.
Potentially related to issue #12945.
Detailed description
I am trying to correct for uneven illumination in a series of microscopy images. The region of interest in these images is always in the center, such that the border of the images only contains background illumination. This allows me to extract the pixel intensities on the border of the image:
I then compute smooth estimates of the pixel intensities for the left/right/top/bottom borders.
I can then interpolate between the fitted border pixel intensities using scipy.interpolate.griddata. However, the result contains a number of NaNs (run code below with fill_nans=False).
I can fill in these NaNs by simply running the interpolation again (run code below with fill_nans=True) , however as the images are large, this is a slow process that I would like to avoid.
Any insight why the output of the first interpolation contains NaN would be appreciated.
Reproducing Code Example
import numpy as np
import matplotlib.pyplot as plt
from skimage.io import imread
from scipy.optimize import curve_fit
from scipy.interpolate import griddata
def correct_uneven_illumination(image, border_width=50, show=False, fill_nans=True):
# Extract border pixel intensities
border_pixel_intensities = dict(
Left = image[:, :border_width].mean(axis=1),
Right = image[:, -border_width:].mean(axis=1),
Top = image[:border_width, :].mean(axis=0),
Bottom = image[-border_width:, :].mean(axis=0)
)
if show:
image_with_border = image.copy()
image_max = image.max()
image_with_border[:, border_width] = 2 * image_max
image_with_border[:, -border_width] = 2 * image_max
image_with_border[ border_width, :] = 2 * image_max
image_with_border[-border_width, :] = 2 * image_max
fig, ax = plt.subplots()
ax.imshow(np.log1p(image_with_border), cmap="gray")
# Get a smooth estimate of the variation in illumination along the borders of the image.
def linear(x, a, b):
return a * x + b
def exponential(x, a, b, c):
return a * np.exp(-b * x) + c
fitted_border_pixel_intensities = dict()
for side, intensities in border_pixel_intensities.items():
x = np.arange(len(intensities))
try: # exponential fit; curve fitting fails sometimes for unknown reasons
(a2, b2, c2), _ = curve_fit(exponential, x, intensities)
fitted_border_pixel_intensities[side] = exponential(x, a2, b2, c2)
except: # fall back to linear fit
(a1, b1), _ = curve_fit(linear, x, intensities)
fitted_border_pixel_intensities[side] = linear(x, a1, b1)
if show:
fig, axes = plt.subplots(2, 2, sharey=True)
axes = axes.ravel()
for side, ax in zip(border_pixel_intensities.keys(), axes):
ax.plot(border_pixel_intensities[side])
ax.plot(fitted_border_pixel_intensities[side], ls="--")
ax.set_title(side)
ax.set_xlabel("Row/Column Index")
ax.set_ylabel("Pixel intensity")
fig.tight_layout()
# Interpolate across the image to compute a correction at every pixel.
fitted_frame = np.zeros_like(image)
fitted_frame[:, 0] = fitted_border_pixel_intensities["Left"]
fitted_frame[:, -1] = fitted_border_pixel_intensities["Right"]
fitted_frame[ 0, :] = fitted_border_pixel_intensities["Top"]
fitted_frame[-1, :] = fitted_border_pixel_intensities["Bottom"]
rows, columns = np.where(fitted_frame)
image_correction = griddata(
points = np.c_[rows, columns],
values = fitted_frame[rows, columns],
xi = np.transpose(np.where(np.ones_like(image))),
method="linear",
)
image_correction = image_correction.reshape(image.shape)
# Fill in NaNs if there are any in the interpolated image.
if np.any(np.isnan(image_correction)) & fill_nans:
rows, columns = np.where(~np.isnan(image_correction))
image_correction = griddata(
points = np.c_[rows, columns],
values = image_correction[rows, columns],
xi = np.transpose(np.where(np.ones_like(image))),
method="nearest",
)
image_correction = image_correction.reshape(image.shape)
# Apply correction
corrected_image = image - image_correction
if show:
fig, axes = plt.subplots(1, 3, sharex=True, sharey=True, figsize=(15,5))
for img, label, ax in zip(
[np.log1p(image), image_correction, np.log1p(corrected_image)],
["Original", "Correction", "Corrected"],
axes):
ax.imshow(img, cmap="gray")
ax.axis("off")
ax.set_title(label)
return corrected_image
if __name__ == "__main__":
image = imread("/path/to/test/image.tif")
corrected = correct_uneven_illumination(image, 50, show=True, fill_nans=True)
plt.show()
Edit: Made small changes to the code to improve its readability and to improve the output figures.
Error message
None
SciPy/NumPy/Python version and system information
1.13.0 1.26.4 sys.version_info(major=3, minor=11, micro=9, releaselevel='final', serial=0)
/home/paul/opt/miniconda3/envs/scientific_stack/lib/python3.11/site-packages/scipy/__config__.py:154: UserWarning: Install `pyyaml` for better output
warnings.warn("Install `pyyaml` for better output", stacklevel=1)
{
"Compilers": {
"c": {
"name": "gcc",
"linker": "ld.bfd",
"version": "11.2.0",
"commands": "/croot/scipy_1714069776893/_build_env/bin/x86_64-conda-linux-gnu-cc",
"args": "-march=nocona, -mtune=haswell, -ftree-vectorize, -fPIC, -fstack-protector-strong, -fno-plt, -O2, -ffunction-sections, -pipe, -isystem, /home/paul/opt/miniconda3/envs/scientific_stack/include, -fdebug-prefix-map=/croot/scipy_1714069776893/work=/usr/local/src/conda/scipy-1.13.0, -fdebug-prefix-map=/home/paul/opt/miniconda3/envs/scientific_stack=/usr/local/src/conda-prefix, -DNDEBUG, -D_FORTIFY_SOURCE=2, -O2, -isystem, /home/paul/opt/miniconda3/envs/scientific_stack/include",
"linker args": "-Wl,-O2, -Wl,--sort-common, -Wl,--as-needed, -Wl,-z,relro, -Wl,-z,now, -Wl,--disable-new-dtags, -Wl,--gc-sections, -Wl,-rpath,/home/paul/opt/miniconda3/envs/scientific_stack/lib, -Wl,-rpath-link,/home/paul/opt/miniconda3/envs/scientific_stack/lib, -L/home/paul/opt/miniconda3/envs/scientific_stack/lib, -march=nocona, -mtune=haswell, -ftree-vectorize, -fPIC, -fstack-protector-strong, -fno-plt, -O2, -ffunction-sections, -pipe, -isystem, /home/paul/opt/miniconda3/envs/scientific_stack/include, -fdebug-prefix-map=/croot/scipy_1714069776893/work=/usr/local/src/conda/scipy-1.13.0, -fdebug-prefix-map=/home/paul/opt/miniconda3/envs/scientific_stack=/usr/local/src/conda-prefix, -DNDEBUG, -D_FORTIFY_SOURCE=2, -O2, -isystem, /home/paul/opt/miniconda3/envs/scientific_stack/include"
},
"cython": {
"name": "cython",
"linker": "cython",
"version": "3.0.8",
"commands": "cython"
},
"c++": {
"name": "gcc",
"linker": "ld.bfd",
"version": "11.2.0",
"commands": "/croot/scipy_1714069776893/_build_env/bin/x86_64-conda-linux-gnu-c++",
"args": "-fvisibility-inlines-hidden, -std=c++17, -fmessage-length=0, -march=nocona, -mtune=haswell, -ftree-vectorize, -fPIC, -fstack-protector-strong, -fno-plt, -O2, -ffunction-sections, -pipe, -isystem, /home/paul/opt/miniconda3/envs/scientific_stack/include, -fdebug-prefix-map=/croot/scipy_1714069776893/work=/usr/local/src/conda/scipy-1.13.0, -fdebug-prefix-map=/home/paul/opt/miniconda3/envs/scientific_stack=/usr/local/src/conda-prefix, -DNDEBUG, -D_FORTIFY_SOURCE=2, -O2, -isystem, /home/paul/opt/miniconda3/envs/scientific_stack/include",
"linker": "ld.bfd",
"version": "11.2.0",
"commands": "/croot/scipy_1714069776893/_build_env/bin/x86_64-conda-linux-gnu-gfortran",
"args": "-fopenmp, -march=nocona, -mtune=haswell, -ftree-vectorize, -fPIC, -fstack-protector-strong, -fno-plt, -O2, -ffunction-sections, -pipe, -isystem, /home/paul/opt/miniconda3/envs/scientific_stack/include, -fdebug-prefix-map=/croot/scipy_1714069776893/work=/usr/local/src/conda/scipy-1.13.0, -fdebug-prefix-map=/home/paul/opt/miniconda3/envs/scientific_stack=/usr/local/src/conda-prefix",
"linker args": "-Wl,-O2, -Wl,--sort-common, -Wl,--as-needed, -Wl,-z,relro, -Wl,-z,now, -Wl,--disable-new-dtags, -Wl,--gc-sections, -Wl,-rpath,/home/paul/opt/miniconda3/envs/scientific_stack/lib, -Wl,-rpath-link,/home/paul/opt/miniconda3/envs/scientific_stack/lib, -L/home/paul/opt/miniconda3/envs/scientific_stack/lib, -fopenmp, -march=nocona, -mtune=haswell, -ftree-vectorize, -fPIC, -fstack-protector-strong, -fno-plt, -O2, -ffunction-sections, -pipe, -isystem, /home/paul/opt/miniconda3/envs/scientific_stack/include, -fdebug-prefix-map=/croot/scipy_1714069776893/work=/usr/local/src/conda/scipy-1.13.0, -fdebug-prefix-map=/home/paul/opt/miniconda3/envs/scientific_stack=/usr/local/src/conda-prefix"
},
"pythran": {
"version": "0.15.0",
"include directory": "../../_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_p/lib/python3.11/site-packages/pythran"
}
},
"Machine Information": {
"host": {
"cpu": "x86_64",
"family": "x86_64",
"endian": "little",
"system": "linux"
},
"build": {
"cpu": "x86_64",
"family": "x86_64",
"endian": "little",
"system": "linux"
},
"cross-compiled": false
},
"Build Dependencies": {
"blas": {
"name": "mkl-sdl",
"found": true,
"version": "2023.1",
"detection method": "pkgconfig",
"include directory": "/home/paul/opt/miniconda3/envs/scientific_stack/include",
"lib directory": "/home/paul/opt/miniconda3/envs/scientific_stack/lib",
"openblas configuration": "unknown",
"pc file directory": "/home/paul/opt/miniconda3/envs/scientific_stack/lib/pkgconfig"
},
"lapack": {
"name": "mkl-sdl",
"found": true,
"version": "2023.1",
"detection method": "pkgconfig",
"include directory": "/home/paul/opt/miniconda3/envs/scientific_stack/include",
"lib directory": "/home/paul/opt/miniconda3/envs/scientific_stack/lib",
"openblas configuration": "unknown",
"pc file directory": "/home/paul/opt/miniconda3/envs/scientific_stack/lib/pkgconfig"
},
"pybind11": {
"name": "pybind11",
"version": "2.12.0",
"detection method": "pkgconfig",
"include directory": "/home/paul/opt/miniconda3/envs/scientific_stack/include"
}
},
"Python Information": {
"path": "/home/paul/opt/miniconda3/envs/scientific_stack/bin/python",
"version": "3.11"
}
}
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 19 (12 by maintainers)
Commits related to this issue
- DOC: Mention limitations of LinearNDInterpolator This documents the issues uncovered in #21910. — committed to DWesl/scipy by DWesl a year ago
There are established methods to correct for uneven illumination, and some of them are implemented in scikit image and OpenCV. However, while these methods probably result in more visually pleasing images, they introduce artifacts into the data themselves. In my case, the images are really scientific measurements, and hence these artifacts are not acceptable. Instead I use the approach outlined above, leveraging the fact that my images have a border that can be assumed to only contain background. This assumption obviously does not hold for images in general, for which the methods in scikit-image and OpenCV have been developed.
@ev-br
The test image above is 1250 x 1500 pixels < 2^21 pixels, so presumably several orders of magnitude too small for this issue to be related to issue #13037.