pydata / pydata/xarray

odd multidimensional extrapolation behaviour with interp

Open
#9,261 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-interpolation
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened?

When using interp on a 3-dimensional DataArray to extract values on a 2-dimensional surface, there is no extrapolation along one of the dimensions of the 2D plane (but the other dimension works). In the MWE, I have created a 5x5x5 cube centred on the origin where x, y, and z run from -2 to 2, then interpolated along a 6x6 plane that is perpendicular to the y-axis, where the plane extends beyond the faces of the cube by 0.5 units. The values for which x are outside the cube extrapolate correctly, however the values for which z are outside the cube become nan.

What did you expect to happen?

Extrapolation should happen along both dimensions of the plane.

Minimal Complete Verifiable Example
>>>import numpy as np
>>>import xarray as xr
>>>x = np.linspace(-2,2,5)
>>>da = xr.DataArray(np.random.rand(5,5,5), coords={"x": x, "y": x, "z": x})
>>>xi = xr.DataArray(np.linspace(-2.5,2.5,6), dims="theta")
>>>yi = xr.DataArray(0.5*np.ones(6), dims="theta")
>>>zi = np.linspace(-2.5,2.5,6)
>>>da.interp(x=xi, y=yi, z=zi, kwargs={"fill_value": None})
<xarray.DataArray (theta: 6, z: 6)> Size: 288B
array([[       nan, 0.65509617, 0.65540961, 0.94953277, 0.96035659,
               nan],
       [       nan, 0.50551769, 0.50022194, 0.55698788, 0.47609523,
               nan],
       [       nan, 0.53349845, 0.55660868, 0.45080263, 0.36583418,
               nan],
       [       nan, 0.6058017 , 0.62612734, 0.49364122, 0.52795018,
               nan],
       [       nan, 0.67642216, 0.63931782, 0.56366723, 0.52998289,
               nan],
       [       nan, 0.87859657, 0.79462258, 0.79821649, 0.47355559,
               nan]])
Coordinates:
    x        (theta) float64 48B -2.5 -1.5 -0.5 0.5 1.5 2.5
    y        (theta) float64 48B 0.5 0.5 0.5 0.5 0.5 0.5
  * z        (z) float64 48B -2.5 -1.5 -0.5 0.5 1.5 2.5
Dimensions without coordinates: theta
MVCE confirmation
  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output

No response

Anything else we need to know?

No response

Environment

/opt/homebrew/Caskroom/miniforge/base/envs/geopython/lib/python3.11/site-packages/_distutils_hack/init.py:26: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")

INSTALLED VERSIONS

commit: None
python: 3.11.8 | packaged by conda-forge | (main, Feb 16 2024, 20:49:36) [Clang 16.0.6 ]
python-bits: 64
OS: Darwin
OS-release: 23.4.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: en_AU.UTF-8
LOCALE: ('en_AU', 'UTF-8')
libhdf5: 1.14.3
libnetcdf: 4.9.2

xarray: 2024.5.0
pandas: 2.2.2
numpy: 1.26.4
scipy: 1.13.1
netCDF4: 1.6.5
pydap: None
h5netcdf: None
h5py: 3.11.0
zarr: None
cftime: 1.6.4
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.8.4
cartopy: 0.23.0
seaborn: 0.13.2
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 70.0.0
pip: 24.0
conda: None
pytest: None
mypy: None
IPython: 8.25.0
sphinx: None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files or tests are named in the issue. Reproduce the supplied MWE with the reported xarray, NumPy, and SciPy versions, then locate the interpolation implementation and relevant tests; done means extrapolation works for both out-of-range plane dimensions without changing in-range results.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.