Skip to content

Fix rotation orientation and scale in Procrustes with reflection=False - #302

Merged
jeremymanning merged 1 commit into
ContextLab:masterfrom
chenyangsu:fix/procrustes-no-reflection
Dec 3, 2026
Merged

jeremymanning merged 1 commit into
ContextLab:masterfrom
chenyangsu:fix/procrustes-no-reflection

Conversation

@chenyangsu

Copy link
Copy Markdown
Contributor

In procrustes.align(..., reflection=False), np.dot(u[:, :nsv] * s, vh) returns the transposed rotation and overwrites s before ss = sum(s). On a 3D 90-degree rotation R, align returns 3 * R.T; on a 2D reflection, ss becomes 0.0 and align returns zeros.

Using a sign vector d for t = np.dot(vh.T * d, u.T) and ss = float(np.sum(s * d)) matches Schonemann (1966) and Umeyama (1991), with tests in tests/test_procrustes.py. Happy to clamp ss at zero for 1D inputs if you prefer.

Testing: pytest -q tests/test_procrustes.py tests/align/ -k "not test_procrustes_func" (56 passed); ruff check (clean).

@codecov-commenter

Copy link
Copy Markdown

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@jeremymanning
jeremymanning merged commit 1db98b2 into ContextLab:master Dec 3, 2026
18 checks passed

Copy link
Copy Markdown
Member

Thanks so much for this contribution, @chenyangsu! This was a real bug and a clean fix. We reviewed it independently before merging:

  • Reproduced on master: with reflection=False, the rotation came back transposed, and with scaling=True the scale was wrong as well (e.g., a 3D 90° rotation returned 3 * R.T, and a 2D reflected target collapsed to all zeros).
  • Verified the fix: given the source @ proj convention, V D Uᵀ is the right optimum. Recovery of known rotations was exact to ~1e-14 across dimensions 2–50, and results matched an independent Kabsch/Umeyama implementation and scipy's Rotation.align_vectors.
  • Default path unchanged: reflection=True and oblique outputs are identical to master (up to 1e-16), and your three new tests fail on master and pass here, so they're genuine regression tests.

On your 1D question: yes, we think clamping is right, i.e. ss = max(float(np.sum(s * d)), 0.0). For m ≥ 2 the sum is never negative (singular values are sorted), so the clamp only removes rounding noise. For 1D anti-correlated data with scaling=True, the current code returns a negative scale, which is effectively a reflection despite reflection=False; the constrained optimum there is a scale of 0 (probably with a warning, since it collapses the output). Master behaves the same way, so that isn't a regression and can be a follow-up.

A few other optional follow-ups, none blocking, if you're interested:

  • assert np.linalg.norm(proj) > 0 in the 2D test would pass on master too (the projection is ~1e-16 rather than exactly 0); a threshold like > 1e-3 would better capture the intent.
  • A padded or reduction=True test case with reflection=False would lock in those paths.
  • A "Fixed" entry in CHANGELOG.md.

Thanks again!


Generated by Claude Code

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.

3 participants