From 2f7b8aca8cd9a45cce734ea620388f30b5e310d2 Mon Sep 17 00:00:00 2001 From: Stefan Appelhoff Date: Tue, 1 Aug 2023 11:16:40 +0200 Subject: [PATCH] use pytest importorskip, following: https://github.com/mne-tools/mne-python/pull/11847 --- mne_bids/commands/tests/test_cli.py | 12 +++------ mne_bids/tests/test_inspect.py | 38 +++++++++-------------------- mne_bids/tests/test_stats.py | 9 +++---- mne_bids/tests/test_write.py | 8 +++--- 4 files changed, 23 insertions(+), 44 deletions(-) diff --git a/mne_bids/commands/tests/test_cli.py b/mne_bids/commands/tests/test_cli.py index 2f9323c86..31f05b18c 100644 --- a/mne_bids/commands/tests/test_cli.py +++ b/mne_bids/commands/tests/test_cli.py @@ -5,14 +5,12 @@ # License: BSD-3-Clause import os.path as op from pathlib import Path -from functools import partial import pytest import mne from mne.datasets import testing -from mne.utils import ArgvSetter, requires_pandas -from mne.utils._testing import requires_module +from mne.utils import ArgvSetter from mne_bids.commands import ( mne_bids_raw_to_bids, @@ -26,10 +24,6 @@ from mne_bids import BIDSPath, read_raw_bids, write_raw_bids -requires_matplotlib = partial( - requires_module, name="matplotlib", call="import matplotlib" -) - data_path = testing.data_path(download=False) base_path = op.join(op.dirname(mne.__file__), "io") subject_id = "01" @@ -343,10 +337,10 @@ def test_crosstalk_to_bids(tmp_path): assert bids_path.meg_crosstalk_fpath.exists() -@requires_pandas @testing.requires_testing_data def test_count_events(tmp_path): """Test mne_bids count_events.""" + pytest.importorskip("pandas") # Check that help is printed check_usage(mne_bids_count_events) @@ -389,10 +383,10 @@ def test_count_events(tmp_path): mne_bids_count_events.run() -@requires_matplotlib @testing.requires_testing_data def test_inspect(tmp_path): """Test mne_bids inspect.""" + pytest.importorskip("matplotlib") # Check that help is printed check_usage(mne_bids_inspect) diff --git a/mne_bids/tests/test_inspect.py b/mne_bids/tests/test_inspect.py index e97005b0a..2f2a3213a 100644 --- a/mne_bids/tests/test_inspect.py +++ b/mne_bids/tests/test_inspect.py @@ -2,14 +2,12 @@ import os.path as op import pytest -from functools import partial import numpy as np from matplotlib.backend_bases import KeyEvent import mne from mne.datasets import testing -from mne.utils._testing import requires_module from mne.viz.utils import _fake_click from mne_bids import ( @@ -25,10 +23,6 @@ from test_read import warning_str -requires_matplotlib = partial( - requires_module, name="matplotlib", call="import matplotlib" -) - _bids_path = BIDSPath( subject="01", session="01", run="01", task="testing", datatype="meg" ) @@ -70,15 +64,14 @@ def setup_bids_test_dir(bids_root): return bids_root -@requires_matplotlib @testing.requires_testing_data @pytest.mark.parametrize("save_changes", (True, False)) @pytest.mark.filterwarnings(warning_str["channel_unit_changed"]) def test_inspect_single_file(tmp_path, save_changes): """Test inspecting a dataset consisting of only a single file.""" - from mne.utils._testing import _click_ch_name - import matplotlib + matplotlib = pytest.importorskip("matplotlib") import matplotlib.pyplot as plt + from mne.utils._testing import _click_ch_name matplotlib.use("Agg") plt.close("all") @@ -117,12 +110,11 @@ def test_inspect_single_file(tmp_path, save_changes): assert old_bads == new_bads -@requires_matplotlib @testing.requires_testing_data @pytest.mark.filterwarnings(warning_str["channel_unit_changed"]) def test_inspect_multiple_files(tmp_path): """Test inspecting a dataset consisting of more than one file.""" - import matplotlib + matplotlib = pytest.importorskip("matplotlib") import matplotlib.pyplot as plt matplotlib.use("Agg") @@ -145,14 +137,13 @@ def test_inspect_multiple_files(tmp_path): raw_fig.canvas.callbacks.process("key_press_event", key_event) -@requires_matplotlib @testing.requires_testing_data @pytest.mark.filterwarnings(warning_str["channel_unit_changed"]) def test_inspect_set_and_unset_bads(tmp_path): """Test marking channels as bad and later marking them as good again.""" - from mne.utils._testing import _click_ch_name - import matplotlib + matplotlib = pytest.importorskip("matplotlib") import matplotlib.pyplot as plt + from mne.utils._testing import _click_ch_name matplotlib.use("Agg") plt.close("all") @@ -221,12 +212,11 @@ def _add_annotation(raw_fig): _fake_click(raw_fig, data_ax, [5.0, 1.0], xform="data", button=1, kind="release") -@requires_matplotlib @testing.requires_testing_data @pytest.mark.filterwarnings(warning_str["channel_unit_changed"]) def test_inspect_annotations(tmp_path): """Test inspection of Annotations.""" - import matplotlib + matplotlib = pytest.importorskip("matplotlib") import matplotlib.pyplot as plt matplotlib.use("Agg") @@ -278,12 +268,11 @@ def test_inspect_annotations(tmp_path): assert raw.annotations == orig_annotations -@requires_matplotlib @testing.requires_testing_data @pytest.mark.filterwarnings(warning_str["channel_unit_changed"]) def test_inspect_annotations_remove_all(tmp_path): """Test behavior if all Annotations are removed by the user.""" - import matplotlib + matplotlib = pytest.importorskip("matplotlib") import matplotlib.pyplot as plt matplotlib.use("Agg") @@ -338,12 +327,11 @@ def test_inspect_annotations_remove_all(tmp_path): assert not events_tsv_fpath.exists() -@requires_matplotlib @testing.requires_testing_data @pytest.mark.filterwarnings(warning_str["channel_unit_changed"]) def test_inspect_dont_show_annotations(tmp_path): """Test if show_annotations=False works.""" - import matplotlib + matplotlib = pytest.importorskip("matplotlib") import matplotlib.pyplot as plt matplotlib.use("Agg") @@ -356,14 +344,13 @@ def test_inspect_dont_show_annotations(tmp_path): assert not raw_fig.mne.annotations -@requires_matplotlib @testing.requires_testing_data @pytest.mark.filterwarnings(warning_str["channel_unit_changed"]) def test_inspect_bads_and_annotations(tmp_path): """Test adding bads and Annotations in one go.""" - from mne.utils._testing import _click_ch_name - import matplotlib + matplotlib = pytest.importorskip("matplotlib") import matplotlib.pyplot as plt + from mne.utils._testing import _click_ch_name matplotlib.use("Agg") plt.close("all") @@ -398,13 +385,12 @@ def test_inspect_bads_and_annotations(tmp_path): assert "BAD_test" in raw.annotations.description -@requires_matplotlib @testing.requires_testing_data @pytest.mark.parametrize("save_changes", (True, False)) @pytest.mark.filterwarnings(warning_str["channel_unit_changed"]) def test_inspect_auto_flats(tmp_path, save_changes): """Test flat channel & segment detection.""" - import matplotlib + matplotlib = pytest.importorskip("matplotlib") import matplotlib.pyplot as plt matplotlib.use("Agg") @@ -458,7 +444,6 @@ def test_inspect_auto_flats(tmp_path, save_changes): assert "BAD_flat" not in raw.annotations.description -@requires_matplotlib @testing.requires_testing_data @pytest.mark.parametrize( ("l_freq", "h_freq"), [(None, None), (1, None), (None, 30), (1, 30)] @@ -466,6 +451,7 @@ def test_inspect_auto_flats(tmp_path, save_changes): @pytest.mark.filterwarnings(warning_str["channel_unit_changed"]) def test_inspect_freq_filter(tmp_path, l_freq, h_freq): """Test frequency filter for Raw display.""" + pytest.importorskip("matplotlib") bids_root = setup_bids_test_dir(tmp_path) bids_path = _bids_path.copy().update(root=bids_root) inspect_dataset(bids_path, l_freq=l_freq, h_freq=h_freq, find_flat=False) diff --git a/mne_bids/tests/test_stats.py b/mne_bids/tests/test_stats.py index 12b58a72a..68e0878ea 100644 --- a/mne_bids/tests/test_stats.py +++ b/mne_bids/tests/test_stats.py @@ -9,7 +9,6 @@ import numpy as np import mne -from mne.utils import requires_pandas from mne.datasets import testing from mne_bids import BIDSPath, write_raw_bids @@ -96,10 +95,10 @@ def _check_counts( (["01"], ["task1", "task2"], [None], [None]), ], ) -@requires_pandas @testing.requires_testing_data def test_count_events(tmp_path, subjects, tasks, runs, sessions): """Test the event counts.""" + pytest.importorskip("pandas") root, events, event_id = _make_dataset(tmp_path, subjects, tasks, runs, sessions) counts = count_events(root) @@ -107,10 +106,10 @@ def test_count_events(tmp_path, subjects, tasks, runs, sessions): _check_counts(counts, events, event_id, subjects, tasks, runs, sessions) -@requires_pandas @testing.requires_testing_data def test_count_events_bids_path(tmp_path): """Test the event counts passing a BIDSPath.""" + pytest.importorskip("pandas") root, events, event_id = _make_dataset( tmp_path, subjects=["01", "02"], tasks=["task1"] ) @@ -125,10 +124,10 @@ def test_count_events_bids_path(tmp_path): _check_counts(counts, events, event_id, subjects=["01"], tasks=["task1"]) -@requires_pandas @testing.requires_testing_data def test_count_no_events_file(tmp_path): """Test count_events with no event present.""" + pytest.importorskip("pandas") raw_fname = data_path / "MEG" / "sample" / "sample_audvis_trunc_raw.fif" raw = mne.io.read_raw(raw_fname) raw.info["line_freq"] = 60.0 @@ -145,10 +144,10 @@ def test_count_no_events_file(tmp_path): count_events(root) -@requires_pandas @testing.requires_testing_data def test_count_no_events_column(tmp_path): """Test case where events.tsv doesn't contain [stim,trial]_type column.""" + pytest.importorskip("pandas") subject, task, run, session, datatype = "01", "task1", "01", "01", "meg" root, events, event_id = _make_dataset( tmp_path, [subject], [task], [run], [session] diff --git a/mne_bids/tests/test_write.py b/mne_bids/tests/test_write.py index 04aedaff8..63ad528e8 100644 --- a/mne_bids/tests/test_write.py +++ b/mne_bids/tests/test_write.py @@ -27,7 +27,7 @@ import mne from mne.datasets import testing -from mne.utils import check_version, requires_version +from mne.utils import check_version from mne.io import anonymize_info from mne.io.constants import FIFF from mne.io.kit.kit import get_kit_info @@ -502,12 +502,12 @@ def test_line_freq(line_freq, _bids_validate, tmp_path): assert eeg_json["PowerLineFrequency"] == "n/a" -@requires_version("pybv", PYBV_VERSION) @testing.requires_testing_data @pytest.mark.filterwarnings(warning_str["channel_unit_changed"]) @pytest.mark.filterwarnings(warning_str["maxshield"]) def test_fif(_bids_validate, tmp_path): """Test functionality of the write_raw_bids conversion for fif.""" + pytest.importorskip("pybv", PYBV_VERSION) bids_root = tmp_path / "bids1" bids_path = _bids_path.copy().update(root=bids_root, datatype="meg") raw_fname = op.join(data_path, "MEG", "sample", "sample_audvis_trunc_raw.fif") @@ -3282,7 +3282,6 @@ def test_sidecar_encoding(_bids_validate, tmp_path): assert_array_equal(raw.annotations.description, raw_read.annotations.description) -@requires_version("pybv", PYBV_VERSION) @pytest.mark.parametrize("dir_name, format, fname, reader", test_converteeg_data) @pytest.mark.filterwarnings( warning_str["channel_unit_changed"], @@ -3294,6 +3293,7 @@ def test_sidecar_encoding(_bids_validate, tmp_path): @testing.requires_testing_data def test_convert_eeg_formats(dir_name, format, fname, reader, tmp_path): """Test conversion of EEG/iEEG manufacturer fmt to BrainVision/EDF.""" + pytest.importorskip("pybv", PYBV_VERSION) bids_root = tmp_path / format raw_fname = data_path / dir_name / fname @@ -3367,7 +3367,6 @@ def test_convert_eeg_formats(dir_name, format, fname, reader, tmp_path): assert_array_almost_equal(raw.get_data(), raw2.get_data()[:, :orig_len], decimal=6) -@requires_version("pybv", PYBV_VERSION) @pytest.mark.parametrize("dir_name, format, fname, reader", test_converteeg_data) @pytest.mark.filterwarnings( warning_str["channel_unit_changed"], @@ -3379,6 +3378,7 @@ def test_convert_eeg_formats(dir_name, format, fname, reader, tmp_path): @testing.requires_testing_data def test_format_conversion_overwrite(dir_name, format, fname, reader, tmp_path): """Test that overwrite works when format is passed to write_raw_bids.""" + pytest.importorskip("pybv", PYBV_VERSION) bids_root = tmp_path / format raw_fname = data_path / dir_name / fname