File size: 36,928 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
from __future__ import annotations

import dataclasses
import json
import time
from pathlib import Path

from .config import (
    DEMOGRAPHY_TYPE_IDS,
    GENERATOR_RECIPE_IDS,
    SOURCE_IDS,
    Config,
    validate_config,
)
from .dataset_state import SAMPLE_GENERATOR_SCHEMA_VERSION
from .utils import time_grid


README_TEMPLATE = """# {dataset_name}

DLCoalSim is a minimally processed coalescent simulation dataset for demographic
history inference from haplotype data.

## Core Philosophy

The dataset stores simulated observations, not model-specific feature engineering:

- bit-packed haplotype genotypes
- sparse missing-genotype coordinates
- diploid individuals represented by adjacent haplotype columns
- variant positions
- observed recombination/mutation maps
- target log10 Ne(t), averaged within each logarithmic time bin
- structured, reproducible sample metadata

Handcrafted features such as SFS, LD, pi, and haplotype diversity are intentionally
not precomputed in the core dataset.

## Dataset Layout

- `samples/shard_*.npz`: sample shards with ragged arrays encoded by offsets
- `samples/shard_*.jsonl.gz`: full per-sample metadata aligned to each shard
- `metadata/samples.csv`: flat metadata summary for filtering and benchmark recipes
- `metadata/samples.jsonl.gz`: full metadata for all samples
- `metadata/samples.parquet`: optional summary parquet when the local environment supports it
- `config.json` and `manifest.json`: generation config and dataset manifest

Shard files are written as lossless NPZ archives using the configured DEFLATE
level. Missing genotypes are stored as sparse per-sample row-major indices
(`missing_flat_idx`, `missing_offsets`) rather than as a dense missing matrix.
Each sample stores a scientific `demography_type_id` and the finer
`generator_recipe_id` naming the sampler that drew it.
with the mappings in `config.json` or `manifest.json`.

Targets are log-time bin averages, not midpoint samples. This keeps short
events such as recent bottlenecks visible in labels when they overlap a time bin.
Every built-in preset writes 64 target bins. The target grid is deliberately
separate from the latent population-history generation grid, so changing target
resolution does not change the sampled history, genotype data, maps, or noise
for a fixed seed. Both grids are recorded in `config.json` and `manifest.json`.
Main simulator samples are generated as diploid individuals (`ploidy=2`), so
`n_haplotypes` must be even. Phase switch noise swaps the two haplotypes within
each diploid individual after sampled switch points; this is recorded as
`phase_switch_model="diploid_pair_switch"` and `phase_pairing`.
`scenario_key` combines the public demography type, map mode, and noise profile
for convenient filtering without making fixed train/validation/test splits.
`generator_recipe_key` provides the same grouping at the fine-grained recipe
level.

Demography sampling uses a registry of twelve named samplers rather than one
large conditional block: `constant`, `spectral_walk`, `size_event`,
`multi_event`, `exponential`, `sawtooth`, `literature_sawtooth`, `bottleneck`,
`event_on_background`, `ooa_chain`, `empirical_human_ne_template`, and
`stdpopsim_single_pop`. An axis that is continuous in nature is one sampler
with a continuous parameter rather than several pinned to points along it, and
the realised parameters go in the metadata so any grouping can be made at
analysis time.

Every history is a function of continuous time. Nothing in the sampler reads
the label resolution, so the target grid can move without moving the ground
truth or the random-number stream. Curves reach msprime through
`epochs_from_curve`, which is defined by an error bound rather than a bin
count: adaptive bisection in log time until each epoch is within
`demography_resolution_log10` of the curve it stands for, with the exact edges
of box-shaped events forced to be breakpoints so an event starts when it starts.
A flat history costs one epoch and a rough one costs as many as its roughness
needs. Continuous histories use msprime growth rates directly, and their labels
are analytic log-time averages of the same trajectory.

Metadata includes the full `demography_breaks`, `demography_values`, and
`demography_growth_rates` needed to reconstruct the ground truth, and for
spectral histories the handful of scalars that regenerate the curve at any
resolution. The public `demography_type` is consolidated into interpretable
scientific classes such as `constant`, `bottleneck`, `exponential_growth`, and
`zigzag`; the exact sampler name remains in `generator_recipe`. Summary fields
include `n_epochs`, `has_recent_event`, `has_ancient_event`, `min_Ne`,
`max_Ne`, `Ne_ratio_max_min`, `recent_min_Ne`, `ancient_mean_Ne`,
`event_severity`, and `event_duration` for filtering and benchmark recipes.
`event_severity` is uniformly `min_Ne / max_Ne`. Shape descriptors include
`log10_ne_range`, `total_variation_log10`, `turn_count`, `direction`, and
`trajectory_form`.
Sample quality metadata includes `variant_density_per_mb`, explicit noise rates,
missing genotype counts, phase switch counts, and observed map rate summaries
(`mean/std/min/max`) for quick corpus filtering without reading the shard arrays.

stdpopsim samples are optional anchors/stress samples. They are disabled by
default (`p_stdpopsim_anchor=0`) and are included only when requested with
`--enable-stdpopsim`. Their target metadata keeps `target_quality` because many
stdpopsim models include population structure, migration, or admixture, so these
anchors should not be treated as the same strict single-population supervision as
the custom simulator samples. Native growth rates are preserved for the selected
population, but the target remains a proxy whenever the source model is
structured.
Two-population migration/admixture stress cases should follow the same anchor
pattern and be labeled with proxy target quality rather than mixed into the strict
single-population supervised core.

## Official Model Inputs

Allowed:

- genotype matrix / haplotypes
- variant positions
- observed recombination map
- observed mutation map
- sequence length and sample size

Not allowed as model input:

- true maps, if included
- demography metadata
- source type
- noise parameters
- tree sequence or TMRCA truth
- target Ne(t)

## Tutorials

Start with:

1. `notebooks/00_quickstart.ipynb` - load a shard and inspect one sample.
2. `notebooks/01_visualize_single_sample.ipynb` - visualize genotypes, maps, and target Ne(t).
3. `notebooks/02_dataset_overview.ipynb` - inspect metadata and dataset composition.
4. `notebooks/03_filter_by_metadata.ipynb` - build custom subsets from sample annotations.
5. `notebooks/04_compute_basic_features.ipynb` - compute simple baseline window features from raw data.
6. `notebooks/05_quality_check_preview.ipynb` - run quick metadata and shard sanity checks.
"""


