Skip to content

Add top-level "time_units" field #1644

Closed

Description

@jeromekelleher

As @hyanwong suggests in #495 (comment) there are very good reasons for having a top-level time_units field in the tree sequence/table collection/file format. One use could be to issue a warning if people are using branch length statistics and using uncalibrated time units, where different branch lengths are not comparable.

I guess the type should be a string. We should probably have a number of "known" values. Two I can think of are

  • generations_ago
  • years_ago
  • uncalibrated

I think the "ago" suffix is necessary, because in principle these could be actual datetime values (we could change the time-travel requirement depending then, if so). Tsinfer (and any other method producing arbitrary-ish time values) should use "uncalibrated". The branch length stats would then issue a warning (in Python) if computed on "uncalibrated" tree sequences.

In terms of how we handle compatibility, I think we have to assume generations_ago if the time_units field isn't present. Otherwise, we would suddenly start issuing confusing warnings to people using existing tree sequences.

How do we deal with time_units that are unknown to the library? Do we

  1. Hard error in C and refuse to create a tree sequence
  2. Raise a warning at load time in Python and treat as "uncalibrated"?
  3. Something else?

Activity

  1. added
    enhancement
    on Aug 24, 2021
  2. petrelharp commented on Aug 25, 2021

    @petrelharp
    Contributor

    For a related issue, see bodkan/slendr#61

  3. jeromekelleher commented on Aug 26, 2021

    @jeromekelleher
    MemberAuthor

    Related in that you'd like to see top-level info on the spatial coordinate system?

  4. petrelharp commented on Aug 26, 2021

    @petrelharp
    Contributor

    Right - well, if we have time_units then we maybe also want to have spatial_units; and there it's a bit more complicated than just putting meters, so we should design the metadata to accomadate this sort of thing. Maybe 'units' : { 'time' : 'generations, 'space': XXX } or something...

  5. jeromekelleher commented on Aug 26, 2021

    @jeromekelleher
    MemberAuthor

    We don't have any actual use within the library for that, though, do we? That's the hurdle for metadata vs data - the library has to do something with it.

  6. petrelharp commented on Aug 26, 2021

    @petrelharp
    Contributor

    Nope - but it's helpful to have more than one example.

    However, I do think that time_units is really naturally metadata in the colloquial sense of the word, and so we really might as well put it in metadata, even if the library makes use of it. Won't we save ourselves a bunch of coding hassle (ie changing the kastore format)?

  7. hyanwong commented on Aug 27, 2021

    @hyanwong
    Member

    I'm ambivalent about whether time_units is in metadata or is stored more like attributes such as sequence_length. True, we foresee that the library might use it, but it's only to raise an error if it's set to an inappropriate value, so is a relatively minor thing that isn't actually required to enable functionality (merely to disable existing functionality), so I can see Peter's pragmatic argument about saving developer time. OTOH that would be breaking the purity of the existing metadata vs inherent properties scheme, and would require other metadata users to derive metadata schemas from a base format rather than create a schema de-novo, so that places a burden on users rather than developers to do the right thing. 🤷

  8. jeromekelleher commented on Aug 27, 2021

    @jeromekelleher
    MemberAuthor

    I think putting it in as metadata it basically rules out us ever using time_units for anything useful, because we'll never be able to access it in the C library if we do. It seems like quite a fundamental thing, to me, so excluding it from the C library forever seems unwise.

    In practise, the amount of developer effort to implement this will be basically the same, either way.

  9. petrelharp commented on Aug 27, 2021

    @petrelharp
    Contributor

    Ah, that's right.

  10. added
    C API
    on Sep 2, 2021
  11. added this to the C API 1.0.0 milestone on Sep 2, 2021
  12. benjeffery commented on Sep 2, 2021

    @benjeffery
    Member

    I've added this to the C1.0 milestone as we should consider this before we release the supported API.

    I think assuming legacy tree-sequences to be gererations-ago isn't quite right as for many that won't be right and could cause confusion/errors. Might it be better to have an unset or legacy value that doesn't raise errors?

  13. jeromekelleher commented on Sep 2, 2021

    @jeromekelleher
    MemberAuthor

    Yes, I agree. We should also have "unknown" which is the default value, and keeps the existing behaviour.

    Then, I think we should actually error out of C API methods that are related to branch lengths if time_units="uncalibrated" with TSK_ERR_TIME_UNCALIBRATED. We can provide an option to methods like branch stats TSK_FORCE_TIME_UNCALIBRATED, which will suppress this error. Then, it's pretty clear that users are making an informed choice about using uncalibrated time.

    It's not clear whether the Python behaviour should be to warn or error - I guess adding the option force_time_uncalibrated to methods isn't too bad?

  14. benjeffery commented on Sep 2, 2021

    @benjeffery
    Member

    Yep seems erroring on fixed string(s) and letting everything else through is best.

    An error in python, whose message says about the option makes sense to me?

    I prefer ALLOW_TIME_UNCALIBRATED to FORCE but that's only a minor thing.

  15. petrelharp commented on Sep 2, 2021

    @petrelharp
    Contributor

    Just to be clear here, it's only tsinfer that'll (currently) be making these uncalibrated tree sequences, right? And for unknown, we won't error? If so, this sounds good.

    I prefer ALLOW_TIME_UNCALIBRATED to FORCE but that's only a minor thing.

    Me, too.

  16. jeromekelleher commented on Sep 2, 2021

    @jeromekelleher
    MemberAuthor

    Yeah, AFAIK tsinfer is the only thing that makes tree sequences with uncalibrated time, and yes, the default will be to assume "unknown", which will maintain the current behviour. So, we can't break anyone's code.

    ALLOW_TIME_UNCALIBRATED

    Sure, SGTM

  17. hyanwong commented on Sep 29, 2021

    @hyanwong
    Member

    Great that this is getting sorted. Thanks @benjeffery and @jeromekelleher . One thing: in tsinfer generated tree sequences, do we want to have time_units="uncalibrated" or something like time_units="frequency-of-derived-allele"? I ask because of a silly corner case concerning the tsinfer.SampleData.from_tree_sequence(ts) method. In particular, should we allow historical samples to be set to non-zero times if the generated tree sequence uses frequency of derived allele as the time unit? If we do, we should probably ensure that the ts in this case has units that match the time_units of the tree sequence produced from .from_tree_sequence.

  18. jeromekelleher commented on Sep 29, 2021

    @jeromekelleher
    MemberAuthor

    I think it's simpler to just make them uncalibrated in tsinfer @hyanwong - the point here is to make users aware that they shouldn't use the time values for anything in particular. If we were to try saying "frequency-of-derived-allele" we'd have to worry about what to do with times of the ultimate roots and also new nodes introduced by path compression.

    We can work around the details in from_tree_sequence in tsinfer.

  19. hyanwong commented on Sep 29, 2021

    @hyanwong
    Member

    Yeah, I think you're right. It's just that they are a specific sort of uncalibrated, which might, I suppose, be useful to know (that's what I hit in the case I explained above). But it's such a minor thing that it's hardly worth worrying about.

  20. jeromekelleher commented on Sep 29, 2021

    @jeromekelleher
    MemberAuthor

    It's just that they are a specific sort of uncalibrated, which might, I suppose, be useful to know (that's what I hit in the case I explained above).

    I agree, but trying to communicate that some times are frequency and some are not would be very tricky. Simpler to just say "times are uncalibrated, and we can change them arbitarily between versions"

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

    C APIenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions