Skip to content

Conversation

@d-chambers
Copy link
Contributor

@d-chambers d-chambers commented Oct 24, 2025

Description

This PR fixes a bug in Patch.align_to_coord where the source and destination lengths can be misaligned. Since this isn't in a release yet its probably not a big deal.

Checklist

I have (if applicable):

  • referenced the GitHub issue this PR closes.
  • documented the new feature with docstrings and/or appropriate doc page.
  • included tests. See testing guidelines.
  • added the "ready_for_review" tag once the PR is ready to be reviewed.

Summary by CodeRabbit

  • Bug Fixes

    • Alignment reversal now correctly handles per-trace available data lengths when shifts vary, preventing destination/source size mismatches and improving reverse alignment accuracy.
  • Tests

    • Added tests covering alignment reversal with varying shift magnitudes and modes to prevent regressions.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 24, 2025

Walkthrough

Updated _get_dest_indices in dascore/proc/align.py to accept an end_shift parameter and compute per-trace source start/end and clamped destination ranges; added tests that generate patches with linearly varying shifts and verify reverse alignment behavior with mode="same".

Changes

Cohort / File(s) Summary
Alignment implementation
dascore/proc/align.py
Changed _get_dest_indices(shifts, start_shift, n_samples, output_size)_get_dest_indices(shifts, start_shift, end_shift, n_samples, output_size); compute source_start/source_end per trace using end_shift and start_shift, derive source_length, clamp to [0, n_samples], and compute per-trace clipped destination end; updated _calculate_shift_info call site.
Alignment tests
tests/test_proc/test_align.py
Added helper _create_patch_with_shifts(start, stop, patch=None) to create patches with linearly varying distance shifts; added test_reverse_shift_with_same_mode and test_reverse_shift_with_larger_shifts to assert reverse alignment returns a Patch and handles varying shift magnitudes.

Possibly related PRs

  • Align to coord #561: Changes to the alignment implementation in dascore/proc/align.py that are directly related to _get_dest_indices behavior and its call sites.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title "fix align_coords bug" directly refers to the main purpose of the pull request, which is to fix a bug in the alignment functionality (align_to_coord). The changes in the raw summary confirm this is the primary focus, with modifications to _get_dest_indices to resolve a destination/source length mismatch issue and corresponding test additions. While the title is somewhat concise and could be more specific about the nature of the bug, it is clear and specific enough that someone reviewing the commit history would understand it addresses a bug fix in the align functionality rather than being misleading or off-topic.
Description Check ✅ Passed The pull request description follows the template structure with both a Description section and a Checklist section present. The description briefly explains the bug being fixed in Patch.align_to_coord relating to source and destination length misalignment. However, the description lacks a reference to a specific GitHub issue (the corresponding checklist item is unchecked), and the content is quite minimal—only a single sentence explaining the bug with an additional comment about the change not yet being in a release. Additionally, while tests are clearly included in the raw summary, the "included tests" checklist item remains unchecked. The template is structurally present and mostly complete, though the substantive content and issue linkage could be improved.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch align_bug

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai bot added bug Something isn't working proc Related to processing module labels Oct 24, 2025
@codecov
Copy link

codecov bot commented Oct 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.92%. Comparing base (7f7676f) to head (e5d0970).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #578   +/-   ##
=======================================
  Coverage   99.92%   99.92%           
=======================================
  Files         128      128           
  Lines       10880    10884    +4     
=======================================
+ Hits        10872    10876    +4     
  Misses          8        8           
Flag Coverage Δ
unittests 99.92% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
tests/test_proc/test_align.py (1)

113-131: Helper is clear; add a note on units to avoid misuse in samples=True tests.

shifts here are absolute-units floats. When used with samples=True later, integer counts are safer to avoid implicit casting/rounding.

Consider documenting this in the docstring or adding an optional dtype parameter (default float) to generate integer shifts when needed.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7f7676f and 611b4f0.

