from ergodic import discover
from ergodic.datasets import load_asia
from ergodic.discovery import bootstrap

# 1. Sample 1,000 patients and run GES once.
asia = load_asia(n=1_000, seed=0)
single = discover(asia.data, method="ges").graph
print(single)

# 2. Rerun GES on 100 resamples of the rows.
boot = bootstrap(asia.data, method="ges", n_bootstrap=100, seed=0)
print(boot)

# 3. How often was each pair adjacent?
for pair, share in sorted(boot.edges.items(), key=lambda item: -item[1])[:10]:
    print(f"{' - '.join(sorted(pair)):16} {share:.2f}")

# 4. Which marks did one pair wear?
for glyph in ["bronc --- smoke", "bronc --> smoke", "bronc <-- smoke"]:
    print(glyph, boot.marks.get(glyph, 0.0))

# 5. Keep the edges found in at least half the runs.
stable = boot.summary(threshold=0.5)
print(stable)
print("dropped:", sorted({e.glyph() for e in single.edges} - {e.glyph() for e in stable.edges}))
