Skip to content

Commit

Permalink
Move stabilise_node_ordering into eval function
Browse files Browse the repository at this point in the history
Fixes #709. I guess we don't need to add this to the changelog as it was never documented in the first place?
  • Loading branch information
hyanwong authored and mergify[bot] committed Sep 22, 2022
1 parent 4446741 commit 69bb8c7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
25 changes: 22 additions & 3 deletions tsinfer/eval_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,6 @@ def run_perfect_inference(
extended_checks=extended_checks,
progress_monitor=progress_monitor,
)
# If time_chunking is turned on we need to stabilise the node ordering in the output
# to ensure that the node IDs are comparable.
inferred_ts = inference.match_samples(
sample_data,
ancestors_ts,
Expand All @@ -720,13 +718,34 @@ def run_perfect_inference(
num_threads=num_threads,
extended_checks=extended_checks,
progress_monitor=progress_monitor,
stabilise_node_ordering=time_chunking and not path_compression,
simplify=False, # Don't simplify until we have stabilised the node order below
)
# If time_chunking is turned on we need to stabilise the node ordering in the output
# to ensure that the node IDs are comparable.
if time_chunking and not path_compression:
inferred_ts = stabilise_node_ordering(inferred_ts)

# to compare against the original, we need to remove unary nodes from the inferred TS
inferred_ts = inferred_ts.simplify(keep_unary=False, filter_sites=False)
return ts, inferred_ts


def stabilise_node_ordering(ts):
# Ensure all the node times are distinct so that they will have
# stable IDs after simplifying. This could possibly also be done
# by reversing the IDs within a time slice. This is used for comparing
# tree sequences produced by perfect inference.
tables = ts.dump_tables()
times = tables.nodes.time
for t in range(1, int(times[0])):
index = np.where(times == t)[0]
k = index.shape[0]
times[index] += np.arange(k)[::-1] / k
tables.nodes.time = times
tables.sort()
return tables.tree_sequence()


def count_sample_child_edges(ts):
"""
Returns an array counting the number of edges where each sample is a child.
Expand Down
21 changes: 2 additions & 19 deletions tsinfer/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,6 @@ def match_samples(
recombination=None, # See :class:`Matcher`
mismatch=None, # See :class:`Matcher`
precision=None,
stabilise_node_ordering=False,
extended_checks=False,
engine=constants.C_ENGINE,
progress_monitor=None,
Expand Down Expand Up @@ -636,9 +635,7 @@ def match_samples(
# we sometimes assume they are in the same order as in the file

manager.match_samples(sample_indexes, sample_times)
ts = manager.finalise(
simplify=simplify, stabilise_node_ordering=stabilise_node_ordering
)
ts = manager.finalise(simplify=simplify)
return ts


Expand Down Expand Up @@ -1641,7 +1638,7 @@ def match_samples(self, sample_indexes, sample_times):
progress_monitor.update()
progress_monitor.close()

def finalise(self, simplify, stabilise_node_ordering):
def finalise(self, simplify):
logger.info("Finalising tree sequence")
ts = self.get_samples_tree_sequence()
if simplify:
Expand All @@ -1650,20 +1647,6 @@ def finalise(self, simplify, stabilise_node_ordering):
"filter_individuals=False, keep_unary=True) on "
f"{ts.num_nodes} nodes and {ts.num_edges} edges"
)
if stabilise_node_ordering:
# Ensure all the node times are distinct so that they will have
# stable IDs after simplifying. This could possibly also be done
# by reversing the IDs within a time slice. This is used for comparing
# tree sequences produced by perfect inference.
tables = ts.dump_tables()
times = tables.nodes.time
for t in range(1, int(times[0])):
index = np.where(times == t)[0]
k = index.shape[0]
times[index] += np.arange(k)[::-1] / k
tables.nodes.time = times
tables.sort()
ts = tables.tree_sequence()
ts = ts.simplify(
samples=list(self.sample_id_map.values()),
filter_sites=False,
Expand Down

0 comments on commit 69bb8c7

Please sign in to comment.