File size: 907 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
import math

import torch

from scripts.diagnose_pca_projection_ceiling import target_pca_metrics


def test_target_pca_metrics_uses_covariance_trace_and_mean_offset():
    local_cov = torch.zeros(2, 3, 3)
    local_cov[0, 0, 0] = 4.0
    local_cov[0, 1, 1] = 3.0
    local_cov[1, 2, 2] = 3.0
    target = {
        "local_cov": local_cov,
        "oracle_energy": torch.tensor([4.0, 3.0, 2.0]),
        "oracle_mean_disp": torch.tensor([[3.0, 0.0, 0.0], [1.0, 0.0, 0.0]]),
        "res_mask": torch.ones(2),
    }

    metrics = target_pca_metrics(target, num_modes=2)

    assert metrics["num_res"] == 2
    assert metrics["pca_explained_fraction"] == 0.7
    assert math.isclose(metrics["pca_optimal_relative_rmse"], math.sqrt(0.3), rel_tol=1e-6)
    assert metrics["mean_offset_energy"] == 10.0
    assert metrics["fluctuation_energy"] == 10.0
    assert metrics["mean_fraction_of_total_energy"] == 0.5