File size: 10,303 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
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
from __future__ import annotations

import dataclasses
import csv
import gzip
import json
import subprocess
import sys
import zipfile
from pathlib import Path

import numpy as np

from scripts.dataset_admin import check_shard
from simulator.config import (
    DEMOGRAPHY_PROB_FIELDS,
    DEMOGRAPHY_TYPE_IDS,
    GENERATOR_RECIPE_IDS,
    Config,
    validate_config,
)
from simulator.dataset_files import write_dataset_files
from simulator.dataset_state import SAMPLE_GENERATOR_SCHEMA_VERSION
from simulator.empirical_maps import EmpiricalMapStore
from simulator.shards import _write_npz_arrays, generate_dataset


def _config(
    out_dir: Path,
    *,
    workers: int,
    compute_chunk_size: int,
) -> Config:
    cfg = Config(
        dataset_name="chunked-io-test",
        out_dir=str(out_dir),
        seed=271828,
        n_samples=4,
        shard_size=2,
        compute_chunk_size=compute_chunk_size,
        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=workers,
        compression=True,
        compression_level=1,
        include_truth=True,
    )
    for field in DEMOGRAPHY_PROB_FIELDS:
        setattr(cfg, field, 0.0)
    cfg.p_constant = 1.0
    validate_config(cfg)
    return cfg


def _assert_logical_outputs_equal(left: Path, right: Path) -> None:
    left_shards = sorted((left / "samples").glob("shard_*.npz"))
    right_shards = sorted((right / "samples").glob("shard_*.npz"))
    assert [path.name for path in left_shards] == [path.name for path in right_shards]
    for left_path, right_path in zip(left_shards, right_shards, strict=True):
        with np.load(left_path, allow_pickle=False) as left_data:
            with np.load(right_path, allow_pickle=False) as right_data:
                assert left_data.files == right_data.files
                for key in left_data.files:
                    assert left_data[key].dtype == right_data[key].dtype, key
                    assert left_data[key].shape == right_data[key].shape, key
                    np.testing.assert_array_equal(left_data[key], right_data[key], err_msg=key)
                    assert (
                        left_data[key].tobytes(order="C")
                        == right_data[key].tobytes(order="C")
                    ), key
        with gzip.open(left_path.with_suffix(".jsonl.gz"), "rb") as handle:
            left_metadata = handle.read()
        with gzip.open(right_path.with_suffix(".jsonl.gz"), "rb") as handle:
            right_metadata = handle.read()
        assert left_metadata == right_metadata

    with gzip.open(left / "metadata" / "samples.jsonl.gz", "rb") as handle:
        left_aggregate = handle.read()
    with gzip.open(right / "metadata" / "samples.jsonl.gz", "rb") as handle:
        right_aggregate = handle.read()
    assert left_aggregate == right_aggregate
    assert (left / "metadata" / "samples.csv").read_bytes() == (
        right / "metadata" / "samples.csv"
    ).read_bytes()


def _sidecar_bytes(dataset: Path) -> bytes:
    parts = []
    for path in sorted((dataset / "samples").glob("shard_*.jsonl.gz")):
        with gzip.open(path, "rb") as handle:
            parts.append(handle.read())
    return b"".join(parts)


def test_npz_writer_honors_compression_level_and_preserves_arrays(tmp_path: Path) -> None:
    arrays = {
        "zeros": np.zeros(256_000, dtype=np.uint8),
        "values": np.arange(128, dtype=np.float32),
        "labels": np.array(["alpha", "beta"], dtype="U8"),
    }
    base = Config(out_dir=str(tmp_path / "unused"))
    level_zero = tmp_path / "level-zero.npz"
    level_nine = tmp_path / "level-nine.npz"
    stored = tmp_path / "stored.npz"

    _write_npz_arrays(
        level_zero,
        arrays,
        dataclasses.replace(base, compression=True, compression_level=0),
    )
    _write_npz_arrays(
        level_nine,
        arrays,
        dataclasses.replace(base, compression=True, compression_level=9),
    )
    _write_npz_arrays(
        stored,
        arrays,
        dataclasses.replace(base, compression=False, compression_level=1),
    )

    for path in (level_zero, level_nine, stored):
        with np.load(path, allow_pickle=False) as data:
            assert data.files == list(arrays)
            for key, expected in arrays.items():
                np.testing.assert_array_equal(data[key], expected)

    with zipfile.ZipFile(level_zero) as archive:
        assert {entry.compress_type for entry in archive.infolist()} == {
            zipfile.ZIP_DEFLATED
        }
    with zipfile.ZipFile(level_nine) as archive:
        assert {entry.compress_type for entry in archive.infolist()} == {
            zipfile.ZIP_DEFLATED
        }
    with zipfile.ZipFile(stored) as archive:
        assert {entry.compress_type for entry in archive.infolist()} == {
            zipfile.ZIP_STORED
        }
    assert level_nine.stat().st_size < level_zero.stat().st_size


