File size: 11,587 Bytes
680a0a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python3
# MIT License, Bryan Cheng, 2026. Part of PANGEA/READOUT.
"""The registry contract, checked across every family at once.

This file exists because of a gap that each family's own tests could not see. Families F
and L emitted per-base-pair profiles -- the G4Hunter track, the i-motif track, the Z-DNA
step energy, and the RNA unpaired-probability and positional-entropy tracks -- that were
never declared in the channel registry. Families C and E had per-family contract tests
and so were clean; D, F and L had none.

The consequence was not cosmetic. `scripts/generate_targets.py` builds the column set of
the training data **from the registry**, so seven real per-base-pair tracks would have
been silently absent from every training example, and nothing in the pipeline would have
reported a problem. The declared-but-not-emitted direction is worse still: it would put
a permanently-NaN column into the training set.

So the contract is checked here in both directions, over all six families together,
against the single entry point every consumer uses.
"""
from __future__ import annotations

import os
import sys

import numpy as np
import pytest

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "src"))
sys.path.insert(0, os.path.join(ROOT, "src", "physics"))

import landscape as LS  # noqa: E402
from channels import REGISTRY, Parity, Resolution  # noqa: E402

# A 61 bp fragment, the length every sequence in library_v2 has.
SEQ = "GCGCAAATTTGCGCATATAGCGCAAATTTGCGCCCCTCCCTCCCTCCCAAATTTGCGCATA"
assert len(SEQ) == 61


@pytest.fixture(scope="module")
def computed():
    return LS.compute_landscape(SEQ, n_ensemble=0)


def test_every_emitted_channel_is_declared(computed):
    prof, glob = computed
    declared = {s.name for s in REGISTRY.specs()}
    undeclared = sorted((set(prof) | set(glob)) - declared)
    assert not undeclared, (
        f"emitted but never declared, so the target generator would drop them: "
        f"{undeclared}")


def test_every_declared_channel_is_emitted(computed):
    prof, glob = computed
    emitted = set(prof) | set(glob)
    missing = sorted({s.name for s in REGISTRY.specs() if s.family in LS.FAMILIES}
                     - emitted)
    assert not missing, (
        f"declared but never emitted, so these would be permanently-NaN columns in the "
        f"training set: {missing}")


def test_declared_resolution_matches_where_the_channel_appears(computed):
    prof, glob = computed
    for s in REGISTRY.specs():
        if s.family not in LS.FAMILIES:
            continue
        if s.resolution is Resolution.PER_BP:
            assert s.name in prof, f"{s.name} declared PER_BP but is a scalar"
            assert s.name not in glob, f"{s.name} is emitted at two resolutions"
        elif s.resolution is Resolution.GLOBAL:
            assert s.name in glob, f"{s.name} declared GLOBAL but is a profile"
            assert s.name not in prof, f"{s.name} is emitted at two resolutions"


def test_no_duplicate_channel_names_across_families():
    names = [s.name for s in REGISTRY.specs()]
    assert len(names) == len(set(names))


def test_every_channel_name_is_prefixed_by_its_family():
    for s in REGISTRY.specs():
        assert s.name.startswith(f"{s.family}_"), (
            f"{s.name} is declared in family {s.family} but is not prefixed by it")


def test_every_production_channel_names_a_source():
    """A channel fit to be a training target has to say where its parameters came from."""
    for s in REGISTRY.specs():
        if s.family in LS.FAMILIES and not s.provisional:
            assert s.source, f"{s.name} is a production channel with no source"


def test_every_provisional_channel_says_why():
    for s in REGISTRY.specs():
        if s.provisional:
            assert len(s.provisional_reason) > 40, s.name


# ======================================================================================
# The packed arrays the generator writes
# ======================================================================================

def test_column_order_is_deterministic():
    """Column order must not depend on import order, or a dataset silently transposes."""
    a = LS.channel_names(Resolution.PER_BP)
    b = LS.channel_names(Resolution.PER_BP)
    assert a == b == sorted(a)


def test_packed_arrays_have_the_declared_shape():
    pb = LS.channel_names(Resolution.PER_BP)
    gl = LS.channel_names(Resolution.GLOBAL)
    P, G = LS.landscape_arrays(SEQ, pb, gl, n_ensemble=0)
    assert P.shape == (len(pb), len(SEQ)), P.shape
    assert G.shape == (len(gl),), G.shape
    assert P.dtype == np.float32 and G.dtype == np.float32


def test_packed_arrays_agree_with_the_dict_form():
    pb = LS.channel_names(Resolution.PER_BP)
    gl = LS.channel_names(Resolution.GLOBAL)
    prof, glob = LS.compute_landscape(SEQ, n_ensemble=0)
    P, G = LS.landscape_arrays(SEQ, pb, gl, n_ensemble=0)
    for i, name in enumerate(pb[:20]):
        np.testing.assert_allclose(P[i], np.asarray(prof[name], dtype=np.float32),
                                   rtol=1e-6, atol=1e-6)
    for i, name in enumerate(gl[:20]):
        a, b = G[i], np.float32(glob[name])
        assert (np.isnan(a) and np.isnan(b)) or abs(a - b) <= 1e-5 * max(abs(b), 1.0)


