canonical_form_report#
- ktch.harmonic.canonical_form_report(estimator, coeffs, *, form='auto', tol=1e-10, atol=0.0)[source]#
Report whether registered coefficients are in a registration’s canonical form.
A per-specimen correction applied after registration can leave every rendering looking right while breaking the correspondence between specimens: a specimen turned on the codomain alone looks like one in a different pose. This check reads the coefficients and says, per specimen, whether they are in the canonical form of
first_orderregistration, one coupled operation away from it, or no coupled operation away from it, and, for the sample, whether every specimen was turned by one rotation. It reports what it finds rather than raising on it; it raises only on input it cannot read (see Raises).- Parameters:
- estimatorobject
The
EllipticFourierAnalysis,DiskHarmonicAnalysis,SphericalHarmonicAnalysisorSphericalHarmonicRegistrationwhose outputcoeffsis. Its parameters (n_dim,n_harmonics, the registration method) give the layout ofcoeffsand, withform="auto", the canonical form to check against. It does not need to be fitted.- coeffsarray-like of shape (n_samples, n_features)
Coefficients in the layout
estimator.transformreturns. ForEllipticFourierAnalysisthe columns appended byreturn_transform=Truemay be present; they are not read.- form{“auto”, “first_order”, “moment”}, default=”auto”
The canonical form to check against.
"auto"follows the estimator’s registration (registration, ormethodforSphericalHarmonicRegistration, with"auto"resolved as the estimator resolves it); unregistered estimators are checked against"first_order".- tolfloat, default=1e-10
How far from the canonical form still counts as in it: the tolerance on each quantity below (relative, see Notes). Use it for a departure the coefficients carry, such as the precision of the software that aligned them.
- atolfloat, default=0.0
Absolute uncertainty of each coefficient, such as the rounding of a file (
5e-7for six decimals). It decides when a first-order block is too thin to have a frame ("degenerate") and how far rounding can move each quantity, which grows as the block thins (see Notes).0takes the rounding of the arithmetic only.
- Returns:
- report
Bunch With
form="first_order"(or"auto"resolving to it):label: ndarray of str, shape (n_samples,)."canonical","coupled","uncoupled"or"degenerate"(see Notes).direction: ndarray of int, shape (n_samples,). The sign of the determinant of the degree-1 block: in 2D1for a counter-clockwise and-1for a clockwise output; for SPHARM1for a right-handed and-1for a left-handed block;0for EFA-3D and DHA-3D, whose block is 3 x 2, and for a block of zero determinant.d_q,d_s,delta_order,sigma_ratio: ndarray of float, shape (n_samples,). The quantities the label is read from (see Notes);nanfor degenerate specimens exceptsigma_ratio.population:Bunchwithstatus("aligned","uniform","not_uniform"or"empty"),side("codomain","parameter","either"or"neither"whenstatusis"uniform",Noneotherwise),d_uandrotation(see Notes).form:"first_order";tol,atol: the tolerances used.
With
form="moment"(or"auto"resolving to it):label: ndarray of str,"canonical"or"not_canonical".d_offdiag,delta_order: ndarray of float (see Notes).population:None;form:"moment";tol,atol.
- report
- Raises:
- TypeError
If
estimatoris not one of the four harmonic estimators.- ValueError
For an unknown
form, a negativetoloratol,coeffsthat is not 2-D, whose width does not match the estimator or whose coefficients are not finite, an estimator withn_dimnot in(2, 3)or without degree-1 modes, orform="first_order"for SPHARM withn_dim != 3.
See also
rotate_spharm_coeffsRotate SPHARM coefficients on the codomain, the parameter sphere, or both (
domain="coupled").
Notes
first_order. Let \(M\) (\(D \times c\)) be the degree-1 block of a specimen: the images of the parameter axes, one per column (EFA and DHA: the cosine and the sine vectors of the first harmonic, \(c = 2\); SPHARM: the parameter axes \(x, y, z\), \(c = 3\)). Its polar decomposition is \(M = Q S\), with \(Q\) having orthonormal columns and \(S = (M^\top M)^{1/2}\) symmetric positive definite. A codomain rotation \(O\) acts as \(Q \to O Q\) and leaves \(S\); a parameter operation acts on degree 1 as \(M \to M A\) with \(A\) orthogonal, which gives \(Q \to Q A\) and \(S \to A^\top S A\). Let \(E\) be the first \(c\) columns of the identity. A coupled operation applies one rotation to both domains so that \(Q = E\) is kept (for SPHARM,
domain="coupled"ofrotate_spharm_coeffs(); for EFA, a start-point shift with the codomain turned by the same angle); it keeps the correspondence between specimens.In 2D a clockwise output, one that keeps a clockwise input’s running direction, has \(\det M < 0\). It is classified from \(M \operatorname{diag}(1, -1)\), and its direction is reported in
directionrather than in the label.degenerate: \(\sigma_{\min}\) is within the uncertainty of the block (below), orsigma_ratio\(= \sigma_{\min} / \sigma_{\max} \le 10^{-12}\), the cut registration itself uses. The first-order ellipse is a segment (the ellipsoid is flat), and \(Q\) is not determined.canonical: \(Q = E\) and \(S\) diagonal with descending entries, the formfirst_orderreturns.coupled: \(Q = E\) and \(S\) not diagonal and descending. One coupled operation takes the specimen to the canonical form. The output ofalign_parameter=Falseis of this kind (canonical when the input’s parameterization is already aligned to its ellipse).uncoupled: \(Q \ne E\). No coupled operation takes the specimen to the canonical form. This is what a per-specimen codomain-only or parameter-only operation applied after registration gives, and also what every specimen of a sample turned by one common rotation gives;populationtells these apart. A left-handed SPHARM block is always of this kind.
The quantities are \(d_Q = \|Q - E\|_F\);
d_s, the Frobenius norm of the off-diagonal part of \(S\) over that of \(S\); anddelta_order, the largest increase between consecutive diagonal entries of \(S\) over the first one (0 when they descend). \(Q = E\) means \(d_Q \le\)tolplus a floor, and \(S\) diagonal and descending meansd_sanddelta_orderboth \(\le\)tolplus a floor. The block is known to \(\epsilon = \mathrm{atol} \sqrt{D c} + \max(D, c)\, u\, \sigma_{\max}\) (\(u\) the unit roundoff), as the cutoffatol + rtol * sigma_maxofscipy.linalg.pinv()reads singular values; the floor of \(d_Q\) is \(\epsilon\) times the condition number of the polar factor, \(1/\sigma_{\min}\) for a \(3 \times 2\) block and \(2 / (\sigma_{c-1} + \sigma_c)\) for a square one (Higham, Functions of Matrices, 2008, Thm. 8.9), and that ofd_sanddelta_orderis \(2 \epsilon / \sigma_{\max}\). A thin \(3 \times 2\) block therefore reads \(Q\) only to \(\epsilon / \sigma_{\min}\).populationjudges the non-degenerate specimens together. With \(\bar Q\) the polar factor of the mean of their \(Q\), each weighted by the inverse square of its tolerance (tolplus its floor), andd_u\(= \max_i \|Q_i - \bar Q\|_F\), the status is"not_uniform"when some \(\|Q_i - \bar Q\|_F\) exceeds its tolerance,"aligned"when \(\bar Q = E\), and"uniform"otherwise: every specimen carries the same orthogonal transformation, as from a convention change applied to the whole sample. With one usable specimen the status is"aligned"or"uniform"; with none it is"empty". For"uniform",sidesays where that transformation acts:"codomain"when every \(S_i\) is diagonal and descending,"parameter"when every \(A_0 S_i A_0^\top\) is (\(A_0 = E^\top \bar Q\)),"either"when both hold (a half turn, a reflection of one axis, or coinciding singular values) and"neither"otherwise; the parameter side requires \(A_0\) to be orthogonal, since a parameter operation gives \(\bar Q = E A\).rotation(\(D \times D\), orthogonal) is then the matrix \(R\) with \(R \bar Q = E\) (in 3D completed by the cross product): left-applied to the coefficient vectors of every specimen, it undoes a common codomain rotation. It is proper except for a SPHARM sample whose specimens are all left-handed (direction-1), which it mirrors back. For a common parameter operation it only expresses that operation on the codomain. In a 2D sample with both directions, a common parameter operation reads as"not_uniform": folding a clockwise specimen by \(\operatorname{diag}(1, -1)\) inverts it (a shift \(A\) becomes \(A^{-1}\)), while a common codomain rotation still reads as uniform.moment.
momentaligns the codomain to the principal axes of the second moment \(\sum_k a_k a_k^\top\) of the non-constant coefficient vectors, and leaves the parameter domain as it is; its outputs are generallyuncoupledin the sense above. Its canonical form is that second moment diagonal with descending entries, read withd_offdiaganddelta_orderasd_sanddelta_orderare read from \(S\), with the floor \(2 \epsilon \|A\|_2 / \|M\|_F\) (\(A\) the non-constant coefficient vectors, \(M\) their second moment, \(\epsilon = \mathrm{atol} \sqrt{D m}\) for \(m\) modes, plus the rounding of the arithmetic). The check sees codomain rotations other than sign changes, and not parameter operations, which leave the second moment unchanged.Examples
>>> from ktch.datasets import load_outline_mosquito_wings >>> from ktch.harmonic import EllipticFourierAnalysis, canonical_form_report >>> outlines = list(load_outline_mosquito_wings().coords[:5]) >>> efa = EllipticFourierAnalysis(n_harmonics=10) >>> coeffs = efa.fit_transform(outlines) >>> report = canonical_form_report(efa, coeffs) >>> report.label.tolist() ['canonical', 'canonical', 'canonical', 'canonical', 'canonical'] >>> report.population.status 'aligned'