Skip to content

Commit

Permalink
Merge pull request #171 from benjeffery/coverage
Browse files Browse the repository at this point in the history
Add coveralls coverage
  • Loading branch information
benjeffery authored Sep 13, 2024
2 parents ed32783 + 09ff1cc commit 1f3bfbd
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,18 @@ jobs:
with:
python-version: '3.11'
cache: 'pip'
- run: pip install -r requirements.txt
- run: python -m pytest tests

- name: Install deps
run: pip install -r requirements.txt

- name: Tests with numba
run: coverage run --source=tsbrowse -m pytest -x tests

- name: Tests with coverage (no numba)
run: TSBROWSE_DISABLE_NUMBA=1 coverage run --source=tsbrowse -m pytest -x tests

- name: Upload coverage
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
coveralls --service=github
5 changes: 2 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
appdirs
click
coverage
coveralls
daiquiri
# It's not clear why we need Dask here, but tests are failing
# otherwise. Pinning to arbitrary working version
dask[dataframe]==2024.4.1
datashader
diskcache
hvplot
Expand Down
56 changes: 56 additions & 0 deletions tsbrowse/jit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import functools
import logging
import os

import numba

logger = logging.getLogger(__name__)

_DISABLE_NUMBA = os.environ.get("TSBROWSE_DISABLE_NUMBA", "0")

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

# We will mostly be using disable numba for debugging and running tests for
# coverage, so raise a loud warning in case this is being used accidentally.

if not ENABLE_NUMBA:
logger.warning(
"numba globally disabled for tsbrowse; performance will be drastically"
" reduced."
)


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


def numba_njit(**numba_kwargs):
def _numba_njit(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs) # pragma: no cover

if ENABLE_NUMBA: # pragma: no cover
combined_kwargs = {**DEFAULT_NUMBA_ARGS, **numba_kwargs}
return numba.jit(**combined_kwargs)(func)
else:
return func

return _numba_njit


def numba_jitclass(spec):
def _numba_jitclass(cls):
if ENABLE_NUMBA: # pragma: no cover
return numba.experimental.jitclass(spec)(cls)
else:
return cls

return _numba_jitclass
14 changes: 7 additions & 7 deletions tsbrowse/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import pandas as pd
import tskit

from . import jit
from .cache import disk_cache


logger = daiquiri.getLogger("tsbrowse")

spec = [
Expand All @@ -28,7 +28,7 @@
]


@numba.experimental.jitclass(spec)
@jit.numba_jitclass(spec)
class TreePosition:
def __init__(
self,
Expand Down Expand Up @@ -91,7 +91,7 @@ def alloc_tree_position(ts):
)


@numba.njit
@jit.numba_njit()
def _compute_per_tree_stats(
tree_pos, num_trees, num_nodes, nodes_time, edges_parent, edges_child
):
Expand Down Expand Up @@ -163,7 +163,7 @@ def compute_per_tree_stats(ts):
)


@numba.njit
@jit.numba_njit()
def _compute_mutation_parent_counts(mutations_parent):
N = mutations_parent.shape[0]
num_parents = np.zeros(N, dtype=np.int32)
Expand All @@ -176,7 +176,7 @@ def _compute_mutation_parent_counts(mutations_parent):
return num_parents


@numba.njit
@jit.numba_njit()
def _compute_mutation_inheritance_counts(
tree_pos,
num_nodes,
Expand Down Expand Up @@ -259,7 +259,7 @@ def compute_mutation_counts(ts):
return MutationCounts(num_parents, num_inheritors, num_descendants)


@numba.njit
@jit.numba_njit()
def _compute_population_mutation_counts(
tree_pos,
num_nodes,
Expand Down Expand Up @@ -398,7 +398,7 @@ def _repr_html_(self):
return self.summary_df._repr_html_()

@staticmethod
@numba.njit
@jit.numba_njit()
def child_bounds(num_nodes, edges_left, edges_right, edges_child):
num_edges = edges_left.shape[0]
child_left = np.zeros(num_nodes, dtype=np.float64) + np.inf
Expand Down

0 comments on commit 1f3bfbd

Please sign in to comment.