File size: 10,623 Bytes
1cb35cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from __future__ import annotations

import dataclasses
from pathlib import Path

import numpy as np
import pytest

from simulator import shards
from simulator.config import DEMOGRAPHY_PROB_FIELDS, Config, validate_config
from simulator.dataset_state import DatasetConfigMismatchError
from simulator.empirical_maps import EmpiricalMapStore
from simulator.shards import (
    DatasetGenerationLockedError,
    DatasetSourceUnavailableError,
    UnsafeOutputDirectoryError,
    dataset_output_lock,
    generate_dataset,
    reuse_completed_dataset_if_compatible,
)
from simulator.stdpopsim_support import StdpopsimSimulationError


def _config(out_dir: Path, *, force: bool = False) -> Config:
    cfg = Config(
        dataset_name="recovery-test",
        out_dir=str(out_dir),
        force=force,
        n_samples=1,
        shard_size=1,
        seq_len=1_000,
        n_haplotypes=4,
        time_bins=4,
        min_time=50.0,
        max_time=5_000.0,
        p_clean_constant=1.0,
        p_random_global=0.0,
        p_custom_hetero=0.0,
        p_empirical_slice=0.0,
        p_stdpopsim_anchor=0.0,
        recomb_map="",
        mut_map="",
        workers=1,
        compression=False,
    )
    for field in DEMOGRAPHY_PROB_FIELDS:
        setattr(cfg, field, 0.0)
    cfg.p_constant = 1.0
    validate_config(cfg)
    return cfg


def test_no_done_cleans_stale_shards_before_regeneration(tmp_path: Path) -> None:
    out_dir = tmp_path / "dataset"
    samples = out_dir / "samples"
    metadata = out_dir / "metadata"
    samples.mkdir(parents=True)
    metadata.mkdir()
    (samples / "shard_99999.npz").write_bytes(b"stale")
    (samples / "shard_99999.jsonl.gz").write_bytes(b"stale")
    (samples / "shard_00000.tmp.npz").write_bytes(b"partial")
    (metadata / "samples.csv").write_text("stale", encoding="utf-8")
    (out_dir / "manifest.json").write_text("stale", encoding="utf-8")

    manifest = generate_dataset(_config(out_dir), EmpiricalMapStore())

    assert manifest["n_samples"] == 1
    assert [path.name for path in samples.glob("shard_*.npz")] == ["shard_00000.npz"]
    assert [path.name for path in samples.glob("shard_*.jsonl.gz")] == [
        "shard_00000.jsonl.gz"
    ]
    assert not (out_dir / "manifest.json").exists()
    assert reuse_completed_dataset_if_compatible(_config(out_dir))["n_samples"] == 1


def test_completed_dataset_with_missing_or_extra_shards_is_rejected(tmp_path: Path) -> None:
    out_dir = tmp_path / "dataset"
    cfg = _config(out_dir)
    generate_dataset(cfg, EmpiricalMapStore())

    missing = out_dir / "samples" / "shard_00000.jsonl.gz"
    missing.unlink()
    with pytest.raises(DatasetConfigMismatchError, match=r"metadata files.*--force"):
        generate_dataset(cfg, EmpiricalMapStore())

    missing.write_bytes(b"not-a-valid-gzip")
    (out_dir / "samples" / "shard_99999.npz").write_bytes(b"stale")
    with pytest.raises(DatasetConfigMismatchError, match=r"NPZ shard files.*--force"):
        generate_dataset(cfg, EmpiricalMapStore())


def test_completed_dataset_missing_recipe_ids_is_rejected(tmp_path: Path) -> None:
    out_dir = tmp_path / "dataset"
    cfg = _config(out_dir)
    generate_dataset(cfg, EmpiricalMapStore())
    shard = out_dir / "samples" / "shard_00000.npz"
    with np.load(shard, allow_pickle=False) as data:
        arrays = {
            key: np.asarray(data[key])
            for key in data.files
            if key != "generator_recipe_id"
        }
    np.savez(shard, **arrays)

    with pytest.raises(
        DatasetConfigMismatchError,
        match=r"missing arrays: generator_recipe_id.*--force",
    ):
        generate_dataset(cfg, EmpiricalMapStore())


def test_completed_dataset_with_misaligned_valid_type_id_is_rejected(
    tmp_path: Path,
) -> None:
    out_dir = tmp_path / "dataset"
    cfg = _config(out_dir)
    generate_dataset(cfg, EmpiricalMapStore())
    shard = out_dir / "samples" / "shard_00000.npz"
    with np.load(shard, allow_pickle=False) as data:
        arrays = {key: np.asarray(data[key]) for key in data.files}
    arrays["demography_type_id"] = np.ones_like(arrays["demography_type_id"])
    np.savez(shard, **arrays)

    with pytest.raises(
        DatasetConfigMismatchError,
        match=r"metadata demography_type_id differs.*--force",
    ):
        generate_dataset(cfg, EmpiricalMapStore())


def test_force_invalidates_done_before_failed_regeneration(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    out_dir = tmp_path / "dataset"
    cfg = _config(out_dir)
    generate_dataset(cfg, EmpiricalMapStore())
    (out_dir / "manifest.json").write_text("previous complete dataset", encoding="utf-8")

    def fail_generation(_task):
        raise RuntimeError("simulated worker failure")

    monkeypatch.setattr(shards, "_generate_shard", fail_generation)
    with pytest.raises(RuntimeError, match="simulated worker failure"):
        generate_dataset(dataclasses.replace(cfg, force=True), EmpiricalMapStore())

    assert not (out_dir / "samples" / "DONE.json").exists()
    assert not (out_dir / "manifest.json").exists()
    assert not list((out_dir / "samples").glob("shard_*"))


def test_failed_chunk_generation_retains_fragments_until_safe_retry_cleanup(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    out_dir = tmp_path / "dataset"
    cfg = dataclasses.replace(
        _config(out_dir),
        n_samples=2,
        shard_size=1,
        compute_chunk_size=1,
    )
    original_generate_shard = shards._generate_shard
    calls = 0

    def fail_after_first_fragment(task):
        nonlocal calls
        calls += 1
        if calls == 2:
            raise RuntimeError("simulated chunk failure")
        return original_generate_shard(task)

    monkeypatch.setattr(shards, "_generate_shard", fail_after_first_fragment)
    with pytest.raises(RuntimeError, match="simulated chunk failure"):
        generate_dataset(cfg, EmpiricalMapStore())

    fragment_dir = out_dir / "samples" / ".fragments"
    assert not (out_dir / "samples" / "DONE.json").exists()
    assert [path.name for path in fragment_dir.glob("fragment_*.npz")] == [
        "fragment_00000000.npz"
    ]
    assert [path.name for path in fragment_dir.glob("fragment_*.jsonl.gz")] == [
        "fragment_00000000.jsonl.gz"
    ]

    resumed_chunk_ids: list[int] = []

    def record_resumed_generation(task):
        resumed_chunk_ids.append(task[0])
        return original_generate_shard(task)

    monkeypatch.setattr(shards, "_generate_shard", record_resumed_generation)
    manifest = generate_dataset(cfg, EmpiricalMapStore())

    assert resumed_chunk_ids == [1]
    assert manifest["n_samples"] == 2
    assert manifest["shards"] == [
        "samples/shard_00000.npz",
        "samples/shard_00001.npz",
    ]
    assert not fragment_dir.exists()


def test_invalid_force_config_is_rejected_before_existing_done_is_touched(
    tmp_path: Path,
) -> None:
    out_dir = tmp_path / "dataset"
    cfg = _config(out_dir)
    generate_dataset(cfg, EmpiricalMapStore())
    done = out_dir / "samples" / "DONE.json"
    original = done.read_bytes()

    with pytest.raises(ValueError, match="n_samples"):
        generate_dataset(
            dataclasses.replace(cfg, force=True, n_samples=0),
            EmpiricalMapStore(),
        )

    assert done.read_bytes() == original


def test_output_lock_rejects_a_concurrent_writer(tmp_path: Path) -> None:
    with dataset_output_lock(tmp_path):
        with pytest.raises(DatasetGenerationLockedError, match="already writing"):
            with dataset_output_lock(tmp_path):
                pass


def test_nonempty_unmarked_output_directory_is_rejected(tmp_path: Path) -> None:
    out_dir = tmp_path / "unrelated"
    out_dir.mkdir()
    note = out_dir / "keep-me.txt"
    note.write_text("user data", encoding="utf-8")

    with pytest.raises(UnsafeOutputDirectoryError, match="non-empty directory"):
        generate_dataset(_config(out_dir), EmpiricalMapStore())

    assert note.read_text(encoding="utf-8") == "user data"
    assert not (out_dir / ".dlcoalsim.lock").exists()


def test_empirical_only_demography_requires_available_templates(tmp_path: Path) -> None:
    out_dir = tmp_path / "dataset"
    cfg = _config(out_dir)
    for field in DEMOGRAPHY_PROB_FIELDS:
        setattr(cfg, field, 0.0)
    cfg.p_empirical_human_ne_template = 1.0
    cfg.empirical_ne_template_dir = str(tmp_path / "missing-templates")
    validate_config(cfg)

    with pytest.raises(DatasetSourceUnavailableError, match="only enabled demography"):
        generate_dataset(cfg, EmpiricalMapStore())

    assert not (out_dir / "samples" / "DONE.json").exists()


def test_enabled_empirical_map_source_fails_closed_when_maps_are_unavailable(
    tmp_path: Path,
) -> None:
    out_dir = tmp_path / "dataset"
    cfg = _config(out_dir)
    cfg.p_empirical_slice = 0.1
    validate_config(cfg)

    with pytest.raises(
        DatasetSourceUnavailableError,
        match="refusing to disable the requested empirical source",
    ):
        generate_dataset(cfg, EmpiricalMapStore())

    assert not (out_dir / "samples" / "DONE.json").exists()


def test_unavailable_optional_demography_is_disabled_with_a_warning(
    tmp_path: Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    out_dir = tmp_path / "dataset"
    cfg = _config(out_dir)
    cfg.p_constant = 1.0
    cfg.p_empirical_human_ne_template = 1.0
    cfg.empirical_ne_template_dir = str(tmp_path / "missing-templates")
    validate_config(cfg)

    manifest = generate_dataset(cfg, EmpiricalMapStore())

    assert manifest["demography_counts"] == {"constant": 1}
    assert manifest["generator_recipe_counts"] == {"constant": 1}
    assert "templates are unavailable" in capsys.readouterr().out


def test_requested_stdpopsim_setup_failure_preserves_existing_dataset(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    out_dir = tmp_path / "dataset"
    cfg = _config(out_dir)
    generate_dataset(cfg, EmpiricalMapStore())
    done = out_dir / "samples" / "DONE.json"
    original = done.read_bytes()

    requested = dataclasses.replace(
        cfg,
        force=True,
        enable_stdpopsim=True,
        p_stdpopsim_anchor=1.0,
    )

    def fail_setup(_cfg: Config) -> None:
        raise StdpopsimSimulationError("dependency missing")

    monkeypatch.setattr(shards, "validate_stdpopsim_setup", fail_setup)
    with pytest.raises(DatasetSourceUnavailableError, match="dependency missing"):
        generate_dataset(requested, EmpiricalMapStore())

    assert done.read_bytes() == original