Skip to content

Conversation

@hyanwong
Copy link
Member

@hyanwong hyanwong commented May 23, 2025

The last step to allow demographies to be instantiated from provenance entries. We exceptionally allow a demography.Population object to be created with an existing ID, but only if generated when decoding provenance JSON. This ensures that the IDs in the events etc match with what we expect. It's a slight hack in that we check for the error message. The alternative is to pick a different error (?RuntimeError) or make our own bespoke error that can be allowed through in this case.

@hyanwong hyanwong force-pushed the pop-id-provenance-use branch from 7403228 to 28bf96b Compare May 23, 2025 18:30
@codecov
Copy link

codecov bot commented May 23, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.23%. Comparing base (7f854ac) to head (dec2a42).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2370   +/-   ##
=======================================
  Coverage   91.23%   91.23%           
=======================================
  Files          20       20           
  Lines       11831    11836    +5     
  Branches     2296     2298    +2     
=======================================
+ Hits        10794    10799    +5     
  Misses        568      568           
  Partials      469      469           
Flag Coverage Δ
C 91.23% <100.00%> (+<0.01%) ⬆️
python 98.69% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hyanwong
Copy link
Member Author

hyanwong commented May 23, 2025

With this in place, you can plot a demes demography from a tree seq generated by stdpopsim, without having to install stedpopsim, know the model etc., which is neat:

import demesdraw
ts = tskit.load("from_msprime_stdpopsim.trees")

for provenance in ts.provenances():
    try:
        cmd, params = msprime.provenance.parse_provenance(provenance, ts)
        if cmd == "sim_ancestry":
            demesdraw.tubes(params["demography"].to_demes())
    except ValueError
        pass

Copy link
Member

@jeromekelleher jeromekelleher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a different way of instantiating Demography objects

try:
demography_object.__init__(**obj)
except ValueError as e:
if str(e).startswith("Population ID should not be set"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This control flow by exception logic is fragile and distributes the logic of creating Demography objects around different modules. We should add a Demography.fromdict method that does this, and can be tested independently;.

Copy link
Member Author

@hyanwong hyanwong Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm returning to some of my open PRs, and realise that this one has an easy fix, because I just found out that you can pass an InitVar parameter to a dataclass. In this case we use it to flag not to worry about making new population IDs in this specific case. I think this is probably the minimally intrusive thing to do.

@hyanwong hyanwong force-pushed the pop-id-provenance-use branch from 28bf96b to dec2a42 Compare December 5, 2025 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants