Skip to content

Allow spectrum='nonzero' with no zero eigenvalue (#112)#157

Merged
zihuaihuai merged 1 commit into
masterfrom
apply-112-moran-spectrum-nonzero
May 4, 2026
Merged

Allow spectrum='nonzero' with no zero eigenvalue (#112)#157
zihuaihuai merged 1 commit into
masterfrom
apply-112-moran-spectrum-nonzero

Conversation

@zihuaihuai

Copy link
Copy Markdown
Collaborator

Supersedes #112.

@jordandekraker's bug report is real: a weight matrix with no kernel (e.g. hippocampal-subfield surfaces with no medial wall) shouldn't raise. The original PR removed the guard outright, but that silently corrupts the spectrum='all' branch — mask_zero.argmax() returns 0 when nothing is True, so the code drops the largest eigenvalue instead of a zero one.

This PR makes the guard branch-aware:

  • spectrum='all' still requires n_zero >= 1 and now raises a clearer message pointing at spectrum='nonzero'.
  • spectrum='nonzero' accepts kernel-free matrices and returns all eigenvalues.

Plus two regression tests covering both branches.

@jordandekraker is credited as Co-author on the squashed commit.

Test plan

  • pytest brainspace/tests/test_null_models.py → 6 passed.

The previous unconditional `if n_zero == 0: raise ValueError` blocked
legitimate use cases like hippocampal-subfield analyses where the weight
matrix has no kernel (no medial wall). Simply removing the guard, as
proposed in #112, would silently corrupt the spectrum='all' branch
(it would drop the largest eigenvalue instead of a zero one).

Make the guard branch-aware: spectrum='all' still requires a zero
eigenvalue and raises a clearer error pointing at spectrum='nonzero';
spectrum='nonzero' now accepts kernel-free matrices and returns all
eigenvalues.

Adds two regression tests covering both branches.

Co-authored-by: Jordan DeKraker <jordandekraker@users.noreply.github.com>
@zihuaihuai zihuaihuai merged commit bdcf214 into master May 4, 2026
16 checks passed
@zihuaihuai zihuaihuai deleted the apply-112-moran-spectrum-nonzero branch May 4, 2026 14:47
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.

1 participant