Skip to content

make sort stricter #705

Closed
Closed

Description

@petrelharp

With subset, we can reorder tables. But we still can't easily take two sets of completely equivalent tables and put them in the same order. From the requirements:

  • nodes we can order using subset
  • pops and indivs will be ordered by earliest node by subset
  • edge order is undefined among parents of the same time
  • mutation order is undefined among mutations of the same time
  • migration order is undefined among migrations of the same time

The ordering could be total by adding parent to the edge sort key, and node to the mutation and migration sort keys.

This would be nice to do since when doing union #623 we would like to make sure that when subsetting on the shared nodes the tables are equal. It'd probably be useful for other testing.

Table sorting is a bottleneck in forwards sims, so perhaps we want to make the "strict sort" optional; and if strict=True we first sort with the above additions to the keys, then do subset(range(num_nodes)). This would be guaranteed to always get the same order no matter the original order of the tables.

Activity

  1. jeromekelleher commented on Jul 3, 2020

    @jeromekelleher
    Member

    Sounds good @petrelharp, I'm all for making a canonical form. I agree we can't introduce these extra restrictions as requirements. I doubt it would make any difference performance-wise, but I don't think we can start sorting populations and individuals by default because this will surely break people's code.

    I think we should use "canonical" as the term rather than strict, to emphasise we're putting tables into a canonical form.

  2. mufernando commented on Jul 6, 2020

    @mufernando
    Member

    Well, it seems the sorting of edges surpass the requirements, because in practice they are being sorted by time, then parent node id, child node id and lastly the left coordinate:

    tskit/c/tskit/tables.c

    Lines 4325 to 4344 in 1374dbb

    cmp_edge(const void *a, const void *b)
    {
    const edge_sort_t *ca = (const edge_sort_t *) a;
    const edge_sort_t *cb = (const edge_sort_t *) b;
    int ret = (ca->time > cb->time) - (ca->time < cb->time);
    /* If time values are equal, sort by the parent node */
    if (ret == 0) {
    ret = (ca->parent > cb->parent) - (ca->parent < cb->parent);
    /* If the parent nodes are equal, sort by the child ID. */
    if (ret == 0) {
    ret = (ca->child > cb->child) - (ca->child < cb->child);
    /* If the child nodes are equal, sort by the left coordinate. */
    if (ret == 0) {
    ret = (ca->left > cb->left) - (ca->left < cb->left);
    }
    }
    }
    return ret;
    }

    So we only need to do add sorting of mutations by nodes if time is tied.

  3. petrelharp commented on Jul 6, 2020

    @petrelharp
    ContributorAuthor

    Ok, great! We should make this clear in the documentation.

  4. mufernando commented on Jul 6, 2020

    @mufernando
    Member

    Ah, also curious about #627, which changes the sorting API.

    Should we go off of that?

  5. jeromekelleher commented on Jul 6, 2020

    @jeromekelleher
    Member

    Ah, also curious about #627, which changes the sorting API.

    Hah, good slot @mufernando. Yes, it would probably be best to base your code off that as I've made some changes there. Better still, why don't I just finish that PR...

  6. mufernando commented on Jul 8, 2020

    @mufernando
    Member

    Some notes on a convo @petrelharp and I had earlier today:

    1. The proposed stricter sorting would make the tables canonical given the order of the nodes.
    2. It's not possible to make the tables canonical without specifying the order of at least the leaves.
    3. If we add (time, node) to the mutation sort key, then if time is missing, this might break things if it ends up just sorting on nodes, which aren't necessarily ordered by time.
    4. The proposal actually needs one more thing: the mutation parent, to be canonical, because mutations can share time and node.
    5. If we add (time, mutation parent, node) to the sort key for mutations, then this would produce a valid set of tables with child mutations always coming after their parents, as long as either (a) mutation times are present, or (b) child mutations come after parents, and parents are specified, in the original tables.
    6. This would not guarantee to maintain the relative ordering of mutations if mutation times and parents are not specified, as our current implementation does, which is important for SLiM, but won't be important for SLiM since SLiM will be adding in mutation time. So, it is changing this behavior, but probably only SLiM uses it.

    thoughts @jeromekelleher? waiting on your input to get started (mostly depends on whether we will modify the current sort definitively, make it an option, or create a different method altogether).

  7. jeromekelleher commented on Jul 9, 2020

    @jeromekelleher
    Member

    This all sounds good @mufernando. Things do get complicated with mutation times being unknown and so on.

    I think it's worth sketching this up in Python before diving in to the C code actually. We'll need some test cases which have mixed known/unknown times, have/and are missing mutation parents. Getting these test cases nailed down and having a (simple) Python sorter where we can quickly get to the semantics that we want will help a lot.

  8. hyanwong commented on Jul 30, 2020

    @hyanwong
    Member

    Just to chip in to say that I depend on having a table sort function that does not change node IDs, so I'd like that to remain the default (which I think was the general idea, anyway, right?). Also that the docs mention "canonical form" for sorting, so we probably want to change the phrasing if by "canonical" we are now meaning a strict sort.

  9. added
    enhancement
    on Sep 29, 2020
  10. added a commit that references this issue on Jan 2, 2021
    dcb8929
  11. added a commit that references this issue on Jan 29, 2021
    28ca24d
  12. added a commit that references this issue on Feb 2, 2021
    986db65
  13. added a commit that references this issue on Feb 2, 2021
    5011987
  14. added a commit that references this issue on Feb 3, 2021
    5bfe9cd
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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions