flip_distances#
- ktch.harmonic.flip_distances(estimator, coeffs, template)[source]#
Distances from each specimen’s flip candidates to a template.
Registration leaves each specimen on one of a few equally valid representatives: the elements of the residual group, which
flipnames by the signs they put on the codomain axes. For each specimen, this applies every candidate to its registered coefficients, asflipdoes, and measures the distance totemplate. A specimen whose nearest candidate is not the identity is registered in a different orientation from the template. Nothing is decided here: no threshold, no warning, and the coefficients are not changed.- Parameters:
- estimatorobject
The
EllipticFourierAnalysis,DiskHarmonicAnalysis,SphericalHarmonicAnalysisorSphericalHarmonicRegistrationwhose outputcoeffsis. Its parameters (n_dim,n_harmonics, the registration method) give the layout ofcoeffsand the candidates. It does not need to be fitted.- coeffsarray-like of shape (n_samples, n_features)
Registered coefficients in the layout
estimator.transformreturns. The columns appended byreturn_transform=Truemay be present; they are not read.- templatearray-like of shape (n_features,)
The coefficients of the shape the specimens are compared with, in the same layout. SPHARM-PDM calls such a shape a flip template. The constant mode is not read.
- Returns:
- result
Bunch candidates: ndarray of shape (n_candidates, n_dim). The codomain signs of each candidate, asfliptakes them; the identity first.first_orderof EFA and DHA in 2D has the identity and the half turn; in 3D also the two that turn the shape over (they reverse the parameterization), whateverreversalis. SPHARMfirst_orderandmomentin 3D have the four proper sign patterns,momentin 2D the identity and-I.distances: ndarray of shape (n_samples, n_candidates).||h . y - t|| / ||t||for each candidateh, specimenyand templatet, over every mode but the constant.nearest: ndarray of int, shape (n_samples,). The position incandidatesof the candidate nearest the template; candidates within 1e-10 of the nearest count as tied, and the first of them (the identity, if it is among them) is taken.
- result
- Raises:
- ValueError
If the estimator does not register (
registration=None), or registers withalign_parameter=False. That setting keeps the input’s parameterization and leaves no candidates (seecanonical_form_report()for its orientation). Also ifcoeffsis not 2-D, iftemplatedoes not have the layout ofcoeffsor its non-constant modes are zero, or if the coefficients ofcoeffsortemplateare not all finite (the columns ofreturn_transformare not read).
See also
canonical_form_reportWhether registered coefficients are in canonical form.
Notes
To move a specimen onto its nearest candidate, register it again with
flip=candidates[nearest]. For SPHARM,rotate_spharm_coeffs(row, np.diag(candidates[nearest]), domain="coupled")does the same tofirst_ordercoefficients.Examples
>>> import numpy as np >>> from ktch.harmonic import SphericalHarmonicRegistration, flip_distances >>> rng = np.random.default_rng(0) >>> width = 3 * (3 + 1) ** 2 >>> reg = SphericalHarmonicRegistration(scale=False) >>> template = reg.fit_transform(rng.standard_normal((1, width)))[0] >>> coeffs = reg.fit_transform(rng.standard_normal((5, width))) >>> result = flip_distances(reg, coeffs, template) >>> result.candidates.shape (4, 3) >>> result.distances.shape (5, 4)