Permute B's eigen-state axis alongside its components - #558
Merged
Merged
Conversation
`parafac2.utils.standardize_pf2` permutes the rows of B (the eigen-state axis) via linear-sum assignment so that B's diagonal is maximal, pairing eigen-state i with component i, and permutes the columns of each projection to match. `order_components_by_energy` then reordered only B's columns, which moves each diagonal entry from (i, i) to (i, order^-1(i)) and destroys that pairing -- so the eigen-state heatmap comes out scrambled and the eigen-state row labels no longer line up with the component numbers used in the A/C plots. Relabel the eigen-state axis by the same permutation, and permute the columns of obsm["projections"] to match. This is an exact relabeling of the eigen-state basis: P_k B is unchanged up to the component permutation, so weighted_projections and the reconstruction come out identical to before. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
parafac2.utils.standardize_pf2finishes by permuting the rows ofB(the eigen-state axis) vialinear_sum_assignmentto maximize|diag(B)|, permuting the columns of every projectionP_kto match, and flipping signs sodiag(B)is non-negative. That pairs eigen-stateiwith componenti.order_components_by_energythen applied its energy ordering to the columns ofA,C, andB. Column-only permutation ofBmoves each diagonal entry from(i, i)to(i, order⁻¹(i)), so the maximal-diagonal form is destroyed:The model itself was never wrong —
weighted_projectionswas recomputed asprojections @ B[:, order], soA,C, cell scores, and the reconstruction were all consistent. What broke was the interpretive structure:plot_eigenstate_factorsrendersPf2_Bas a rank x rank heatmap and it came out scrambled, and the eigen-state row labels no longer corresponded to the component numbers used in the A/C plots.Fix
Relabel the eigen-state axis by the same permutation (
B[np.ix_(order, order)]) and permute the columns ofobsm["projections"]to match. This is an exact relabeling of the eigen-state basis:P_k Bis unchanged up to the component permutation, soweighted_projectionsand the reconstruction are bit-for-bit what the previous code produced, whilediag(B)and its non-negativity survive the ordering.Verified end to end on a synthetic fit: the diagonal of
Bis now exactly unchanged by the ordering step (sum|diag|identical before and after), andweighted_projections == projections @ Bstill holds.Tests
test_order_components_by_energy_preserves_diagonal_B— a diagonally dominantB, asstandardize_pf2produces, stays diagonally dominant with a positive diagonal after ordering. This fails onmain.test_order_components_by_energy_relabels_projection_columns— projection columns are permuted to match, andP_k Bis unchanged up to the component permutation.test_order_components_by_energy_reorders_weights_and_Band thetest_order_components_by_energy_is_a_permutationhypothesis invariant updated to assert both axes ofBare permuted.Full suite: 137 passed, 2 skipped.
Note for reviewers
obsm["projections"]is now written in the relabeled basis. It is consistent everywhere it is used within a run (PaCMAP input,factor_ioround-trip,weighted_projectionsreconstruction), but projections saved by an older version are in the old eigen-state basis and should not be mixed with aPf2_Bfrom this version.🤖 Generated with Claude Code