Skip to content

Fix: invalid value in divide#49

Open
christianbrodbeck wants to merge 2 commits intoEelbrain:masterfrom
christianbrodbeck:fix/invalid-value
Open

Fix: invalid value in divide#49
christianbrodbeck wants to merge 2 commits intoEelbrain:masterfrom
christianbrodbeck:fix/invalid-value

Conversation

@christianbrodbeck
Copy link
Member

Should prevent the RuntimeWarning discussed in. @yaylim let me know if it actually does.

Closes #48

@christianbrodbeck christianbrodbeck marked this pull request as ready for review December 11, 2025 21:01
@yaylim
Copy link
Collaborator

yaylim commented Dec 11, 2025

I just tested it, and I am not getting the same error message anymore. But if I do not exclude the bad channels, the issue still persists (which is expected). I am not getting an error message stating that there are flat channels in the MEG data in that case though.

@christianbrodbeck
Copy link
Member Author

So if I understand correctly, you don't get invalid value in divide when you exclude bad channels, but you do get invalid value in divide when you do not exclude bad channels. That would mean we've identified the source of the issue, but my PR is not doing what it's supposed to do. Can you debug my PR with your data so that we properly raise the error and do not get invalid value in divide when users supply bad data? You can make a PR into my PR (or create a new PR from your own branch if that's easier).

@yaylim
Copy link
Collaborator

yaylim commented Jan 8, 2026

The invalid value in divide error occurs when one of the MEG channels contains only zeros. This situation arises because during the pre-whitening stage the MEG channel is set to all zeros. The whitening filter is created using the noise covariance matrix, which is calculated from the empty-room recordings. During the eigenvalue decomposition performed in the construction of the pre-whitening filter (in _inv_sqrtm), the real parts of the eigenvalues are compared against a threshold defined by _R_tol. If any rows fail to exceed this threshold, those rows are set to zero. Consequently, during the pre-whitening stage, the MEG data are multiplied by the resulting filter, causing the corresponding channel to become entirely zero. Therefore, the root cause of this issue is not flat MEG channels in the data itself, but rather the properties of the empty-room recordings used to compute the noise covariance matrix. @proloyd I would appreciate your input on this.

@proloyd
Copy link
Collaborator

proloyd commented Jan 22, 2026

@yaylim we can also check if the diagonal of the covariance matrix contain any zeros, and accordingly raise error just the way @christianbrodbeck raises the error in his commit. That should address the problem and notify the user possible changes that they have to make.

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.

Invalid value encountered in divide

3 participants

Comments