Download bbflow_base/tests/test_template_prior_diagnostics.py from hanghang214/data_transfer: direct link, hf CLI and curl.
- Browser
- Download file 7.79 kB
-
https://huggingface.co/datasets/hanghang214/data_transfer/resolve/main/bbflow_base/tests/test_template_prior_diagnostics.py
- Command line
-
hf download hf://datasets/hanghang214/data_transfer/bbflow_base/tests/test_template_prior_diagnostics.py
-
curl -L -o test_template_prior_diagnostics.py https://huggingface.co/datasets/hanghang214/data_transfer/resolve/main/bbflow_base/tests/test_template_prior_diagnostics.py
7.79 kB
| import pytest | |
| import torch | |
| from bbflow.models.latent import LatentEncoder | |
| class _Cfg: | |
| dim = 16 | |
| num_modes = 1 | |
| motion_hidden_dim = 16 | |
| motion_dist_bins = 4 | |
| motion_dist_width = 4.0 | |
| motion_message_passing_layers = 1 | |
| template_residual_scale = 0.1 | |
| template_remove_rotation = True | |
| def test_remove_rigid_component_clears_known_translation_and_rotation(): | |
| encoder = LatentEncoder(_Cfg()) | |
| coords = torch.tensor([[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0]]]) | |
| mask = torch.ones(1, 4) | |
| centered = coords - coords.mean(dim=1, keepdim=True) | |
| omega = torch.tensor([0.0, 0.0, 2.0]) | |
| translation = torch.tensor([0.5, -0.25, 0.75]) | |
| rigid = torch.cross(omega[None, None, :].expand_as(centered), centered, dim=-1) + translation[None, None, :] | |
| u = rigid[:, :, None, :] | |
| before = encoder.rigid_component_metrics({"u_global": u}, coords, mask) | |
| cleaned = encoder._remove_rigid_component(u, centered, mask) | |
| after = encoder.rigid_component_metrics({"u_global": cleaned}, coords, mask) | |
| assert before["rigid_translation_abs_max"] > 1e-2 | |
| assert before["rigid_rotation_omega_abs_max"] > 1e-2 | |
| assert after["rigid_translation_abs_max"] < 1e-4 | |
| assert after["rigid_rotation_omega_abs_max"] < 1e-3 | |
| def test_motion_alignment_metrics_reports_far_sign_agreement(): | |
| encoder = LatentEncoder(_Cfg()) | |
| condition = {"cross_cov_pred": torch.tensor([[[0.0, 1.0, -2.0], [1.0, 0.0, -3.0], [-2.0, -3.0, 0.0]]])} | |
| targets = {"cross_cov": torch.tensor([[[0.0, 2.0, -1.0], [2.0, 0.0, -4.0], [-1.0, -4.0, 0.0]]])} | |
| mask = torch.ones(1, 3) | |
| dists = torch.tensor([[[0.0, 20.0, 20.0], [20.0, 0.0, 20.0], [20.0, 20.0, 0.0]]]) | |
| metrics = encoder.motion_alignment_metrics(condition, targets, mask, dists) | |
| assert metrics["cross_cov_far_top10_sign_agree"] == 1.0 | |
| assert metrics["cross_cov_far_corr"] > 0.9 | |
| def test_template_prior_is_equivariant_under_global_rotation(): | |
| torch.manual_seed(7) | |
| cfg = _Cfg() | |
| cfg.num_modes = 2 | |
| encoder = LatentEncoder(cfg) | |
| trans = torch.tensor([[[0.0, 0.0, 0.0], [1.2, 0.1, 0.0], [0.3, 1.1, 0.2], [1.0, 1.2, 0.5], [0.2, 0.4, 1.3]]]) | |
| rotmats = torch.eye(3).reshape(1, 1, 3, 3).repeat(1, trans.shape[1], 1, 1) | |
| seq = torch.zeros(1, trans.shape[1], 21) | |
| seq[0, torch.arange(trans.shape[1]), torch.arange(trans.shape[1]) % 21] = 1.0 | |
| mask = torch.ones(1, trans.shape[1]) | |
| rotation = torch.tensor([[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]) | |
| condition, _ = encoder.forward_prior(trans, rotmats, seq, mask) | |
| trans_rot = torch.einsum("ij,bnj->bni", rotation, trans) | |
| rotmats_rot = torch.einsum("ij,bnjk->bnik", rotation, rotmats) | |
| condition_rot, _ = encoder.forward_prior(trans_rot, rotmats_rot, seq, mask) | |
| expected_u = torch.einsum("ij,bnkj->bnki", rotation, condition["u_global"].float()) | |
| expected_center = torch.einsum("ij,bkj->bki", rotation, condition["template_center"].float()) | |
| expected_omega = torch.einsum("ij,bkj->bki", rotation, condition["template_omega"].float()) | |
| expected_velocity = torch.einsum("ij,bkj->bki", rotation, condition["template_velocity"].float()) | |
| assert torch.allclose(condition_rot["u_global"].float(), expected_u, atol=1e-4, rtol=1e-4) | |
| assert torch.allclose(condition_rot["template_center"].float(), expected_center, atol=1e-5, rtol=1e-5) | |
| assert torch.allclose(condition_rot["template_omega"].float(), expected_omega, atol=1e-5, rtol=1e-5) | |
| assert torch.allclose(condition_rot["template_velocity"].float(), expected_velocity, atol=1e-5, rtol=1e-5) | |
| def test_directional_node_condition_is_local_frame_rotation_invariant(): | |
| torch.manual_seed(11) | |
| cfg = _Cfg() | |
| cfg.num_modes = 2 | |
| encoder = LatentEncoder(cfg) | |
| trans = torch.tensor([[[0.0, 0.0, 0.0], [1.2, 0.1, 0.0], [0.3, 1.1, 0.2], [1.0, 1.2, 0.5], [0.2, 0.4, 1.3]]]) | |
| rotmats = torch.eye(3).reshape(1, 1, 3, 3).repeat(1, trans.shape[1], 1, 1) | |
| seq = torch.zeros(1, trans.shape[1], 21) | |
| seq[0, torch.arange(trans.shape[1]), torch.arange(trans.shape[1]) % 21] = 1.0 | |
| mask = torch.ones(1, trans.shape[1]) | |
| rotation = torch.tensor([[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]) | |
| condition, _ = encoder.forward_prior(trans, rotmats, seq, mask) | |
| trans_rot = torch.einsum("ij,bnj->bni", rotation, trans) | |
| rotmats_rot = torch.einsum("ij,bnjk->bnik", rotation, rotmats) | |
| condition_rot, _ = encoder.forward_prior(trans_rot, rotmats_rot, seq, mask) | |
| assert condition["disp_node_cond"].shape == (1, trans.shape[1], 3) | |
| assert condition["disp_edge_cond"].shape == (1, trans.shape[1], trans.shape[1], 1) | |
| assert torch.allclose( | |
| condition_rot["disp_node_cond"].float(), | |
| condition["disp_node_cond"].float(), | |
| atol=1e-4, | |
| rtol=1e-4, | |
| ) | |
| assert torch.allclose( | |
| condition_rot["disp_edge_cond"].float(), | |
| condition["disp_edge_cond"].float(), | |
| atol=1e-4, | |
| rtol=1e-4, | |
| ) | |
| def test_enm_features_are_sign_safe_and_rotation_invariant(): | |
| cfg = _Cfg() | |
| cfg.enm_num_modes = 2 | |
| cfg.enm_cutoff = 3.0 | |
| encoder = LatentEncoder(cfg) | |
| trans = torch.tensor([[[0.0, 0.0, 0.0], [1.0, 0.2, 0.0], [0.1, 1.1, 0.3], [1.1, 1.0, 0.5], [0.4, 0.3, 1.2]]]) | |
| rotmats = torch.eye(3).reshape(1, 1, 3, 3).repeat(1, trans.shape[1], 1, 1) | |
| mask = torch.ones(1, trans.shape[1]) | |
| rotation = torch.tensor([[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]) | |
| node_feat = encoder._enm_node_features(trans, rotmats, mask) | |
| edge_feat = encoder._enm_pair_features(trans, mask) | |
| trans_rot = torch.einsum("ij,bnj->bni", rotation, trans) | |
| rotmats_rot = torch.einsum("ij,bnjk->bnik", rotation, rotmats) | |
| node_feat_rot = encoder._enm_node_features(trans_rot, rotmats_rot, mask) | |
| edge_feat_rot = encoder._enm_pair_features(trans_rot, mask) | |
| assert list(node_feat.shape) == [1, trans.shape[1], cfg.enm_num_modes] | |
| assert list(edge_feat.shape) == [1, trans.shape[1], trans.shape[1], cfg.enm_num_modes] | |
| assert torch.isfinite(node_feat).all() | |
| assert torch.isfinite(edge_feat).all() | |
| assert node_feat.abs().sum() > 0 | |
| assert edge_feat.abs().sum() > 0 | |
| assert torch.allclose(node_feat_rot, node_feat, atol=1e-4, rtol=1e-4) | |
| assert torch.allclose(edge_feat_rot, edge_feat, atol=1e-4, rtol=1e-4) | |
| def test_enm_forward_features_are_forbidden(): | |
| cfg = _Cfg() | |
| cfg.use_enm_features = True | |
| with pytest.raises(ValueError, match="ENM cannot be used as a forward feature"): | |
| LatentEncoder(cfg) | |
| def test_enm_warmup_loss_aligns_direction_subspace_not_ordered_modes(): | |
| cfg = _Cfg() | |
| cfg.num_modes = 2 | |
| cfg.enm_num_modes = 2 | |
| cfg.enm_cutoff = 3.0 | |
| cfg.template_remove_rotation = False | |
| encoder = LatentEncoder(cfg) | |
| trans = torch.tensor([[[0.0, 0.0, 0.0], [1.0, 0.2, 0.0], [0.1, 1.1, 0.3], [1.1, 1.0, 0.5], [0.4, 0.3, 1.2]]]) | |
| mask = torch.ones(1, trans.shape[1]) | |
| targets = encoder._enm_motion_targets(trans, mask) | |
| enm = targets["enm_modes"] | |
| rotation_in_mode_space = torch.tensor([[0.0, -1.0], [1.0, 0.0]]) | |
| rotated_enm = torch.einsum("bnkd,kl->bnld", enm, rotation_in_mode_space) | |
| condition = { | |
| "u_global": rotated_enm, | |
| "local_cov_pred": targets["local_cov"], | |
| "cross_cov_pred": targets["cross_cov"], | |
| "rmsf_pred": targets["rmsf"], | |
| } | |
| loss = encoder.enm_initialization_loss(condition, trans, mask) | |
| assert loss["enm_warmup_loss"] < 1e-5 | |
| assert loss["enm_warmup_subspace_loss"] < 1e-5 | |
| assert loss["enm_warmup_subspace_overlap"] > 0.999 | |
| assert loss["enm_warmup_cross_cov_far_top10_sign_agree"] >= 0.0 | |