"""Unit tests for build_lap_uniform.py's file picking and chunk draw (python -m unittest test_build_lap_uniform). Needs numpy and the Loong source tree (build_lap_selected imports it lazily; nothing here downloads).""" import math from pathlib import Path import sys import unittest import numpy as np sys.path.insert(0, str(Path(__file__).resolve().parent)) import build_lap_uniform as uniform # noqa: E402 def rows(layout): """layout: {source: [(task, split), ...]} -> episode rows as episode_rows returns them (split-group only).""" out = [] for source, episodes in layout.items(): for task, split in episodes: out.append({"source": source, "task": task, "split": split, "uuid": f"{source}/{len(out)}"}) return out class QuotaTest(unittest.TestCase): def test_quotas_are_proportional_and_exact(self): self.assertEqual(sum(uniform.QUOTAS.values()), 750_000) pool = sum(uniform.TRAIN_CANDIDATES.values()) for name, quota in uniform.QUOTAS.items(): self.assertLess(abs(quota - 750_000 * uniform.TRAIN_CANDIDATES[name] / pool), 1) self.assertNotIn("agibot_beta", uniform.QUOTAS) class PickFilesTest(unittest.TestCase): def test_every_task_reaches_its_fraction_and_val_only_files_follow(self): layout = {f"t{t}-{f}": [(f"task{t}", "train")] * 10 for t in range(4) for f in range(6)} layout["shared"] = [("task0", "train")] * 5 + [("task0", "val")] * 2 layout.update({f"val-{f}": [("task9", "val")] * 6 for f in range(6)}) data = rows(layout) picked, coverage = uniform.pick_files(data, 1 / 3, np.random.default_rng(0)) for task, (have, total) in coverage.items(): self.assertGreaterEqual(have, math.ceil(total / 3), task) val_only = [s for s in picked if s.startswith("val-")] self.assertEqual(len(val_only), 2) # 12 of 36 val-only episodes again, _ = uniform.pick_files(data, 1 / 3, np.random.default_rng(0)) self.assertEqual(picked, again) other, _ = uniform.pick_files(data, 1 / 3, np.random.default_rng(1)) self.assertNotEqual(picked, other) def test_a_task_in_one_file_is_taken_whole(self): picked, coverage = uniform.pick_files(rows({"a": [("small", "train")] * 3, "b": [("big", "train")] * 30, "c": [("big", "train")] * 30, "d": [("big", "train")] * 30}), 1 / 3, np.random.default_rng(0)) self.assertIn("a", picked) self.assertEqual(coverage["small"], (3, 3)) self.assertEqual(coverage["big"], (30, 90)) class DrawUniformTest(unittest.TestCase): def test_counts_per_episode_follow_the_rate_and_total(self): episode = np.repeat(np.arange(50), np.arange(10, 60)) # 50 episodes of 10..59 candidates train = np.ones(len(episode), bool) train[episode >= 40] = False val = ~train chosen = uniform.draw_uniform(episode, [train, val], 0.355, np.random.default_rng(0)) self.assertEqual(len(chosen), len(set(chosen.tolist()))) counts = np.bincount(episode[chosen], minlength=50) sizes = np.bincount(episode) self.assertTrue((np.abs(counts - 0.355 * sizes) < 1).all()) self.assertEqual(int(train[chosen].sum()), round(0.355 * train.sum())) def test_draw_is_uniform_within_episodes(self): episode = np.zeros(20, int) hits = np.zeros(20) rng = np.random.default_rng(0) for _ in range(4000): hits[uniform.draw_uniform(episode, [np.ones(20, bool)], 0.25, rng)] += 1 self.assertLess(np.abs(hits / 4000 - 0.25).max(), 0.03) if __name__ == "__main__": unittest.main()