Skip to content

Commit

Permalink
Move erp functions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
withmywoessner committed Dec 16, 2024
1 parent f6249a2 commit 633d644
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion mne/stats/tests/test_erp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@

from mne import Epochs, read_events
from mne.io import read_raw_fif
from mne.stats.erp import compute_sme
from mne.stats.erp import (
compute_sme,
get_peak,
get_area,
get_frac_peak_latency,
get_frac_area_latency,
)

base_dir = Path(__file__).parents[2] / "io" / "tests" / "data"
raw = read_raw_fif(base_dir / "test_raw.fif")
Expand All @@ -19,6 +25,7 @@ def test_compute_sme():
"""Test SME computation."""
epochs = Epochs(raw, events)
sme = compute_sme(epochs, start=0, stop=0.1)
print(sme)
assert sme.shape == (376,)

with pytest.raises(TypeError, match="int or float"):
Expand All @@ -29,3 +36,60 @@ def test_compute_sme():
compute_sme(epochs, -1.2, 0.3)
with pytest.raises(ValueError, match="out of bounds"):
compute_sme(epochs, -0.1, 0.8)


def test_get_peak():
"""Test peak computation."""
epochs = Epochs(raw, events)
evoked = epochs.average()
peak_df = get_peak(evoked, tmin=0, tmax=0.1)
evoked_pos = evoked.copy()
evoked_pos.data = abs(evoked_pos.data)
get_peak(evoked_pos, tmin=0, tmax=0.1, mode="neg", strict=False)

evoked_neg = evoked.copy()
evoked_neg.data = -abs(evoked_neg.data)
get_peak(evoked_neg, tmin=0, tmax=0.1, mode="pos", strict=False)

assert peak_df.shape == (366, 3)

with pytest.raises(ValueError, match="No negative values encountered"):
evoked_pos.data = abs(evoked_pos.data)
get_peak(evoked_pos, tmin=0, tmax=0.1, mode="neg", strict=True)
with pytest.raises(ValueError, match="No positive values encountered"):
get_peak(evoked_neg, tmin=0, tmax=0.1, mode="pos", strict=True)


def test_get_area():
"""Test area computation."""
epochs = Epochs(raw, events)
evoked = epochs.average()
area_df = get_area(evoked, tmin=0, tmax=0.1)
assert area_df.shape == (366, 2)


def test_get_frac_peak_latency():
"""Test fractional peak latency computation."""
epochs = Epochs(raw, events)
evoked = epochs.average()
frac_peak_df = get_frac_peak_latency(evoked, frac=0.5, tmin=0, tmax=0.1)
assert frac_peak_df.shape == (366, 4)

with pytest.raises(ValueError, match="No negative values encountered"):
evoked_pos = evoked.copy()
evoked_pos.data = abs(evoked_pos.data)
get_frac_peak_latency(evoked, frac=0.5, tmin=0,
tmax=0.1, mode="neg", strict=True)
with pytest.raises(ValueError, match="No positive values encountered"):
evoked_neg = evoked.copy()
evoked_neg.data = -abs(evoked_neg.data)
get_frac_peak_latency(evoked, frac=0.5, tmin=0,
tmax=0.1, mode="pos", strict=True)


def test_get_frac_area_latency():
"""Test fractional area latency computation."""
epochs = Epochs(raw, events)
evoked = epochs.average()
frac_area_df = get_frac_area_latency(evoked, frac=0.5, tmin=0, tmax=0.1)
assert frac_area_df.shape == (366, 3)

0 comments on commit 633d644

Please sign in to comment.