Skip to content

Commit

Permalink
Update action test framework to be more generic and add epochs_delete
Browse files Browse the repository at this point in the history
  • Loading branch information
teekuningas committed Feb 26, 2024
1 parent 18ed5bd commit d60faa6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 18 deletions.
7 changes: 4 additions & 3 deletions meggie/actions/epochs_create/tests/test_epochs_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@


class TestEpochsCreate(BaseTestAction):
def test_create_epochs_dialog(self):
def test_epochs_create(self):

self.run_action(
tab_id="epochs",
action_name="epochs_create",
handler=CreateEpochs,
dialog_path="meggie.actions.epochs_create.dialogs.createEpochsFromEventsDialogMain",
patch_paths=[
"meggie.actions.epochs_create.dialogs.createEpochsFromEventsDialogMain"
],
)
dialog = self.find_dialog(CreateEpochsFromEventsDialog)

Expand Down
15 changes: 15 additions & 0 deletions meggie/actions/epochs_delete/tests/test_epochs_delete.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_delete import DeleteEpochs


class TestEpochsDelete(BaseTestAction):
def test_epochs_delete(self):

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

self.run_action(
action_name="epochs_delete",
handler=DeleteEpochs,
data=data,
patch_paths=["meggie.actions.epochs_delete"],
)
29 changes: 29 additions & 0 deletions meggie/utilities/generate_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
from meggie.mainwindow.preferences import PreferencesHandler

from meggie.experiment import initialize_new_experiment
from meggie.utilities.events import find_events
from meggie.utilities.events import find_stim_channel
from meggie.datatypes.epochs.epochs import Epochs


def create_experiment(
Expand Down Expand Up @@ -139,9 +142,35 @@ def create_evoked_conditions_experiment(
)


def create_test_experiment(experiment_folder, experiment_name, n_subjects=2):
"""Generate experiment with data for testing."""

experiment = create_evoked_conditions_experiment(
experiment_folder, experiment_name, n_subjects=n_subjects
)

# create trivial epochs
for subject in experiment.subjects.values():
raw = subject.get_raw()
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)

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

return experiment


# allow creating experiment from the command line
if __name__ == "__main__":
type_, experiment_folder, experiment_name = sys.argv[1:]

if type_ == "test_experiment":
create_test_experiment(experiment_folder, experiment_name)

if type_ == "evoked_conditions":
create_evoked_conditions_experiment(experiment_folder, experiment_name)
37 changes: 22 additions & 15 deletions meggie/utilities/testing.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import tempfile
import importlib
import pytest
import json
import os
import pkg_resources

from PyQt5.QtWidgets import QApplication, QMainWindow

from meggie.utilities.generate_experiments import create_evoked_conditions_experiment
from meggie.utilities.generate_experiments import create_test_experiment

os.environ["QT_QPA_PLATFORM"] = "offscreen"

Expand Down Expand Up @@ -52,25 +53,31 @@ def setup_common(self, qtbot, monkeypatch):
yield

def setup_experiment(self):
self.experiment = create_evoked_conditions_experiment(
self.dirpath, "test_experiment", n_subjects=1
)
self.experiment = create_test_experiment(self.dirpath, "test_experiment")
self.experiment.activate_subject("sample_01-raw")

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

self.monkeypatch.setattr(
".".join([dialog_path, "exc_messagebox"]),
patched_exc_messagebox,
)
self.monkeypatch.setattr(
".".join([dialog_path, "messagebox"]),
patched_messagebox,
)
data = {"tab_id": tab_id}
for patch_path in patch_paths:
module = importlib.import_module(patch_path)

if getattr(module, "exc_messagebox", None):
self.monkeypatch.setattr(
".".join([patch_path, "exc_messagebox"]),
patched_exc_messagebox,
)

if getattr(module, "messagebox", None):
self.monkeypatch.setattr(
".".join([patch_path, "messagebox"]),
patched_messagebox,
)

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

Expand Down

0 comments on commit d60faa6

Please sign in to comment.