Skip to content

Fix rolling_filter window=1 edge case by returning early and add test#888

Open
VPK570 wants to merge 8 commits intoneuroinformatics-unit:mainfrom
VPK570:fix/rolling-filter-window-1
Open

Fix rolling_filter window=1 edge case by returning early and add test#888
VPK570 wants to merge 8 commits intoneuroinformatics-unit:mainfrom
VPK570:fix/rolling-filter-window-1

Conversation

@VPK570
Copy link

@VPK570 VPK570 commented Mar 13, 2026

Description

What is this PR

  • Bug fix
  • Addition of a new feature
  • Other

Why is this PR needed?

Calling rolling_filter with window=1 currently returns an empty DataArray.
A window size of 1 is a valid no-op filter and should return the original data unchanged.

The issue occurs because when window=1, half_window = window // 2 = 0.
The trim step then calls isel(time=slice(0, -0)), which evaluates to slice(0, 0) and results in an empty array.

What does this PR do?

  • Adds an early return in rolling_filter when window == 1
  • Prevents unnecessary processing and avoids the trimming bug
  • Adds a unit test to verify that the output equals the input when window=1

References

Closes #887

How has this PR been tested?

  • Added a unit test test_rolling_filter_window_1 that checks the output equals the input when window=1.
  • Ran the full test suite with pytest to ensure existing tests pass and no regressions were introduced.

Is this a breaking change?

No. This change only fixes an edge case and does not modify existing behavior for other window sizes.

Does this PR require an update to the documentation?

No. This change fixes an internal bug and does not introduce any new functionality.

@sonarqubecloud
Copy link


def test_rolling_filter_window_1():
"""window=1 should return data unchanged."""
rng = np.random.default_rng()

Check notice

Code scanning / SonarCloud

Results that depend on random number generation should be reproducible Low test

Provide a seed for this random generator. See more on SonarQube Cloud
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.

rolling_filter returns empty array when window=1

1 participant