-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Implement higher order interpolations in jax.scipy.ndimage #33970
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @hexane360, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a substantial upgrade to Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request implements higher-order interpolations for jax.scipy.ndimage.map_coordinates, which is a great enhancement. The implementation uses an affine associative scan for the IIR spline prefilters, which is a solid approach for performance on accelerators. The code is well-structured, with new interpolation orders and spline filtering logic cleanly integrated. The tests are comprehensive, covering the new orders and prefiltering options, and correctly handling differences with SciPy's boundary behavior.
My feedback includes a few suggestions for improving code clarity and maintainability, such as adding comments to explain complex formulas and magic numbers, moving an import to the top level, and simplifying some code patterns. Overall, this is a high-quality contribution.
| def _quartic_indices_and_weights(coordinate: Array) -> list[tuple[Array, Array]]: | ||
| (index, t) = _filter_index_and_weight(coordinate, even=True) | ||
| t_sq = t**2 | ||
| y = t + 1 | ||
| t1 = 1 - t | ||
| return [ | ||
| (index - 2, (0.5 - t)**4 / 24.0), | ||
| (index - 1, y * (y * (y * (5.0 - y) / 6.0 - 1.25) + 5.0 / 24.0) + 55.0 / 96.0), | ||
| (index, t_sq * (t_sq * 0.25 - 0.625) + 115.0 / 192.0), | ||
| (index + 1, t1 * (t1 * (t1 * (5.0 - t1) / 6.0 - 1.25) + 5.0 / 24.0) + 55.0 / 96.0), | ||
| (index + 2, (t + 0.5)**4 / 24.0), | ||
| ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| n_pad = 0 | ||
| if order > 1 and prefilter: | ||
| if mode in ('nearest', 'constant'): | ||
| n_pad = 12 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| idx = jnp.arange(0, arr.size - 1, dtype=arr.dtype) | ||
| z_n = z**(arr.dtype.type(arr.size) - 1) | ||
| return ( | ||
| jnp.sum(z**idx * (arr[:-1] + z_n * arr[:0:-1])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def _init_wrap_causal(arr: Array, z: float) -> Array: | ||
| idx = jnp.arange(1, arr.size, dtype=arr.dtype) | ||
| return ( | ||
| arr[0] + jnp.sum(z**idx * arr[:0:-1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def _spline_filter1d( | ||
| input: Array, order: int, axis: int, mode: str = 'mirror', | ||
| ) -> Array: | ||
| from jax._src.lax.control_flow.loops import associative_scan |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| (causal_fn, anticausal_fn) = _SPLINE_BOUNDARY_FNS.get(mode, (None, None)) | ||
| if causal_fn is None or anticausal_fn is None: | ||
| raise ValueError("Boundary mode '{}' not supported for pre-filtering".format(mode)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This get followed by a check for the default value can be made slightly more direct by checking for key existence first.
| (causal_fn, anticausal_fn) = _SPLINE_BOUNDARY_FNS.get(mode, (None, None)) | |
| if causal_fn is None or anticausal_fn is None: | |
| raise ValueError("Boundary mode '{}' not supported for pre-filtering".format(mode)) | |
| if mode not in _SPLINE_BOUNDARY_FNS: | |
| raise ValueError("Boundary mode '{}' not supported for pre-filtering".format(mode)) | |
| causal_fn, anticausal_fn = _SPLINE_BOUNDARY_FNS[mode] |
This is a draft PR implementing higher-order interpolations in jax.scipy.ndimage.
The IIR spline prefilters are implemented with an affine associative scan.
This might not be mergeable immediately; it seems like the spline filters have some stability problems with single-precision floats, especially with orders above 3. Therefore, the tests for spline_filter and map_coordinates with order > 3 have significantly reduced precision. Feedback or contributions are welcome.
Closes: #3928