from __future__ import annotations from pathlib import Path import pytest import torch from gnn4colliders.config import Config, load_config from gnn4colliders.data import PackedEvents, pack_events, prepare ROOT = Path(__file__).resolve().parents[1] FIXTURES = ROOT / "tests/fixtures" #: 64-event fixtures of the five GamGam samples, labels 0..4 (ttH..ZH). GAMGAM_FIXTURES = sorted((FIXTURES / "atlas_opendata_gamgam").glob("mc_*_64.root")) TTH_FIXTURE = FIXTURES / "ttH_NLO_64.root" PRETRAINED = ROOT / "Pretrained_GNN" #: ``train.device`` of the fixture runs: ``pytest --device mps`` (default cpu). DEVICE = "cpu" def pytest_addoption(parser: pytest.Parser) -> None: parser.addoption("--device", default="cpu", help="train.device of fixture runs") def pytest_configure(config: pytest.Config) -> None: global DEVICE DEVICE = config.getoption("--device") def random_events(count: int = 40, *, seed: int = 0, classes: int = 3) -> PackedEvents: generator = torch.Generator().manual_seed(seed) objects = [ torch.rand((int(n), 7), generator=generator) for n in torch.randint(1, 6, (count,), generator=generator) ] return pack_events(objects, torch.arange(count) % classes) def fixture_config( name: str, files: list[Path], labels: list[int], tmp_path: Path, *overrides: str ) -> Config: """``configs/.yaml`` on local fixture files, on ``DEVICE``, in tmp_path.""" entries = [ {"path": str(path), "label": label} for path, label in zip(files, labels) ] return load_config( ROOT / "configs" / f"{name}.yaml", [ f"data.files={entries}", "data.hf_repo=null", f"data.cache={tmp_path}/events.pt", "data.prepare_workers=0", "data.loader_workers=0", "data.pin_memory=false", "data.batch_size=64", f"train.device={DEVICE}", f"run_dir={tmp_path}/run", *overrides, ], ) @pytest.fixture def gamgam(tmp_path: Path) -> Config: """The GamGam config on its prepared fixtures.""" config = fixture_config("gamgam", GAMGAM_FIXTURES, list(range(5)), tmp_path) prepare(config.data) return config