-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Linear interpolation gives negative output values with non-negative inputs #9404
Comments
Thanks for opening your first issue here at xarray! Be sure to follow the issue template! |
I will work on this in conjunction with #7704 |
@jameswilburlewis thanks for suggesting the workaround. Passing import xarray as xr
import numpy as np
time_strings_input = np.array(['2018-07-01T13:02:16.892474880',
'2018-07-01T13:02:16.922475008',
'2018-07-01T13:02:16.952474880'])
values_input = np.array([0.028584518, 0., 0.013626526],dtype=np.float32)
input_times_npdt64 = np.array([np.datetime64(t) for t in time_strings_input])
interp_to_times_npdt64 = np.array(input_times_npdt64[1])
input_times_float64 = input_times_npdt64.astype(np.float64)
interp_to_time_float64 = interp_to_times_npdt64.astype(np.float64)
data_array = xr.DataArray(values_input,dims=['time'],coords={'time':('time',input_times_float64)})
bad_result = data_array.interp({"time": interp_to_time_float64},method='linear')
good_result = data_array.interp({"time": interp_to_time_float64},method='slinear')
print(bad_result.values) # -3.469446951953614e-18
print(good_result.values) # 0.0 Another observation is that your example works as intended if you drop upgrade I'm inclined to leave the behavior of As far as I can tell the question of deprecating |
Amazing.
👍 An addition to the docstring and the narrative docs would be massively helpful |
@hollymandel : The example code was extracted from a somewhat messier use case, where the data type involved was indeed np.float32. I did notice that the problem didn't seem to occur with 64-bit inputs, but doing that promotion would have incurred an undesirable time/memory penalty for our use case. But thanks very much for pointing out that I can specify 'slinear' interpolation in the xarray.interp() call to get the behavior I'm looking for....somehow I missed that option when I was looking at the xarray and scipy docs. I think that suggestion resolves this issue, so I'll go ahead and close it. Thanks again for your help! |
What happened?
I have some time-series data that contains non-negative values (with a few zeroes). When I call the interp() method with method='linear', and pass a time corresponding exactly to one of the zero values, the result is a (tiny) negative value. This causes problems later on when I pass the interpolated data to another routine that requires non-negative inputs.
What did you expect to happen?
I expected the interpolated data to contain zeroes rather than negative values where the input data had zeroes.
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
None
Anything else we need to know?
This is actually an issue with scipy.interpolate.interp1d. See: scipy/scipy#21459
A comment on that issue suggests using make_interp_spline() with k=1, which does give the desired output. numpy.interp() also gives the desired output.
It seems unlikely that scipy will fix the issue in interp1d, due to it being considered legacy code. Is there any chance that xarray might support the make_interp_spline way of doing it?
Environment
INSTALLED VERSIONS
commit: None
python: 3.9.18 (main, Sep 11 2023, 08:25:10)
[Clang 14.0.6 ]
python-bits: 64
OS: Darwin
OS-release: 22.6.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: None
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.2
libnetcdf: 4.9.3-development
xarray: 2024.7.0
pandas: 2.1.0
numpy: 1.25.2
scipy: 1.13.1
netCDF4: 1.6.4
pydap: None
h5netcdf: None
h5py: 3.10.0
zarr: None
cftime: 1.6.2
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.7.3
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 65.5.1
pip: 22.3.1
conda: None
pytest: None
mypy: None
IPython: 8.18.1
sphinx: 7.3.7
The text was updated successfully, but these errors were encountered: