Skip to content

Commit

Permalink
Added coverage testing for fastmath.py
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlaw committed Jan 26, 2025
1 parent db1958a commit 92a2b41
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions stumpy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"STUMPY_EXCL_ZONE_DENOM": 4,
"STUMPY_FASTMATH_TRUE": True,
"STUMPY_FASTMATH_FLAGS": {"nsz", "arcp", "contract", "afn", "reassoc"},
"STUMPY_FASTMATH_FASTMATH._ADD_ASSOC": True,
}

# In addition to these configuration variables, there exist config variables
Expand Down
14 changes: 12 additions & 2 deletions stumpy/fastmath.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import importlib

import numba
from numba import njit

from . import config
Expand Down Expand Up @@ -52,8 +53,17 @@ def _set(module_name, func_name, flag):
"""
module = importlib.import_module(f".{module_name}", package="stumpy")
func = getattr(module, func_name)
func.targetoptions["fastmath"] = flag
func.recompile()
try:
func.targetoptions["fastmath"] = flag
func.recompile()
except AttributeError as e:
if numba.config.DISABLE_JIT and (
str(e) == "'function' object has no attribute 'targetoptions'"
or str(e) == "'function' object has no attribute 'recompile'"
):
pass
else: # pragma: no cover
raise

return

Expand Down
4 changes: 2 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ set_ray_coveragerc()
show_coverage_report()
{
set_ray_coveragerc
coverage report -m --fail-under=100 --skip-covered --omit=fastmath.py,docstring.py,min_versions.py,ray_python_version.py,tests/test_fastmath.py $fcoveragerc
coverage report -m --fail-under=100 --skip-covered --omit=fastmath.py,docstring.py,min_versions.py,ray_python_version.py $fcoveragerc
}

gen_coverage_xml_report()
{
# This function saves the coverage report in Cobertura XML format, which is compatible with codecov
set_ray_coveragerc
coverage xml -o $fcoveragexml --fail-under=100 --omit=fastmath.py,docstring.py,min_versions.py,ray_python_version.py,tests/test_fastmath.py $fcoveragerc
coverage xml -o $fcoveragexml --fail-under=100 --omit=fastmath.py,docstring.py,min_versions.py,ray_python_version.py $fcoveragerc
}

test_custom()
Expand Down
14 changes: 8 additions & 6 deletions tests/test_fastmath.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import numba
import numpy as np
import pytest

from stumpy import fastmath

if numba.config.DISABLE_JIT:
pytest.skip("Skipping Tests JIT is disabled", allow_module_level=True)


def test_set():
# The test is done by changing the value of fastmath flag for
Expand All @@ -21,7 +17,10 @@ def test_set():
# case2: flag={'reassoc', 'nsz'}
fastmath._set("fastmath", "_add_assoc", flag={"reassoc", "nsz"})
out = fastmath._add_assoc(0, np.inf)
assert out == 0.0
if numba.config.DISABLE_JIT:
assert np.isnan(out)
else: # pragma: no cover
assert out == 0.0

# case3: flag={'reassoc'}
fastmath._set("fastmath", "_add_assoc", flag={"reassoc"})
Expand All @@ -41,4 +40,7 @@ def test_reset():
# and then reset it to the default value, i.e. `True`
fastmath._set("fastmath", "_add_assoc", False)
fastmath._reset("fastmath", "_add_assoc")
assert fastmath._add_assoc(0.0, np.inf) == 0.0
if numba.config.DISABLE_JIT:
assert np.isnan(fastmath._add_assoc(0.0, np.inf))
else: # pragma: no cover
assert fastmath._add_assoc(0.0, np.inf) == 0.0

0 comments on commit 92a2b41

Please sign in to comment.