Skip to content

Commit

Permalink
FIX: Connectivity saved to .nc and other fixes (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
marsipu authored Dec 8, 2023
1 parent fb1fe41 commit c2b64bc
Show file tree
Hide file tree
Showing 26 changed files with 1,058 additions and 754 deletions.
80 changes: 38 additions & 42 deletions mne_pipeline_hd/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
Github: https://github.com/marsipu/mne-pipeline-hd
"""

import logging
import os
import re
import sys
from importlib import resources
from os.path import join

import qtpy
from qtpy.QtCore import QTimer, Qt
Expand All @@ -19,14 +20,27 @@
from mne_pipeline_hd.gui.gui_utils import StdoutStderrStream, UncaughtHook
from mne_pipeline_hd.gui.welcome_window import WelcomeWindow
from mne_pipeline_hd.pipeline.legacy import legacy_import_check
from mne_pipeline_hd.pipeline.pipeline_utils import ismac, islin, QS
from mne_pipeline_hd.pipeline.pipeline_utils import (
ismac,
islin,
QS,
iswin,
init_logging,
logger,
)

# Check for changes in required packages
legacy_import_check()

import qdarktheme # noqa: E402


def init_streams():
# Redirect stdout and stderr to capture it later in GUI
sys.stdout = StdoutStderrStream("stdout")
sys.stderr = StdoutStderrStream("stderr")


def main():
app_name = "mne-pipeline-hd"
organization_name = "marsipu"
Expand All @@ -40,6 +54,8 @@ def main():
app.setApplicationName(app_name)
app.setOrganizationName(organization_name)
app.setOrganizationDomain(domain_name)
# For Spyder to make console accessible again
app.lastWindowClosed.connect(app.quit)

# Avoid file-dialog-problems with custom file-managers in linux
if islin:
Expand All @@ -54,37 +70,23 @@ def main():
# # Set multiprocessing method to spawn
# multiprocessing.set_start_method('spawn')

# Redirect stdout to capture it later in GUI
sys.stdout = StdoutStderrStream("stdout")
# Redirect stderr to capture it later in GUI
sys.stderr = StdoutStderrStream("stderr")
init_streams()

debug_mode = os.environ.get("MNEPHD_DEBUG", False) == "true"
init_logging(debug_mode)

# Initialize Logger (root)
logger = logging.getLogger()
if debug_mode:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(QS().value("log_level", defaultValue=logging.INFO))
formatter = logging.Formatter(
"%(asctime)s: %(message)s", datefmt="%Y/%m/%d %H:%M:%S"
)
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
logger.info("Starting MNE-Pipeline HD")
logger().info("Starting MNE-Pipeline HD")

# Show Qt-binding
if any([qtpy.PYQT5, qtpy.PYQT6]):
qt_version = qtpy.PYQT_VERSION
else:
qt_version = qtpy.PYSIDE_VERSION
logger.info(f"Using {qtpy.API_NAME} {qt_version}")
logger().info(f"Using {qtpy.API_NAME} {qt_version}")

# Initialize Exception-Hook
if debug_mode:
logger.info("Debug-Mode is activated")
logger().info("Debug-Mode is activated")
else:
qt_exception_hook = UncaughtHook()
# this registers the exception_hook() function
Expand All @@ -103,40 +105,34 @@ def main():
if app_style not in ["dark", "light", "auto"]:
app_style = "auto"

if app_style == "dark":
qdarktheme.setup_theme("dark")
qdarktheme.setup_theme(app_style)
st = qdarktheme.load_stylesheet(app_style)
is_dark = "background:rgba(32, 33, 36, 1.000)" in st
if is_dark:
icon_name = "mne_pipeline_icon_dark.png"
elif app_style == "light":
qdarktheme.setup_theme("light")
icon_name = "mne_pipeline_icon_light.png"
# Fix ToolTip-Problem on Windows
# https://github.com/5yutan5/PyQtDarkTheme/issues/239
if iswin:
match = re.search(r"QToolTip \{([^\{\}]+)\}", st)
if match is not None:
replace_str = "QToolTip {" + match.group(1) + ";border: 0px}"
st = st.replace(match.group(0), replace_str)
QApplication.instance().setStyleSheet(st)
else:
qdarktheme.setup_theme("auto")
st = qdarktheme.load_stylesheet("auto")
if "background:rgba(32, 33, 36, 1.000)" in st:
icon_name = "mne_pipeline_icon_dark.png"
else:
icon_name = "mne_pipeline_icon_light.png"

icon_path = resources.files(mne_pipeline_hd.extra) / icon_name
icon_name = "mne_pipeline_icon_light.png"

icon_path = join(resources.files(mne_pipeline_hd.extra), icon_name)
app_icon = QIcon(str(icon_path))
app.setWindowIcon(app_icon)

# Initiate WelcomeWindow
WelcomeWindow()

# Redirect stdout to capture it later in GUI
sys.stdout = StdoutStderrStream("stdout")
# Redirect stderr to capture it later in GUI
sys.stderr = StdoutStderrStream("stderr")

# Command-Line interrupt with Ctrl+C possible
timer = QTimer()
timer.timeout.connect(lambda: None)
timer.start(500)

# For Spyder to make console accessible again
app.lastWindowClosed.connect(app.quit)

sys.exit(app.exec())


Expand Down
101 changes: 67 additions & 34 deletions mne_pipeline_hd/development/console_widget_speed.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,75 @@
# -*- coding: utf-8 -*-
import sys
from time import perf_counter

import numpy as np
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton

from mne_pipeline_hd.gui.gui_utils import ConsoleWidget

app = QApplication(sys.argv)
widget = QWidget()
layout = QVBoxLayout()
cw = ConsoleWidget()
layout.addWidget(cw)
close_bt = QPushButton("Close")
close_bt.clicked.connect(widget.close)
layout.addWidget(close_bt)
widget.setLayout(layout)
widget.show()
last_time = perf_counter()

performance_buffer = list()


def test_write():
global last_time
cw.write_progress("\r" + (f"Test {len(performance_buffer)}" * 1000))
diff = perf_counter() - last_time
performance_buffer.append(diff)
if len(performance_buffer) >= 100:
fps = 1 / np.mean(performance_buffer)
print(f"Performance is: {fps:.2f} FPS")
performance_buffer.clear()
last_time = perf_counter()


timer = QTimer()
timer.timeout.connect(test_write)
timer.start(1)

sys.exit(app.exec())
test_text = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus.
Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed,
dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper
congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est
eleifend mi, non fermentum diam nisl sit amet erat. Duis semper. Duis arcu
massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue.
Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue.
Praesent egestas leo in pede. Praesent blandit odio eu enim. Pellentesque
sed dui ut augue blandit sodales. Vestibulum ante ipsum primis in faucibus
orci luctus et ultrices posuere cubilia Curae; Aliquam nibh. Mauris ac mauris
sed pede pellentesque fermentum. Maecenas adipiscing ante non diam sodales
hendrerit.
\r Progress: 0%
\r Progress: 10%
\r Progress: 20%
\r Progress: 30%
\r Progress: 40%
\r Progress: 50%
\r Progress: 60%
\r Progress: 70%
\r Progress: 80%
\r Progress: 90%
\r Progress: 100%"""


class SpeedWidget(QWidget):
def __init__(self):
super().__init__()

layout = QVBoxLayout(self)
self.cw = ConsoleWidget()
layout.addWidget(self.cw)
startbt = QPushButton("Start")
startbt.clicked.connect(self.start)
layout.addWidget(startbt)
stopbt = QPushButton("Stop")
stopbt.clicked.connect(self.stop)
layout.addWidget(stopbt)
close_bt = QPushButton("Close")
close_bt.clicked.connect(self.close)
layout.addWidget(close_bt)

self.test_text = test_text.split("\n")
self.line_idx = 0

self.timer = QTimer(self)
self.timer.timeout.connect(self.write)

def start(self):
self.timer.start(42)

def stop(self):
self.timer.stop()

def write(self):
if self.line_idx >= len(self.test_text):
self.line_idx = 0
text = self.test_text[self.line_idx]
self.cw.write_stdout(text)
self.line_idx += 1


if __name__ == "__main__":
app = QApplication(sys.argv)
w = SpeedWidget()
w.show()
sys.exit(app.exec())
25 changes: 13 additions & 12 deletions mne_pipeline_hd/extra/functions.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
;alias;target;tab;group;matplotlib;mayavi;dependencies;module;pkg_name;func_args
find_bads;Find Bad Channels;MEEG;Compute;Preprocessing;False;False;;operations;basic;meeg,n_jobs
filter_data;Filter;MEEG;Compute;Preprocessing;False;False;;operations;basic;meeg,filter_target,highpass,lowpass,filter_length,l_trans_bandwidth,h_trans_bandwidth,filter_method,iir_params,fir_phase,fir_window,fir_design,skip_by_annotation,fir_pad,n_jobs,enable_cuda,erm_t_limit,bad_interpolation
notch_filter;Notch Filter;MEEG;Compute;Preprocessing;False;False;;operations;basic;meeg,notch_frequencies,n_jobs
interpolate_bads;Interpolate Bads;MEEG;Compute;Preprocessing;False;False;;operations;basic;meeg,bad_interpolation
add_erm_ssp;Empty-Room SSP;MEEG;Compute;Preprocessing;True;False;;operations;basic;meeg,erm_ssp_duration,erm_n_grad,erm_n_mag,erm_n_eeg,n_jobs,show_plots
eeg_reference_raw;Set EEG Reference;MEEG;Compute;Preprocessing;False;False;;operations;basic;meeg,ref_channels
find_events;Find events;MEEG;Compute;events;False;False;;operations;basic;meeg,stim_channels,min_duration,shortest_event,adjust_timeline_by_msec
Expand All @@ -10,7 +12,6 @@ estimate_noise_covariance;Noise-Covariance;MEEG;Compute;Preprocessing;False;Fals
run_ica;Run ICA;MEEG;Compute;Preprocessing;False;False;;operations;basic;meeg,ica_method,ica_fitto,n_components,ica_noise_cov,ica_remove_proj,ica_reject,ica_autoreject,overwrite_ar,ch_types,ch_names,reject_by_annotation,ica_eog,eog_channel,ica_ecg,ecg_channel
apply_ica;Apply ICA;MEEG;Compute;Preprocessing;False;False;;operations;basic;meeg,ica_apply_target,n_pca_components
get_evokeds;Get Evokeds;MEEG;Compute;events;False;False;;operations;basic;meeg
interpolate_bads;Interpolate Bads;MEEG;Compute;Preprocessing;False;False;;operations;basic;meeg,bad_interpolation
compute_psd_raw;Compute PSD (Raw);MEEG;Compute;Time-Frequency;False;False;;operations;basic;meeg,psd_method,n_jobs
compute_psd_epochs;Compute PSD (Epochs);MEEG;Compute;Time-Frequency;False;False;;operations;basic;meeg,psd_method,n_jobs
tfr;Time-Frequency;MEEG;Compute;Time-Frequency;False;False;;operations;basic;meeg,tfr_freqs,tfr_n_cycles,tfr_average,tfr_use_fft,tfr_baseline,tfr_baseline_mode,tfr_method,multitaper_bandwidth,stockwell_width,n_jobs
Expand All @@ -26,9 +27,9 @@ morph_labels_from_fsaverage;;FSMRI;Compute;MRI-Preprocessing;False;False;;operat
create_inverse_operator;;MEEG;Compute;Inverse;False;False;;operations;basic;meeg
source_estimate;;MEEG;Compute;Inverse;False;False;;operations;basic;meeg,inverse_method,pick_ori,lambda2
apply_morph;;MEEG;Compute;Inverse;False;False;;operations;basic;meeg,morph_to
label_time_course;;MEEG;Compute;Inverse;False;False;;operations;basic;meeg,target_labels,target_parcellation,extract_mode
label_time_course;;MEEG;Compute;Inverse;False;False;;operations;basic;meeg,target_labels,extract_mode
ecd_fit;;MEEG;Compute;Inverse;False;False;;operations;basic;meeg,ecd_times,ecd_positions,ecd_orientations,t_epoch
src_connectivity;;MEEG;Compute;Inverse;False;False;;operations;basic;meeg,target_labels,target_parcellation,inverse_method,lambda2,con_methods,con_frequencies,con_time_window,n_jobs
src_connectivity;;MEEG;Compute;Time-Frequency;False;False;;operations;basic;meeg,target_labels,inverse_method,lambda2,con_methods,con_fmin,con_fmax,con_time_window,n_jobs
grand_avg_evokeds;;Group;Compute;Grand-Average;False;False;;operations;basic;group,ga_interpolate_bads,ga_drop_bads
grand_avg_tfr;;Group;Compute;Grand-Average;False;False;;operations;basic;group
grand_avg_morphed;;Group;Compute;Grand-Average;False;False;;operations;basic;group,morph_to
Expand Down Expand Up @@ -61,21 +62,21 @@ plot_evoked_white;;MEEG;Plot;Evoked;True;False;;plot;basic;meeg,show_plots
plot_evoked_image;;MEEG;Plot;Evoked;True;False;;plot;basic;meeg,show_plots
plot_compare_evokeds;;MEEG;Plot;Evoked;True;False;;plot;basic;meeg,show_plots
plot_gfp;;MEEG;Plot;Evoked;True;False;;plot;basic;meeg,show_plots
plot_stc;Plot Source-Estimate;MEEG;Plot;Inverse;True;True;;plot;basic;meeg,target_labels,target_parcellation,label_colors,stc_surface,stc_hemi,stc_views,stc_time,stc_clim,stc_roll,stc_azimuth,stc_elevation,backend_3d
plot_stc;Plot Source-Estimate;MEEG;Plot;Inverse;True;True;;plot;basic;meeg,target_labels,label_colors,stc_surface,stc_hemi,stc_views,stc_time,stc_clim,stc_roll,stc_azimuth,stc_elevation,backend_3d
plot_stc_interactive;;MEEG;Plot;Inverse;True;True;;plot;basic;meeg,stc_surface,stc_hemi,stc_views,stc_time,stc_clim,stc_roll,stc_azimuth,stc_elevation,backend_3d
plot_labels;;FSMRI;Plot;Inverse;True;True;;plot;basic;fsmri,target_labels,target_parcellation,label_colors,stc_hemi,stc_surface,stc_views,backend_3d
plot_animated_stc;Plot Source-Estimate Video;MEEG;Plot;Inverse;True;True;;plot;basic;meeg,target_labels,target_parcellation,label_colors,stc_surface,stc_hemi,stc_views,stc_time,stc_clim,stc_roll,stc_azimuth,stc_elevation,stc_animation_span,stc_animation_dilat,backend_3d
plot_labels;;FSMRI;Plot;Inverse;True;True;;plot;basic;fsmri,target_labels,label_colors,stc_hemi,stc_surface,stc_views,backend_3d
plot_animated_stc;Plot Source-Estimate Video;MEEG;Plot;Inverse;True;True;;plot;basic;meeg,target_labels,label_colors,stc_surface,stc_hemi,stc_views,stc_time,stc_clim,stc_roll,stc_azimuth,stc_elevation,stc_animation_span,stc_animation_dilat,backend_3d
plot_snr;;MEEG;Plot;Inverse;True;False;;plot;basic;meeg,show_plots
plot_label_time_course;;MEEG;Plot;Inverse;True;False;;plot;basic;meeg,show_plots
plot_label_time_course;;MEEG;Plot;Inverse;True;False;;plot;basic;meeg,label_colors,show_plots
plot_ecd;;MEEG;Plot;Inverse;True;True;;plot;basic;meeg
plot_src_connectivity;;MEEG;Plot;Time-Frequency;True;False;;plot;basic;meeg,show_plots
plot_src_connectivity;;MEEG;Plot;Time-Frequency;True;False;;plot;basic;meeg,label_colors,show_plots
plot_grand_avg_evokeds;;Group;Plot;Grand-Average;True;False;;plot;basic;group,show_plots
plot_grand_avg_tfr;;Group;Plot;Grand-Average;True;False;;plot;basic;group,show_plots
plot_grand_avg_stc;;Group;Plot;Grand-Average;True;True;;plot;basic;group,target_labels,target_parcellation,label_colors,stc_surface,stc_hemi,stc_views,stc_time,stc_clim,stc_roll,stc_azimuth,stc_elevation,backend_3d
plot_grand_avg_stc_anim;;Group;Plot;Grand-Average;True;True;;plot;basic;group,target_labels,target_parcellation,label_colors,stc_surface,stc_hemi,stc_views,stc_time,stc_clim,stc_roll,stc_azimuth,stc_elevation,stc_animation_span,stc_animation_dilat,backend_3d
plot_grand_avg_stc;;Group;Plot;Grand-Average;True;True;;plot;basic;group,target_labels,label_colors,stc_surface,stc_hemi,stc_views,stc_time,stc_clim,stc_roll,stc_azimuth,stc_elevation,backend_3d
plot_grand_avg_stc_anim;;Group;Plot;Grand-Average;True;True;;plot;basic;group,target_labels,label_colors,stc_surface,stc_hemi,stc_views,stc_time,stc_clim,stc_roll,stc_azimuth,stc_elevation,stc_animation_span,stc_animation_dilat,backend_3d
plot_grand_average_stc_interactive;;Group;Plot;Grand-Average;True;True;;plot;basic;group,label_colors,stc_surface,stc_hemi,stc_views,stc_time,stc_clim,stc_roll,stc_azimuth,stc_elevation,backend_3d
plot_grand_avg_ltc;;Group;Plot;Grand-Average;True;False;;plot;basic;group,show_plots
plot_grand_avg_connect;;Group;Plot;Grand-Average;True;False;;plot;basic;group,morph_to,show_plots,connectivity_vmin,connectivity_vmax,con_group_boundaries
plot_grand_avg_ltc;;Group;Plot;Grand-Average;True;False;;plot;basic;group,label_colors,show_plots
plot_grand_avg_connect;;Group;Plot;Grand-Average;True;False;;plot;basic;group,label_colors,show_plots
plot_ica_components;Plot ICA-Components;MEEG;Plot;ICA;True;False;;plot;basic;meeg,show_plots,close_func
plot_ica_sources;Plot ICA-Sources;MEEG;Plot;ICA;True;False;;plot;basic;meeg,ica_source_data,show_plots,close_func
plot_ica_overlay;Plot ICA-Overlay;MEEG;Plot;ICA;True;False;;plot;basic;meeg,ica_overlay_data,show_plots
Expand Down
7 changes: 3 additions & 4 deletions mne_pipeline_hd/extra/parameters.csv
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ stc_azimuth;;Inverse;70;;Azimuth for view for Source Estimate Plots;IntGui;{'max
stc_elevation;;Inverse;60;;Elevation for view for Source Estimate Plots;IntGui;{'max_val': 360}
stc_animation_span;;Inverse;(0,0.5);s;time-span for stc-animation[s];TupleGui;
stc_animation_dilat;;Inverse;20;;time-dilation for stc-animation;IntGui;
target_parcellation;Target Parcellation;Inverse;aparc;;The parcellation to use.;StringGui;
target_labels;Target Labels;Inverse;[];;;LabelGui;
label_colors;;Inverse;{};;Set custom colors for labels.;ColorGui;{'keys': 'target_labels', 'none_select':True}
extract_mode;Label-Extraction-Mode;Inverse;auto;;mode for extracting label-time-course from Source-Estimate;ComboGui;{'options': ['auto', 'max', 'mean', 'mean_flip', 'pca_flip']}
con_methods;;Connectivity;['coh'];;methods for connectivity;CheckListGui;{'options': ['coh', 'cohy', 'imcoh', 'plv', 'ciplv', 'ppc', 'pli', 'pli2_unbiased', 'wpli', 'wpli2_debiased']}
con_frequencies;;Connectivity;(30, 80);;frequencies for connectivity;TupleGui;{'none_select': True, 'step': 1}
con_fmin;;Connectivity;30;;lower frequency/frequencies for connectivity;MultiTypeGui;{'type_selection': True, 'types': ['float', 'list']}
con_fmax;;Connectivity;80;;upper frequency/frequencies for connectivity;MultiTypeGui;{'type_selection': True, 'types': ['float', 'list']}
con_time_window;;Connectivity;(0, 0.5);;time-window for connectivity;TupleGui;{'none_select': True, 'step': 0.001}
ecd_times;;Inverse;{};;;DictGui;
ecd_positions;;Inverse;{};;;DictGui;
Expand All @@ -96,9 +96,8 @@ erm_n_mag;;Preprocessing;2;;The number of projections for Magnetometer;IntGui;
erm_n_eeg;;Preprocessing;0;;The number of projections for EEG;IntGui;
ga_interpolate_bads;;Grand-Average;True;;If to interpolate bad channels for the Grand-Average;BoolGui;
ga_drop_bads;;Grand-Average;True;;If to drop bad channels for the Grand-Average;BoolGui;
connectivity_vmin;;Connectivity;None;;Minimum value for colormap;FloatGui;{'step': 0.01, 'none_select':True}
connectivity_vmax;;Connectivity;None;;Maximum value for colormap;FloatGui;{'step': 0.01, 'none_select':True}
psd_method;;Time-Frequency;welch;;The method for spectral estimation;ComboGui;{'options': ['welch', 'multitaper']}
psd_topomap_bands;;Time-Frequency;None;;The frequency bands for the topomap-plot;DictGui;{'none_select': True}
backend_3d;3D-Backend;Plot;pyvistaqt;;Choose the 3D-Backend for Brain-plots.;ComboGui;{'options': ['pyvistaqt', 'notebook']}
con_group_boundaries;;Connectivity;None;;Set group-boundaries for circular plot.;FuncGui;{'none_select': True}
notch_frequencies;;Preprocessing;50;;Set frequencies for Notch filtering;FuncGui;""
Loading

0 comments on commit c2b64bc

Please sign in to comment.