Skip to content

here are node tables that are not equals( ) and yet have no detectable differences #3244

Closed

Description

@petrelharp

This tree sequence: ts1.zip and this one: ts2.zip (note, these are not tszipped despite the suffix) have node tables that if you do
tsa.tables.nodes.assert_equals(tb.tables.nodes) then you get:

Traceback (most recent call last):
  File "<python-input-28>", line 1, in <module>
    ta.assert_equals(tb)
    ~~~~~~~~~~~~~~~~^^^^
  File "/home/peter/projects/tskit-dev/tsvenv/lib/python3.13/site-packages/tskit/tables.py", line 465, in assert_equals
    raise AssertionError(
    ^^^^^^^^^^^^^^^^^^^^^
    ...<2 lines>...
    )  # pragma: no cover
    ^
AssertionError: Tables differ in an undetected way - this is a bug, please report an issue on gitub

Indeed, examining their flags, population, individual, time, metadata, and metadata_offset vectors and their metadata_schema individually says they're all identical. The way this error comes up is if the lower-level tree sequence equals method returns False but then we can't find the difference in python.

The problem seems to be in metadata, since doing

>>> tsa.tables.assert_equals(tsb.tables, ignore_metadata=True)

does not produce this error.

Activity

  1. petrelharp commented on Aug 1, 2025

    @petrelharp
    ContributorAuthor

    The problem is with the metadata_schema: the bit of the C code that returns false is this one:

                  && tsk_memcmp(self->metadata_schema, other->metadata_schema,
                         self->metadata_schema_length * sizeof(char))
                         == 0;
    

    Indeed, after doing

    ts1 = tskit.load("ts1.zip")
    ts2 = tskit.load("ts2.zip")
    a = ts1.tables.nodes.metadata_schema
    b = ts2.tables.nodes.metadata_schema
    

    then

    # a == b
    True
    

    but

    #  a.schema == b.schema
    False
    

    And the difference seems to be because of ordering in the OrderedDict representations:

    # dict(b.schema['examples'][0]) == dict(b.schema['examples'][0])
    True
    

    Here's the two examples entries:

    >>> a.schema['examples']
    [OrderedDict({'slim_id': 123, 'is_vacant': 0})]
    >>> b.schema['examples']
    [OrderedDict({'is_vacant': 0, 'slim_id': 123})]
    

    Digging in further, equality of metadata schema is determined by equality of the underlying _string attributes, which was produced by tskit.canonical_json. Here we have

    >>> a == b
    True
    >>> a.schema == b.schema
    False
    >>> a._schema == b._schema
    True
    >>> a._string == b._string
    True
    

    So what is the .schema attribute? It is the underlying ._unmodified_schema, which is just whatever got passed in to create the schema in the first place.

    So, the problem is coming in because equality of MetadataSchemas is not the same thing as equality of their schema. And at this point I'm a bit lost about what to do, honestly.

  2. petrelharp commented on Aug 1, 2025

    @petrelharp
    ContributorAuthor

    Where's the problem? It's either our notion of equality or in the implementation of assert_equals.

    If it's the notion of equality, the problem is that (a) we'd like to do equality that doesn't care about order, but (b) that stuff isn't accessible at the C level, which is just comparing bytes. So a fix would be to store a canonicalized JSON string and compare on that instead of whatever was passed in.

    If it's the implementation of assert_equals then we just need to add a clause to it that checks if the MetadataSchema.schema are equal (not just the MetadataSchema).

    The latter is certainly easier.

  3. petrelharp commented on Aug 1, 2025

    @petrelharp
    ContributorAuthor

    The reason I hit this seems to be because the ordering that SLiM has got the node metadata schema in isn't the same as what tskit.canonical_json( ) produces (whoops), and for some reason tskit.NodeTable.set_columns "resets" the metadata schema. Note that tskit.NodeTable.clear() does not reset the metadata, so the more usual "clear; for x in y; append" pattern doesn't end up rewriting the metadata schema like this.

  4. jeromekelleher commented on Aug 1, 2025

    @jeromekelleher
    Member

    @benjeffery any thoughts on this one?

  5. benjeffery commented on Aug 4, 2025

    @benjeffery
    Member

    Easiest way seems to be to fix the python assert_equals to detect this state of events where the only difference in C is different order metadata schema dicts, and allow that as equality. Happy to pick that up.

  6. petrelharp commented on Aug 4, 2025

    @petrelharp
    ContributorAuthor

    That sounds good. I think there's two other things to do:

    1. at least document the fact that you can have schema1 == schema2 but schema1.schema != schema2.schema (But note if we changed this behavior then this would fix this issue also.)

    2. It seems to me that also the fact that set_columns resets the metadata schema (while clear does not) is a different bug? What do you think about that?

  7. benjeffery commented on Aug 5, 2025

    @benjeffery
    Member

    I've filed #3246 for the assert equals case. As for set_columns the idea there is that by not specifying a column you are clearing it. I'm not sure we could change the behaviour here without possible breakage.

  8. petrelharp commented on Aug 7, 2025

    @petrelharp
    ContributorAuthor

    As for set_columns the idea there is that by not specifying a column you are clearing it. I'm not sure we could change the behaviour here without possible breakage.

    Sorry, my point is that both clear and set_columns keeps the metadata schema, but clear keeps it at the byte level while set_columns re-encodes it. I've filed a new bug for that one, #3249.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions