Datasets:
Formats:
parquet
Size:
100K - 1M
Tags:
population-genetics
demographic-inference
effective-population-size
coalescent
msprime
simulation
License:
Download generator/code/tests/test_config.py from Larrivhan/NeCast-simulations: direct link, hf CLI and curl.
- Browser
- Download file 8.56 kB
-
https://huggingface.co/datasets/Larrivhan/NeCast-simulations/resolve/main/generator/code/tests/test_config.py
- Command line
-
hf download hf://datasets/Larrivhan/NeCast-simulations/generator/code/tests/test_config.py
-
curl -L -o test_config.py https://huggingface.co/datasets/Larrivhan/NeCast-simulations/resolve/main/generator/code/tests/test_config.py
8.56 kB
| 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")) | |
| def test_builtin_presets_are_valid(preset: str) -> None: | |
| validate_config(apply_preset(Config(preset=preset))) | |
| 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 | |
| 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)) | |
| 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)) | |
| 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) | |
| def test_boolean_settings_reject_integer_substitutes(name: str) -> None: | |
| with pytest.raises(ValueError, match=name): | |
| validate_config(dataclasses.replace(valid_config(), **{name: 1})) | |