import numpy as np import pytest import frozen import pooled from pooled import power_check, run_one, views def _labels(t: int = 4000, seed: int = 0): """A regime label sequence with the world's own 40-80 step block structure.""" rng = np.random.default_rng(seed) lab, sw, g = [], [], 0 while len(lab) < t: g = int(rng.choice([i for i in range(4) if i != g])) n = int(rng.integers(40, 81)) sw += [True] + [False] * (n - 1) lab += [g] * n return np.array(lab[:t]), sw[:t] def test_views_preserve_the_representation(): """Pooling must aggregate the entities it claims to, in a stable order.""" rep = np.random.default_rng(0).normal(size=(50, 16, 32)) v = views(rep) assert v["single"] is rep assert v["mean"].shape == (50, 1, 32) assert v["concat"].shape == (50, 1, 16 * 32) assert np.allclose(v["mean"][:, 0], rep.mean(1)) for i in range(16): # concat block i is always particle i assert np.array_equal(v["concat"][:, 0, i * 32:(i + 1) * 32], rep[:, i]) def test_single_view_reproduces_the_frozen_probe_exactly(): """The `single` row must be the published baseline, not a re-implementation of it. Same organism, same world, same estimator: bit-identical or the comparison this file draws against `results/frozen.md` is not against anything. """ kw = dict(train_steps=300, frozen_steps=300) mine = run_one(seed=0, **kw)["probes"] theirs = frozen.run_one("grp_k0", seed=0, **kw)["probes"] for tag in ("M", "h"): assert mine[f"{tag}_single_lsq"]["acc_entity"] == theirs[f"{tag}_lsq"]["acc_entity"] assert mine[f"{tag}_single_lsq"]["chance_shift"] == theirs[f"{tag}_lsq"]["chance_shift"] def test_power_check_finds_a_code_that_is_only_in_the_pool(): """The positive control: hypothesis (b) made true by construction. In `distributed` no entity holds more than one of the regime's two bits, so a per-entity probe caps near 0.5 while the pool holds both. If this pipeline could not find that, a null on the real data would carry no information. """ lab, sw = _labels() pc = power_check(lab, sw, seed=0) gap = lambda k: pc[k]["acc_entity"] - pc[k]["chance_shift"] assert gap("distributed_single") < 0.30 assert gap("distributed_mean") > 0.60 assert gap("distributed_concat") > 0.60 # and pooling lifts a merely redundant code too -- the confound the `h` row controls assert gap("redundant_mean") - gap("redundant_single") > 0.20 def test_the_power_check_can_fail(monkeypatch): """Break the pooling on purpose; the positive control must notice. `views` is replaced by one whose `mean` and `concat` read entity 0 alone -- the shape of every downstream call is unchanged, only the aggregation is gone. A test that still passed here would be defending nothing. """ def broken(rep): t, n, d = rep.shape one = rep[:, :1] return {"single": rep, "mean": one, "concat": np.tile(one, (1, 1, n)).reshape(t, 1, n * d)} monkeypatch.setattr(pooled, "views", broken) lab, sw = _labels() pc = power_check(lab, sw, seed=0) gap = lambda k: pc[k]["acc_entity"] - pc[k]["chance_shift"] with pytest.raises(AssertionError): assert gap("distributed_mean") > 0.60