FEATURES_PY = r'''
"""Baseline feature recipes for DLCoalSim."""
import numpy as np


def unpack_genotypes(genotype_packed, n_haplotypes):
    bits = np.unpackbits(genotype_packed, axis=1, bitorder="little")
    return bits[:, :n_haplotypes].astype(np.uint8)


def window_indices(positions_bp, seq_len, n_windows):
    w = np.floor(positions_bp.astype(float) / (seq_len / n_windows)).astype(int)
    return np.clip(w, 0, n_windows - 1)


def basic_window_stats(positions_bp, G_vh, seq_len, n_windows=512):
    out = np.zeros((n_windows, 4), dtype=np.float32)
    if len(positions_bp) == 0:
        return out
    H = G_vh.shape[1]
    wi = window_indices(positions_bp, seq_len, n_windows)
    for w in range(n_windows):
        idx = np.where(wi == w)[0]
        if len(idx) == 0:
            continue
        X = G_vh[idx]
        ac = X.sum(axis=1)
        p = ac / max(1, H)
        out[w, 0] = len(idx) / max(1.0, seq_len / n_windows)
        out[w, 1] = np.mean(2 * p * (1 - p))
        informative = (ac > 0) & (ac < H)
        out[w, 2] = np.mean(ac[informative] == 1) if informative.any() else 0.0
        out[w, 3] = np.mean(p) if len(p) else 0.0
    return out
'''


LOADER_PY = r'''
"""Tiny loader example for DLCoalSim shards."""

# Substituted from ``SAMPLE_GENERATOR_SCHEMA_VERSION`` when this script is
# written.  See the note in ``validate_dataset.py``.
import gzip
import json
from pathlib import Path

import numpy as np

SUPPORTED_GENERATOR_SCHEMA_VERSION = __GENERATOR_SCHEMA_VERSION__


class DLCoalSimShard:
    def __init__(self, shard_path):
        self.shard_path = Path(shard_path)
        self.data = np.load(self.shard_path, allow_pickle=False)
        self.generator_schema_version = int(self.data["generator_schema_version"][0])
        if self.generator_schema_version != SUPPORTED_GENERATOR_SCHEMA_VERSION:
            raise ValueError(
                f"unsupported generator schema: {self.generator_schema_version}"
            )
        self.n = len(self.data["sample_id"])
        self.n_haplotypes = int(self.data["n_haplotypes"][0])
        self.packed_hap_bytes = int(self.data["packed_hap_bytes"][0])
        meta_path = self.shard_path.with_suffix(".jsonl.gz")
        self.metadata = []
        if meta_path.exists():
            with gzip.open(meta_path, "rt", encoding="utf-8") as f:
                self.metadata = [json.loads(line) for line in f]

    def __len__(self):
        return self.n

    def _slice_offsets(self, key, offsets_key, i):
        off = self.data[offsets_key]
        return self.data[key][off[i]:off[i + 1]]

    def _map_slice(self, prefix, i):
        """One row's map: its breakpoints and the rates between them.

        A map stores one offsets array, not two.  A rate lives *between* two
        breakpoints, so a row holds one fewer rate than positions -- clamped at
        zero, because a row with no map holds none of either rather than minus
        one rate.
        """
        offsets = self.data[f"{prefix}_offsets"]
        lengths = np.diff(np.asarray(offsets, dtype=np.int64))
        rate_starts = np.concatenate(([0], np.cumsum(np.maximum(lengths - 1, 0))))
        return (
            self.data[f"{prefix}_pos"][offsets[i]:offsets[i + 1]],
            self.data[f"{prefix}_rate"][rate_starts[i]:rate_starts[i + 1]],
        )

    def get(self, i, unpack=True):
        v0, v1 = self.data["variant_offsets"][i], self.data["variant_offsets"][i + 1]
        pos = self.data["variant_positions_bp"][v0:v1]
        packed = self.data["genotype_packed"][v0:v1]
        m0, m1 = self.data["missing_offsets"][i], self.data["missing_offsets"][i + 1]
        missing_flat_idx = self.data["missing_flat_idx"][m0:m1].astype(np.int64, copy=False)
        if unpack:
            G = np.unpackbits(packed, axis=1, bitorder="little")[:, :self.n_haplotypes].astype(np.uint8)
            M = np.zeros((len(pos), self.n_haplotypes), dtype=np.uint8)
            if len(missing_flat_idx):
                M[missing_flat_idx // self.n_haplotypes, missing_flat_idx % self.n_haplotypes] = 1
        else:
            G = packed
            M = {
                "storage": str(self.data["missing_storage"][0]),
                "flat_idx": missing_flat_idx,
            }
        seq = self.data["sequence_length"]
        seq_len = int(seq[i] if len(seq) > 1 else seq[0])
        sample = {
            "sample_id": str(self.data["sample_id"][i]),
            "source_id": int(self.data["source_id"][i]),
            "demography_type_id": int(self.data["demography_type_id"][i]),
            "generator_recipe_id": int(self.data["generator_recipe_id"][i]),
            "generator_schema_version": self.generator_schema_version,
            "sequence_length": seq_len,
            "n_haplotypes": self.n_haplotypes,
            "positions_bp": pos,
            "genotypes": G,
            "missing_mask": M,
            "observed_recombination": {
                "position_bp": self._map_slice("obs_rec", i)[0],
                "rate": self._map_slice("obs_rec", i)[1],
            },
            "observed_mutation": {
                "position_bp": self._map_slice("obs_mut", i)[0],
                "rate": self._map_slice("obs_mut", i)[1],
            },
            "target_log10_ne": self.data["target_log10_ne"][i],
        }
        if self.metadata:
            sample["metadata"] = self.metadata[i]
        return sample
'''


VIZ_PY = r'''
"""Reusable visualization helpers for DLCoalSim tutorial notebooks."""
from pathlib import Path

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

try:
    from IPython.display import display
except ImportError:  # pragma: no cover - only used outside notebooks
    display = print

try:
    from .loader import DLCoalSimShard
except ImportError:
    from loader import DLCoalSimShard


def plot_target_curve(sample, time_mids=None):
    y = np.asarray(sample["target_log10_ne"], dtype=float)
    if time_mids is None:
        x = np.arange(len(y))
        x_label = "Time bin"
        log_x = False
    else:
        x = np.asarray(time_mids, dtype=float)[: len(y)]
        x_label = "Generations ago"
        log_x = True

    plt.figure(figsize=(6, 4))
    plt.plot(x, y, marker="o", linewidth=1)
    if log_x:
        plt.xscale("log")
    plt.xlabel(x_label)
    plt.ylabel("log10 Ne")
    plt.title(f"Target Ne(t): {sample.get('sample_id', '')}")
    plt.tight_layout()
    plt.show()


def plot_genotype_heatmap(sample, max_variants=300, max_haplotypes=64):
    G = np.asarray(sample["genotypes"])
    if G.size == 0:
        print("No variants in this sample.")
        return

    G_show = G[:max_variants, :max_haplotypes]
    plt.figure(figsize=(8, 5))
    plt.imshow(G_show, aspect="auto", interpolation="nearest")
    plt.xlabel("Haplotypes")
    plt.ylabel("Variants")
    plt.title("Haplotype genotype matrix")
    plt.tight_layout()
    plt.show()


def plot_variant_density(sample, n_windows=200):
    pos = np.asarray(sample["positions_bp"])
    seq_len = int(sample["sequence_length"])
    if len(pos) == 0:
        print("No variants in this sample.")
        return

    bins = np.linspace(0, seq_len, n_windows + 1)
    counts, _ = np.histogram(pos, bins=bins)
    centers = 0.5 * (bins[:-1] + bins[1:])

    plt.figure(figsize=(8, 3))
    plt.plot(centers, counts)
    plt.xlabel("Position (bp)")
    plt.ylabel("Variant count")
    plt.title("Variant density")
    plt.tight_layout()
    plt.show()


def _plot_piecewise_map(map_dict, title, ylabel):
    pos = np.asarray(map_dict["position_bp"], dtype=float)
    rate = np.asarray(map_dict["rate"], dtype=float)
    if len(pos) < 2 or len(rate) == 0:
        print(f"No map data for {title}.")
        return

    x = pos[:-1]
    plt.figure(figsize=(8, 3))
    plt.step(x, rate, where="post")
    plt.xlabel("Position (bp)")
    plt.ylabel(ylabel)
    plt.title(title)
    plt.tight_layout()
    plt.show()


def plot_observed_maps(sample):
    _plot_piecewise_map(
        sample["observed_recombination"],
        "Observed recombination map",
        "Rate per bp per generation",
    )
    _plot_piecewise_map(
        sample["observed_mutation"],
        "Observed mutation map",
        "Rate per bp per generation",
    )


def show_metadata_card(sample):
    meta = sample.get("metadata", {})
    keys = [
        "sample_id",
        "source_type",
        "scenario_key",
        "generator_recipe",
        "generator_recipe_id",
        "demography_type",
        "demography_type_id",
        "history_family",
        "trajectory_form",
        "curve_origin",
        "dataset_regime",
        "direction",
        "time_regime",
        "map_mode",
        "noise_profile",
        "n_variants",
        "variant_density_per_mb",
        "num_trees",
        "n_epochs",
        "has_recent_event",
        "has_ancient_event",
        "min_Ne",
        "max_Ne",
        "Ne_ratio_max_min",
        "event_severity",
        "event_duration",
        "log10_ne_range",
        "total_variation_log10",
        "turn_count",
        "genotype_error",
        "missing_rate",
        "n_missing_genotypes",
        "phase_switch_pair_count",
    ]
    rows = [(k, meta.get(k, "")) for k in keys if k in meta]
    display(pd.DataFrame(rows, columns=["field", "value"]))


def plot_column_distribution(df, column):
    counts = df[column].value_counts(dropna=False)
    plt.figure(figsize=(8, 4))
    counts.plot(kind="bar")
    plt.xlabel(column)
    plt.ylabel("Count")
    plt.title(f"Distribution of {column}")
    plt.tight_layout()
    plt.show()


def plot_numeric_distribution(df, column, bins=50):
    plt.figure(figsize=(6, 4))
    df[column].dropna().hist(bins=bins)
    plt.xlabel(column)
    plt.ylabel("Count")
    plt.title(f"Distribution of {column}")
    plt.tight_layout()
    plt.show()


def _as_shards(dataset_or_loader):
    if hasattr(dataset_or_loader, "get"):
        return [dataset_or_loader]
    if isinstance(dataset_or_loader, (str, Path)):
        root = Path(dataset_or_loader)
        if root.is_file():
            return [DLCoalSimShard(root)]
        return [DLCoalSimShard(p) for p in sorted((root / "samples").glob("shard_*.npz"))]
    return list(dataset_or_loader)


def _sample_by_id(shards, sample_id):
    target = str(sample_id)
    for shard in shards:
        ids = [str(x) for x in shard.data["sample_id"]]
        if target in ids:
            return shard.get(ids.index(target))
    raise KeyError(f"sample_id not found in provided shards: {sample_id}")


def plot_random_targets(dataset_or_loader, metadata, n=20, time_mids=None, random_state=0):
    df = metadata.sample(min(n, len(metadata)), random_state=random_state)
    shards = _as_shards(dataset_or_loader)

    plt.figure(figsize=(7, 4))
    for _, row in df.iterrows():
        sample = _sample_by_id(shards, row["sample_id"])
        y = np.asarray(sample["target_log10_ne"], dtype=float)
        if time_mids is None:
            x = np.arange(len(y))
        else:
            x = np.asarray(time_mids, dtype=float)[: len(y)]
        plt.plot(x, y, alpha=0.45, linewidth=1)
    if time_mids is not None:
        plt.xscale("log")
        plt.xlabel("Generations ago")
    else:
        plt.xlabel("Time bin")
    plt.ylabel("log10 Ne")
    plt.title(f"Random target curves (n={len(df)})")
    plt.tight_layout()
    plt.show()
'''


