From e7a003a7fd8f00cb44726def19e4952c1cc65e3a Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Fri, 6 Dec 2024 15:40:17 -0800 Subject: [PATCH] better metadata rebuild method --- doc/long_vignettes/tskitconvert_vignette.md | 7 ++++++- tests/test_metadata_roundtrips_via_simulation.py | 9 +++++++-- tests/test_tskit_metadata.py | 9 ++++++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/doc/long_vignettes/tskitconvert_vignette.md b/doc/long_vignettes/tskitconvert_vignette.md index 87141c30a..cc7c466db 100644 --- a/doc/long_vignettes/tskitconvert_vignette.md +++ b/doc/long_vignettes/tskitconvert_vignette.md @@ -159,9 +159,14 @@ We can pass the `params` object on when exporting the data to `tskit`: ```{code-cell} python ts = pop.dump_tables_to_tskit(model_params=params) def rebuild_params(md): + import copy + import inspect import fwdpy11 + md = copy.deepcopy(md) for i in dir(fwdpy11): - exec(f"from fwdpy11 import {i}") + if md.find(i) > 0: + if inspect.isclass(eval(f"fwdpy11.{i}")): + md = md.replace(i, "fwdpy11." + i) recovered_params = fwdpy11.ModelParams(**eval(md)) return recovered_params recovered_params = rebuild_params(ts.metadata["model_params"]) diff --git a/tests/test_metadata_roundtrips_via_simulation.py b/tests/test_metadata_roundtrips_via_simulation.py index 237fd1c5d..68b362d17 100644 --- a/tests/test_metadata_roundtrips_via_simulation.py +++ b/tests/test_metadata_roundtrips_via_simulation.py @@ -201,11 +201,16 @@ def test_metadata_roundtrip_single_deme_sim_with_parameters(rng, pdict, pop, inc provenance = json.loads(ts.provenance(0).record) def rebuild_pdict(provenance): + import copy import fwdpy11 + import inspect + params = copy.deepcopy(provenance["parameters"]["params_dict"]) for i in dir(fwdpy11): - exec(f"from fwdpy11 import {i}") - params_dict = eval(provenance["parameters"]["params_dict"]) + if params.find(i) > 0: + if inspect.isclass(eval(f"fwdpy11.{i}")): + params = params.replace(i, "fwdpy11." + i) + params_dict = eval(params) return params_dict params_dict = rebuild_pdict(provenance) diff --git a/tests/test_tskit_metadata.py b/tests/test_tskit_metadata.py index 769c80b49..32c538ccd 100644 --- a/tests/test_tskit_metadata.py +++ b/tests/test_tskit_metadata.py @@ -16,8 +16,9 @@ # You should have received a copy of the GNU General Public License # along with fwdpy11. If not, see . # +import copy +import inspect -import demes import fwdpy11 import pytest import tskit @@ -62,10 +63,12 @@ def test_single_model_params(pop, pdict1): ts = pop.dump_tables_to_tskit(model_params=mp) # reconstruct + params = copy.deepcopy(ts.metadata["model_params"]) for i in dir(fwdpy11): - exec(f"from fwdpy11 import {i}") + if inspect.isclass(eval(f"fwdpy11.{i}")): + params = params.replace(i, "fwdpy11." + i) - mp_rebuilt = fwdpy11.ModelParams(**eval(ts.metadata["model_params"])) + mp_rebuilt = fwdpy11.ModelParams(**eval(params)) assert mp == mp_rebuilt