Skip to content

Commit 851b75a

Browse files
Add low-level fromdict and use in high-level code
1 parent 4e820b3 commit 851b75a

File tree

3 files changed

+45
-36
lines changed

3 files changed

+45
-36
lines changed

python/_tskitmodule.c

+26
Original file line numberDiff line numberDiff line change
@@ -6054,6 +6054,28 @@ TableCollection_asdict(TableCollection *self, PyObject *args, PyObject *kwds)
60546054
return ret;
60556055
}
60566056

6057+
static PyObject *
6058+
TableCollection_fromdict(TableCollection *self, PyObject *args)
6059+
{
6060+
int err;
6061+
PyObject *ret = NULL;
6062+
PyObject *dict = NULL;
6063+
6064+
if (TableCollection_check_state(self) != 0) {
6065+
goto out;
6066+
}
6067+
if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict)) {
6068+
goto out;
6069+
}
6070+
err = parse_table_collection_dict(self->tables, dict);
6071+
if (err != 0) {
6072+
goto out;
6073+
}
6074+
ret = Py_BuildValue("");
6075+
out:
6076+
return ret;
6077+
}
6078+
60576079
static PyGetSetDef TableCollection_getsetters[] = {
60586080
{ .name = "individuals",
60596081
.get = (getter) TableCollection_get_individuals,
@@ -6178,6 +6200,10 @@ static PyMethodDef TableCollection_methods[] = {
61786200
.ml_meth = (PyCFunction) TableCollection_asdict,
61796201
.ml_flags = METH_VARARGS | METH_KEYWORDS,
61806202
.ml_doc = "Returns the table collection in dictionary encoding. " },
6203+
{ .ml_name = "fromdict",
6204+
.ml_meth = (PyCFunction) TableCollection_fromdict,
6205+
.ml_flags = METH_VARARGS | METH_KEYWORDS,
6206+
.ml_doc = "Sets the state of this table collection from the specified dict" },
61816207
{ NULL } /* Sentinel */
61826208
};
61836209

python/tests/test_lowlevel.py

+15
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,28 @@ def test_asdict(self):
417417
assert isinstance(d, dict)
418418
assert len(d) > 0
419419

420+
def test_fromdict(self):
421+
for ts in self.get_example_tree_sequences():
422+
tc1 = _tskit.TableCollection(sequence_length=ts.get_sequence_length())
423+
ts.dump_tables(tc1)
424+
d = tc1.asdict()
425+
tc2 = _tskit.TableCollection(sequence_length=0)
426+
tc2.fromdict(d)
427+
assert tc1.equals(tc2)
428+
420429
def test_asdict_bad_args(self):
421430
ts = msprime.simulate(10, random_seed=1242)
422431
tc = ts.tables._ll_tables
423432
for bad_type in [None, 0.1, "str"]:
424433
with pytest.raises(TypeError):
425434
tc.asdict(force_offset_64=bad_type)
426435

436+
def test_fromdict_bad_args(self):
437+
tc = _tskit.TableCollection(0)
438+
for bad_type in [None, 0.1, "str"]:
439+
with pytest.raises(TypeError):
440+
tc.fromdict(bad_type)
441+
427442

428443
class TestTableMethods:
429444
"""

python/tskit/tables.py

+4-36
Original file line numberDiff line numberDiff line change
@@ -2649,45 +2649,13 @@ def dump(self, file_or_path):
26492649

26502650
# Unpickle support
26512651
def __setstate__(self, state):
2652-
self.__init__(state["sequence_length"])
2653-
self.metadata_schema = tskit.parse_metadata_schema(state["metadata_schema"])
2654-
self.metadata = self.metadata_schema.decode_row(state["metadata"])
2655-
self.individuals.set_columns(**state["individuals"])
2656-
self.nodes.set_columns(**state["nodes"])
2657-
self.edges.set_columns(**state["edges"])
2658-
self.migrations.set_columns(**state["migrations"])
2659-
self.sites.set_columns(**state["sites"])
2660-
self.mutations.set_columns(**state["mutations"])
2661-
self.populations.set_columns(**state["populations"])
2662-
self.provenances.set_columns(**state["provenances"])
2652+
self.__init__()
2653+
self._ll_tables.fromdict(state)
26632654

26642655
@classmethod
26652656
def fromdict(self, tables_dict):
2666-
tables = TableCollection(tables_dict["sequence_length"])
2667-
try:
2668-
tables.metadata_schema = tskit.parse_metadata_schema(
2669-
tables_dict["metadata_schema"]
2670-
)
2671-
except KeyError:
2672-
pass
2673-
try:
2674-
tables.metadata = tables.metadata_schema.decode_row(tables_dict["metadata"])
2675-
except KeyError:
2676-
pass
2677-
tables.individuals.set_columns(**tables_dict["individuals"])
2678-
tables.nodes.set_columns(**tables_dict["nodes"])
2679-
tables.edges.set_columns(**tables_dict["edges"])
2680-
tables.migrations.set_columns(**tables_dict["migrations"])
2681-
tables.sites.set_columns(**tables_dict["sites"])
2682-
tables.mutations.set_columns(**tables_dict["mutations"])
2683-
tables.populations.set_columns(**tables_dict["populations"])
2684-
tables.provenances.set_columns(**tables_dict["provenances"])
2685-
2686-
# Indexes must be last as other wise the check for their consistency will fail
2687-
try:
2688-
tables.indexes = TableCollectionIndexes(**tables_dict["indexes"])
2689-
except KeyError:
2690-
pass
2657+
tables = TableCollection()
2658+
tables._ll_tables.fromdict(tables_dict)
26912659
return tables
26922660

26932661
def copy(self):

0 commit comments

Comments
 (0)