Skip to content

Commit

Permalink
Add check that population is simulating (#762)
Browse files Browse the repository at this point in the history
* Add is_simulating to Population.

* Add tests.

* Update change log.
  • Loading branch information
molpopgen committed Jul 5, 2021
1 parent 2bedde4 commit b79aa80
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 34 deletions.
7 changes: 7 additions & 0 deletions doc/misc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
Major changes are listed below. Each release likely contains fiddling with back-end code,
updates to latest `fwdpp` version, etc.

## Next release

C++ back-end

* A population can now be checked that it is- or is not- being simulated.
{pr}`762`

## 0.15.2

Point release
Expand Down
4 changes: 4 additions & 0 deletions fwdpy11/_types/population_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def fixation_times(self) -> Iterable[int]:
def generation(self) -> int:
return self._generation # type: ignore

@property
def _is_simulating(self) -> bool:
return self.is_simulating # type: ignore

@property
def genetic_values(self) -> np.ndarray:
"""
Expand Down
3 changes: 2 additions & 1 deletion fwdpy11/headers/fwdpy11/types/Population.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ namespace fwdpy11

fwdpp::uint_t N;
fwdpp::uint_t generation;
bool is_simulating;

std::shared_ptr<fwdpp::ts::std_table_collection> tables;
std::vector<fwdpp::ts::table_index_t> alive_nodes, preserved_sample_nodes;
Expand All @@ -69,7 +70,7 @@ namespace fwdpy11
std::vector<double> genetic_value_matrix, ancient_sample_genetic_value_matrix;

Population(fwdpp::uint_t N_, const double L)
: fwdpp_base{N_}, N{N_}, generation{0},
: fwdpp_base{N_}, N{N_}, generation{0}, is_simulating{false},
tables(init_tables(N_, L)), alive_nodes{}, preserved_sample_nodes{},
genetic_value_matrix{}, ancient_sample_genetic_value_matrix{}
{
Expand Down
70 changes: 37 additions & 33 deletions fwdpy11/src/evolve_population/evolvets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ simplification(
bool reset_treeseqs_to_alive_nodes_after_simplification,
const fwdpy11::DiploidPopulation_temporal_sampler &post_simplification_recorder,
SimplificationState &simplifier_state,
fwdpp::ts::simplify_tables_output & simplification_output,
fwdpp::ts::simplify_tables_output &simplification_output,
fwdpp::ts::edge_buffer &new_edge_buffer,
std::vector<fwdpp::ts::table_index_t> &alive_at_last_simplification,
fwdpy11::DiploidPopulation &pop)
{
fwdpy11::simplify_tables(
pop, pop.mcounts_from_preserved_nodes, alive_at_last_simplification, *pop.tables,
simplifier_state, simplification_output,
new_edge_buffer, preserve_selected_fixations,
simulating_neutral_variants, suppress_edge_table_indexing);
fwdpy11::simplify_tables(pop, pop.mcounts_from_preserved_nodes,
alive_at_last_simplification, *pop.tables, simplifier_state,
simplification_output, new_edge_buffer,
preserve_selected_fixations, simulating_neutral_variants,
suppress_edge_table_indexing);
if (pop.mcounts.size() != pop.mcounts_from_preserved_nodes.size())
{
throw std::runtime_error("evolvets: count vector size mismatch after "
Expand Down Expand Up @@ -140,6 +140,7 @@ final_population_cleanup(
throw std::runtime_error("mutation container size does not equal mutation "
"count container size(s)");
}
pop.is_simulating = false;
}

void
Expand Down Expand Up @@ -375,6 +376,7 @@ evolve_with_tree_sequences(

clear_edge_table_indexes(*pop.tables);
fwdpp::ts::simplify_tables_output simplification_output;
pop.is_simulating = true;
for (std::uint32_t gen = 0; gen < simlen && !stopping_criteron_met; ++gen)
{
++pop.generation;
Expand Down Expand Up @@ -422,13 +424,13 @@ evolve_with_tree_sequences(
pop.N = static_cast<std::uint32_t>(pop.diploids.size());
if (current_demographic_state->will_go_globally_extinct() == true)
{
simplification(
preserve_selected_fixations, simulating_neutral_variants,
suppress_edge_table_indexing,
reset_treeseqs_to_alive_nodes_after_simplification,
post_simplification_recorder, *simplifier_state,
simplification_output, *new_edge_buffer,
alive_at_last_simplification, pop);
simplification(preserve_selected_fixations,
simulating_neutral_variants,
suppress_edge_table_indexing,
reset_treeseqs_to_alive_nodes_after_simplification,
post_simplification_recorder, *simplifier_state,
simplification_output, *new_edge_buffer,
alive_at_last_simplification, pop);
simplifier_state.reset(nullptr);
new_edge_buffer.reset(nullptr);
final_population_cleanup(
Expand All @@ -444,13 +446,13 @@ evolve_with_tree_sequences(

if (gen % simplification_interval == 0.0)
{
simplification(
preserve_selected_fixations, simulating_neutral_variants,
suppress_edge_table_indexing,
reset_treeseqs_to_alive_nodes_after_simplification,
post_simplification_recorder, *simplifier_state,
simplification_output,
*new_edge_buffer, alive_at_last_simplification, pop);
simplification(preserve_selected_fixations,
simulating_neutral_variants,
suppress_edge_table_indexing,
reset_treeseqs_to_alive_nodes_after_simplification,
post_simplification_recorder, *simplifier_state,
simplification_output, *new_edge_buffer,
alive_at_last_simplification, pop);
simplified = true;
}
else
Expand Down Expand Up @@ -504,14 +506,17 @@ evolve_with_tree_sequences(
}
}
simplification_output.preserved_mutations.erase(
std::remove(begin(simplification_output.preserved_mutations),
end(simplification_output.preserved_mutations),
std::numeric_limits<std::size_t>::max()),
std::remove(
begin(simplification_output.preserved_mutations),
end(simplification_output.preserved_mutations),
std::numeric_limits<std::size_t>::max()),
end(simplification_output.preserved_mutations));
if (simulating_neutral_variants)
{
genetics.mutation_recycling_bin
= fwdpp::ts::make_mut_queue(simplification_output.preserved_mutations, pop.mutations.size());
= fwdpp::ts::make_mut_queue(
simplification_output.preserved_mutations,
pop.mutations.size());
}
else
{
Expand All @@ -524,7 +529,8 @@ evolve_with_tree_sequences(
else
{
genetics.mutation_recycling_bin = fwdpp::ts::make_mut_queue(
simplification_output.preserved_mutations, pop.mutations.size());
simplification_output.preserved_mutations,
pop.mutations.size());
}
}

Expand Down Expand Up @@ -587,14 +593,12 @@ evolve_with_tree_sequences(

if (!simplified)
{
simplification(
preserve_selected_fixations, simulating_neutral_variants,
suppress_edge_table_indexing,
reset_treeseqs_to_alive_nodes_after_simplification,
post_simplification_recorder, *simplifier_state,
simplification_output,
*new_edge_buffer,
alive_at_last_simplification, pop);
simplification(preserve_selected_fixations, simulating_neutral_variants,
suppress_edge_table_indexing,
reset_treeseqs_to_alive_nodes_after_simplification,
post_simplification_recorder, *simplifier_state,
simplification_output, *new_edge_buffer,
alive_at_last_simplification, pop);
if (!preserve_selected_fixations)
{
fwdpp::ts::remove_fixations_from_haploid_genomes(
Expand Down
1 change: 1 addition & 0 deletions fwdpy11/src/fwdpy11_types/PopulationBase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ init_PopulationBase(py::module& m)
py::class_<fwdpy11::Population>(m, "PopulationBase")
.def_readonly("_N", &fwdpy11::Population::N)
.def_readonly("_generation", &fwdpy11::Population::generation)
.def_readonly("_is_simulating", &fwdpy11::Population::is_simulating)
.def_readonly("_mutations", &fwdpy11::Population::mutations)
.def_property_readonly("_mutations_ndarray",
[](const fwdpy11::Population& self) {
Expand Down
3 changes: 3 additions & 0 deletions tests/test_tree_sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, seed, samplesize, timepoints):
self.data = []

def __call__(self, pop, recorder):
assert pop._is_simulating is True
if len(self.timepoints) > 0:
if self.timepoints[0] == pop.generation:
s = np.random.choice(pop.N, self.samplesize, replace=False)
Expand Down Expand Up @@ -561,7 +562,9 @@ def setUpClass(self):
self.params, self.rng, self.pop = set_up_quant_trait_model(3.0)
self.stimes = [i for i in range(1, 101)]
self.recorder = Recorder(42, 10, self.stimes)
assert self.pop._is_simulating is False
fwdpy11.evolvets(self.rng, self.pop, self.params, 100, self.recorder)
assert self.pop._is_simulating is False
assert (
max(self.pop.mcounts) == 2 * self.pop.N
), "Nothing fixed, so test case is not helpful"
Expand Down

0 comments on commit b79aa80

Please sign in to comment.