-
-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Skip tests for optional/extra dependencies when not installed (#1113)
* Skip tests for optional/extra dependencies when not installed * Update changelog * Fix pystan_version() * Fix pylint issues * Relocate numba specific tests in test_diagnostics (and skip if not installed) * Relocate numba specific tests in test_utils (and skip if not installed) * Relocate numba specific tests in test_stats (and skip if not installed) * Add CI environment variable * Add custom importorskip for CI * Use internal `importorskip` * Displayed skipped files in pytest output (due to importorskip) * Move CI env variable detection into `importorskip` * Test `importorskip` for local/ci machines with `monkeypatch` * Ignore vscode config * Properly test for local machine with monkeypatch deleting CI env * Add back in some parts of `pytest.importorskip` to our `importorskip` to fix errors * Clarify reason text when test skipped for lack of pystan/pystan3 * Refactor helper function to test running on CI machine * Ensure individual tests for external requirements only skip locally * Use `importlib.import_module` in `importorskip` * Attempt to fix failing CI imports * Revert import method * Correct skip logic * Breakup import statements * Correct `pydocstyle` issues * Correct pystan skip logic
- Loading branch information
Showing
23 changed files
with
452 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ target/ | |
|
||
# IDE configs | ||
.idea/ | ||
.vscode/ | ||
|
||
saved_animations/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
"""Test Diagnostic methods""" | ||
import importlib | ||
|
||
# pylint: disable=redefined-outer-name, no-member, too-many-public-methods | ||
import numpy as np | ||
import pytest | ||
|
||
from ...data import load_arviz_data | ||
from ..helpers import running_on_ci | ||
from ...rcparams import rcParams | ||
from ...stats import bfmi, geweke, mcse, rhat | ||
from ...stats.diagnostics import _mc_error, ks_summary | ||
from ...utils import Numba | ||
from .test_diagnostics import data # pylint: disable=unused-import | ||
|
||
|
||
pytestmark = pytest.mark.skipif( # pylint: disable=invalid-name | ||
(importlib.util.find_spec("numba") is None) & ~running_on_ci(), | ||
reason="test requires numba which is not installed", | ||
) | ||
|
||
rcParams["data.load"] = "eager" | ||
|
||
|
||
def test_numba_bfmi(): | ||
"""Numba test for bfmi.""" | ||
state = Numba.numba_flag | ||
school = load_arviz_data("centered_eight") | ||
data_md = np.random.rand(100, 100, 10) | ||
Numba.disable_numba() | ||
non_numba = bfmi(school.posterior["mu"].values) | ||
non_numba_md = bfmi(data_md) | ||
Numba.enable_numba() | ||
with_numba = bfmi(school.posterior["mu"].values) | ||
with_numba_md = bfmi(data_md) | ||
assert np.allclose(non_numba_md, with_numba_md) | ||
assert np.allclose(with_numba, non_numba) | ||
assert state == Numba.numba_flag | ||
|
||
|
||
@pytest.mark.parametrize("method", ("rank", "split", "folded", "z_scale", "identity")) | ||
def test_numba_rhat(method): | ||
"""Numba test for mcse.""" | ||
state = Numba.numba_flag | ||
school = np.random.rand(100, 100) | ||
Numba.disable_numba() | ||
non_numba = rhat(school, method=method) | ||
Numba.enable_numba() | ||
with_numba = rhat(school, method=method) | ||
assert np.allclose(with_numba, non_numba) | ||
assert Numba.numba_flag == state | ||
|
||
|
||
@pytest.mark.parametrize("method", ("mean", "sd", "quantile")) | ||
def test_numba_mcse(method, prob=None): | ||
"""Numba test for mcse.""" | ||
state = Numba.numba_flag | ||
school = np.random.rand(100, 100) | ||
if method == "quantile": | ||
prob = 0.80 | ||
Numba.disable_numba() | ||
non_numba = mcse(school, method=method, prob=prob) | ||
Numba.enable_numba() | ||
with_numba = mcse(school, method=method, prob=prob) | ||
assert np.allclose(with_numba, non_numba) | ||
assert Numba.numba_flag == state | ||
|
||
|
||
def test_ks_summary_numba(): | ||
"""Numba test for ks_summary.""" | ||
state = Numba.numba_flag | ||
data = np.random.randn(100, 100) | ||
Numba.disable_numba() | ||
non_numba = (ks_summary(data)["Count"]).values | ||
Numba.enable_numba() | ||
with_numba = (ks_summary(data)["Count"]).values | ||
assert np.allclose(non_numba, with_numba) | ||
assert Numba.numba_flag == state | ||
|
||
|
||
def test_geweke_numba(): | ||
"""Numba test for geweke.""" | ||
state = Numba.numba_flag | ||
data = np.random.randn(100) | ||
Numba.disable_numba() | ||
non_numba = geweke(data) | ||
Numba.enable_numba() | ||
with_numba = geweke(data) | ||
assert np.allclose(non_numba, with_numba) | ||
assert Numba.numba_flag == state | ||
|
||
|
||
@pytest.mark.parametrize("batches", (1, 20)) | ||
@pytest.mark.parametrize("circular", (True, False)) | ||
def test_mcse_error_numba(batches, circular): | ||
"""Numba test for mcse_error.""" | ||
data = np.random.randn(100, 100) | ||
state = Numba.numba_flag | ||
Numba.disable_numba() | ||
non_numba = _mc_error(data, batches=batches, circular=circular) | ||
Numba.enable_numba() | ||
with_numba = _mc_error(data, batches=batches, circular=circular) | ||
assert np.allclose(non_numba, with_numba) | ||
assert state == Numba.numba_flag |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pytest | ||
from _pytest.outcomes import Skipped | ||
|
||
from ..helpers import importorskip | ||
|
||
|
||
def test_importorskip_local(monkeypatch): | ||
"""Test ``importorskip`` run on local machine with non-existent module, which should skip.""" | ||
monkeypatch.delenv("ARVIZ_CI_MACHINE", raising=False) | ||
with pytest.raises(Skipped): | ||
importorskip("non-existent-function") | ||
|
||
|
||
def test_importorskip_ci(monkeypatch): | ||
"""Test ``importorskip`` run on CI machine with non-existent module, which should fail.""" | ||
monkeypatch.setenv("ARVIZ_CI_MACHINE", 1) | ||
with pytest.raises(ModuleNotFoundError): | ||
importorskip("non-existent-function") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.