Skip to content

Add metadata criteria to TreeSequence row accessors. #2239

Open

Description

@benjeffery

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.

Activity

  1. added
    enhancement
    Python API
    on May 4, 2022
  2. hyanwong commented on Sep 27, 2022

    @hyanwong
    Member

    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")?

  3. benjeffery commented on Sep 27, 2022

    @benjeffery
    MemberAuthor

    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.

  4. modified the milestones: Python 0.5.6, on Oct 9, 2023
  5. hyanwong commented on Jun 4, 2025

    @hyanwong
    Member

    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:

    1. Variable argument types (yuck)
    2. Marginally less efficient (have to check if it's an int or dict), and more complex accessor code
    3. The name id_ is a bit overloaded
    4. Slightly messy having to raise an error for multiple nodes (but I can't see how else to do it)
    5. 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:

    1. Explicitly disallows providing both ID and metadata-match params
    2. 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
    3. No kwarg name-collision
    4. Nice consistent extension (c) when multiple objects can be returned.
  6. benjeffery commented on Jun 4, 2025

    @benjeffery
    MemberAuthor

    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.

  7. modified the milestones: , Python 1.0 on Jun 4, 2025
  8. hyanwong commented on Jun 4, 2025

    @hyanwong
    Member

    What if we want a single population, e.g. named "EUR"? This is perhaps the most common use-case. Would we need to do

    list(ts.populations_by_metadata(name="EUR"))[0]

    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_metadata AND ts.population_by_metadata methods, the latter erroring out when there are multiple matches?

  9. jeromekelleher commented on Jun 4, 2025

    @jeromekelleher
    Member

    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.

  10. hyanwong commented on Jun 4, 2025

    @hyanwong
    Member

    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

  11. benjeffery commented on Jun 4, 2025

    @benjeffery
    MemberAuthor

    I'd expect something like:

    eur_pop = next(ts.population_by_metadata(name="EUR"))
    pop_samples  = ts.samples_by_population(eur_pop.id)

    ?

  12. hyanwong commented on Jun 4, 2025

    @hyanwong
    Member

    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:

    pop_samples = next(ts.population_by_metadata(name="EUR")).samples
    
  13. benjeffery commented on Jun 4, 2025

    @benjeffery
    MemberAuthor

    Ok, we have site.mutations so pop.samples makes sense too.

  14. jeromekelleher commented on Jun 4, 2025

    @jeromekelleher
    Member

    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.

  15. hyanwong commented on Jun 4, 2025

    @hyanwong
    Member

    Let's hold on a bit on this please

    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).

  16. removed this from the Python 1.0 milestone on Nov 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Python APIenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions