Skip to content

Error handling for scipy.interpolate interp1d #566

Open
@mitch-1211

Description

@mitch-1211

Describe the bug
Computing profile metrics using profile.compute(metrics=PenumbraLeftMetric()) can result in a value error such as ValueError: A value (101.48292925156349) in x_new is above the interpolation range's maximum value (100.04518204205883).

To Reproduce
This occurred when creating an InflectionDerivativeProfilePhysical for a 1x1 cm profile. Input data were already interpolated to 0.1mm.
Calling prof.compute(metrics=PenumbraLeftMetric()) eventually results in a call to scipy.interpolate interp1d, around line 276 in x_at_y in profile.py:

    def x_at_y(self, y: float | np.ndarray, side: str) -> np.ndarray | float:
        """Interpolated y-values. Can use floats as indices."""
        # I can't get UnivariateSpline to work here because it wants strictly increasing
        # data. So we use interp1d instead
        s = self.x_idx_at_x(self.center_idx)
        if side == LEFT:
            f = interp1d(x=self.values[:s], y=self.x_values[:s])
        elif side == RIGHT:
            f = interp1d(x=self.values[s:], y=self.x_values[s:])
        new_x = f(y)
        if new_x.size == 1:
            return float(new_x)
        return f(y)

Expected behavior
A potential solution is to include the argument bounds_error = False in the calls to interp1d, in this case NaN is returned, rather than a value error.

Alternatively users can use a try except block to note if this error occurs.

Additional context
Having run this on many water tank measured profiles of various field sizes, this error seems to only be occurring for one particular dataset. In general the compute metrics functions are robust, this may just add an extra layer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions