File size: 1,239 Bytes
24599ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import torch

from bbflow.models.latent import LatentEncoder
from scripts.probe_condition_ensemble_cov import align_to_reference as probe_align_to_reference


def _known_rotation_case():
    reference = torch.tensor(
        [
            [0.0, 0.0, 0.0],
            [2.0, 0.0, 0.0],
            [0.0, 1.0, 0.0],
            [0.0, 0.0, 3.0],
            [1.0, 2.0, 4.0],
        ],
        dtype=torch.float32,
    )
    rotation = torch.tensor(
        [[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]],
        dtype=torch.float32,
    )
    translation = torch.tensor([4.0, -3.0, 2.0])
    moving = reference @ rotation + translation
    mask = torch.ones(reference.shape[0])
    return reference, moving, mask


def test_latent_kabsch_recovers_known_row_vector_rotation():
    reference, moving, mask = _known_rotation_case()
    aligned = LatentEncoder.align_to_reference(moving[None], reference, mask)
    assert torch.allclose(aligned[0], reference, atol=1e-5, rtol=1e-5)


def test_probe_kabsch_recovers_known_row_vector_rotation():
    reference, moving, mask = _known_rotation_case()
    aligned = probe_align_to_reference(moving[None], reference, mask)
    assert torch.allclose(aligned[0], reference, atol=1e-5, rtol=1e-5)