Repository navigation
data: Fix negative slice bounds along modulo-indexed Dimensions - #3086
Open
MaxFreedomPollard wants to merge 1 commit into
Open
MaxFreedomPollard wants to merge 1 commit into
MaxFreedomPollard wants to merge 1 commit into
Conversation
`index_apply_modulo` negated `idx.start % modulo`, which Python already makes nonnegative, so a negative bound on the time axis of a buffered TimeFunction picked the wrong slots. With time_order=2, `u.data[-1:]` returned slots 1 and 2, `u.data[:-1]` returned nothing, and assignments through such slices wrote to the wrong slots or to none at all. Wrap the magnitude of a negative bound instead, so that a negative bound within the buffer selects the same entries as in NumPy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3086 +/- ##
==========================================
- Coverage 84.27% 79.78% -4.50%
==========================================
Files 258 258
Lines 57421 57432 +11
Branches 4911 4911
==========================================
- Hits 48392 45822 -2570
- Misses 8205 10729 +2524
- Partials 824 881 +57
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
mloubout
approved these changes
Oct 23, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Slicing the time axis of a buffered
TimeFunctionwith a negative bound selects the wrong time slots, both when reading and when writing. Withtime_order=2, the usual setup for a second order wave equation, on main:The default
time_order=1is affected too:u.data[:-1]comes back empty there as well.The time Dimension of a buffered
TimeFunctionis modulo-indexed, soData._index_glb_to_locmaps each index throughindex_apply_moduloindevito/data/utils.py. For a negative slice bound that function returns-(idx.start % modulo), or-(idx.stop % (modulo + 1))for the stop. Python's%already gives a nonnegative remainder for a negative operand, so with three slots a start of-1becomes-2, a start of-2becomes-1, and a stop of-1becomes-3.The fix wraps the magnitude of a negative bound and keeps its sign, i.e.
-(-idx.start % modulo)and-(-idx.stop % (modulo + 1)). A negative bound within the buffer now selects the same entries as in NumPy. I checked every slice with a start in[-n, n)orNone, a stop in[-n, n]orNone, and a step ofNone, ±1 or ±2, for buffers of 1 to 7 slots. All of them now match NumPy. Before the fix, 84 of the 280 cases for three slots did not. A negative bound beyond the buffer wraps the same way an integer index does, sou.data[-4:]now starts at the slotu.data[-4]refers to. Nonnegative bounds are unchanged. So is anyDatawithout modulo indexing, which covers everyFunction, savedTimeFunctionandSparseTimeFunction.Verified on macOS (Apple silicon, Python 3.12,
DEVITO_ARCH=clang,DEVITO_LANGUAGE=C):tests/test_data.py::TestDataBasic::test_negative_slicing_moduloreads and writes through eight slices with negative bounds and compares the results against NumPy. Six of the eight fail on main and all pass with the fix.pytest -m "not parallel" tests/test_data.py: 97 passed, 2 skipped.tests/test_builtins.py,test_buffering.py,test_checkpointing.py,test_dimension.py,test_save.py,test_timestepping.py,test_operator.pyandtest_docstrings.py: 484 passed. The 11 failures are environmental and identical on main: 8 cannot download scipy'sascentimage, and 3 need OpenMP, which Apple clang lacks.tests/test_sparse.py,test_interpolation.pyandtest_tensors.py: 268 passed.ruff check --preview, flake8 and typos are clean.🤖 Generated with Claude Code