Skip to content

Commit

Permalink
Add rest of the epochs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teekuningas committed Feb 26, 2024
1 parent d60faa6 commit 18b3f99
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from meggie.utilities.testing import BaseTestAction
from meggie.actions.epochs_delete_from_all import DeleteEpochsFromAll


class TestEpochsDeleteFromAll(BaseTestAction):
def test_epochs_delete_from_all(self):

data = {"outputs": {"epochs": ["Epochs"]}}

self.run_action(
action_name="epochs_delete_from_all",
handler=DeleteEpochsFromAll,
data=data,
patch_paths=["meggie.actions.epochs_delete_from_all"],
)
16 changes: 16 additions & 0 deletions meggie/actions/epochs_info/tests/test_epochs_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from meggie.utilities.testing import BaseTestAction
from meggie.actions.epochs_info import Info


class TestEpochsInfo(BaseTestAction):
def test_epochs_info(self):

data = {"outputs": {"epochs": ["Epochs"]}}

content = self.run_action(
action_name="epochs_info",
handler=Info,
data=data,
patch_paths=["meggie.actions.epochs_info"],
)
assert content
15 changes: 15 additions & 0 deletions meggie/actions/epochs_plot/tests/test_epochs_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from meggie.utilities.testing import BaseTestAction
from meggie.actions.epochs_plot import PlotEpochs


class TestPlotEpochs(BaseTestAction):
def test_epochs_plot(self):

data = {"outputs": {"epochs": ["Epochs"]}}

self.run_action(
action_name="epochs_plot",
handler=PlotEpochs,
data=data,
patch_paths=["meggie.actions.epochs_plot"],
)
15 changes: 15 additions & 0 deletions meggie/actions/epochs_plot_image/tests/test_epochs_plot_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from meggie.utilities.testing import BaseTestAction
from meggie.actions.epochs_plot_image import PlotEpochsImage


class TestPlotEpochsImage(BaseTestAction):
def test_epochs_plot_image(self):

data = {"outputs": {"epochs": ["Epochs"]}}

self.run_action(
action_name="epochs_plot_image",
handler=PlotEpochsImage,
data=data,
patch_paths=["meggie.actions.epochs_plot_image"],
)
19 changes: 17 additions & 2 deletions meggie/utilities/generate_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,25 @@ def create_test_experiment(experiment_folder, experiment_name, n_subjects=2):
stim_channel = find_stim_channel(raw)
events = find_events(raw, stim_channel)

mne_epochs = mne.Epochs(raw, events, {"1": 1, "2": 2}, -0.1, 0.2)
params = {
"tmin": -0.1,
"tmax": 0.2,
"bstart": -0.1,
"bend": 0.2,
}
category = {"1": 1, "2": 2}

mne_epochs = mne.Epochs(
raw,
events,
category,
tmin=params["tmin"],
tmax=params["tmax"],
baseline=(params["bstart"], params["bend"]),
)

epochs_directory = subject.epochs_directory
epochs = Epochs("Epochs", epochs_directory, {}, content=mne_epochs)
epochs = Epochs("Epochs", epochs_directory, params, content=mne_epochs)
epochs.save_content()
subject.add(epochs, "epochs")

Expand Down
22 changes: 21 additions & 1 deletion meggie/utilities/testing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import tempfile
import logging
import importlib
import pytest
import json
Expand Down Expand Up @@ -40,6 +41,10 @@ def patched_exc_messagebox(parent, exc, exec_=False):
raise exc


def patched_logger_exception(msg):
raise Exception(msg)


class BaseTestAction:
@pytest.fixture(autouse=True)
def setup_common(self, qtbot, monkeypatch):
Expand All @@ -58,6 +63,20 @@ def setup_experiment(self):

def run_action(self, action_name, handler, data={}, patch_paths=[]):

# patch mne's plt_show to not show plots
utils = importlib.import_module("mne.viz.utils")
epochs = importlib.import_module("mne.viz.epochs")

def patched_plt_show(*args, **kwargs):
utils.plt_show(show=False, fig=None, **kwargs)

self.monkeypatch.setattr(epochs, "plt_show", patched_plt_show)

# patch logger to raise exceptions
logger = logging.getLogger("ui_logger")
self.monkeypatch.setattr(logger, "exception", patched_logger_exception)

# patch messageboxes to raise exceptions
for patch_path in patch_paths:
module = importlib.import_module(patch_path)

Expand All @@ -73,13 +92,14 @@ def run_action(self, action_name, handler, data={}, patch_paths=[]):
patched_messagebox,
)

# call the action handler
merged_data = {"tab_id": "test_tab"}
merged_data.update(data)
action_spec = load_action_spec(action_name)
self.action_instance = handler(
self.experiment, merged_data, self.mock_main_window, action_spec
)
self.action_instance.run()
return self.action_instance.run()

def find_dialog(self, dialog_class):
dialog = None
Expand Down

0 comments on commit 18b3f99

Please sign in to comment.