Skip to content

🐛 Fix ideal_bandpass filter #2237

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

Merged
merged 9 commits into from
Jun 13, 2025
Merged

🐛 Fix ideal_bandpass filter #2237

merged 9 commits into from
Jun 13, 2025

Conversation

birajstha
Copy link
Contributor

@birajstha birajstha commented May 27, 2025

Fixes

Fixes numpy.linalg.LinAlgError: singular matrix error in #2225 by @shnizzedy
Fixes eigenvector centrality crash in in-progress #2230 by @shnizzedy

Description

This PR fixes a bug in the ideal_bandpass function where the frequency mask was incorrectly applied, which could cause the filter to fail or behave unexpectedly, especially for certain edge cases in bandpass frequency selection.

The issue was caused by an incorrect use of boolean logic (f_data[freq_mask is not True] = 0.0) which did not apply the mask elementwise. This has now been corrected to f_data[~freq_mask] = 0.0.

Technical details

Replaced

f_data[freq_mask is not True] = 0.0

with

f_data[~freq_mask] = 0.0

which applies the negation elementwise, as intended.

The FFT zero-padding and bandpass index calculation remain unchanged in terms of functionality. Variable naming was slightly improved for readability (e.g., nyquist_freq used instead of repeated sample_freq / 2.0).

Tests

Manually inspected non-zero outputs from this function.

Screenshots

Before fix
image

After fix
image

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the develop branch of the repository.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added tests for the changes I made (if applicable).
  • I updated the changelog.
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@birajstha birajstha force-pushed the fix/ideal_bandpass branch from 06ed6cf to 4b0bf06 Compare May 27, 2025 17:36
@birajstha birajstha requested a review from a team May 27, 2025 18:11
Copy link

codecov bot commented May 27, 2025

Codecov Report

Attention: Patch coverage is 95.74468% with 2 lines in your changes missing coverage. Please review.

Project coverage is 28.3%. Comparing base (fde14a5) to head (bae5da4).
Report is 10 commits behind head on develop.

Files with missing lines Patch % Lines
CPAC/nuisance/bandpass.py 85.7% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##           develop   #2237     +/-   ##
=========================================
+ Coverage     28.1%   28.3%   +0.2%     
=========================================
  Files          230     230             
  Lines        25941   25978     +37     
  Branches      4081    4081             
=========================================
+ Hits          7299    7353     +54     
+ Misses       18013   17996     -17     
  Partials       629     629             
Files with missing lines Coverage Δ
CPAC/nuisance/tests/test_bandpass.py 100.0% <100.0%> (ø)
CPAC/nuisance/bandpass.py 42.7% <85.7%> (+21.8%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@birajstha birajstha changed the title adding to changelog 🐛 Fix ideal_bandpass filter May 27, 2025
@birajstha birajstha moved this to 👀 In review in C-PAC Development Jun 12, 2025
@birajstha birajstha linked an issue Jun 12, 2025 that may be closed by this pull request
9 tasks
Copy link
Member

@shnizzedy shnizzedy left a comment

Choose a reason for hiding this comment

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

This is great. And it resolved the crash I was hitting in #2230!

We could add a parameterization where LowCutoff > nyquist_freq and HighCutoff > nyquist_freq or HighCutoff is None(1) and this other parameterization and get full coverage of this PR, but I also think it's good to go as-is.

Footnotes

  1. Is it conceptually correct that those are both > nyquist_freq? I see it was already that way, and I don't know enough about filtering terminology to know if that's intended

@birajstha
Copy link
Contributor Author

@shnizzedy , Yes nyquist_freq is the hard cutoff, we do not want anything above this frequency.

@birajstha birajstha merged commit 157285f into develop Jun 13, 2025
30 checks passed
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in C-PAC Development Jun 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

🐛 Resolve 1.8.8 crashes & regressions
2 participants