Skip to content

Commit

Permalink
Replace timestamped output folder with a dialog where user selects th…
Browse files Browse the repository at this point in the history
…e destination
  • Loading branch information
teekuningas committed Feb 22, 2025
1 parent 983955e commit ad772a8
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 108 deletions.
32 changes: 27 additions & 5 deletions meggie/actions/evoked_save/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""Contains save evoked action handling."""

import os

from PyQt5 import QtWidgets

from meggie.utilities.messaging import exc_messagebox
from meggie.utilities.validators import assert_arrays_same
from meggie.utilities.filemanager import homepath

from meggie.mainwindow.dynamic import Action
from meggie.mainwindow.dynamic import subject_action

from meggie.actions.evoked_save.controller.evoked import save_channel_averages
from meggie.actions.evoked_save.controller.evoked import save_all_channels
Expand Down Expand Up @@ -38,25 +42,43 @@ def option_handler(selected_option):
"output_option": selected_option,
"channel_groups": self.experiment.channel_groups,
}

default_filename = (
selected_name + "_all_subjects_channel_averages_evoked.csv"
if selected_option == "channel_averages"
else selected_name + "_all_subjects_all_channels_evoked.csv"
)
filepath, _ = QtWidgets.QFileDialog.getSaveFileName(
self.window,
"Save Evoked to CSV",
os.path.join(homepath(), default_filename),
"CSV Files (*.csv);;All Files (*)",
)
if not filepath:
return

try:
self.handler(self.experiment.active_subject, params)
self.handler(self.experiment.active_subject, filepath, params)
except Exception as exc:
exc_messagebox(self.window, exc)

dialog = OutputOptions(self.window, handler=option_handler)
dialog.show()

@subject_action
def handler(self, subject, params):
def handler(self, subject, filepath, params):
""" """
if params["output_option"] == "channel_averages":
save_channel_averages(
self.experiment,
params["name"],
params["channel_groups"],
filepath,
do_meanwhile=self.window.update_ui,
)
else:
save_all_channels(
self.experiment, params["name"], do_meanwhile=self.window.update_ui
self.experiment,
params["name"],
filepath,
do_meanwhile=self.window.update_ui,
)
13 changes: 2 additions & 11 deletions meggie/actions/evoked_save/controller/evoked.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Contains controlling logic for the evoked implementation."""

import logging
import os

import meggie.utilities.filemanager as filemanager

Expand All @@ -23,7 +22,7 @@ def _create_averages(mne_evoked, channel_groups):


@threaded
def save_all_channels(experiment, selected_name):
def save_all_channels(experiment, selected_name, path):
"""Saves all channels of evoked item to a csv file."""
column_names = []
row_descs = []
Expand All @@ -46,16 +45,12 @@ def save_all_channels(experiment, selected_name):
row_desc = (subject.name, key, ch_name)
row_descs.append(row_desc)

folder = filemanager.create_timestamped_folder(experiment)
fname = selected_name + "_all_subjects_all_channels_evoked.csv"
path = os.path.join(folder, fname)

filemanager.save_csv(path, csv_data, column_names, row_descs)
logging.getLogger("ui_logger").info("Saved the csv file to " + path)


@threaded
def save_channel_averages(experiment, selected_name, channel_groups):
def save_channel_averages(experiment, selected_name, channel_groups, path):
"""Saves channel averages to a csv file."""
column_names = []
row_descs = []
Expand All @@ -78,9 +73,5 @@ def save_channel_averages(experiment, selected_name, channel_groups):
row_desc = (subject.name, key, ch_type, area)
row_descs.append(row_desc)

folder = filemanager.create_timestamped_folder(experiment)
fname = selected_name + "_all_subjects_channel_averages_evoked.csv"
path = os.path.join(folder, fname)

filemanager.save_csv(path, csv_data, column_names, row_descs)
logging.getLogger("ui_logger").info("Saved the csv file to " + path)
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

class TestEvokedSaveChannelAverages(BaseTestAction):
def test_evoked_save_channel_averages(self):

data = {"outputs": {"evoked": ["Evoked"]}}

self.run_action(
action_name="evoked_save",
handler=SaveEvoked,
data=data,
)

dialog = self.find_dialog(OutputOptions)
dialog.ui.radioButtonChannelAverages.setChecked(True)
dialog.accept()
33 changes: 28 additions & 5 deletions meggie/actions/spectrum_save/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""Contains save spectrum action handling."""

import os

from PyQt5 import QtWidgets

from meggie.utilities.messaging import exc_messagebox
from meggie.utilities.validators import assert_arrays_same
from meggie.utilities.filemanager import homepath

from meggie.mainwindow.dynamic import Action
from meggie.mainwindow.dynamic import subject_action

from meggie.actions.spectrum_save.controller.spectrum import save_channel_averages
from meggie.actions.spectrum_save.controller.spectrum import save_all_channels
Expand Down Expand Up @@ -36,25 +40,44 @@ def option_handler(selected_option):
"output_option": selected_option,
"channel_groups": self.experiment.channel_groups,
}

default_filename = (
selected_name + "_all_subjects_channel_averages_spectrum.csv"
if selected_option == "channel_averages"
else selected_name + "_all_subjects_all_channels_spectrum.csv"
)
filepath, _ = QtWidgets.QFileDialog.getSaveFileName(
self.window,
"Save Spectrum to CSV",
os.path.join(homepath(), default_filename),
"CSV Files (*.csv);;All Files (*)",
)

if not filepath:
return

try:
self.handler(self.experiment.active_subject, params)
self.handler(self.experiment.active_subject, filepath, params)
except Exception as exc:
exc_messagebox(self.window, exc)

dialog = OutputOptions(self.window, handler=option_handler)
dialog.show()

@subject_action
def handler(self, subject, params):
def handler(self, subject, filepath, params):
""" """
if params["output_option"] == "channel_averages":
save_channel_averages(
self.experiment,
params["name"],
params["channel_groups"],
filepath,
do_meanwhile=self.window.update_ui,
)
else:
save_all_channels(
self.experiment, params["name"], do_meanwhile=self.window.update_ui
self.experiment,
params["name"],
filepath,
do_meanwhile=self.window.update_ui,
)
13 changes: 2 additions & 11 deletions meggie/actions/spectrum_save/controller/spectrum.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Contains controlling logic for the spectrum saving."""

import os
import logging

import numpy as np
Expand All @@ -14,7 +13,7 @@


@threaded
def save_all_channels(experiment, selected_name):
def save_all_channels(experiment, selected_name, path):
"""Saves all channesl of a spectrum item to a csv file."""
column_names = []
row_descs = []
Expand All @@ -32,17 +31,13 @@ def save_all_channels(experiment, selected_name):
row_desc = (subject.name, key, ch_name)
row_descs.append(row_desc)

folder = filemanager.create_timestamped_folder(experiment)
fname = selected_name + "_all_subjects_all_channels_spectrum.csv"
path = os.path.join(folder, fname)

filemanager.save_csv(path, csv_data, column_names, row_descs)
logging.getLogger("ui_logger").info("Saved the csv file to " + path)


@threaded
def save_channel_averages(
experiment, selected_name, channel_groups, log_transformed=False
experiment, selected_name, channel_groups, path, log_transformed=False
):
"""Saves channel averages of a spectrum item to a csv file."""
column_names = []
Expand Down Expand Up @@ -76,9 +71,5 @@ def save_channel_averages(
row_desc = (subject.name, key, ch_type, area)
row_descs.append(row_desc)

folder = filemanager.create_timestamped_folder(experiment)
fname = selected_name + "_all_subjects_channel_averages_spectrum.csv"
path = os.path.join(folder, fname)

filemanager.save_csv(path, csv_data, column_names, row_descs)
logging.getLogger("ui_logger").info("Saved the csv file to " + path)
27 changes: 23 additions & 4 deletions meggie/actions/tfr_save/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""Contains save tfr action handling."""

import os

from PyQt5 import QtWidgets

from meggie.utilities.messaging import exc_messagebox
from meggie.utilities.validators import assert_arrays_same
from meggie.utilities.filemanager import homepath

from meggie.mainwindow.dynamic import Action
from meggie.mainwindow.dynamic import subject_action

from meggie.utilities.dialogs.TFROutputOptionsMain import TFROutputOptions

Expand Down Expand Up @@ -37,8 +41,22 @@ def option_handler(params):
params["channel_groups"] = self.experiment.channel_groups
params["name"] = selected_name

default_filename = (
selected_name + "_all_subjects_channel_averages_tfr.csv"
if params["output_option"] == "channel_averages"
else selected_name + "_all_subjects_all_channels_tfr.csv"
)
filepath, _ = QtWidgets.QFileDialog.getSaveFileName(
self.window,
"Save TFR to CSV",
os.path.join(homepath(), default_filename),
"CSV Files (*.csv);;All Files (*)",
)
if not filepath:
return

try:
self.handler(self.experiment.active_subject, params)
self.handler(self.experiment.active_subject, filepath, params)
except Exception as exc:
exc_messagebox(self.window, exc)

Expand All @@ -47,8 +65,7 @@ def option_handler(params):
)
dialog.show()

@subject_action
def handler(self, subject, params):
def handler(self, subject, filepath, params):
""" """
if params["output_option"] == "all_channels":
save_tfr_all_channels(
Expand All @@ -61,6 +78,7 @@ def handler(self, subject, params):
params["tmax"],
params["fmin"],
params["fmax"],
filepath,
do_meanwhile=self.window.update_ui,
)
else:
Expand All @@ -75,5 +93,6 @@ def handler(self, subject, params):
params["fmin"],
params["fmax"],
params["channel_groups"],
filepath,
do_meanwhile=self.window.update_ui,
)
23 changes: 12 additions & 11 deletions meggie/actions/tfr_save/controller/tfr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Contains controlling logic for the tfr implementation"""

import os
import logging

import mne
Expand All @@ -14,7 +13,7 @@

@threaded
def save_tfr_all_channels(
experiment, tfr_name, blmode, blstart, blend, tmin, tmax, fmin, fmax
experiment, tfr_name, blmode, blstart, blend, tmin, tmax, fmin, fmax, path
):
"""Saves all channels of tfr item to a csv file."""
column_names = []
Expand Down Expand Up @@ -53,17 +52,23 @@ def save_tfr_all_channels(
row_desc = (subject.name, key, ch_names[ix], freqs[iy])
row_descs.append(row_desc)

folder = filemanager.create_timestamped_folder(experiment)
fname = tfr_name + "_all_subjects_all_channels_tfr.csv"
path = os.path.join(folder, fname)

filemanager.save_csv(path, csv_data, column_names, row_descs)
logging.getLogger("ui_logger").info("Saved the csv file to " + path)


@threaded
def save_tfr_channel_averages(
experiment, tfr_name, blmode, blstart, blend, tmin, tmax, fmin, fmax, channel_groups
experiment,
tfr_name,
blmode,
blstart,
blend,
tmin,
tmax,
fmin,
fmax,
channel_groups,
path,
):
"""Saves channel averages of tfr item to a csv file."""
column_names = []
Expand Down Expand Up @@ -110,9 +115,5 @@ def save_tfr_channel_averages(
row_desc = (subject.name, key, ch_type, area, freqs[iy])
row_descs.append(row_desc)

folder = filemanager.create_timestamped_folder(experiment)
fname = tfr_name + "_all_subjects_channel_averages_tfr.csv"
path = os.path.join(folder, fname)

filemanager.save_csv(path, csv_data, column_names, row_descs)
logging.getLogger("ui_logger").info("Saved the csv file to " + path)
Loading

0 comments on commit ad772a8

Please sign in to comment.