Datasets:
Formats:
parquet
Size:
100K - 1M
Tags:
population-genetics
demographic-inference
effective-population-size
coalescent
msprime
simulation
License:
File size: 6,586 Bytes
ee5b44c | 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 | from __future__ import annotations
import sys
import pytest
from simulator import cli
from simulator.config import Config
from simulator.dataset_state import DatasetConfigMismatchError
from simulator.empirical_maps import EmpiricalMapStore
from simulator.shards import (
DatasetGenerationIncompleteError,
DatasetGenerationLockedError,
dataset_output_lock,
)
def test_cli_reports_completed_dataset_mismatch_without_traceback(
monkeypatch: pytest.MonkeyPatch,
tmp_path,
) -> None:
cfg = Config(out_dir=str(tmp_path), recomb_map="", mut_map="")
monkeypatch.setattr(cli, "parse_args", lambda: cfg)
monkeypatch.setattr(cli, "load_empirical_maps", lambda _cfg: EmpiricalMapStore())
def reject_reuse(_cfg, _empirical, **_kwargs):
raise DatasetConfigMismatchError("configuration changed; rerun with --force")
monkeypatch.setattr(cli, "generate_dataset", reject_reuse)
with pytest.raises(SystemExit, match=r"^\[error\].*--force$"):
cli.main()
def test_cli_rejects_enabled_empirical_source_when_maps_are_unavailable(
monkeypatch: pytest.MonkeyPatch,
tmp_path,
) -> None:
cfg = Config(
out_dir=str(tmp_path),
recomb_map="",
mut_map="",
p_clean_constant=0.0,
p_random_global=0.0,
p_custom_hetero=0.0,
p_empirical_slice=1.0,
p_stdpopsim_anchor=0.0,
)
monkeypatch.setattr(cli, "parse_args", lambda: cfg)
monkeypatch.setattr(cli, "load_empirical_maps", lambda _cfg: EmpiricalMapStore())
with pytest.raises(
SystemExit,
match="refusing to disable the requested empirical source",
):
cli.main()
def test_unknown_preset_is_an_argparse_error_without_traceback(
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
monkeypatch.setattr(sys, "argv", ["dlcoalsim", "--preset", "unknown"])
with pytest.raises(SystemExit) as exc_info:
cli.parse_args()
assert exc_info.value.code == 2
stderr = capsys.readouterr().err
assert "Unknown preset" in stderr
assert "Traceback" not in stderr
def test_unusable_output_path_has_a_clean_cli_error(
monkeypatch: pytest.MonkeyPatch,
tmp_path,
) -> None:
parent_file = tmp_path / "not-a-directory"
parent_file.write_text("occupied", encoding="utf-8")
cfg = Config(
out_dir=str(parent_file / "dataset"),
recomb_map="",
mut_map="",
)
monkeypatch.setattr(cli, "parse_args", lambda: cfg)
with pytest.raises(SystemExit, match=r"^\[error\] filesystem operation failed"):
cli.main()
def test_parse_args_records_distributed_generation_options(
monkeypatch: pytest.MonkeyPatch,
tmp_path,
) -> None:
monkeypatch.setattr(
sys,
"argv",
[
"dlcoalsim",
"--generate-only",
"--rank",
"2",
"--world-size",
"5",
"--out-dir",
str(tmp_path),
],
)
cfg = cli.parse_args()
assert cfg._generation_mode == "generate"
assert cfg._distributed_rank == 2
assert cfg._distributed_world_size == 5
def test_auto_mode_uses_slurm_rank_partition(
monkeypatch: pytest.MonkeyPatch,
tmp_path,
) -> None:
cfg = Config(out_dir=str(tmp_path), recomb_map="", mut_map="")
monkeypatch.setattr(cli, "parse_args", lambda: cfg)
monkeypatch.setattr(cli, "load_empirical_maps", lambda _cfg: EmpiricalMapStore())
monkeypatch.setenv("SLURM_PROCID", "2")
monkeypatch.setenv("SLURM_NTASKS", "4")
calls: list[tuple[int, int]] = []
def generate_partition(_cfg, _empirical, *, rank, world_size):
calls.append((rank, world_size))
return {"generated_chunks": 3, "reused_chunks": 1}
monkeypatch.setattr(cli, "generate_dataset_partition", generate_partition)
monkeypatch.setattr(
cli,
"generate_dataset",
lambda *_args, **_kwargs: pytest.fail("single-node generator was called"),
)
cli.main()
assert calls == [(2, 4)]
def test_prepare_only_loads_maps_and_allows_force(
monkeypatch: pytest.MonkeyPatch,
tmp_path,
) -> None:
cfg = Config(
out_dir=str(tmp_path),
recomb_map="",
mut_map="",
force=True,
)
cfg._generation_mode = "prepare"
cfg._distributed_rank = None
cfg._distributed_world_size = None
empirical = EmpiricalMapStore()
calls: list[tuple[Config, EmpiricalMapStore]] = []
monkeypatch.setattr(cli, "parse_args", lambda: cfg)
monkeypatch.setattr(cli, "load_empirical_maps", lambda _cfg: empirical)
monkeypatch.setattr(
cli,
"prepare_dataset",
lambda called_cfg, called_empirical: calls.append(
(called_cfg, called_empirical)
),
)
cli.main()
assert calls == [(cfg, empirical)]
def test_finalize_only_skips_map_loading_and_writes_dataset_files(
monkeypatch: pytest.MonkeyPatch,
tmp_path,
) -> None:
cfg = Config(out_dir=str(tmp_path), recomb_map="", mut_map="")
cfg._generation_mode = "finalize"
cfg._distributed_rank = None
cfg._distributed_world_size = None
manifest = {"n_samples": cfg.n_samples}
writes: list[tuple[Config, dict]] = []
monkeypatch.setattr(cli, "parse_args", lambda: cfg)
monkeypatch.setattr(
cli,
"load_empirical_maps",
lambda _cfg: pytest.fail("finalization loaded empirical maps"),
)
monkeypatch.setattr(
cli,
"finalize_dataset",
lambda _cfg, **_kwargs: manifest,
)
def record_write(called_cfg, called_manifest):
with pytest.raises(DatasetGenerationLockedError):
with dataset_output_lock(tmp_path):
pass
writes.append((called_cfg, called_manifest))
monkeypatch.setattr(cli, "write_dataset_files", record_write)
cli.main()
assert writes == [(cfg, manifest)]
def test_finalize_only_reports_incomplete_generation_cleanly(
monkeypatch: pytest.MonkeyPatch,
tmp_path,
) -> None:
cfg = Config(out_dir=str(tmp_path), recomb_map="", mut_map="")
cfg._generation_mode = "finalize"
cfg._distributed_rank = None
cfg._distributed_world_size = None
monkeypatch.setattr(cli, "parse_args", lambda: cfg)
def reject_finalize(_cfg, **_kwargs):
raise DatasetGenerationIncompleteError("2 chunks are still missing")
monkeypatch.setattr(cli, "finalize_dataset", reject_finalize)
with pytest.raises(SystemExit, match=r"^\[error\] 2 chunks are still missing$"):
cli.main()
|