📒 Files selected for processing (2)
  • dascore/proc/align.py (2 hunks)
  • tests/test_proc/test_align.py (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/test_proc/test_align.py (4)
dascore/examples.py (1)
  • get_example_patch (659-706)
dascore/proc/coords.py (1)
  • update_coords (228-253)
dascore/proc/align.py (1)
  • align_to_coord (221-371)
dascore/core/patch.py (2)
  • Patch (28-511)
  • shape (286-299)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (18)
  • GitHub Check: test_code_min_deps (ubuntu-latest, 3.12)
  • GitHub Check: test_code_min_deps (windows-latest, 3.13)
  • GitHub Check: test_code_min_deps (windows-latest, 3.12)
  • GitHub Check: test_code_min_deps (ubuntu-latest, 3.13)
  • GitHub Check: test_code_min_deps (macos-latest, 3.13)
  • GitHub Check: test_code (macos-latest, 3.13)
  • GitHub Check: test_code (windows-latest, 3.13)
  • GitHub Check: test_code (windows-latest, 3.11)
  • GitHub Check: test_code (windows-latest, 3.12)
  • GitHub Check: test_code (macos-latest, 3.10)
  • GitHub Check: test_code (windows-latest, 3.10)
  • GitHub Check: test_code (macos-latest, 3.12)
  • GitHub Check: test_code (macos-latest, 3.11)
  • GitHub Check: test_code (ubuntu-latest, 3.10)
  • GitHub Check: test_code (ubuntu-latest, 3.12)
  • GitHub Check: test_code (ubuntu-latest, 3.11)
  • GitHub Check: test_code (ubuntu-latest, 3.13)
  • GitHub Check: Run benchmarks
🔇 Additional comments (3)
dascore/proc/align.py (2)

94-96: Call site updated correctly.

Routing end_shift into _get_dest_indices aligns with the new signature and intended behavior.


41-41: All call sites correctly updated to 5-argument signature.

Verification confirms the function signature change at line 41 is consistent with its only call site (lines 94–96). No remaining callers using the old 4-argument form detected.

tests/test_proc/test_align.py (1)

425-445: Good regression coverage for reverse with mode='same'.

This reproduces the prior misaligned dest/src sizes and guards against it. Assertions are appropriate.

Comment on lines 446 to 471
def test_reverse_shift_with_larger_shifts(self, random_patch):
"""
Test with larger varying shifts to ensure edge cases are handled.
This creates a more extreme case where the shifts vary significantly,
which makes the bug more likely to occur.
"""
in_patch = random_patch
patch = _create_patch_with_shifts(start=0, stop=100, patch=in_patch)
# Align with samples=True, mode="same"
aligned = patch.align_to_coord(
relative=False,
reverse=False,
samples=True,
mode="same",
time="shifts",
)
# Reverse - this should work without broadcasting error
reversed_patch = aligned.align_to_coord(
relative=False,
reverse=False,
samples=True,
mode="same",
time="shifts",
)
assert isinstance(reversed_patch, dc.Patch)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Reverse step uses reverse=False; test is not exercising reversal.

The second alignment should set reverse=True to actually reverse the first alignment. Also mirror the shape assertion for stronger validation.

Apply this diff:

-        reversed_patch = aligned.align_to_coord(
-            relative=False,
-            reverse=False,
-            samples=True,
-            mode="same",
-            time="shifts",
-        )
+        reversed_patch = aligned.align_to_coord(
+            relative=False,
+            reverse=True,
+            samples=True,
+            mode="same",
+            time="shifts",
+        )
+        # Parity with the first reverse test
+        assert reversed_patch.shape == aligned.shape
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def test_reverse_shift_with_larger_shifts(self, random_patch):
"""
Test with larger varying shifts to ensure edge cases are handled.
This creates a more extreme case where the shifts vary significantly,
which makes the bug more likely to occur.
"""
in_patch = random_patch
patch = _create_patch_with_shifts(start=0, stop=100, patch=in_patch)
# Align with samples=True, mode="same"
aligned = patch.align_to_coord(
relative=False,
reverse=False,
samples=True,
mode="same",
time="shifts",
)
# Reverse - this should work without broadcasting error
reversed_patch = aligned.align_to_coord(
relative=False,
reverse=False,
samples=True,
mode="same",
time="shifts",
)
assert isinstance(reversed_patch, dc.Patch)
def test_reverse_shift_with_larger_shifts(self, random_patch):
"""
Test with larger varying shifts to ensure edge cases are handled.
This creates a more extreme case where the shifts vary significantly,
which makes the bug more likely to occur.
"""
in_patch = random_patch
patch = _create_patch_with_shifts(start=0, stop=100, patch=in_patch)
# Align with samples=True, mode="same"
aligned = patch.align_to_coord(
relative=False,
reverse=False,
samples=True,
mode="same",
time="shifts",
)
# Reverse - this should work without broadcasting error
reversed_patch = aligned.align_to_coord(
relative=False,
reverse=True,
samples=True,
mode="same",
time="shifts",
)
# Parity with the first reverse test
assert reversed_patch.shape == aligned.shape
assert isinstance(reversed_patch, dc.Patch)
🤖 Prompt for AI Agents
In tests/test_proc/test_align.py around lines 446 to 471, the reverse step
mistakenly calls align_to_coord with reverse=False so it doesn't undo the first
alignment; change the second call to reverse=True to actually reverse the first
alignment and add a mirrored shape assertion (e.g., assert reversed_patch.shape
== aligned.shape or assert reversed_patch.samples.shape ==
in_patch.samples.shape) after the reversal to validate the result matches
expected dimensions.

@codspeed-hq
Copy link

codspeed-hq bot commented Oct 24, 2025

CodSpeed Performance Report

Merging #578 will not alter performance

Comparing align_bug (e5d0970) with master (7f7676f)

Summary

✅ 54 untouched

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
tests/test_proc/test_align.py (1)

446-471: Past review addressed: reverse=True is now correct. Consider adding shape assertion for consistency.

The reverse=True parameter on line 466 is now correct (addressing the past review comment). However, for consistency with test_reverse_shift_with_same_mode (line 444), consider adding a shape assertion after the reversal to strengthen validation.

Apply this diff to add the shape assertion:

     assert isinstance(reversed_patch, dc.Patch)
+    assert reversed_patch.shape == aligned.shape
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 611b4f0 and e5d0970.

📒 Files selected for processing (2)
  • dascore/proc/align.py (2 hunks)
  • tests/test_proc/test_align.py (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/test_proc/test_align.py (4)
dascore/examples.py (1)
  • get_example_patch (659-706)
dascore/proc/coords.py (1)
  • update_coords (228-253)
dascore/proc/align.py (1)
  • align_to_coord (221-371)
dascore/core/patch.py (2)
  • Patch (28-511)
  • shape (286-299)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (18)
  • GitHub Check: test_code (windows-latest, 3.13)
  • GitHub Check: test_code (macos-latest, 3.13)
  • GitHub Check: test_code (ubuntu-latest, 3.12)
  • GitHub Check: test_code (windows-latest, 3.10)
  • GitHub Check: test_code (windows-latest, 3.12)
  • GitHub Check: test_code (macos-latest, 3.10)
  • GitHub Check: test_code (macos-latest, 3.11)
  • GitHub Check: test_code (windows-latest, 3.11)
  • GitHub Check: test_code (ubuntu-latest, 3.11)
  • GitHub Check: test_code (ubuntu-latest, 3.10)
  • GitHub Check: test_code (ubuntu-latest, 3.13)
  • GitHub Check: test_code_min_deps (macos-latest, 3.12)
  • GitHub Check: test_code_min_deps (ubuntu-latest, 3.12)
  • GitHub Check: test_code_min_deps (ubuntu-latest, 3.13)
  • GitHub Check: test_code_min_deps (windows-latest, 3.13)
  • GitHub Check: test_code_min_deps (macos-latest, 3.13)
  • GitHub Check: test_code_min_deps (windows-latest, 3.12)
  • GitHub Check: Run benchmarks
🔇 Additional comments (4)
dascore/proc/align.py (2)

41-53: LGTM! Bug fix correctly implements robust per-trace clamping.

The updated _get_dest_indices function now properly computes per-trace source data availability and clamps all indices to valid ranges. This prevents the broadcast error that occurred when source and destination lengths misaligned with varying shifts in mode="same". The implementation follows the defensive clamping pattern suggested in the past review.


94-96: Call site correctly updated with end_shift parameter.

The function call properly passes the new end_shift parameter required by the updated signature.

tests/test_proc/test_align.py (2)

113-131: Helper function is well-designed for test reusability.

The _create_patch_with_shifts utility cleanly generates patches with linearly varying shifts across the distance dimension, facilitating multiple test scenarios.


425-444: Test effectively verifies the bug fix for mode="same" reverse alignment.

This test reproduces the original bug scenario (varying shifts causing broadcast errors during reverse alignment) and confirms the fix works. The shape assertion on line 444 provides good validation of the round-trip behavior.

@d-chambers d-chambers merged commit 5ace1f7 into master Oct 24, 2025
23 checks passed
@d-chambers d-chambers deleted the align_bug branch October 24, 2025 16:16
@d-chambers d-chambers mentioned this pull request Nov 19, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working proc Related to processing module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants