File size: 4,051 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import torch

from bbflow.models.embedder import Embedder
from bbflow.models.latent import LatentEncoder


def test_frame_condition_can_skip_unused_statistics_without_changing_injection():
    class Cfg:
        dim = 16
        num_modes = 3
        mixture_components = 2
        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
        q_lstsq_ridge = 1e-4

    encoder = LatentEncoder(Cfg())
    modes = torch.randn(2, 5, 3, 3)
    coefficients = torch.randn(2, 3)
    rotations = torch.eye(3).reshape(1, 1, 3, 3).repeat(2, 5, 1, 1)
    mask = torch.ones(2, 5)

    full = encoder.condition_from_mode_coefficients(
        modes, coefficients, rotations, mask, include_statistics=True
    )
    lean = encoder.condition_from_mode_coefficients(
        modes, coefficients, rotations, mask, include_statistics=False
    )

    for key in ("node_cond", "edge_cond", "disp_node_cond", "disp_edge_cond"):
        assert torch.allclose(lean[key], full[key])
    for key in ("local_cov_pred", "cross_cov_pred", "rmsf_pred"):
        assert key not in lean


def test_cross_concat_expand_path_matches_materialized_tile_path():
    feats = torch.randn(2, 5, 7)
    expected = torch.cat(
        [
            torch.tile(feats[:, :, None, :], (1, 1, 5, 1)),
            torch.tile(feats[:, None, :, :], (1, 5, 1, 1)),
        ],
        dim=-1,
    ).float().reshape(2, 25, -1)

    actual = Embedder._cross_concat(None, feats, 2, 5)

    assert torch.equal(actual, expected)

def test_equilibrium_distance_cache_reuses_fixed_structure():
    from types import SimpleNamespace

    from bbflow.models.bbflow_module import BBFlowModule

    holder = SimpleNamespace(equilibrium_distance_cache={})
    coords = torch.randn(2, 6, 3)
    names = ["protein_a", "protein_b"]

    first = BBFlowModule._equilibrium_spatial_dists(holder, names, coords)
    second = BBFlowModule._equilibrium_spatial_dists(holder, names, coords.clone())

    assert torch.allclose(first, torch.cdist(coords, coords))
    assert torch.equal(second, first)
    assert len(holder.equilibrium_distance_cache) == 2

def test_corrupt_batch_shallow_copy_does_not_mutate_input_tensors():
    from types import MethodType

    from bbflow.data.interpolant_bbflow import InterpolantBBFlow

    interpolant = object.__new__(InterpolantBBFlow)
    interpolant._device = torch.device("cpu")

    def sample_from_prior(self, batch_size, num_res, trans_equilibrium, rotmats_equilibrium):
        return torch.zeros_like(trans_equilibrium), torch.eye(3).reshape(1, 1, 3, 3).repeat(batch_size, num_res, 1, 1)

    def corrupt_trans(self, trans_1, trans_0, t, res_mask):
        return trans_1 + trans_0 + t[..., None]

    def corrupt_rotmats(self, rotmats_1, rotmats_0, t, res_mask):
        return rotmats_1 + 0.0 * rotmats_0 + 0.0 * t[..., None]

    interpolant.sample_from_prior = MethodType(sample_from_prior, interpolant)
    interpolant._corrupt_trans = MethodType(corrupt_trans, interpolant)
    interpolant._corrupt_rotmats = MethodType(corrupt_rotmats, interpolant)

    batch = {
        "trans_equilibrium": torch.randn(2, 4, 3),
        "rotmats_equilibrium": torch.eye(3).reshape(1, 1, 3, 3).repeat(2, 4, 1, 1),
        "trans_1": torch.randn(2, 4, 3),
        "rotmats_1": torch.eye(3).reshape(1, 1, 3, 3).repeat(2, 4, 1, 1),
        "res_mask": torch.ones(2, 4),
        "pdb_name": ["a", "b"],
    }
    before = {key: value.clone() for key, value in batch.items() if torch.is_tensor(value)}

    out = interpolant.corrupt_batch(batch, t=torch.full((2, 1), 0.25))

    assert "t" not in batch
    assert "trans_t" not in batch
    assert "rotmats_t" not in batch
    for key, value in before.items():
        assert torch.equal(batch[key], value)
    assert out["trans_1"] is batch["trans_1"]
    assert torch.allclose(out["trans_t"], batch["trans_1"] + 0.25)
    assert torch.allclose(out["rotmats_t"], batch["rotmats_1"])