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
here are node tables that are not equals( ) and yet have no detectable differences #3244
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.
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.
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 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.
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.
That sounds good. I think there's two other things to do:
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.)
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?
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.
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.
Activity
petrelharp commentedon Aug 1, 2025
The problem is with the
metadata_schema: the bit of the C code that returns false is this one:Indeed, after doing
then
but
And the difference seems to be because of ordering in the OrderedDict representations:
Here's the two
examplesentries:Digging in further, equality of metadata schema is determined by equality of the underlying
_stringattributes, which was produced bytskit.canonical_json. Here we haveSo what is the
.schemaattribute? 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 theirschema. And at this point I'm a bit lost about what to do, honestly.petrelharp commentedon Aug 1, 2025
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_equalsthen we just need to add a clause to it that checks if theMetadataSchema.schemaare equal (not just theMetadataSchema).The latter is certainly easier.
petrelharp commentedon Aug 1, 2025
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 reasontskit.NodeTable.set_columns"resets" the metadata schema. Note thattskit.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.jeromekelleher commentedon Aug 1, 2025
@benjeffery any thoughts on this one?
benjeffery commentedon Aug 4, 2025
Easiest way seems to be to fix the python
assert_equalsto 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.petrelharp commentedon Aug 4, 2025
That sounds good. I think there's two other things to do:
at least document the fact that you can have
schema1 == schema2butschema1.schema != schema2.schema(But note if we changed this behavior then this would fix this issue also.)It seems to me that also the fact that
set_columnsresets the metadata schema (whilecleardoes not) is a different bug? What do you think about that?benjeffery commentedon Aug 5, 2025
I've filed #3246 for the assert equals case. As for
set_columnsthe 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.petrelharp commentedon Aug 7, 2025
Sorry, my point is that both
clearandset_columnskeeps the metadata schema, butclearkeeps it at the byte level whileset_columnsre-encodes it. I've filed a new bug for that one, #3249.