Skip to content

Use post-filter site count for window filtering in local PCA - #447

Merged
nspope merged 4 commits into
mainfrom
fix/373-local-pca-validity-gate
Nov 20, 2026
Merged

nspope merged 4 commits into
mainfrom
fix/373-local-pca-validity-gate

Conversation

@nspope

@nspope nspope commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Closes #373

local_pca (every engine) and local_pca_jackknife decided window validity from window.n_variants, counted before missing_data='exclude' removed incomplete sites, so a window with no complete site looked valid and got degenerate eigvals/sumsq/eigvecs instead of NaN. _local_pca_with_jackknife's jackknife gate also compared against X.shape[1] (one-hot columns), which can exceed the real site count, so it could disagree with local_pca_jackknife on the same window.

_centered_block/_prepare_centered now also return the site count left after the exclude filter, and all four window-processing paths re-gate on it instead of the pre-filter or one-hot-column counts. Block splitting inside the jackknife still operates on X's own columns, since that's what's actually indexed.

@nspope
nspope force-pushed the fix/373-local-pca-validity-gate branch from 391666b to 6e46d87 Compare November 20, 2026 04:49

@andrewkern andrewkern left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Checked this against main on all the paths: the three local_pca engines, local_pca_jackknife, lostruct(jackknife=True) and windowed_analysis. The empty-after-exclude window is NaN everywhere now, and the two jackknife gates agree. Notes:

  1. The same output is still there under 'include' when a window has no usable call: every call missing gives eigvals [0, 0] and sumsq 0 on all three engines, because the site count is 10 but there are no one-hot columns. A monomorphic window does the same. Gating on zero columns as well would cover it; fine as a follow-up if you'd rather keep this one to #373.
  2. Needs a rebase; the changelog and the pipeline map moved on main.
  3. Small: no test for local_pca_jackknife or the lostruct path with a window the exclude filter empties. Both behave in my runs.

local_pca (every engine) and local_pca_jackknife decided window
validity from window.n_variants, counted before missing_data='exclude'
removed incomplete sites, so a window with no complete site looked
valid and got degenerate eigvals/sumsq/eigvecs instead of NaN.
_local_pca_with_jackknife's jackknife gate also compared against
X.shape[1] (one-hot columns), which can exceed the real site count, so
it could disagree with local_pca_jackknife on the same window.

_centered_block/_prepare_centered now also return the site count left
after the exclude filter, and all four window-processing paths re-gate
on it instead of the pre-filter or one-hot-column counts. Block
splitting inside the jackknife still operates on X's own columns,
since that's what's actually indexed.
The real-site-count gate still passed a window whose calls are all
missing under missing_data='include' (the exclude filter never runs,
so the site count is unchanged), leaving X with zero one-hot columns
and producing eigvals of 0, sumsq of 0, and a degenerate PC1 instead
of NaN. local_pca (every engine) and _local_pca_with_jackknife now
also reject a window whose standardized X has no columns at all.
local_pca_jackknife already rejected this case through its block-width
gate, so it needed no change.
local_pca already had this case covered for all three engines, and the
jackknife paths had it covered for the exclude-filter-empties-it case,
but _local_pca_with_jackknife's own zero-one-hot-column branch (the
all-missing case under missing_data='include') had no test exercising
it. Confirmed it fails against the pre-fix code (eigvals [0, 0]).
Leaving out the one and only block empties every window entirely, but
that standardizes to a valid-looking all-zero Gram instead of NaN, so
local_pca_jackknife, _local_pca_with_jackknife, and lostruct(jackknife=True)
silently returned an SE of 0.0 for n_blocks=1 instead of erroring or
giving NaN. Confirmed empirically before the fix; n_blocks=0 already
raised, but with an unhelpful ZeroDivisionError.
@nspope
nspope force-pushed the fix/373-local-pca-validity-gate branch from 6e46d87 to a9bd4d3 Compare November 20, 2026 17:49
@nspope
nspope merged commit 7fbe1d2 into main Nov 20, 2026
2 checks passed
@nspope
nspope deleted the fix/373-local-pca-validity-gate branch November 20, 2026 18:25
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.

Local PCA decides window validity from the wrong count: before the 'exclude' filter, and in one-hot columns for the jackknife

2 participants