VALIDATE_DATASET_PY = r'''
"""Quick DLCoalSim dataset validation checks."""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

import numpy as np

# Substituted from ``SAMPLE_GENERATOR_SCHEMA_VERSION`` when this script is
# written, so the check follows the generator instead of being edited by hand
# at every bump.
SUPPORTED_GENERATOR_SCHEMA_VERSION = __GENERATOR_SCHEMA_VERSION__
import pandas as pd

try:
    from .loader import DLCoalSimShard
except ImportError:
    from loader import DLCoalSimShard


def _check_monotone(name, arr):
    if len(arr) > 1 and np.any(np.diff(arr) < 0):
        raise AssertionError(f"{name} positions are not monotone")


def validate_dataset(data_dir):
    data_dir = Path(data_dir)
    meta_path = data_dir / "metadata" / "samples.csv"
    manifest_path = data_dir / "manifest.json"
    if not meta_path.exists():
        raise FileNotFoundError(meta_path)
    if not manifest_path.exists():
        raise FileNotFoundError(manifest_path)

    df = pd.read_csv(meta_path)
    manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
    demography_type_ids = manifest.get("demography_type_ids")
    generator_recipe_ids = manifest.get("generator_recipe_ids")
    generator_schema_version = manifest.get("generator_schema_version")
    time_edges = manifest.get("time_edges")
    time_mids = manifest.get("time_mids")
    assert isinstance(demography_type_ids, dict), "manifest has no demography type ID map"
    assert isinstance(generator_recipe_ids, dict), "manifest has no generator recipe ID map"
    assert (
        generator_schema_version == SUPPORTED_GENERATOR_SCHEMA_VERSION
    ), "unsupported generator schema"
    assert isinstance(time_mids, list) and time_mids, "manifest has no target time grid"
    resolution = manifest.get("demography_resolution_log10")
    assert (
        isinstance(resolution, (int, float)) and float(resolution) > 0.0
    ), "manifest has no demography resolution"
    target_bins = len(time_mids)
    assert isinstance(time_edges, list) and len(time_edges) == target_bins + 1, (
        "invalid target time edges"
    )
    target_edges = np.asarray(time_edges, dtype=float)
    assert np.isfinite(target_edges).all() and np.all(np.diff(target_edges) > 0), (
        "target time edges are invalid"
    )
    assert df["sample_id"].is_unique, "sample_id values must be unique"
    for column in [
        "demography_type",
        "demography_type_id",
        "generator_recipe",
        "generator_recipe_id",
    ]:
        assert column in df, f"missing metadata column: {column}"
    assert df["n_variants"].ge(0).all(), "n_variants must be non-negative"
    assert df["sequence_length"].gt(0).all(), "sequence_length must be positive"
    for column in ["mean_obs_rec_rate", "mean_obs_mut_rate", "variant_density_per_mb"]:
        if column in df:
            assert np.isfinite(df[column]).all(), f"{column} contains non-finite values"

    shards = sorted((data_dir / "samples").glob("shard_*.npz"))
    assert shards, "no shard_*.npz files found"
    seen = 0
    for shard_path in shards:
        shard = DLCoalSimShard(shard_path)
        for i in range(len(shard)):
            sample = shard.get(i, unpack=False)
            assert np.isfinite(sample["target_log10_ne"]).all(), "target contains non-finite values"
            assert sample["target_log10_ne"].dtype == np.float32, (
                "target dtype must be float32"
            )
            assert sample["target_log10_ne"].shape == (target_bins,), (
                "target width differs from manifest time grid"
            )
            assert (
                sample["generator_schema_version"]
                == SUPPORTED_GENERATOR_SCHEMA_VERSION
            ), "shard schema mismatch"
            assert sample["demography_type_id"] >= 0, "invalid demography_type_id"
            assert sample["generator_recipe_id"] >= 0, "invalid generator_recipe_id"
            meta = sample.get("metadata", {})
            assert (
                int(meta.get("generator_schema_version", -1))
                == SUPPORTED_GENERATOR_SCHEMA_VERSION
            ), (
                "metadata schema mismatch"
            )
            assert int(meta.get("target_time_bins", -1)) == target_bins, (
                "metadata target bin count mismatch"
            )
            assert float(
                meta.get("demography_resolution_log10", -1.0)
            ) == float(resolution), "metadata demography resolution mismatch"
            demography_type = str(meta.get("demography_type", ""))
            generator_recipe = str(meta.get("generator_recipe", ""))
            assert demography_type in demography_type_ids, "unknown demography_type"
            assert generator_recipe in generator_recipe_ids, "unknown generator_recipe"
            assert sample["demography_type_id"] == int(
                demography_type_ids[demography_type]
            ), "demography type ID/string mismatch"
            assert sample["generator_recipe_id"] == int(
                generator_recipe_ids[generator_recipe]
            ), "generator recipe ID/string mismatch"
            assert sample["sequence_length"] > 0, "sample sequence_length must be positive"
            _check_monotone("variant", sample["positions_bp"])
            _check_monotone("observed recombination", sample["observed_recombination"]["position_bp"])
            _check_monotone("observed mutation", sample["observed_mutation"]["position_bp"])
            missing = sample["missing_mask"]
            mf = np.asarray(missing["flat_idx"], dtype=np.int64)
            if len(mf):
                assert int(mf.min()) >= 0, "missing flat index out of range"
                assert int(mf.max()) < len(sample["positions_bp"]) * sample["n_haplotypes"], "missing flat index out of range"
                assert np.all(np.diff(mf) >= 0), "missing flat indices must be sorted"
            seen += 1

    expected = int(manifest.get("samples", {}).get("n_samples", len(df)))
    assert seen == expected == len(df), f"sample count mismatch: shards={seen}, metadata={len(df)}, manifest={expected}"
    print(f"OK: {seen} samples validated in {data_dir}")


def main(argv=None):
    parser = argparse.ArgumentParser()
    parser.add_argument("data_dir", nargs="?", default=".", help="DLCoalSim dataset directory")
    args = parser.parse_args(argv)
    validate_dataset(args.data_dir)


if __name__ == "__main__":
    main(sys.argv[1:])
'''


def _source_lines(text: str) -> list[str]:
    return [line + "\n" for line in text.strip("\n").splitlines()]


def _md(text: str) -> dict:
    return {"cell_type": "markdown", "metadata": {}, "source": _source_lines(text)}


def _code(text: str) -> dict:
    return {"cell_type": "code", "execution_count": None, "metadata": {}, "outputs": [], "source": _source_lines(text)}


def _notebook(cells: list[dict]) -> dict:
    return {
        "cells": cells,
        "metadata": {
            "kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"},
            "language_info": {"name": "python", "pygments_lexer": "ipython3"},
        },
        "nbformat": 4,
        "nbformat_minor": 5,
    }


COMMON_SETUP = r'''
from pathlib import Path
import json
import sys

DATA_DIR = Path("..").resolve()
if not (DATA_DIR / "manifest.json").exists():
    DATA_DIR = Path(".").resolve()
if str(DATA_DIR) not in sys.path:
    sys.path.insert(0, str(DATA_DIR))

SAMPLES_DIR = DATA_DIR / "samples"
META_PATH = DATA_DIR / "metadata" / "samples.csv"
SHARD_PATH = sorted(SAMPLES_DIR.glob("shard_*.npz"))[0]
CONFIG = json.loads((DATA_DIR / "config.json").read_text())
TIME_MIDS = CONFIG.get("time_mids")
'''


def tutorial_notebooks() -> dict[str, dict]:
    intro = """This notebook is part of the DLCoalSim tutorial suite.

DLCoalSim stores minimally processed simulated observations rather than model-specific precomputed statistics. You are encouraged to compute your own features from haplotypes, variant positions, and observed maps."""

    return {
        "00_quickstart.ipynb": _notebook(
            [
                _md(f"# DLCoalSim Quickstart\n\n{intro}\n\nThis notebook shows how to load one shard, inspect one sample, and access haplotypes, variant positions, observed maps, target log10 Ne(t), and metadata."),
                _code(COMMON_SETUP),
                _code("import pandas as pd\n\nfrom scripts.loader import DLCoalSimShard"),
                _code("df = pd.read_csv(META_PATH)\ndf.head()"),
                _code("shard = DLCoalSimShard(SHARD_PATH)\nsample = shard.get(0)\n\nsample.keys()"),
                _code(
                    'print("sample_id:", sample["sample_id"])\n'
                    'print("sequence_length:", sample["sequence_length"])\n'
                    'print("positions:", sample["positions_bp"].shape)\n'
                    'print("genotypes:", sample["genotypes"].shape)\n'
                    'print("target:", sample["target_log10_ne"].shape)\n'
                    'print("metadata keys:", list(sample["metadata"].keys())[:20])'
                ),
            ]
        ),
        "01_visualize_single_sample.ipynb": _notebook(
            [
                _md(f"# Visualize One Sample\n\n{intro}\n\nThis notebook visualizes the raw observations and target for one sample."),
                _code(COMMON_SETUP),
                _code(
                    "import pandas as pd\n\n"
                    "from scripts.loader import DLCoalSimShard\n"
                    "from scripts.viz import (\n"
                    "    plot_target_curve,\n"
                    "    plot_genotype_heatmap,\n"
                    "    plot_variant_density,\n"
                    "    plot_observed_maps,\n"
                    "    show_metadata_card,\n"
                    ")"
                ),
                _code("df = pd.read_csv(META_PATH)\nshard = DLCoalSimShard(SHARD_PATH)\nsample = shard.get(0)\nsample['metadata']"),
                _md("The target is a log-time bin average of log10 Ne, not a single midpoint sample."),
                _code("plot_target_curve(sample, time_mids=TIME_MIDS)"),
                _md("Rows are variant sites and columns are haplotypes. Adjacent haplotype columns are the two haplotypes of one diploid individual."),
                _code("plot_genotype_heatmap(sample, max_variants=300, max_haplotypes=64)"),
                _code("plot_variant_density(sample, n_windows=200)"),
                _code("plot_observed_maps(sample)"),
                _code("show_metadata_card(sample)"),
            ]
        ),
        "02_dataset_overview.ipynb": _notebook(
            [
                _md(f"# Dataset Overview\n\n{intro}\n\nThis notebook reads metadata only for most plots, then samples a few target curves from shards."),
                _code(COMMON_SETUP),
                _code("import pandas as pd\n\nfrom scripts.viz import plot_column_distribution, plot_numeric_distribution, plot_random_targets"),
                _code("df = pd.read_csv(META_PATH)\ndf.head()"),
                _code("plot_column_distribution(df, 'source_type')"),
                _code("plot_column_distribution(df, 'demography_type')"),
                _code("plot_column_distribution(df, 'generator_recipe')"),
                _code("plot_column_distribution(df, 'map_mode')"),
                _code("plot_column_distribution(df, 'noise_profile')"),
                _code("plot_numeric_distribution(df, 'n_variants', bins=50)"),
                _code("plot_numeric_distribution(df, 'Ne_ratio_max_min', bins=50)"),
                _code("plot_numeric_distribution(df, 'variant_density_per_mb', bins=50)"),
                _code("plot_random_targets(DATA_DIR, df, n=20, time_mids=TIME_MIDS, random_state=0)"),
            ]
        ),
        "03_filter_by_metadata.ipynb": _notebook(
            [
                _md(f"# Filter By Metadata\n\n{intro}\n\nThe simulator creates a fully annotated sample pool. Use metadata to create your own train/validation/test or stress subsets."),
                _code(COMMON_SETUP),
                _code("import pandas as pd\n\ndf = pd.read_csv(META_PATH)\ndf.head()"),
                _code("recent = df[df['has_recent_event'] == True]\nrecent.head()"),
                _code("clean = df[df['source_type'] == 'clean_constant_map']\nclean.head()"),
                _code("strong = df[df['Ne_ratio_max_min'] > 20]\nstrong.head()"),
                _code("empirical = df[df['map_mode'] == 'empirical_slice']\nempirical.head()"),
                _code("high_quality = df[(df['variant_density_per_mb'] > 100) & (df['missing_rate'] < 0.01)]\nhigh_quality.head()"),
                _code(
                    "OUT = DATA_DIR / 'benchmarks' / 'tutorial_examples'\n"
                    "OUT.mkdir(parents=True, exist_ok=True)\n"
                    "recent['sample_id'].to_csv(OUT / 'recent_samples.txt', index=False, header=False)\n"
                    "strong['sample_id'].to_csv(OUT / 'strong_demography_samples.txt', index=False, header=False)\n"
                    "OUT"
                ),
            ]
        ),
        "04_compute_basic_features.ipynb": _notebook(
            [
                _md(f"# Compute Basic Features\n\n{intro}\n\nDLCoalSim-Core does not precompute SFS, LD, pi, or haplotype features. This notebook shows a baseline window feature recipe that you can replace."),
                _code(COMMON_SETUP),
                _code("import matplotlib.pyplot as plt\n\nfrom scripts.loader import DLCoalSimShard\nfrom scripts.features import basic_window_stats"),
                _code("shard = DLCoalSimShard(SHARD_PATH)\nsample = shard.get(0)\n\nG = sample['genotypes']\npos = sample['positions_bp']\nseq_len = sample['sequence_length']\n\nX = basic_window_stats(pos, G, seq_len, n_windows=512)\nX.shape"),
                _code("plt.figure(figsize=(8, 3))\nplt.plot(X[:, 0])\nplt.xlabel('Window')\nplt.ylabel('SNP density')\nplt.tight_layout()\nplt.show()"),
                _code("plt.figure(figsize=(8, 3))\nplt.plot(X[:, 1])\nplt.xlabel('Window')\nplt.ylabel('Pi proxy')\nplt.tight_layout()\nplt.show()"),
                _code("plt.figure(figsize=(8, 3))\nplt.plot(X[:, 2])\nplt.xlabel('Window')\nplt.ylabel('Singleton fraction')\nplt.tight_layout()\nplt.show()"),
                _md("The output `X` is only a baseline recipe. For model development, build your own feature extractor from haplotypes, variant positions, and observed maps."),
            ]
        ),
        "05_quality_check_preview.ipynb": _notebook(
            [
                _md(f"# Quality Check Preview\n\n{intro}\n\nUse this notebook for quick sanity checks before sharing or training on a generated dataset."),
                _code(COMMON_SETUP),
                _code("import numpy as np\nimport pandas as pd\n\nfrom scripts.validate_dataset import validate_dataset"),
                _code("df = pd.read_csv(META_PATH)\ndf.describe(include='all')"),
                _code("assert df['n_variants'].ge(0).all()\nassert df['sequence_length'].gt(0).all()\nassert np.isfinite(df['variant_density_per_mb']).all()\nassert np.isfinite(df['mean_obs_rec_rate']).all()\nassert np.isfinite(df['mean_obs_mut_rate']).all()\nprint('metadata checks passed')"),
                _code("df[['source_type', 'demography_type', 'generator_recipe', 'map_mode', 'noise_profile']].apply(lambda s: s.value_counts()).fillna(0)"),
                _code("df[['n_variants', 'variant_density_per_mb', 'missing_rate', 'n_missing_genotypes', 'mean_obs_rec_rate', 'mean_obs_mut_rate', 'Ne_ratio_max_min']].hist(figsize=(12, 8), bins=40);"),
                _code("validate_dataset(DATA_DIR)"),
            ]
        ),
    }


