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 flip names by the signs they put on the codomain axes. For each specimen, this applies every candidate to its registered coefficients, as flip does, and measures the distance to template. 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, SphericalHarmonicAnalysis or SphericalHarmonicRegistration whose output coeffs is. Its parameters (n_dim, n_harmonics, the registration method) give the layout of coeffs and the candidates. It does not need to be fitted.

coeffsarray-like of shape (n_samples, n_features)

Registered coefficients in the layout estimator.transform returns. The columns appended by return_transform=True may 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:
resultBunch
  • candidates : ndarray of shape (n_candidates, n_dim). The codomain signs of each candidate, as flip takes them; the identity first. first_order of 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), whatever reversal is. SPHARM first_order and moment in 3D have the four proper sign patterns, moment in 2D the identity and -I.

  • distances : ndarray of shape (n_samples, n_candidates). ||h . y - t|| / ||t|| for each candidate h, specimen y and template t, over every mode but the constant.

  • nearest : ndarray of int, shape (n_samples,). The position in candidates of 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.

Raises:
ValueError

If the estimator does not register (registration=None), or registers with align_parameter=False. That setting keeps the input’s parameterization and leaves no candidates (see canonical_form_report() for its orientation). Also if coeffs is not 2-D, if template does not have the layout of coeffs or its non-constant modes are zero, or if the coefficients of coeffs or template are not all finite (the columns of return_transform are not read).

See also

canonical_form_report

Whether 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 to first_order coefficients.

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)