.ipynb

Checking the side of 3D curves and surfaces#

A 3D curve’s running direction, and a surface’s disk orientation, decide which side of the shape faces +z after registration: for a leaf, which side is its upper surface. Registration keeps the side that the input’s parameterization gives, because it cannot read the side from the shape or the pose with certainty (see Registration). This guide finds specimens traced the other way round, fixes them, and shows the alternative for data whose side carries no meaning.

import numpy as np

from ktch.datasets import load_outline_leaf_bending
from ktch.harmonic import EllipticFourierAnalysis, flip_distances

coords = list(load_outline_leaf_bending().coords)

To imitate inconsistent digitization, three leaves are traced the other way round:

def turn_over(curve):
    """The curve traced the other way round, from the same first point."""
    return np.roll(curve[::-1], 1, axis=0)


coords_mixed = [turn_over(c) if i in (4, 21, 37) else c for i, c in enumerate(coords)]

Finding the specimens on the other side#

Register as usual, then compare every candidate of each specimen with a template: the registered coefficients of a specimen whose side you have checked, here the first leaf. A specimen nearest a candidate that reverses the running direction (its first two signs differ) is on the other side from the template:

efa = EllipticFourierAnalysis(n_harmonics=20, n_dim=3)
coef = efa.fit_transform(coords_mixed)

result = flip_distances(efa, coef, coef[0])
nearest = result.candidates[result.nearest]
turned = np.flatnonzero(nearest[:, 0] != nearest[:, 1])
turned
array([ 4, 21, 37])

result.distances holds the distance of every candidate, one column per row of result.candidates. With one specimen as the template, the distances also include how much the shapes differ. Compare the candidates of a specimen with each other, not the distances of different specimens:

result.distances[turned].round(2)
array([[0.6 , 0.6 , 0.21, 0.21],
       [0.38, 0.38, 0.31, 0.31],
       [0.64, 0.64, 0.35, 0.36]])

Which side is the right one is biological knowledge: the diagnostic only says which specimens disagree with the template.

Fixing the input#

Once the side is decided, trace those specimens again the other way round. For a 3D curve, reverse the point list and keep its first point. For a disk-parameterized surface (DiskHarmonicAnalysis), negate the angle of its disk coordinates, \(\theta \to -\theta\), in r_theta.

coords_fixed = [turn_over(c) if i in turned else c for i, c in enumerate(coords_mixed)]
coef_fixed = efa.fit_transform(coords_fixed)
np.allclose(coef_fixed, efa.fit_transform(coords))
True

Letting the shape decide the side#

When the side carries no meaning for the question, reversal="align" lets sign_method decide it, and the running direction of the input no longer matters. With sign_method="shape" the side is the one along which the skewness of the curve is positive:

efa_align = EllipticFourierAnalysis(
    n_harmonics=20, n_dim=3, sign_method="shape", reversal="align"
)
coef_align = efa_align.fit_transform(coords)
np.allclose(coef_align, efa_align.fit_transform(coords_mixed))
True

Two cautions:

  • For a nearly flat shape the skewness along the normal is small, and the noise of the data can decide the side. Registering a few specimens digitized twice shows whether it does.

  • The shape can choose, for a whole dataset, the side opposite to the one the inputs give. For these leaves it does: compared with reversal="never", which keeps the digitized side, reversal="align" turns every leaf over. The digitized side is consistent across specimens, but it is not the side of positive skewness.

efa_keep = EllipticFourierAnalysis(
    n_harmonics=20, n_dim=3, sign_method="shape", reversal="never"
)
coef_keep = efa_keep.fit_transform(coords)
changed = ~np.isclose(coef_keep, coef_align).all(axis=1)
int(changed.sum()), len(coords)
(60, 60)

See also