Skip to content

Commit

Permalink
Remove table collection copies from tskit_tools.iterate_timepoints_wi…
Browse files Browse the repository at this point in the history
…th_individuals. (#859)

* Remove table collection copies from
tskit_tools.iterate_timepoints_with_individuals.

Fixes #851

* Update change log
  • Loading branch information
molpopgen committed Nov 23, 2021
1 parent 75c58a6 commit 46b6efd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
6 changes: 6 additions & 0 deletions doc/misc/changelog.md
Expand Up @@ -10,6 +10,12 @@ Changes to `fwdpy11.conditional_models`:
* Change name of `kwarg` to `track_added_mutation`.
PR {pr}`858`

Back end changes:

* Remove extra copies of tskit table collections from the implementation of {func}`fwdpy11.tskit_tools.iterate_timepoints_with_individuals`.
PR {pr}`859`
Issue {issue}`851`

Bug fixes:

* Fixed a "use after move" error in C++ code used to pickle {class}`fwdpy11.DiscreteDemography`.
Expand Down
18 changes: 12 additions & 6 deletions fwdpy11/tskit_tools/__init__.py
Expand Up @@ -73,22 +73,28 @@ def iterate_timepoints_with_individuals(
If `False`, `None` will be yielded.
"""

# Get rows of the node table where the nodes are in individuals
nodes_in_individuals = np.where(ts.tables.nodes.individual != tskit.NULL)[0]
nodes_in_individuals = []
node_times = []
for individual in ts.individuals():
for node in individual.nodes:
nodes_in_individuals.append(node)
node_times.append(ts.node(node).time)

# Get the times
node_times = ts.tables.nodes.time[nodes_in_individuals]
node_times = np.array(node_times)
nodes_in_individuals = np.array(nodes_in_individuals)

unique_node_times = np.unique(node_times)

for utime in unique_node_times[::-1]:
# Get the node tables rows in individuals at this time
x = np.where(node_times == utime)
node_table_rows = nodes_in_individuals[x]
assert np.all(ts.tables.nodes.time[node_table_rows] == utime)
assert np.all(np.array([ts.node(i).time for i in node_table_rows]) == utime)

# Get the individuals
individuals = np.unique(ts.tables.nodes.individual[node_table_rows])
individuals = np.unique(
np.array([ts.node(i).individual for i in node_table_rows])
)
assert not np.any(individuals == tskit.NULL)

if decode_metadata is True:
Expand Down

0 comments on commit 46b6efd

Please sign in to comment.