diff --git a/arch/tests/univariate/test_positions_and_alias.py b/arch/tests/univariate/test_positions_and_alias.py index 94124050ad..e2f7f3fd2d 100644 --- a/arch/tests/univariate/test_positions_and_alias.py +++ b/arch/tests/univariate/test_positions_and_alias.py @@ -6,7 +6,8 @@ in Python. """ -import matplotlib.pyplot as plt +from arch.compat.matplotlib import HAS_MATPLOTLIB + import numpy as np from numpy.testing import assert_allclose import pandas as pd @@ -29,6 +30,9 @@ ) from arch.utility.array import cutoff_to_index, find_index +if HAS_MATPLOTLIB: + import matplotlib.pyplot as plt + NOBS = 300 DATES = pd.date_range("2000-01-03", periods=NOBS, freq="B") PARAMS = np.array([0.05, 0.05, 0.1, 0.8]) @@ -183,6 +187,7 @@ def test_forecast_start_below_minus_nobs(result, model): model.forecast(result.params, horizon=2, start=start) +@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed") def test_hedgehog_plot_negative_start(result): kwargs = {"horizon": 3, "step": 50} fig_neg = result.hedgehog_plot(start=-100, **kwargs) diff --git a/arch/tests/univariate/test_string_options.py b/arch/tests/univariate/test_string_options.py index 360ccb9544..b008280a78 100644 --- a/arch/tests/univariate/test_string_options.py +++ b/arch/tests/univariate/test_string_options.py @@ -1,8 +1,9 @@ """String options of the univariate models are not case sensitive""" +from arch.compat.matplotlib import HAS_MATPLOTLIB + from functools import partial -import matplotlib.pyplot as plt import numpy as np from numpy.testing import assert_allclose import pandas as pd @@ -30,6 +31,9 @@ ) from arch.univariate.base import ARCHModelForecast, _align_forecast +if HAS_MATPLOTLIB: + import matplotlib.pyplot as plt + # A string that is not an option is a ValueError and anything else a TypeError BAD_VALUES = [ pytest.param("bogus", ValueError, id="bogus"), @@ -242,6 +246,7 @@ def test_forecast_method_invalid(returns, result, bad, exc, use_result): @pytest.mark.parametrize("annualize", ["d", "w", "m"]) +@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed") def test_plot_annualize_case(result, annualize): scale = {"d": 252, "w": 52, "m": 12}[annualize] expected = result.conditional_volatility * np.sqrt(scale) @@ -258,6 +263,7 @@ def test_plot_annualize_case(result, annualize): @pytest.mark.parametrize( ("bad", "exc"), [("bogus", ValueError), ("Q", ValueError), (1, TypeError)] ) +@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed") def test_plot_annualize_invalid(result, bad, exc): with pytest.raises(exc, match=r"annualize must be one of 'd', 'w' or 'm'; got "): result.plot(annualize=bad) @@ -265,6 +271,7 @@ def test_plot_annualize_invalid(result, bad, exc): @pytest.mark.parametrize("plot_type", ["volatility", "mean"]) +@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed") def test_hedgehog_plot_type_case(result, plot_type, returns): try: for spelling in (plot_type, plot_type.upper(), plot_type.title()): @@ -281,6 +288,7 @@ def test_hedgehog_plot_type_case(result, plot_type, returns): @pytest.mark.parametrize(("bad", "exc"), BAD_VALUES) +@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed") def test_hedgehog_plot_invalid(result, bad, exc): with pytest.raises(exc, match=r"plot_type must be one of 'volatility' or"): result.hedgehog_plot(plot_type=bad) diff --git a/arch/tests/univariate/test_validation_base.py b/arch/tests/univariate/test_validation_base.py index 0b2f2b9844..7b9c0c84e2 100644 --- a/arch/tests/univariate/test_validation_base.py +++ b/arch/tests/univariate/test_validation_base.py @@ -7,12 +7,13 @@ functions in ``arch.univariate.base`` and of ``arch.univariate.diagnostics``. """ +from arch.compat.matplotlib import HAS_MATPLOTLIB + from collections.abc import Callable from fractions import Fraction from types import MappingProxyType from typing import Any -import matplotlib.pyplot as plt import numpy as np from numpy.testing import assert_allclose import pandas as pd @@ -43,6 +44,9 @@ from arch.univariate.diagnostics import excess_kurtosis, hill_estimator, var_ratio from arch.utility.exceptions import StartingValueWarning +if HAS_MATPLOTLIB: + import matplotlib.pyplot as plt + NOBS = 250 INDEX = pd.date_range("2000-01-03", periods=NOBS, freq="B") PARAMS = np.array([0.05, 0.05, 0.1, 0.8]) @@ -414,7 +418,8 @@ def call(**kwargs: Any) -> Any: try: return getattr(fitted(), name)(**kwargs) finally: - plt.close("all") + if HAS_MATPLOTLIB: + plt.close("all") return call @@ -950,6 +955,8 @@ def constraints(**kwargs: Any): ), ) def test_validation(row): + if not HAS_MATPLOTLIB and row.target in ("plot", "hedgehog"): + pytest.skip("matplotlib not installed") run_row(row) @@ -1002,6 +1009,7 @@ def test_forecast_of_fixed_result_checks_arguments(): assert fixed.forecast(horizon=np.int64(2)).mean.shape[1] == 2 +@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed") def test_plot_error_does_not_leave_a_figure(): plt.close("all") with pytest.raises(ValueError, match=r"^annualize must be"):