File: test_lof.py

package info (click to toggle)
python-mne 1.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 131,492 kB
  • sloc: python: 213,302; javascript: 12,910; sh: 447; makefile: 144
file content (38 lines) | stat: -rw-r--r-- 1,093 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.

from pathlib import Path

import pytest

from mne.io import read_raw_fif
from mne.preprocessing import find_bad_channels_lof

base_dir = Path(__file__).parent.parent.parent / "io" / "tests" / "data"
raw_fname = base_dir / "test_raw.fif"


@pytest.mark.parametrize(
    "n_neighbors, ch_type, n_ch, n_bad",
    [
        (8, "eeg", 60, 8),
        (10, "grad", 204, 2),
        (20, "mag", 102, 0),
        (30, "grad", 204, 2),
    ],
)
def test_lof(n_neighbors, ch_type, n_ch, n_bad):
    """Test LOF detection."""
    pytest.importorskip("sklearn")
    raw = read_raw_fif(raw_fname).load_data()
    assert raw.info["bads"] == []
    bads, scores = find_bad_channels_lof(
        raw, n_neighbors, picks=ch_type, return_scores=True
    )
    bads_2 = find_bad_channels_lof(raw, n_neighbors, picks=ch_type)
    assert len(scores) == n_ch
    assert len(bads) == n_bad
    assert bads == bads_2
    with pytest.raises(ValueError, match="channel type"):
        find_bad_channels_lof(raw)