Skip to content

Fix MultiIndex error in compute_forward_displacement (#794)#799

Merged
sfmig merged 7 commits intoneuroinformatics-unit:mainfrom
Tushar7012:fix-multiindex-reindex
Feb 27, 2026
Merged

Fix MultiIndex error in compute_forward_displacement (#794)#799
sfmig merged 7 commits intoneuroinformatics-unit:mainfrom
Tushar7012:fix-multiindex-reindex

Conversation

@Tushar7012
Copy link
Contributor

Replace reindex(data.coords) with reindex_like(data) to avoid a RuntimeError when DataArray coordinates originate from pandas MultiIndex levels that carry the _no_setting_name flag.

  • Fix _compute_forward_displacement (line 129)
  • Fix compute_displacement (line 115, deprecated)
  • Add regression test test_forward_displacement_with_multiindex_coords

Before submitting a pull request (PR), please read the contributing guide.


Description

What is this PR

  • Bug fix
  • Addition of a new feature
  • Other

Why is this PR needed?

When compute_forward_displacement is called with an xarray DataArray created from a pandas MultiIndex (via to_xarray()), the current implementation fails with a RuntimeError.
This happens because reindex(data.coords) internally attempts to rename index levels, which pandas explicitly disallows for MultiIndex objects with the _no_setting_name flag.

As a result, valid inputs can unexpectedly fail even though the underlying data is correct.

What does this PR do?

This PR replaces the use of reindex(data.coords, fill_value=0) with reindex_like(data, fill_value=0) in both the active and deprecated code paths.

reindex_like() provides the same alignment and fill behavior while avoiding index-level name mutation, making the function safe for MultiIndex-backed inputs. A regression test has also been added to ensure this case remains supported going forward.


References


How has this PR been tested?

  • Added a regression test (test_forward_displacement_with_multiindex_coords) based on the reproducible example provided in the issue
  • Verified that the test fails on the current main branch and passes with this change
  • Ran the full test suite locally to ensure no existing behavior is affected

Is this a breaking change?

No. This change preserves existing behavior and only affects internal reindexing logic to improve robustness for specific input types.


Does this PR require an update to the documentation?

No. This is an internal bug fix and does not change the public API or user-facing behavior.


Checklist

  • The code has been tested locally
  • Tests have been added to cover the reported issue
  • The documentation has been updated to reflect any changes (not required)
  • The code has been formatted with pre-commit

…s-unit#794)

Replace reindex(data.coords) with reindex_like(data) to avoid RuntimeError when DataArray coordinates have the _no_setting_name flag from pandas MultiIndex levels.

- Fix _compute_forward_displacement (line 129)

- Fix compute_displacement (line 115, deprecated)

- Add regression test test_forward_displacement_with_multiindex_coords
Copilot AI review requested due to automatic review settings February 3, 2026 18:35
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a RuntimeError in compute_forward_displacement (and the deprecated compute_displacement) when the input xarray.DataArray originates from a pandas MultiIndex (e.g., via Series.to_xarray()), by switching to reindex_like() to avoid index-level name mutation.

Changes:

  • Replace reindex(data.coords) with reindex_like(data) in _compute_forward_displacement.
  • Apply the same fix in the deprecated compute_displacement path.
  • Add a regression unit test covering MultiIndex-derived coordinate inputs (issue #794).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
movement/kinematics/kinematics.py Uses reindex_like(data, fill_value=0) to align diff results back to the original array without triggering MultiIndex level-name mutation.
tests/test_unit/test_kinematics/test_kinematics.py Adds a regression test ensuring compute_forward_displacement works with DataArrays created from a pandas MultiIndex via .to_xarray().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov
Copy link

codecov bot commented Feb 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (ddec403) to head (0d7ddff).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #799   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           36        36           
  Lines         2205      2205           
=========================================
  Hits          2205      2205           

☔ 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.

@sfmig sfmig force-pushed the fix-multiindex-reindex branch from 95a8873 to 220314a Compare February 4, 2026 11:40
This commit reverts accidental changes to documentation and configuration files, bringing them back to the state of the current main branch.

Files restored:

- CONTRIBUTING.md

- docs/source/*

- pyproject.toml

- tests/test_unit/test_filtering.py

- etc.
@Tushar7012 Tushar7012 force-pushed the fix-multiindex-reindex branch from 220314a to 0f5e6a5 Compare February 4, 2026 15:00
@Tushar7012
Copy link
Contributor Author

Tushar7012 commented Feb 4, 2026

Hey @sfmig ,Thank you for the feedback. I apologize for the accidental inclusion of unrelated documentation changes. I have updated the PR to strictly focus on the MultiIndex bug fix and its accompanying regression test...

@sfmig sfmig self-requested a review February 16, 2026 10:28
@sonarqubecloud
Copy link

Copy link
Member

@sfmig sfmig left a comment

Choose a reason for hiding this comment

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

Hi @Tushar7012 thanks for this and apologies for the slow response!
I think this is good to go now, I just rephrased a bit the docstring in the test for clarity.

@sfmig sfmig added this pull request to the merge queue Feb 27, 2026
Merged via the queue into neuroinformatics-unit:main with commit 1b9d294 Feb 27, 2026
25 checks passed
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.

MultiIndex error with specific inputs to compute_forward_displacement

3 participants