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
Add metadata criteria to TreeSequence row accessors. #2239
Something like ts.node(metadata=('key', 'subkey', 'value') would be a handy convenience to have. It would mean things like ts.node(metadata=('name', 'es423')) would work well or ts.individual(metadata=('accession','id','BOB')).
We considered hardcoding for example ts.node(name="es423") but this would be very restrictive unless we allowed any kwarg which would then be a name-collision compatibility problem if we ever added any other kwargs to the method.
We considered hardcoding for example ts.node(name="es423") but this would be very restrictive unless we allowed any kwarg which would then be a name-collision compatibility problem if we ever added any other kwargs to the method.
It this is something we do want, could we also have a separate method as syntactic sugar, e.g. node_from_metadata(name="my_node")?
I'm not sure I see the advantage over say ts.node(metadata_query={'name':'my_node'}) We already have too many methods on the ts class so there has to be a compelling reason to add one.
Revisiting this in light of #1697, and two further considerations, in addition to the problem of name-collision compatibility with kwargs:
1 . One problem is that the metadata query e.g. for nodes could potentially return more than one node (there is no guarantee that the names are unique, although msprime does guarantee that for populations).
2. The id_ argument to e.g. ts.node(id_) is unwanted if we specify metadata
So my proposal is
a. To allow the id_ argument to these Python objects to be (exceptionally) either an integer or a dict. If a dict, the keys correspond to metadata keys and the values to metadata values: multiple keys are anded together. An example would be ts.population({'name': 'YRI'}) or ts.individual({'accession':'HG001'})
b. If the resulting metadata specification returns more than one object, an error is raised, in the same way as an error is raised when accessing Tree.root in a tree with multiple roots
c. The generator-returning functions like ts.nodes() also allow an argument that is a dictionary (perhaps we require this to be kw-only though), so we can do ts.populations(md={'superpopulation': 'AFR'}) to get a set of populations.
Disadvantages:
Variable argument types (yuck)
Marginally less efficient (have to check if it's an int or dict), and more complex accessor code
The name id_ is a bit overloaded
Slightly messy having to raise an error for multiple nodes (but I can't see how else to do it)
Is it clear that the keys are ANDed rather than e.g. ORed? That's a general problem with any system like this, though
Advantages:
Explicitly disallows providing both ID and metadata-match params
Clear and concise (to me) API: ts.population({"name": "YRI"}) is less typing and more likely to be used than ts.population(metadata={"name": "YRI"}). I would find this easier when teaching this stuff
No kwarg name-collision
Nice consistent extension (c) when multiple objects can be returned.
I'm going to disagree with my past self here and suggest that ts.nodes_by_metadata(key=val) makes more sense than overloading ts.nodes with confusing semantics. I'll add this to 1.0.
I think that's overthinking things a bit @hyanwong - there's loads of cases where a generator returning a single value is accessed by pop = next(ts.populations_by_metadata(name="EUR")). It's not that complicated, and better than complicated overloading semantics.
In my experience having complicated function parameter semantics ends up with unexpected consequences every time. The fact that we might not see what those complications are up front doesn't have any bearing.
Given that ensuring long-term stability is a key organising principle for tskit, we should favour new methods where it makes sense rather than making other methods more complicated --- where it makes sense. I'm with @benjeffery on this one, and metadata searching is something we should do explicitly in a different function namespace.
OK, I guess that means different semantics from ts.samples() then, and we think about that separately.
I was sort-of hoping that you might be able to eventually do ts.population({"name": "EUR"}).samples() but I guess we'll just have to figure out how to specify getting samples by population metadata in some other way. See #1697
That other issue was about ways to avoid having to use the population IDs?
But it would be easy enough to implement a .samples() attribute for a population object, like we have a .population attribute for individuals. That way you could do this, without touching the IDs at all:
Let's hold on a bit on this please - the site.mutations comes directly from the C layer and already exists. I would like to have a concerted discussion about what we actually need for 1.0 and the paper before embarking on significant development projects. Making things slightly nicer for education purposes is not necessary for either, and every PR to review takes time away from the tasks that are vital.
Metadata overall is "good enough" in my opinion, in that users can do what they need to do (albeit in not the most elegant or obvious manner). Other things (like: numba support, ARG traversal and dealing with mutation parents properly) are not and need to be addressed in the very short time available.
Yes, I agree. Other things have priority. I'm just putting down my thoughts here, as I'm hitting these issues when writing tutorial notebooks. There's no urgency to actually do this until we have general agreement.
The main issue (IMO) is that we can't simply get the samples for a given population, by population name. And worse, it appears as if we can, but no error/warning is given: ts.samples(population="AFR") is very natural, doesn't error, but is incorrect (returns an empty list).
Activity
hyanwong commentedon Sep 27, 2022
It this is something we do want, could we also have a separate method as syntactic sugar, e.g.
node_from_metadata(name="my_node")?benjeffery commentedon Sep 27, 2022
I'm not sure I see the advantage over say
ts.node(metadata_query={'name':'my_node'})We already have too many methods on the ts class so there has to be a compelling reason to add one.hyanwong commentedon Jun 4, 2025
Revisiting this in light of #1697, and two further considerations, in addition to the problem of name-collision compatibility with kwargs:
1 . One problem is that the metadata query e.g. for nodes could potentially return more than one node (there is no guarantee that the names are unique, although msprime does guarantee that for populations).
2. The
id_argument to e.g.ts.node(id_)is unwanted if we specify metadataSo my proposal is
a. To allow the
id_argument to these Python objects to be (exceptionally) either an integer or a dict. If a dict, the keys correspond to metadata keys and the values to metadata values: multiple keys areandedtogether. An example would bets.population({'name': 'YRI'})orts.individual({'accession':'HG001'})b. If the resulting metadata specification returns more than one object, an error is raised, in the same way as an error is raised when accessing
Tree.rootin a tree with multiple rootsc. The generator-returning functions like
ts.nodes()also allow an argument that is a dictionary (perhaps we require this to be kw-only though), so we can dots.populations(md={'superpopulation': 'AFR'})to get a set of populations.Disadvantages:
id_is a bit overloadedAdvantages:
ts.population({"name": "YRI"})is less typing and more likely to be used thants.population(metadata={"name": "YRI"}). I would find this easier when teaching this stuffbenjeffery commentedon Jun 4, 2025
I'm going to disagree with my past self here and suggest that
ts.nodes_by_metadata(key=val)makes more sense than overloadingts.nodeswith confusing semantics. I'll add this to 1.0.hyanwong commentedon Jun 4, 2025
What if we want a single population, e.g. named "EUR"? This is perhaps the most common use-case. Would we need to do
I think that's complex enough that people won't use it, TBH. And it doesn't check if you (accidentally) have multiple matches.
Or do we have both
ts.populations_by_metadataANDts.population_by_metadatamethods, the latter erroring out when there are multiple matches?jeromekelleher commentedon Jun 4, 2025
I think that's overthinking things a bit @hyanwong - there's loads of cases where a generator returning a single value is accessed by
pop = next(ts.populations_by_metadata(name="EUR")). It's not that complicated, and better than complicated overloading semantics.In my experience having complicated function parameter semantics ends up with unexpected consequences every time. The fact that we might not see what those complications are up front doesn't have any bearing.
Given that ensuring long-term stability is a key organising principle for tskit, we should favour new methods where it makes sense rather than making other methods more complicated --- where it makes sense. I'm with @benjeffery on this one, and metadata searching is something we should do explicitly in a different function namespace.
hyanwong commentedon Jun 4, 2025
OK, I guess that means different semantics from
ts.samples()then, and we think about that separately.I was sort-of hoping that you might be able to eventually do
ts.population({"name": "EUR"}).samples()but I guess we'll just have to figure out how to specify getting samples by population metadata in some other way. See #1697benjeffery commentedon Jun 4, 2025
I'd expect something like:
?
hyanwong commentedon Jun 4, 2025
That other issue was about ways to avoid having to use the population IDs?
But it would be easy enough to implement a
.samples()attribute for a population object, like we have a .population attribute for individuals. That way you could do this, without touching the IDs at all:benjeffery commentedon Jun 4, 2025
Ok, we have
site.mutationssopop.samplesmakes sense too.jeromekelleher commentedon Jun 4, 2025
Let's hold on a bit on this please - the site.mutations comes directly from the C layer and already exists. I would like to have a concerted discussion about what we actually need for 1.0 and the paper before embarking on significant development projects. Making things slightly nicer for education purposes is not necessary for either, and every PR to review takes time away from the tasks that are vital.
Metadata overall is "good enough" in my opinion, in that users can do what they need to do (albeit in not the most elegant or obvious manner). Other things (like: numba support, ARG traversal and dealing with mutation parents properly) are not and need to be addressed in the very short time available.
hyanwong commentedon Jun 4, 2025
Yes, I agree. Other things have priority. I'm just putting down my thoughts here, as I'm hitting these issues when writing tutorial notebooks. There's no urgency to actually do this until we have general agreement.
The main issue (IMO) is that we can't simply get the samples for a given population, by population name. And worse, it appears as if we can, but no error/warning is given:
ts.samples(population="AFR")is very natural, doesn't error, but is incorrect (returns an empty list).