Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto_flush_edges #2183

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
- Enable `additional_nodes` and `coalescing_segments_only` flags for `DTWF` and `FIXED_PEDIDGREE` models ({issue}`2129`, {issue}`2133`, {issue}`2167`, {pr}`2176`,
{user}`GertjanBisschop`)

**Maintenance**:
- Automatically flush edges whenever an edge with a different parent is added to the edge buffer. ({issue}`2182`, {pr}`2183`, {user}`GertjanBisschop`)

## [1.2.0] - 2022-05-18

**New features**
Expand Down
11 changes: 5 additions & 6 deletions algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,11 @@ def store_edge(self, left, right, parent, child):
"""
Stores the specified edge to the output tree sequence.
"""
if len(self.edge_buffer) > 0:
last_edge = self.edge_buffer[-1]
if last_edge.parent != parent:
self.flush_edges()

self.edge_buffer.append(
tskit.Edge(left=left, right=right, parent=parent, child=child)
)
Expand Down Expand Up @@ -1731,7 +1736,6 @@ def census_event(self, time):
for pop in self.P:
for ancestor in pop.iter_ancestors():
seg = ancestor
self.flush_edges()
u = self.tables.nodes.add_row(
time=time, flags=msprime.NODE_IS_CEN_EVENT, population=pop.id
)
Expand All @@ -1757,7 +1761,6 @@ def store_additional_nodes_edges(self, flag, new_node_id, z):
if new_node_id == -1:
new_node_id = self.store_node(z.population, flags=flag)
else:
self.flush_edges()
self.update_node_flag(new_node_id, flag)
self.store_arg_edges(z, new_node_id)
return new_node_id
Expand Down Expand Up @@ -1798,8 +1801,6 @@ def merge_ancestors(self, H, pop_id, label, new_node_id=-1):
coalescence = True
if new_node_id == -1:
new_node_id = self.store_node(pop_id)
else:
self.flush_edges()
# We must also break if the next left value is less than
# any of the right values in the current overlap set.
if left not in self.S:
Expand Down Expand Up @@ -1944,8 +1945,6 @@ def merge_two_ancestors(self, population_index, label, x, y, u=-1):
if u == -1:
self.store_node(population_index)
u = len(self.tables.nodes) - 1
else:
self.flush_edges()
# Put in breakpoints for the outer edges of the coalesced
# segment
left = x.left
Expand Down
29 changes: 11 additions & 18 deletions lib/msprime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1814,9 +1814,19 @@ msp_store_edge(msp_t *self, double left, double right, tsk_id_t parent, tsk_id_t
{
int ret = 0;
tsk_edge_t *edge;
tsk_edge_t last_edge;
const double *node_time = self->tables->nodes.time;

tsk_bug_assert(parent < (tsk_id_t) self->tables->nodes.num_rows);
if (self->num_buffered_edges > 0) {
last_edge = self->buffered_edges[self->num_buffered_edges - 1];
if (last_edge.parent != parent) {
ret = msp_flush_edges(self);
if (ret != 0) {
goto out;
}
}
}
if (self->num_buffered_edges == self->max_buffered_edges - 1) {
/* Grow the array */
self->max_buffered_edges *= 2;
Expand Down Expand Up @@ -1923,10 +1933,6 @@ msp_store_additional_nodes_edges(msp_t *self, segment_t *z, tsk_id_t u, uint32_t
}
*new_node_id = ret;
} else {
ret = msp_flush_edges(self);
if (ret != 0) {
goto out;
}
// don't mark sample nodes as PASS_THROUGH
if (!(self->tables->nodes.flags[u] == TSK_NODE_IS_SAMPLE
&& flag == MSP_NODE_IS_PASS_THROUGH)) {
Expand Down Expand Up @@ -2928,11 +2934,6 @@ msp_merge_two_ancestors(msp_t *self, population_id_t population_id, label_id_t l
ret = (int) new_node_id;
goto out;
}
} else {
ret = msp_flush_edges(self);
if (ret != 0) {
goto out;
}
}
}
v = new_node_id;
Expand Down Expand Up @@ -3175,11 +3176,6 @@ msp_merge_ancestors(msp_t *self, avl_tree_t *Q, population_id_t population_id,
ret = (int) new_node_id;
goto out;
}
} else {
ret = msp_flush_edges(self);
if (ret != 0) {
goto out;
}
}
}
/* Insert overlap counts for bounds, if necessary */
Expand Down Expand Up @@ -3804,6 +3800,7 @@ msp_reset(msp_t *self)
self->num_rejected_ca_events = 0;
self->num_trapped_re_events = 0;
self->num_multiple_re_events = 0;
self->num_buffered_edges = 0;
memset(self->num_migration_events, 0, N * N * sizeof(size_t));

if (self->start_time < DBL_MAX) {
Expand Down Expand Up @@ -6557,10 +6554,6 @@ msp_census_event(msp_t *self, demographic_event_t *event)

while (seg != NULL) {
// Add an edge to the edge table.
ret = msp_flush_edges(self);
if (ret != 0) {
goto out;
}
ret = tsk_node_table_add_row(&self->tables->nodes,
MSP_NODE_IS_CEN_EVENT, event->time, i, TSK_NULL, NULL, 0);
if (ret < 0) {
Expand Down