def test_compute_chunks_and_worker_count_are_lossless(tmp_path: Path) -> None:
    baseline_dir = tmp_path / "baseline"
    chunked_dir = tmp_path / "chunked"
    parallel_dir = tmp_path / "parallel"

    baseline = generate_dataset(
        _config(baseline_dir, workers=1, compute_chunk_size=2),
        EmpiricalMapStore(),
    )
    chunked = generate_dataset(
        _config(chunked_dir, workers=1, compute_chunk_size=1),
        EmpiricalMapStore(),
    )
    parallel = generate_dataset(
        _config(parallel_dir, workers=2, compute_chunk_size=1),
        EmpiricalMapStore(),
    )

    assert baseline["shards"] == chunked["shards"] == parallel["shards"]
    assert baseline["demography_counts"] == {"constant": 4}
    assert baseline["generator_recipe_counts"] == {"constant": 4}
    _assert_logical_outputs_equal(baseline_dir, chunked_dir)
    _assert_logical_outputs_equal(chunked_dir, parallel_dir)
    assert not (baseline_dir / "samples" / ".fragments").exists()
    assert not (chunked_dir / "samples" / ".fragments").exists()
    assert not (parallel_dir / "samples" / ".fragments").exists()

    for dataset in (baseline_dir, chunked_dir, parallel_dir):
        for shard_path in sorted((dataset / "samples").glob("shard_*.npz")):
            assert len(check_shard(shard_path)) == 2
            with np.load(shard_path, allow_pickle=False) as data:
                assert data["demography_type_id"].dtype == np.int16
                assert data["generator_recipe_id"].dtype == np.int16
                np.testing.assert_array_equal(
                    data["generator_schema_version"],
                    np.array([SAMPLE_GENERATOR_SCHEMA_VERSION], dtype=np.int16),
                )
                np.testing.assert_array_equal(
                    data["demography_type_id"],
                    np.full(
                        len(data["sample_id"]),
                        DEMOGRAPHY_TYPE_IDS["constant"],
                    ),
                )
                np.testing.assert_array_equal(
                    data["generator_recipe_id"],
                    np.full(
                        len(data["sample_id"]),
                        GENERATOR_RECIPE_IDS["constant"],
                    ),
                )
        with gzip.open(dataset / "metadata" / "samples.jsonl.gz", "rb") as handle:
            aggregate = handle.read()
        assert aggregate == _sidecar_bytes(dataset)
        with (dataset / "metadata" / "samples.csv").open(
            newline="",
            encoding="utf-8",
        ) as handle:
            rows = list(csv.DictReader(handle))
        assert {row["demography_type"] for row in rows} == {"constant"}
        assert {row["generator_recipe"] for row in rows} == {"constant"}
        assert {int(row["demography_type_id"]) for row in rows} == {
            DEMOGRAPHY_TYPE_IDS["constant"]
        }
        assert {int(row["generator_recipe_id"]) for row in rows} == {
            GENERATOR_RECIPE_IDS["constant"]
        }
        assert all("log10_ne_range" in row for row in rows)

    baseline_cfg = _config(baseline_dir, workers=1, compute_chunk_size=2)
    write_dataset_files(baseline_cfg, baseline)
    config_json = json.loads((baseline_dir / "config.json").read_text(encoding="utf-8"))
    manifest_json = json.loads((baseline_dir / "manifest.json").read_text(encoding="utf-8"))
    for payload in (config_json, manifest_json):
        assert payload["demography_type_ids"] == DEMOGRAPHY_TYPE_IDS
        assert payload["generator_recipe_ids"] == GENERATOR_RECIPE_IDS
        assert (
            payload["generator_schema_version"]
            == SAMPLE_GENERATOR_SCHEMA_VERSION
        )
    subprocess.run(
        [
            sys.executable,
            str(baseline_dir / "scripts" / "validate_dataset.py"),
            str(baseline_dir),
        ],
        check=True,
    )


def test_fragment_metadata_honors_compression_level(tmp_path: Path) -> None:
    level_zero_dir = tmp_path / "metadata-level-zero"
    level_nine_dir = tmp_path / "metadata-level-nine"
    level_zero = dataclasses.replace(
        _config(level_zero_dir, workers=2, compute_chunk_size=1),
        compression_level=0,
    )
    level_nine = dataclasses.replace(
        _config(level_nine_dir, workers=2, compute_chunk_size=1),
        compression_level=9,
    )

    generate_dataset(level_zero, EmpiricalMapStore())
    generate_dataset(level_nine, EmpiricalMapStore())

    zero_sidecars = sorted((level_zero_dir / "samples").glob("shard_*.jsonl.gz"))
    nine_sidecars = sorted((level_nine_dir / "samples").glob("shard_*.jsonl.gz"))
    assert [path.name for path in zero_sidecars] == [
        path.name for path in nine_sidecars
    ]
    for zero_path, nine_path in zip(zero_sidecars, nine_sidecars, strict=True):
        with gzip.open(zero_path, "rb") as handle:
            zero_metadata = handle.read()
        with gzip.open(nine_path, "rb") as handle:
            nine_metadata = handle.read()
        assert zero_metadata == nine_metadata
    assert sum(path.stat().st_size for path in nine_sidecars) < sum(
        path.stat().st_size for path in zero_sidecars
    )