Skip to content

Commit

Permalink
fix comb function in python 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
willdumm committed Jan 30, 2024
1 parent bab151d commit dda2379
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions historydag/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Utility functions and classes for working with HistoryDag objects."""

import ete3
import math
from math import log, exp, isfinite
from collections import Counter
from functools import wraps
Expand All @@ -24,6 +23,29 @@
)
from typing import TYPE_CHECKING


try:
from math import comb
except ImportError:

def comb(n, k):
"""
A fast way to calculate binomial coefficients
from https://stackoverflow.com/a/3025547
https://en.wikipedia.org/wiki/Binomial_coefficient#Multiplicative_formula
"""
if 0 <= k <= n:
ntok = 1
ktok = 1
for t in range(1, min(k, n - k) + 1):
ntok *= n
ktok *= t
n -= 1
return ntok // ktok
else:
return 0


if TYPE_CHECKING:
from historydag.dag import HistoryDagNode, HistoryDag

Expand Down Expand Up @@ -1207,7 +1229,7 @@ def count_resolved_clade_supports(
support = binary_support(unflattened_clade_size, num_children)
# ... so this check need only be done num_children times
if support > threshold:
yield (math.comb(num_children, unflattened_clade_size), support)
yield (comb(num_children, unflattened_clade_size), support)


def iter_resolved_clade_supports(
Expand Down

0 comments on commit dda2379

Please sign in to comment.