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

Let CloughTocher2DInterpolator fit "nearest" for points outside the convex hull (instead of nans) #14386

Closed
chausies opened this issue Jul 11, 2021 · 6 comments · Fixed by #17299
Labels
enhancement A new feature or improvement scipy.interpolate
Milestone

Comments

@chausies
Copy link

Currently, CloughTocher2DInterpolator can only fill in a constant value (nan by default) when queried outside the convex hull of the input points. But this absolutely isn't ideal.

What would be much better is to use the value of the nearest neighbor, which NearestNDInterpolator does by default. For CloughTocher2DInterpolator, which already constructs the Delaunay Triangulation of the input points, finding the nearest point on the convex hull should be efficient.

Here's just my naive implementation for filling in the nearest neighbor value outside the convex hull of the inputs.

import matplotlib.pylab as P
from scipy.interpolate import CloughTocher2DInterpolator as CT
def my_CT(xy, z):
  x = xy[:, 0]
  y = xy[:, 1]
  f = CT(xy, z)
  def new_f(xx, yy):
    xx = P.array(xx) + [0.0]
    yy = P.array(yy) + [0.0]
    zz = f(xx, yy)
    nan = P.isnan(zz)
    if nan.any():
      inds = P.argmin(
          (x.reshape(-1, 1) - xx[nan])**2 + 
          (y.reshape(-1, 1) - yy[nan])**2
          , 0)
      zz[nan] = z[inds]
    return zz
  return new_f
@tupui tupui added enhancement A new feature or improvement scipy.interpolate labels Jul 12, 2021
@ev-br ev-br added this to N-D scattered in scipy.interpolate Apr 29, 2022
@ev-br ev-br moved this from N-D scattered to DOC in scipy.interpolate Jul 29, 2022
ev-br added a commit to ev-br/scipy that referenced this issue Oct 27, 2022
The example is contributed by Ajay Shanker Tripathi in scipygh-14386
@ev-br
Copy link
Member

ev-br commented Oct 27, 2022

@chausies Thank you for the example! I've added it in gh-17299, do you want me to add you as a co-author of the relevant commit? I'll then need your email address to properly format the co-authored-by git entry.

ev-br added a commit to ev-br/scipy that referenced this issue Oct 28, 2022
The example is contributed by Ajay Shanker Tripathi in scipygh-14386
ev-br added a commit to ev-br/scipy that referenced this issue Nov 1, 2022
The example is contributed by Ajay Shanker Tripathi in scipygh-14386
scipy.interpolate automation moved this from DOC to Done Nov 14, 2022
mdhaber pushed a commit that referenced this issue Nov 14, 2022
* DOC: interpolate: add an example for interp1d(..., fill_value=(left, right))

closes gh-16338

* DOC: interpolate: add an example of CubicSpline.extend to control the extrapolation

The example is contributed by Shamus Husheer in gh-14472.

Co-authored-by: Shamus Husheer <s.husheer@gmail.com>

* DOC: interpolate: add a worked example of interpolation+asymptotics

* DOC: interpolate: add an example of CT + nearest-neighbor extrapolation

The example is contributed by Ajay Shanker Tripathi in gh-14386
@j-bowhay j-bowhay added this to the 1.10.0 milestone Nov 14, 2022
@chausies
Copy link
Author

chausies commented Jan 2, 2023

@ev-br thanks so much! Sorry for not replying sooner. If it's not too late, I wouldn't mind being credited. My email is chausies @ stanford.edu .
Let me know if it works out. Cheers!

@ev-br
Copy link
Member

ev-br commented Jan 2, 2023

Well, the relevant scipy commit does list your name, which was taken from your GH profile: 61ff61f

For adding a git-recognized co-authored-by stanza, it unfortunately is too late: rewriting the commit in the main branch is a big no-no, it would wreak too much havoc on everyone who pulled main over the last few months.

Let me ask our release manager @tylerjereddy if it is still possible to manually add a name to the list of authors for the upcoming 1.10 release?

@tylerjereddy
Copy link
Contributor

@ev-br I manually added @chausies to the author list in #17696 (comment)

@ev-br
Copy link
Member

ev-br commented Jan 2, 2023

Thank you Tyler!

@chausies
Copy link
Author

chausies commented Jan 4, 2023

Thank you so much @tylerjereddy @ev-br! Now I can tell everyone I've hit the big leagues ^_^.
Keep up the great work! Happy New Years!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A new feature or improvement scipy.interpolate
Projects
Development

Successfully merging a pull request may close this issue.

5 participants