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

Implement linear extrapolation #94

Merged
merged 14 commits into from
Sep 26, 2024
Merged

Implement linear extrapolation #94

merged 14 commits into from
Sep 26, 2024

Conversation

timmens
Copy link
Member

@timmens timmens commented Sep 24, 2024

In this PR, I add capabilities to perform linear extrapolation.

In particular, I

  • Implement a custom map_coordinates function that allows for linear extrapolation. (A discussion on the JAX repo implied that they would not integrate this feature)
  • Remove interpolation options. Since we only want to perform linear inter- and extrapolation, there is no need for added complexity.
  • Update the regression tests since the results are different. Notably, the results now look better (more correct) than before. This also shows that the linear extrapolation helps with the solution and simulation.

Closes #83

How it works

To extrapolate linearly, one has to adjust the function that computes weights and indices of the coordinate points. For the linear interpolation, the JAX code can be found here.

This implementation implicitly assumes that coordinate is in the domain (interpolation). For values outside of the domain (extrapolation), one has to adjust the first line to

lower = jnp.clip(jnp.floor(coordinate), 0, input_size - 2)

where input_size corresponds to a dimension of the input shape. This leads to the same behavior for interpolation but assigns the correct indices and weights needed for linear extrapolation for coordinates outside the domain.

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Copy link
Member

@hmgaudecker hmgaudecker left a comment

Choose a reason for hiding this comment

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

Very cool. Had a brief look and leaving the note so I can see down the road what has changed from this point onwards.

tests/test_ndimage.py Outdated Show resolved Hide resolved
tests/test_ndimage.py Outdated Show resolved Hide resolved
tests/test_ndimage.py Outdated Show resolved Hide resolved
src/lcm/ndimage.py Show resolved Hide resolved
@timmens timmens requested a review from hmgaudecker September 25, 2024 13:42
Copy link
Member

@hmgaudecker hmgaudecker left a comment

Choose a reason for hiding this comment

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

Excellent, thank you!!!

Just a tiny comment.

src/lcm/ndimage.py Show resolved Hide resolved
@timmens timmens merged commit 7e8e3f8 into main Sep 26, 2024
7 checks passed
@timmens timmens deleted the custom-map-coordinates branch September 26, 2024 12:25
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.

ENH: Add linear extrapolation to map_coordinates
2 participants