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

import dataclasses

import numpy as np
import pytest

from simulator.config import (
    DEMOGRAPHY_PROB_FIELDS,
    FLOAT32_MIN_POSITIVE,
    INT16_MAX,
    SOURCE_PROB_FIELDS,
    UINT32_MAX,
    Config,
    apply_preset,
    validate_config,
)
from simulator.noise import flat_index_dtype


def valid_config() -> Config:
    return apply_preset(Config(preset="mini"))


@pytest.mark.parametrize("preset", ["mini", "core_v01", "full_v01"])
def test_builtin_presets_are_valid(preset: str) -> None:
    validate_config(apply_preset(Config(preset=preset)))


@pytest.mark.parametrize("preset", ["mini", "core_v01", "full_v01"])
def test_builtin_presets_target_64_bins(preset: str) -> None:
    cfg = apply_preset(Config(preset=preset))

    assert cfg.time_bins == 64
    assert cfg.demography_resolution_log10 > 0.0


@pytest.mark.parametrize(
    ("changes", "message"),
    [
        ({"n_samples": 0}, "n_samples"),
        ({"n_samples": True}, "n_samples"),
        ({"shard_size": 0}, "shard_size"),
        ({"compute_chunk_size": 0}, "compute_chunk_size"),
        ({"seq_len": 0}, "seq_len"),
        ({"time_bins": 0}, "time_bins"),
        ({"demography_resolution_log10": 0.0}, "demography_resolution_log10"),
        ({"demography_resolution_log10": -1.0}, "demography_resolution_log10"),
        ({"n_haplotypes": 1}, "at least 2"),
        ({"n_haplotypes": 3}, "even"),
        ({"n_haplotypes": INT16_MAX + 1}, "int16"),
        ({"min_time": 0.0}, "min_time"),
        ({"min_time": 500.0, "max_time": 500.0}, "max_time"),
        ({"min_time": float("nan")}, "min_time"),
        ({"baseline_mu": 0.0}, "baseline_mu"),
        ({"baseline_rec": -1.0e-8}, "baseline_rec"),
        ({"baseline_mu": FLOAT32_MIN_POSITIVE / 2.0}, "float32"),
        ({"baseline_mu": 5.1e-7}, "synthetic maps"),
        ({"baseline_rec": 1.1e-4}, "baseline_rec"),
        ({"p_clean_constant": -0.01}, "p_clean_constant"),
        ({"genotype_error_min": -0.01}, "genotype_error_min"),
        ({"genotype_error_max": 1.01}, "genotype_error_max"),
        ({"genotype_error_min": 0.2, "genotype_error_max": 0.1}, "must not exceed"),
        ({"missing_rate_min": -0.01}, "missing_rate_min"),
        ({"missing_rate_max": 1.01}, "missing_rate_max"),
        ({"missing_rate_min": 0.2, "missing_rate_max": 0.1}, "must not exceed"),
        ({"phase_switch_rate_per_het_min": -0.1}, "phase_switch_rate_per_het_min"),
        (
            {"phase_switch_rate_per_het_min": 0.2, "phase_switch_rate_per_het_max": 0.1},
            "must not exceed",
        ),
        ({"hotspot_missing_prob": -0.1}, "hotspot_missing_prob"),
        ({"hotspot_missing_prob": 1.1}, "hotspot_missing_prob"),
        ({"observed_map_noise": -0.1}, "observed_map_noise"),
        ({"observed_map_noise": float("inf")}, "observed_map_noise"),
        ({"empirical_ne_template_log10_shift_sd": -0.1}, "log10_shift"),
        ({"empirical_ne_template_contrast_sd": -0.1}, "contrast"),
        ({"empirical_ne_template_time_warp_sd": -0.1}, "time_warp"),
        ({"empirical_ne_template_bin_noise_sd": -0.1}, "bin_noise"),
        ({"map_segments_min": 0}, "map_segments_min"),
        ({"map_segments_min": 10, "map_segments_max": 9}, "map_segments_min"),
        ({"max_retries": 0}, "max_retries"),
        ({"workers": 0}, "workers"),
        ({"workers": -2}, "workers"),
        ({"compression_level": -1}, "compression_level"),
        ({"compression_level": 10}, "compression_level"),
        ({"compression_level": True}, "compression_level"),
        ({"seed": 1.5}, "seed"),
        ({"dataset_name": ""}, "dataset_name"),
        ({"preset": "unknown"}, "preset"),
        ({"out_dir": ""}, "out_dir"),
        ({"out_dir": "."}, "dedicated dataset directory"),
        ({"demography_mixture_version": ""}, "demography_mixture_version"),
        ({"empirical_ne_template_dir": ""}, "empirical_ne_template_dir"),
        ({"stdpopsim_species": ""}, "stdpopsim_species"),
        ({"stdpopsim_contig_length": -1}, "stdpopsim_contig_length"),
        ({"recomb_map_unit": "centimorgans"}, "recomb_map_unit"),
        ({"mut_map_unit": "unknown"}, "mut_map_unit"),
    ],
)
def test_invalid_scalar_and_range_settings_are_rejected(changes: dict, message: str) -> None:
    with pytest.raises(ValueError, match=message):
        validate_config(dataclasses.replace(valid_config(), **changes))


def test_source_mixture_requires_a_positive_weight() -> None:
    cfg = dataclasses.replace(valid_config(), **{name: 0.0 for name in SOURCE_PROB_FIELDS})
    with pytest.raises(ValueError, match="source probability"):
        validate_config(cfg)


def test_disabled_stdpopsim_weight_is_not_an_enabled_source() -> None:
    source_weights = {name: 0.0 for name in SOURCE_PROB_FIELDS}
    source_weights["p_stdpopsim_anchor"] = 1.0
    cfg = dataclasses.replace(valid_config(), **source_weights, enable_stdpopsim=False)
    with pytest.raises(ValueError, match="enabled source probability"):
        validate_config(cfg)

    validate_config(dataclasses.replace(cfg, enable_stdpopsim=True))


def test_demography_mixture_requires_a_positive_weight() -> None:
    cfg = dataclasses.replace(valid_config(), **{name: 0.0 for name in DEMOGRAPHY_PROB_FIELDS})
    with pytest.raises(ValueError, match="demography probability"):
        validate_config(cfg)


def test_sequence_positions_must_fit_uint32_storage() -> None:
    with pytest.raises(ValueError, match="uint32 position"):
        validate_config(dataclasses.replace(valid_config(), seq_len=UINT32_MAX + 1))


@pytest.mark.parametrize(
    ("seq_len", "n_haplotypes"),
    [(60_000_000, 64), (80_000_000, 128), (80_000_000, 256), (40_000_000, 256)],
)
def test_a_long_sequence_with_many_haplotypes_is_accepted(
    seq_len: int, n_haplotypes: int
) -> None:
    """The sparse missing index addresses variants, not bases.

    Every configuration here was rejected by a precondition that multiplied
    ``seq_len`` by ``n_haplotypes``, which is the size of the matrix only if
    every base is segregating.  Variants are three orders of magnitude rarer,
    and the width that is actually used is chosen per row from the matrix that
    was built.
    """

    validate_config(
        dataclasses.replace(
            valid_config(), seq_len=seq_len, n_haplotypes=n_haplotypes
        )
    )


def test_the_flat_index_width_follows_the_matrix_it_addresses() -> None:
    limit = np.iinfo(np.uint32).max
    assert flat_index_dtype(0) == np.uint32
    assert flat_index_dtype(limit) == np.uint32
    assert flat_index_dtype(limit + 1) == np.uint64
    assert flat_index_dtype(10 * limit) == np.uint64


def test_stdpopsim_contig_positions_must_fit_uint32_storage() -> None:
    cfg = dataclasses.replace(valid_config(), stdpopsim_contig_length=UINT32_MAX + 1)
    with pytest.raises(ValueError, match="uint32 position"):
        validate_config(cfg)


def test_a_long_stdpopsim_contig_is_accepted() -> None:
    validate_config(
        dataclasses.replace(
            valid_config(),
            n_haplotypes=256,
            stdpopsim_contig_length=UINT32_MAX // 2 + 1,
        )
    )


def test_probability_fields_are_weights_and_need_not_sum_to_one() -> None:
    source_weights = {name: 0.0 for name in SOURCE_PROB_FIELDS}
    source_weights["p_custom_hetero"] = 4.0
    demography_weights = {name: 0.0 for name in DEMOGRAPHY_PROB_FIELDS}
    demography_weights["p_constant"] = 2.0
    validate_config(dataclasses.replace(valid_config(), **source_weights, **demography_weights))


@pytest.mark.parametrize(
    "source_mix",
    [
        "",
        ", ,",
        "smcpp_1kg:",
        ":1",
        "smcpp_1kg:not-a-number",
        "smcpp_1kg:-0.1",
        "smcpp_1kg:nan",
        "smcpp_1kg:0,phlash_unified:0",
        "smcpp_1kg:1,smcpp_1kg:0",
        "smcpp_1kg:1e308,phlash_unified:1e308",
        "smcpp_typo:1",
    ],
)
def test_invalid_empirical_ne_source_mix_is_rejected(source_mix: str) -> None:
    cfg = dataclasses.replace(valid_config(), empirical_ne_template_source_mix=source_mix)
    with pytest.raises(ValueError, match="empirical_ne_template_source_mix"):
        validate_config(cfg)


@pytest.mark.parametrize(
    "name",
    [
        "force",
        "compression",
        "include_truth",
        "enable_stdpopsim",
        "mut_map_scale_to_baseline",
    ],
)
def test_boolean_settings_reject_integer_substitutes(name: str) -> None:
    with pytest.raises(ValueError, match=name):
        validate_config(dataclasses.replace(valid_config(), **{name: 1}))