Skip to content

Commit

Permalink
use pytest importorskip, following: mne-tools/mne-python#11847
Browse files Browse the repository at this point in the history
  • Loading branch information
sappelhoff committed Aug 1, 2023
1 parent f4ad8d5 commit 2f7b8ac
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 44 deletions.
12 changes: 3 additions & 9 deletions mne_bids/commands/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
38 changes: 12 additions & 26 deletions mne_bids/tests/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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"
)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -458,14 +444,14 @@ 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)]
)
@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)
9 changes: 4 additions & 5 deletions mne_bids/tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -96,21 +95,21 @@ 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)

_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"]
)
Expand All @@ -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
Expand All @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions mne_bids/tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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"],
Expand All @@ -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

Expand Down Expand Up @@ -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"],
Expand All @@ -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

Expand Down

0 comments on commit 2f7b8ac

Please sign in to comment.