Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the numba cache to be used, for development #441

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions tests/exact_moments.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import numpy as np
import scipy
from scipy.special import betaln
from scipy.special import gammaln
from math import lgamma


def moments(a_i, b_i, a_j, b_j, y_ij, mu_ij):
Expand All @@ -33,7 +33,7 @@ def moments(a_i, b_i, a_j, b_j, y_ij, mu_ij):
s2 = s1 * (a + 1) * (b + 1) / (c + 1)
d1 = s1 * exp(f1 - f0)
d2 = s2 * exp(f2 - f0)
logl = f0 + betaln(y_ij + 1, a) + gammaln(b) - b * log(t)
logl = f0 + betaln(y_ij + 1, a) + lgamma(b) - b * log(t)
mn_j = d1 / t
sq_j = d2 / t**2
va_j = sq_j - mn_j**2
Expand All @@ -56,7 +56,7 @@ def rootward_moments(t_j, a_i, b_i, y_ij, mu_ij):
b = s + 1
z = t_j * r
if t_j == 0.0:
logl = gammaln(s) - s * log(r)
logl = lgamma(s) - s * log(r)
mn_i = s / r
va_i = s / r**2
return logl, mn_i, va_i
Expand All @@ -65,7 +65,7 @@ def rootward_moments(t_j, a_i, b_i, y_ij, mu_ij):
f2 = float(mpmath.log(mpmath.hyperu(a + 2, b + 2, z)))
d0 = -a * exp(f1 - f0)
d1 = -(a + 1) * exp(f2 - f1)
logl = f0 - b_i * t_j + (b - 1) * log(t_j) + gammaln(a)
logl = f0 - b_i * t_j + (b - 1) * log(t_j) + lgamma(a)
mn_i = t_j * (1 - d0)
va_i = t_j**2 * d0 * (d1 - d0)
return logl, mn_i, va_i
Expand Down Expand Up @@ -112,7 +112,7 @@ def unphased_moments(a_i, b_i, a_j, b_j, y_ij, mu_ij):
s2 = s1 * (a + 1) * (b + 1) / (c + 1)
d1 = s1 * exp(f1 - f0)
d2 = s2 * exp(f2 - f0)
logl = f0 + betaln(a_j, a_i) + gammaln(b) - b * log(t)
logl = f0 + betaln(a_j, a_i) + lgamma(b) - b * log(t)
mn_j = d1 / t
sq_j = d2 / t**2
va_j = sq_j - mn_j**2
Expand All @@ -130,7 +130,7 @@ def twin_moments(a_i, b_i, y_ij, mu_ij):
"""
s = a_i + y_ij
r = b_i + 2 * mu_ij
logl = log(2) * y_ij + gammaln(s) - log(r) * s
logl = log(2) * y_ij + lgamma(s) - log(r) * s
mn_i = s / r
va_i = s / r**2
return logl, mn_i, va_i
Expand All @@ -151,7 +151,7 @@ def sideways_moments(t_i, a_j, b_j, y_ij, mu_ij):
f2 = float(mpmath.log(mpmath.hyperu(a + 2, b + 2, z)))
d0 = -a * exp(f1 - f0)
d1 = -(a + 1) * exp(f2 - f1)
logl = f0 - mu_ij * t_i + (b - 1) * log(t_i) + gammaln(a)
logl = f0 - mu_ij * t_i + (b - 1) * log(t_i) + lgamma(a)
mn_j = -t_i * d0
va_j = t_i**2 * d0 * (d1 - d0)
return logl, mn_j, va_j
Expand Down
7 changes: 5 additions & 2 deletions tests/test_hypergeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
Test cases for numba-fied hypergeometric functions
"""

from math import lgamma

import mpmath
import numdifftools as nd
import numpy as np
Expand All @@ -38,8 +40,8 @@ class TestPolygamma:
Test numba-fied gamma functions
"""

def test_gammaln(self, x):
assert np.isclose(hypergeo._gammaln(x), float(mpmath.re(mpmath.loggamma(x))))
def test_lgamma(self, x):
assert np.isclose(lgamma(x), float(mpmath.re(mpmath.loggamma(x))))

def test_digamma(self, x):
assert np.isclose(hypergeo._digamma(x), float(mpmath.psi(0, x)))
Expand Down Expand Up @@ -120,6 +122,7 @@ def _2f1_validate(a_i, b_i, a_j, b_j, y, mu, offset=1.0):
val = mpmath.re(mpmath.hyp2f1(A, B, C, z, maxterms=1e7))
return val / offset

@pytest.mark.skip(reason="_hyp2f1_unity now an inner function for numba")
def test_2f1(self, pars):
a_i, b_i, a_j, b_j, y, mu = pars
A = a_j
Expand Down
27 changes: 27 additions & 0 deletions tsdate/accelerate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
from typing import Callable

from numba import jit

# By default we disable the numba cache. See e.g.
# https://github.com/sgkit-dev/sgkit/blob/main/sgkit/accelerate.py
_DISABLE_CACHE = os.environ.get("TSDATE_DISABLE_NUMBA_CACHE", "1")

try:
CACHE_NUMBA = {"0": True, "1": False}[_DISABLE_CACHE]
except KeyError as e: # pragma: no cover
raise KeyError(
"Environment variable 'TSDATE_DISABLE_NUMBA_CACHE' must be '0' or '1'"
) from e


DEFAULT_NUMBA_ARGS = {
"nopython": True,
"cache": CACHE_NUMBA,
}


def numba_jit(*args, **kwargs) -> Callable: # pragma: no cover
kwargs_ = DEFAULT_NUMBA_ARGS.copy()
kwargs_.update(kwargs)
return jit(*args, **kwargs_)
Loading