Skip to content

Commit

Permalink
better metadata rebuild method
Browse files Browse the repository at this point in the history
  • Loading branch information
molpopgen committed Dec 6, 2024
1 parent 4e05036 commit e7a003a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
7 changes: 6 additions & 1 deletion doc/long_vignettes/tskitconvert_vignette.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
9 changes: 7 additions & 2 deletions tests/test_metadata_roundtrips_via_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions tests/test_tskit_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
# You should have received a copy of the GNU General Public License
# along with fwdpy11. If not, see <http://www.gnu.org/licenses/>.
#
import copy
import inspect

import demes
import fwdpy11
import pytest
import tskit
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit e7a003a

Please sign in to comment.