Skip to content
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

Added pchip Interpolator #4871

Merged
merged 8 commits into from
Feb 24, 2025
Merged

Added pchip Interpolator #4871

merged 8 commits into from
Feb 24, 2025

Conversation

Rishab87
Copy link
Contributor

Description

Added pchip Interpolator support in PyBaMM

Fixes #4590

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #)

Important checks:

Please confirm the following before marking the PR as ready for review:

  • No style issues: nox -s pre-commit
  • All tests pass: nox -s tests
  • The documentation builds: nox -s doctests
  • Code is commented for hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

Copy link

codecov bot commented Feb 23, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.70%. Comparing base (725a42c) to head (2978a09).
Report is 2 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #4871   +/-   ##
========================================
  Coverage    98.70%   98.70%           
========================================
  Files          304      304           
  Lines        23452    23483   +31     
========================================
+ Hits         23149    23180   +31     
  Misses         303      303           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Rishab87 Rishab87 marked this pull request as ready for review February 24, 2025 05:46
@Rishab87 Rishab87 requested a review from a team as a code owner February 24, 2025 05:46
Copy link
Member

@MarcBerliner MarcBerliner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the nice work on this @Rishab87! I added a few comments

@@ -281,3 +282,19 @@ def test_voltage_without_directions(self):

voltage = solution["Terminal voltage [V]"].entries
assert np.allclose(voltage, 2.5, atol=1e-3)

def test_pchip_interpolation_experiment(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a few more tests:

  • Comparing the results with scipy.interpolate.PchipInterpolator
  • Check non-uniform x grids
  • Check that it throws an error for non-increasing x inputs
  • Check left/right extrapolation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added these tests

)
x_np = np.array(symbol.x[0])
y_np = np.array(symbol.y)
pchip_interp = interpolate.PchipInterpolator(x_np, y_np, axis=0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the axis=0 argument is unnecessary because of the upstream check for 1D interpolation, but I could be wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah its not required

+ h11 * h_val_mx * d1
)

cond = casadi.logic_and(x_sym >= x0, x_sym <= x1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we try to extrapolate outside the x bounds, this will return the value defined by

result = casadi.MX.zeros(x_sym.shape)

which can be problematic during simulation. Can you please add extrapolation that aligns with scipy's?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added extrapolation aligning with scipy

d0 = casadi.MX(d_np[i])
d1 = casadi.MX(d_np[i + 1])

t = (x_sym - x0) / h_val_mx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is minor, but can we keep a consistent notation with either t or x but not both?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed this to use x

x_np = np.array(symbol.x[0])
y_np = np.array(symbol.y)
pchip_interp = interpolate.PchipInterpolator(x_np, y_np, axis=0)
d_np = pchip_interp.derivative()(x_np)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we access this array directly from the properties of the pchip class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes i think so as protected attribute

@Rishab87
Copy link
Contributor Author

@MarcBerliner thanks for your review, I've added made changes according to your comments

Copy link
Member

@MarcBerliner MarcBerliner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Rishab87, this is great. I just added one minor changelog update, which I missed the first time around. Otherwise, it's good to go

Co-authored-by: Marc Berliner <[email protected]>
@Rishab87
Copy link
Contributor Author

@MarcBerliner I've applied that change!

@MarcBerliner MarcBerliner enabled auto-merge (squash) February 24, 2025 18:06
@MarcBerliner MarcBerliner merged commit 603fe6f into pybamm-team:develop Feb 24, 2025
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding pchip interpolator to pybamm.Interpolant class.
2 participants