Skip to content

Commit 813f4ed

Browse files
duncanMRmergify[bot]
authored andcommitted
Fix draw_svg warning for mutations above unplotted nodes
1 parent b06a718 commit 813f4ed

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

python/tests/test_drawing.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MIT License
22
#
3-
# Copyright (c) 2018-2022 Tskit Developers
3+
# Copyright (c) 2018-2023 Tskit Developers
44
# Copyright (C) 2017 University of Oxford
55
#
66
# Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -1615,10 +1615,12 @@ def test_draw_multiroot(self):
16151615

16161616
def test_draw_mutations_over_roots(self):
16171617
t = self.get_mutations_over_roots_tree()
1618-
svg = t.draw()
1619-
self.verify_basic_svg(svg)
1620-
svg = t.draw_svg()
1621-
self.verify_basic_svg(svg)
1618+
with pytest.warns(UserWarning, match="nodes which are not present"):
1619+
svg = t.draw()
1620+
self.verify_basic_svg(svg)
1621+
with pytest.warns(UserWarning, match="nodes which are not present"):
1622+
svg = t.draw_svg()
1623+
self.verify_basic_svg(svg)
16221624

16231625
def test_draw_unary(self):
16241626
t = self.get_unary_node_tree()
@@ -2742,20 +2744,23 @@ def test_known_svg_ts_mutation_times_logscale(self, overwrite_viz, draw_plotbox)
27422744
svg, "ts_mut_times_logscale.svg", overwrite_viz, width=200 * ts.num_trees
27432745
)
27442746

2745-
def test_known_svg_ts_mut_no_edges(self, overwrite_viz, draw_plotbox, caplog):
2747+
def test_known_svg_ts_mut_no_edges(self, overwrite_viz, draw_plotbox):
27462748
# An example with some muts on axis but not on a visible node
27472749
ts = msprime.simulate(10, random_seed=2, mutation_rate=1)
27482750
tables = ts.dump_tables()
27492751
tables.edges.clear()
27502752
tables.mutations.time = np.full_like(tables.mutations.time, tskit.UNKNOWN_TIME)
27512753
ts_no_edges = tables.tree_sequence()
2752-
svg = ts_no_edges.draw_svg(debug_box=draw_plotbox)
2753-
assert "not present in the displayed tree" in caplog.text
2754-
self.verify_known_svg(
2755-
svg, "ts_mutations_no_edges.svg", overwrite_viz, width=200 * ts.num_trees
2756-
)
2754+
with pytest.warns(UserWarning, match="nodes which are not present"):
2755+
svg = ts_no_edges.draw_svg(debug_box=draw_plotbox)
2756+
self.verify_known_svg(
2757+
svg,
2758+
"ts_mutations_no_edges.svg",
2759+
overwrite_viz,
2760+
width=200 * ts.num_trees,
2761+
)
27572762

2758-
def test_known_svg_ts_timed_mut_no_edges(self, overwrite_viz, draw_plotbox, caplog):
2763+
def test_known_svg_ts_timed_mut_no_edges(self, overwrite_viz, draw_plotbox):
27592764
# An example with some muts on axis but not on a visible node
27602765
ts = msprime.simulate(10, random_seed=2, mutation_rate=1)
27612766
tables = ts.dump_tables()
@@ -2764,14 +2769,15 @@ def test_known_svg_ts_timed_mut_no_edges(self, overwrite_viz, draw_plotbox, capl
27642769
ts.num_mutations, dtype=tables.mutations.time.dtype
27652770
)
27662771
ts_no_edges = tables.tree_sequence()
2767-
svg = ts_no_edges.draw_svg(debug_box=draw_plotbox)
2768-
assert "not present in the displayed tree" in caplog.text
2769-
self.verify_known_svg(
2770-
svg,
2771-
"ts_mutations_timed_no_edges.svg",
2772-
overwrite_viz,
2773-
width=200 * ts.num_trees,
2774-
)
2772+
2773+
with pytest.warns(UserWarning, match="nodes which are not present"):
2774+
svg = ts_no_edges.draw_svg(debug_box=draw_plotbox)
2775+
self.verify_known_svg(
2776+
svg,
2777+
"ts_mutations_timed_no_edges.svg",
2778+
overwrite_viz,
2779+
width=200 * ts.num_trees,
2780+
)
27752781

27762782
def test_known_svg_ts_multiroot(self, overwrite_viz, draw_plotbox, caplog):
27772783
tables = wf.wf_sim(

python/tskit/drawing.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MIT License
22
#
3-
# Copyright (c) 2018-2022 Tskit Developers
3+
# Copyright (c) 2018-2023 Tskit Developers
44
# Copyright (c) 2015-2017 University of Oxford
55
#
66
# Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -25,7 +25,6 @@
2525
"""
2626
import collections
2727
import itertools
28-
import logging
2928
import math
3029
import numbers
3130
import operator
@@ -1365,9 +1364,11 @@ def __init__(
13651364
else:
13661365
unplotted.append(mutation.id + self.offsets.mutation)
13671366
if len(unplotted) > 0:
1368-
logging.warning(
1367+
warnings.warn(
13691368
f"Mutations {unplotted} are above nodes which are not present in the "
1370-
"displayed tree, so are not plotted on the topology."
1369+
"displayed tree, so are not plotted on the topology.",
1370+
UserWarning,
1371+
stacklevel=2,
13711372
)
13721373
self.left_extent = tree.interval.left
13731374
self.right_extent = tree.interval.right

0 commit comments

Comments
 (0)