RocketPy icon indicating copy to clipboard operation
RocketPy copied to clipboard

BUG: N-D Function with linear interpolation sometimes returns NaN or raises errors for valid-looking queries

Open MateusStano opened this issue 9 months ago • 7 comments

Describe the bug

When using Function(..., interpolation="linear") with N-D input data, nan values or error can be raised depending on the data set:

  • One query returns nan silently.
  • Another query raises an exception.

This happens in in this example:

from rocketpy import Function

data = [
    [0.0, 0.0, 0.000],
    [0.0, 0.1, 0.1],
    [0.0, 0.4, 0.4],
    [0.3, 0.2, 0.15],
]
f = Function(data, interpolation="linear")
print(f(0.3, 0.3))   # returns nan

aa = Function([[1, 1, 10], [2, 2, 20], [3, 3, 30]], interpolation="linear")
print(aa(4, 4))       # raises error
  • bb(0.3, 0.3) returns nan without explicit warning/error.
  • aa(4, 4) raises an exception.

This appears to happen only with very specific point configurations/datasets. Please investigate the underlying cause and clarify intended behavior for these edge cases.

MateusStano avatar Feb 22 '26 21:02 MateusStano

My two cents:

  1. The ND Interpolator from scipy returns nan for points outside the convex hull (i.e. the boundary of the domain triangulation). Therefore likely there is an issue with the Function logic that identify if it should extrapolate or not (in the get_value_nd).
  2. For the second case, your set of points is colinear. It is a degenerate domain whence no triagulation can be made. I would expect that the scipy library would raise an exception that is detailed enough for us not to have to implement this manual check.

phmbressan avatar Feb 22 '26 22:02 phmbressan

Agent run 41735ddc07b34ef38ac32a720181980a started.

AlwaysTwilight avatar Feb 22 '26 23:02 AlwaysTwilight

Agent run 41735ddc07b34ef38ac32a720181980a finished without PR: Baseline pytest failed; aborting run

AlwaysTwilight avatar Feb 22 '26 23:02 AlwaysTwilight

My two cents:

  1. The ND Interpolator from scipy returns nan for points outside the convex hull (i.e. the boundary of the domain triangulation). Therefore likely there is an issue with the Function logic that identify if it should extrapolate or not (in the get_value_nd).
  2. For the second case, your set of points is colinear. It is a degenerate domain whence no triagulation can be made. I would expect that the scipy library would raise an exception that is detailed enough for us not to have to implement this manual check.

@phmbressan would you say we should open an issue on scipy's repo instead of making the comparison ourselves?

Gui-FernandesBR avatar Feb 23 '26 00:02 Gui-FernandesBR

@phmbressan would you say we should open an issue on scipy's repo instead of making the comparison ourselves?

I don't think so. The behavior we are seeing is what is expected/documented from scipy. It is the Function that is incorrectly using the Interpolator for points outside the domain (this is an educated guess, I haven't investigated the issue myself).

Now, if the scipy interpolator should support native extrapolation, I believe there is already an issue in their repository about this. I can find the link later.

phmbressan avatar Feb 23 '26 08:02 phmbressan

@MateusStano does #927 solve this issue?

Gui-FernandesBR avatar Mar 14 '26 16:03 Gui-FernandesBR

Nope, it does not fix it

MateusStano avatar Mar 21 '26 03:03 MateusStano