You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
sim_ancestry(initial_state=ts) does not use population names #1528
Right now, it looks like sim_ancestry(initial_state=ts, demography=d, ...) matches up the populations in the Demography object, in order, to the populations in the initial state. The population table in the output is the same as in the initial state (this is good!). However, there's the possibility for confusion here, because names are not matched. For instance:
shows that 0 and 1, the samples in A, have coalesced, and so have been assigned to what we called B in the Demography.
5 7 6
┃ ┃ ┃
4 ┃ ┃
┏┻┓ ┃ ┃
0 1 2 3
(and, the populations are still labeled A and B, as before).
BTW, if the demography has one more population than the tables, we get this error:
msprime._msprime.InputError: Input error in population configuration: Population ID out of bounds.
and if the tables have one more population than the demography, we get:
ValueError: migration matrix must be a N x N square matrix encoded as a list-of-lists or numpy array, where N is the number of populations defined in the population_configurations. The diagonal elements of this matrix must be zero. For example, a valid matrix for a 3 population system is [[0, 1, 1], [1, 0, 1], [1, 1, 0]]
What if we had a Demography.from_tree_sequence() that returned a demography reflecting the populations in the tree sequence, which tries to recover as much as it can from the metadata? At a minimum it will have the right number of populations and if names/descriptions exist in the metadata it'll use those too. (No reason we couldn't read initial_size etc as well, but let's leave that for now)
I like that idea (it would solve tskit-dev/pyslim#106, in particular). It doesn't totally address this, though - would we then just throw an informative error message if populations don't match?
It doesn't totally address this, though - would we then just throw an informative error message if populations don't match?
Yes, I think we could be stricter then about what we accept as an input demography. I guess what we'd do is
demography_check = Demography.from_tree_sequence(initial_state)
if demography_check.num_populations != demography.num_populations:
raise ValueError("Blah blah")
for check_pop, pop in zip(demography_check.populations, demography.populations):
if check_pop.name != pop.name:
raise ValueError("Incompatible population names")
So, if you follow the pattern above as a user this error should never be thrown (since both are using from_tree_sequence to derive the names), and it should catch errors otherwise.
Ah: suppose we've got a tree sequence with samples in two populations, but want to simulate them having come from a population split some time in the past. This requires adding another population that isn't in the tree sequence we've passed in, like
d = Demography.from_tree_sequence(initial_state)
d.add_population(initial_size=100, name='ancestral')
... but currently, sim_ancestry(initial_state=ts, demograph=d) won't add populations.
Yeah, good point. So, we require that the first initial_state.num_populations pops are equivalent to the ones from initial_state, and we add populations to the tables for any additional populations from the demography.
What will populations not having any lineages in them look like in the result, btw? Can we make it clear that those won't be used?
We won't know that just by inspecting the population table, we'd have to something fancy like create a simulator from the initial state and then inspect it's state. We could then mark those pops that have no lineages initially as "inactive", but it might get complicated.
Activity
jeromekelleher commentedon Mar 12, 2021
Hmm, very nice @petrelharp. Let's see.
What if we had a
Demography.from_tree_sequence()that returned a demography reflecting the populations in the tree sequence, which tries to recover as much as it can from the metadata? At a minimum it will have the right number of populations and if names/descriptions exist in the metadata it'll use those too. (No reason we couldn't readinitial_sizeetc as well, but let's leave that for now)Then, the way to do things would be
Would that be any better?
petrelharp commentedon Mar 12, 2021
I like that idea (it would solve tskit-dev/pyslim#106, in particular). It doesn't totally address this, though - would we then just throw an informative error message if populations don't match?
jeromekelleher commentedon Mar 12, 2021
Yes, I think we could be stricter then about what we accept as an input demography. I guess what we'd do is
So, if you follow the pattern above as a user this error should never be thrown (since both are using from_tree_sequence to derive the names), and it should catch errors otherwise.
What do you think?
jeromekelleher commentedon Mar 12, 2021
Gives a nice way to inspect the populations in the ts too.
petrelharp commentedon Mar 12, 2021
Sounds good!
petrelharp commentedon Mar 12, 2021
What will populations not having any lineages in them look like in the result, btw? Can we make it clear that those won't be used?
petrelharp commentedon Mar 12, 2021
Ah: suppose we've got a tree sequence with samples in two populations, but want to simulate them having come from a population split some time in the past. This requires adding another population that isn't in the tree sequence we've passed in, like
... but currently,
sim_ancestry(initial_state=ts, demograph=d)won't add populations.jeromekelleher commentedon Mar 12, 2021
Yeah, good point. So, we require that the first
initial_state.num_populationspops are equivalent to the ones frominitial_state, and we add populations to the tables for any additional populations from the demography.That should allow us do stuff like
We won't know that just by inspecting the population table, we'd have to something fancy like create a simulator from the initial state and then inspect it's state. We could then mark those pops that have no lineages initially as "inactive", but it might get complicated.
Probably not necessary for a first pass though?
petrelharp commentedon Mar 12, 2021
Totally. Sounds good.
jeromekelleher commentedon Mar 12, 2021
OK, will do this once the current batch of docs updates is done.
Add Demography.from_tree_sequence.
Add Demography.from_tree_sequence.
Add Demography.from_tree_sequence.
Add Demography.from_tree_sequence.
Add Demography.from_tree_sequence.
Add Demography.from_tree_sequence and clarify docs.
Add Demography.from_tree_sequence and clarify docs.