Skip to content

Commit

Permalink
add mid property
Browse files Browse the repository at this point in the history
Add minimal changelog

Add pr number to changelog

Add test tree method

Black linter

Fix flake8 lint
  • Loading branch information
currocam committed Jun 21, 2024
1 parent d3c59ba commit a55d4c0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion python/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
- Add ``Table.drop_metadata`` to make clearing metadata from tables easy.
(:user:`jeromekelleher`, :pr:`2944`)

- Add ``Interval.mid`` and ``Tree.mid`` properties to return the midpoint of the interval.
(:user:`currocam`, :pr:`2960`)

**Bugfixes**

- Fix to the folded, expected allele frequency spectrum (i.e.,
`TreeSequence.allele_frequency_spectrum(mode="branch", polarised=False)`,
which was half as big as it should have been. (:user:`petrelharp`,
:user:`nspope`, :pr:`2933`)

--------------------
[0.5.6] - 2023-10-10
--------------------
Expand Down
4 changes: 4 additions & 0 deletions python/tests/test_highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4009,6 +4009,7 @@ def test_apis(self):
assert t1.get_parent_dict() == t1.parent_dict
assert t1.get_total_branch_length() == t1.total_branch_length
assert t1.span == t1.interval.right - t1.interval.left
assert t1.mid == t1.interval.left + (t1.interval.left - t1.interval.right) / 2
# node properties
root = t1.get_root()
for node in t1.nodes():
Expand Down Expand Up @@ -4131,6 +4132,9 @@ def test_interval(self):
assert tree.interval.span == pytest.approx(
breakpoints[i + 1] - breakpoints[i]
)
assert tree.interval.mid == pytest.approx(
breakpoints[i] + (breakpoints[i + 1] - breakpoints[i]) / 2
)

def verify_tree_arrays(self, tree):
ts = tree.tree_sequence
Expand Down
20 changes: 20 additions & 0 deletions python/tskit/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ def span(self) -> float | int:
"""
return self.right - self.left

@property
def mid(self) -> float | int:
"""
The middle point of this interval, simply ``left+(right-left)/2``.
"""
return self.left + (self.right - self.left) / 2


class EdgeDiff(NamedTuple):
interval: Interval
Expand Down Expand Up @@ -1686,6 +1693,19 @@ def span(self):
"""
return self.interval.span

@property
def mid(self):
"""
Returns the midpoint of the genomic interval that this tree represents
the history of. This is defined as :math:`(l + (r - l) / 2)`, where
:math:`(l, r)` is the genomic interval returned by
:attr:`~Tree.interval`.
:return: The genomic distance covered by this tree.
:rtype: float
"""
return self.interval.mid

def get_sample_size(self):
# Deprecated alias for self.sample_size
return self.sample_size
Expand Down

0 comments on commit a55d4c0

Please sign in to comment.