spectral_connectivity_time(faverage=True) averages over roughly twice the requested band when a band does not end at the top of the frequency grid

In mne_connectivity/spectral/time.py, _foi_average contains

f_e += 1 if f_s == f_e else f_e

Since Python parses this as f_e += (1 if f_s == f_e else f_e), a band whose start and end bins differ gets its end index doubled instead of incremented. With freqs = np.linspace(1, 45, 133) and fmin=(1, 8, 13), fmax=(4, 13, 30), the delta average covers 1 to 7 Hz, alpha 8 to 24.7 Hz while you expect 8 to 13 Hz.

Introduced in commit 4476efae (PR #104), released in 0.5 (2023-01-14); still present on main at the same line. spectral_connectivity_epochs has its own averaging and is not affected. Is this a real bug or am I missing something?

Thank you!

Marieke

Hi Marieke,

Welcome to the forums, and thanks for raising this!

That’s not the intended behaviour, and should be fixed. I’ve opened an issue to track this.

A PR would be welcome, if you are comfortable submitting one. Otherwise, I could pick this up.

Cheers,
Thomas

Hi Thomas,

Thanks a lot for your quick reply and for confirming. I opened a PR: [MRG] Fix faverage end index in spectral_connectivity_time by mariekedevylder · Pull Request #479 · mne-tools/mne-connectivity · GitHub . It applies the change you proposed, adds a test comparing faverage=True against the mean of the per-frequency output, and includes the changelog entry. It is my first contribution here, so please let me know if I missed anything.

Thank you!

Marieke