Skip to content

Commit 4d81529

Browse files
committed
Add test for concordence with lwt
1 parent a05f3c1 commit 4d81529

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

python/lwt_interface/dict_encoding_testlib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,8 @@ def test_index(self):
609609
del d["indexes"]
610610
lwt = lwt_module.LightweightTableCollection()
611611
lwt.fromdict(d)
612-
# and a tc without indexes doesn't have the key
613-
assert "indexes" not in lwt.asdict()
612+
# and a tc without indexes has empty dict
613+
assert lwt.asdict()["indexes"] == {}
614614

615615
# Both columns must be provided, if one is
616616
for col in ("insertion", "removal"):

python/lwt_interface/tskit_lwt_interface.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,11 +1594,6 @@ write_table_arrays(tsk_table_collection_t *tables, PyObject *dict)
15941594
};
15951595

15961596
for (j = 0; j < sizeof(table_descs) / sizeof(*table_descs); j++) {
1597-
if (strcmp(table_descs[j].name, "indexes") == 0
1598-
&& !tsk_table_collection_has_index(tables, 0)) {
1599-
continue;
1600-
}
1601-
16021597
table_dict = PyDict_New();
16031598
if (table_dict == NULL) {
16041599
goto out;

python/tests/test_tables.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,10 +1326,7 @@ def test_index(self):
13261326
index = tskit.TableCollectionIndexes()
13271327
assert index.edge_insertion_order is None
13281328
assert index.edge_removal_order is None
1329-
assert index.asdict() == {
1330-
"edge_insertion_order": None,
1331-
"edge_removal_order": None,
1332-
}
1329+
assert index.asdict() == {}
13331330

13341331

13351332
class TestSortTables:
@@ -2265,7 +2262,7 @@ def test_asdict(self):
22652262
t = ts.tables
22662263
self.add_metadata(t)
22672264
d1 = {
2268-
"encoding_version": (1, 1),
2265+
"encoding_version": (1, 2),
22692266
"sequence_length": t.sequence_length,
22702267
"metadata_schema": str(t.metadata_schema),
22712268
"metadata": t.metadata_schema.encode_row(t.metadata),
@@ -2607,6 +2604,41 @@ def test_indexes_roundtrip(self, simple_ts_fixture):
26072604
tables.drop_index()
26082605
assert not tskit.TableCollection.fromdict(tables.asdict()).has_index()
26092606

2607+
def test_asdict_lwt_concordence(self, ts_fixture):
2608+
def check_concordence(d1, d2):
2609+
assert set(d1.keys()) == set(d2.keys())
2610+
for k1, v1 in d1.items():
2611+
v2 = d2[k1]
2612+
assert type(v1) == type(v2)
2613+
if type(v1) == dict:
2614+
assert set(v1.keys()) == set(v2.keys())
2615+
for sk1, sv1 in v1.items():
2616+
sv2 = v2[sk1]
2617+
assert type(sv1) == type(sv2)
2618+
if type(sv1) == np.ndarray:
2619+
assert np.array_equal(sv1, sv2) or (
2620+
np.all(tskit.is_unknown_time(sv1))
2621+
and np.all(tskit.is_unknown_time(sv2))
2622+
)
2623+
elif type(sv1) in [bytes, str]:
2624+
assert sv1 == sv2
2625+
else:
2626+
raise AssertionError()
2627+
2628+
else:
2629+
assert v1 == v2
2630+
2631+
tables = ts_fixture.dump_tables()
2632+
assert tables.has_index()
2633+
lwt = _tskit.LightweightTableCollection()
2634+
lwt.fromdict(tables.asdict())
2635+
check_concordence(lwt.asdict(), tables.asdict())
2636+
2637+
tables.drop_index()
2638+
lwt = _tskit.LightweightTableCollection()
2639+
lwt.fromdict(tables.asdict())
2640+
check_concordence(lwt.asdict(), tables.asdict())
2641+
26102642

26112643
class TestEqualityOptions:
26122644
def test_equals_provenance(self):

python/tskit/tables.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class TableCollectionIndexes:
146146
edge_removal_order: np.ndarray = attr.ib(default=None)
147147

148148
def asdict(self):
149-
return attr.asdict(self)
149+
return attr.asdict(self, filter=lambda k, v: v is not None)
150150

151151

152152
def keep_with_offset(keep, data, offset):
@@ -2081,7 +2081,7 @@ def provenances(self):
20812081
@property
20822082
def indexes(self):
20832083
indexes = self._ll_tables.indexes
2084-
return TableCollectionIndexes(**indexes) if indexes is not None else None
2084+
return TableCollectionIndexes(**indexes)
20852085

20862086
@indexes.setter
20872087
def indexes(self, indexes):
@@ -2140,7 +2140,7 @@ def asdict(self):
21402140
map of table names to the tables themselves was returned.
21412141
"""
21422142
ret = {
2143-
"encoding_version": (1, 1),
2143+
"encoding_version": (1, 2),
21442144
"sequence_length": self.sequence_length,
21452145
"metadata_schema": str(self.metadata_schema),
21462146
"metadata": self.metadata_schema.encode_row(self.metadata),
@@ -2152,9 +2152,8 @@ def asdict(self):
21522152
"mutations": self.mutations.asdict(),
21532153
"populations": self.populations.asdict(),
21542154
"provenances": self.provenances.asdict(),
2155+
"indexes": self.indexes.asdict(),
21552156
}
2156-
if self.indexes is not None:
2157-
ret["indexes"] = self.indexes.asdict()
21582157
return ret
21592158

21602159
@property

0 commit comments

Comments
 (0)