def write_dataset_files(cfg: Config, sample_manifest: dict) -> None:
    validate_config(cfg)
    out = Path(cfg.out_dir)
    out.mkdir(parents=True, exist_ok=True)
    manifest_path = out / "manifest.json"
    manifest_path.unlink(missing_ok=True)
    manifest_path.with_name(f"{manifest_path.name}.tmp").unlink(missing_ok=True)

    def write_atomic(path: Path, text: str) -> None:
        tmp_path = path.with_name(f"{path.name}.tmp")
        tmp_path.write_text(text, encoding="utf-8")
        tmp_path.replace(path)

    edges, mids = time_grid(cfg)
    config_dict = dataclasses.asdict(cfg)
    config_dict["time_edges"] = edges.tolist()
    config_dict["time_mids"] = mids.tolist()
    config_dict["source_ids"] = SOURCE_IDS
    config_dict["demography_type_ids"] = DEMOGRAPHY_TYPE_IDS
    config_dict["generator_recipe_ids"] = GENERATOR_RECIPE_IDS
    config_dict["generator_schema_version"] = SAMPLE_GENERATOR_SCHEMA_VERSION
    write_atomic(
        out / "config.json",
        json.dumps(config_dict, indent=2, ensure_ascii=False),
    )

    manifest = {
        "dataset_name": cfg.dataset_name,
        "created_at": time.strftime("%Y-%m-%d %H:%M:%S"),
        "samples": sample_manifest,
        "time_bins": int(cfg.time_bins),
        "time_edges": edges.tolist(),
        "time_mids": mids.tolist(),
        "demography_resolution_log10": float(cfg.demography_resolution_log10),
        "source_ids": SOURCE_IDS,
        "demography_type_ids": DEMOGRAPHY_TYPE_IDS,
        "generator_recipe_ids": GENERATOR_RECIPE_IDS,
        "generator_schema_version": SAMPLE_GENERATOR_SCHEMA_VERSION,
    }
    (out / "README.md").write_text(README_TEMPLATE.format(dataset_name=cfg.dataset_name), encoding="utf-8")

    scripts = out / "scripts"
    scripts.mkdir(exist_ok=True)
    (scripts / "__init__.py").write_text("", encoding="utf-8")
    (scripts / "features.py").write_text(FEATURES_PY, encoding="utf-8")
    (scripts / "loader.py").write_text(
        LOADER_PY.replace(
            "__GENERATOR_SCHEMA_VERSION__", str(SAMPLE_GENERATOR_SCHEMA_VERSION)
        ),
        encoding="utf-8",
    )
    (scripts / "viz.py").write_text(VIZ_PY, encoding="utf-8")
    (scripts / "validate_dataset.py").write_text(
        VALIDATE_DATASET_PY.replace(
            "__GENERATOR_SCHEMA_VERSION__", str(SAMPLE_GENERATOR_SCHEMA_VERSION)
        ),
        encoding="utf-8",
    )

    notebooks = out / "notebooks"
    notebooks.mkdir(exist_ok=True)
    for name, notebook in tutorial_notebooks().items():
        (notebooks / name).write_text(json.dumps(notebook, indent=2, ensure_ascii=False), encoding="utf-8")

    # The root manifest is the final publication marker for all dataset support
    # files; publish it atomically only after every other artifact succeeds.
    write_atomic(
        manifest_path,
        json.dumps(manifest, indent=2, ensure_ascii=False),
    )