From 705fa7a94dd051031fdab23ae623a68e94753c7f Mon Sep 17 00:00:00 2001 From: Graham Gower Date: Sat, 2 May 2020 13:23:19 +0200 Subject: [PATCH] Avoid side-effects in the SLiM engine. --- stdpopsim/slim_engine.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/stdpopsim/slim_engine.py b/stdpopsim/slim_engine.py index bb40c4a99..149490095 100644 --- a/stdpopsim/slim_engine.py +++ b/stdpopsim/slim_engine.py @@ -40,6 +40,7 @@ import os import sys +import copy import string import tempfile import subprocess @@ -511,6 +512,10 @@ def slim_makescript( scaling_factor, burn_in): pop_names = [pc.metadata["id"] for pc in demographic_model.population_configurations] + # Use copies of these so that the time frobbing below doesn't have + # side-effects in the caller's model. + demographic_events = copy.deepcopy(demographic_model.demographic_events) + extended_events = copy.deepcopy(extended_events) # Reassign event times according to integral SLiM generations. # This collapses the time deltas used in HomSap/AmericanAdmixture_4B11. @@ -523,8 +528,9 @@ def fix_time(event): t = (round(t.time / scaling_factor)-1) * scaling_factor else: t = round(t / scaling_factor) * scaling_factor + assert t >= 0, f"{attr}: {getattr(event, attr)}" setattr(event, attr, t) - for event in demographic_model.demographic_events: + for event in demographic_events: fix_time(event) for event in extended_events: fix_time(event) @@ -534,7 +540,7 @@ def fix_time(event): dd = msprime.DemographyDebugger( population_configurations=demographic_model.population_configurations, migration_matrix=demographic_model.migration_matrix, - demographic_events=demographic_model.demographic_events) + demographic_events=demographic_events) epochs = sorted(dd.epochs, key=lambda e: e.start_time, reverse=True) T = [round(e.start_time*demographic_model.generation_time) for e in epochs]