From 8a958e958d7bf52f60abc0b318e49fc09a945059 Mon Sep 17 00:00:00 2001 From: Yan Wong Date: Thu, 29 Sep 2022 10:23:48 +0100 Subject: [PATCH] Access the left and right as an interval --- python/CHANGELOG.rst | 2 ++ python/tests/test_highlevel.py | 8 ++++++++ python/tskit/trees.py | 10 ++++++++++ 3 files changed, 20 insertions(+) diff --git a/python/CHANGELOG.rst b/python/CHANGELOG.rst index ecddf5b99d..cec1f6e50c 100644 --- a/python/CHANGELOG.rst +++ b/python/CHANGELOG.rst @@ -43,6 +43,8 @@ - Add ``Interval.mid`` and ``Tree.mid`` properties to return the midpoint of the interval. (:user:`currocam`, :pr:`2960`) +- Edges now have an ``.interval`` attribute returning a ``tskit.Interval`` object. + (:user:`hyanwong`, :pr:`2531`) -------------------- [0.5.8] - 2024-06-27 diff --git a/python/tests/test_highlevel.py b/python/tests/test_highlevel.py index f8d224ce4e..72b3e4f205 100644 --- a/python/tests/test_highlevel.py +++ b/python/tests/test_highlevel.py @@ -1576,6 +1576,14 @@ def test_edge_span_property(self, ts): for edge in ts.edges(): assert edge.span == edge.right - edge.left + @pytest.mark.parametrize("ts", get_example_tree_sequences()) + def test_edge_interval_property(self, ts): + for edge in ts.edges(): + assert edge.interval == (edge.left, edge.right) + if ts.num_trees == 1 and ts.num_edges > 0: + for edge in ts.edges(): + assert edge.interval == ts.first().interval + def test_edgesets(self): tested = False # We manual loop in this test to test the example tree sequences are working diff --git a/python/tskit/trees.py b/python/tskit/trees.py index 08ea2dbe0a..5d8f507510 100644 --- a/python/tskit/trees.py +++ b/python/tskit/trees.py @@ -319,6 +319,16 @@ def span(self): """ return self.right - self.left + @property + def interval(self): + """ + Returns the left and right positions of this edge as an :class:`Interval` + + :return: The interval covered by this edge. + :rtype: :class:`Interval` + """ + return Interval(self.left, self.right) + @metadata_module.lazy_decode() @dataclass