def test_most_channels_are_not_all_nan_on_a_real_fragment():
    """A fragment should light up most of the landscape.

    Without a minimised structure, and with the ensemble off, a known set of channels is
    legitimately NaN: the backbone torsions, the hydrogen-bond distances, the ensemble
    standard deviations, sugar pucker, and the salt and temperature condition deltas. The
    bound below is loose on purpose -- the point is to catch a family that has stopped
    computing anything at all, which a per-family test would not notice if the family
    were simply never called.
    """
    pb = LS.channel_names(Resolution.PER_BP)
    gl = LS.channel_names(Resolution.GLOBAL)
    P, G = LS.landscape_arrays(SEQ, pb, gl, n_ensemble=0)
    live_p = float(np.mean([np.isfinite(row).any() for row in P]))
    live_g = float(np.mean(np.isfinite(G)))
    assert live_p > 0.6, f"only {live_p:.0%} of per-bp channels produced any value"
    assert live_g > 0.8, f"only {live_g:.0%} of global channels produced a value"


def test_every_family_contributes_channels(computed):
    prof, glob = computed
    emitted = set(prof) | set(glob)
    for fam in LS.FAMILIES:
        got = [n for n in emitted if n.startswith(f"{fam}_")]
        assert got, f"family {fam} contributed nothing"


def test_the_registry_is_the_size_the_plan_implies():
    """PLAN.md budgets 215 + 98 + 90 + 85 + 53 + 60 for C, B, D, E, F, L.

    Family B is deliberately short: only the GFN2 tier has been computed, and the SAPT
    decomposition, the DFT tiers, transfer integrals, molecular electrostatic potentials
    and pKa shifts are not declared at all rather than declared empty.
    """
    counts = {f: len(REGISTRY.by_family(f)) for f in LS.FAMILIES}
    assert counts["C"] == 212 and counts["E"] == 89 and counts["D"] == 52
    assert counts["F"] == 63 and counts["L"] == 78
    assert counts["B"] == 34, (
        f"Family B has {counts['B']} channels against the plan's 98; if the DFT or SAPT "
        "tiers have been added this bound is stale")


# ======================================================================================
# Import order
# ======================================================================================

def test_importing_landscape_alone_populates_the_registry():
    """The registry must be complete the moment `landscape` is importable.

    Every family module registers its channels as a side effect of being imported. When
    `landscape` imported them lazily inside `compute_landscape`, a caller that asked for
    the column set *first* -- which is precisely what the target generator does -- saw an
    empty registry. The generator built a dataset with zero channels, wrote it to disk,
    and reported success. Nothing raised.

    This runs the check in a subprocess so it is a genuine fresh interpreter: inside this
    test session every family has already been imported by other tests, which is exactly
    the condition that would hide the bug.
    """
    import subprocess
    code = (
        "import sys; sys.path.insert(0, %r); sys.path.insert(0, %r)\n"
        "import landscape as LS\n"
        "from channels import Resolution\n"
        "p = len(LS.channel_names(Resolution.PER_BP))\n"
        "g = len(LS.channel_names(Resolution.GLOBAL))\n"
        "print(p, g)\n"
        "assert p > 200 and g > 250, (p, g)\n"
        % (os.path.join(ROOT, "src", "physics"), os.path.join(ROOT, "src"))
    )
    r = subprocess.run([sys.executable, "-W", "ignore", "-c", code],
                       capture_output=True, text=True)
    assert r.returncode == 0, (
        "importing `landscape` alone did not populate the channel registry:\n"
        f"stdout={r.stdout}\nstderr={r.stderr[-800:]}")


def test_generator_refuses_an_empty_channel_set(monkeypatch):
    """The generator's second line of defence against the same failure."""
    sys.path.insert(0, os.path.join(ROOT, "src", "data"))
    import generate_targets as gt

    monkeypatch.setattr(gt.LS, "channel_names", lambda *a, **k: [])
    monkeypatch.setattr(gt, "read_library", lambda *a, **k: (["x"], ["ACGT" * 15], ["s"]))
    monkeypatch.setattr(sys, "argv",
                        ["generate_targets.py", "--library", "x", "--out", "y"])
    with pytest.raises(RuntimeError, match="registry is empty or partial"):
        gt.main()


# ======================================================================================
# The split has to actually handle what the QC gate waves through
# ======================================================================================

def test_similarity_split_colocates_identical_sequences():
    """The QC gate downgrades duplicate sequences from a problem to a warning on the
    grounds that the similarity split assigns identical sequences to the same side.
    That claim is load-bearing, so it is tested rather than asserted in a message.

    Three sequences in the 191,344-fragment library appear twice, where the Drosophila
    genomic panel and the DeepSTARR MPRA elements reach the same locus by different
    routes. Under a random split those would leak; under the similarity split they must
    not.
    """
    sys.path.insert(0, os.path.join(ROOT, "src", "data"))
    sys.path.insert(0, os.path.join(ROOT, "src", "model"))
    import splits as sp

    rng = np.random.default_rng(0)
    base = ["".join(rng.choice(list("ACGT"), 61)) for _ in range(40)]
    # Plant three exact duplicates, as the real library contains.
    seqs = base + [base[3], base[17], base[29]]

    labels, _ = sp.group_by_similarity(seqs, threshold=0.5, seed=0)
    for original, copy in ((3, 40), (17, 41), (29, 42)):
        assert labels[original] == labels[copy], (
            f"identical sequences at {original} and {copy} landed in different groups, "
            "so the similarity split would leak them between train and validation and "
            "the QC gate's warning-not-problem treatment of duplicates is unsound")

# `test_similarity_split_puts_whole_groups_on_one_side` lives in tests/test_pangea.py:
# it imports `train`, which imports torch, and this file runs in the physics environment
# where torch is deliberately absent. The grouping half of the claim is tested above,
# which needs only `splits` and numpy.