-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
_figure.py
847 lines (737 loc) · 31.2 KB
/
_figure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
"""Base classes and functions for 2D browser backends."""
# Authors: Daniel McCloy <dan@mccloy.info>
# Martin Schulz <dev@earthman-music.de>
#
# License: Simplified BSD
import importlib
from abc import ABC, abstractmethod
from collections import OrderedDict
from contextlib import contextmanager
from copy import deepcopy
from itertools import cycle
import numpy as np
from .. import verbose, get_config, set_config
from ..annotations import _sync_onset
from ..defaults import _handle_default
from ..utils import logger, _validate_type, _check_option
from ..io.pick import _DATA_CH_TYPES_SPLIT
from .backends._utils import VALID_BROWSE_BACKENDS
from .utils import _get_color_list, _setup_plot_projector, _show_browser
MNE_BROWSER_BACKEND = None
backend = None
class BrowserParams:
"""Container object for 2D browser parameters."""
def __init__(self, **kwargs):
# default key to close window
self.close_key = "escape"
vars(self).update(**kwargs)
class BrowserBase(ABC):
"""A base class containing for the 2D browser.
This class contains all backend-independent attributes and methods.
"""
def __init__(self, **kwargs):
from .. import BaseEpochs
from ..io import BaseRaw
from ..preprocessing import ICA
self.backend_name = None
self._data = None
self._times = None
self.mne = BrowserParams(**kwargs)
inst = kwargs.get("inst", None)
ica = kwargs.get("ica", None)
# what kind of data are we dealing with?
if isinstance(ica, ICA):
self.mne.instance_type = "ica"
elif isinstance(inst, BaseRaw):
self.mne.instance_type = "raw"
elif isinstance(inst, BaseEpochs):
self.mne.instance_type = "epochs"
else:
raise TypeError(
"Expected an instance of Raw, Epochs, or ICA, " f"got {type(inst)}."
)
logger.debug(f"Opening {self.mne.instance_type} browser...")
self.mne.ica_type = None
if self.mne.instance_type == "ica":
if isinstance(self.mne.ica_inst, BaseRaw):
self.mne.ica_type = "raw"
elif isinstance(self.mne.ica_inst, BaseEpochs):
self.mne.ica_type = "epochs"
self.mne.is_epochs = "epochs" in (self.mne.instance_type, self.mne.ica_type)
# things that always start the same
self.mne.ch_start = 0
self.mne.projector = None
if hasattr(self.mne, "projs"):
self.mne.projs_active = np.array([p["active"] for p in self.mne.projs])
self.mne.whitened_ch_names = list()
if hasattr(self.mne, "noise_cov"):
self.mne.use_noise_cov = self.mne.noise_cov is not None
# allow up to 10000 zorder levels for annotations
self.mne.zorder = dict(
patch=0,
grid=1,
ann=2,
events=10003,
bads=10004,
data=10005,
mag=10006,
grad=10007,
scalebar=10008,
vline=10009,
)
# additional params for epochs (won't affect raw / ICA)
self.mne.epoch_traces = list()
self.mne.bad_epochs = list()
if inst is not None:
self.mne.sampling_period = np.diff(inst.times[:2])[0] / inst.info["sfreq"]
# annotations
self.mne.annotations = list()
self.mne.hscroll_annotations = list()
self.mne.annotation_segments = list()
self.mne.annotation_texts = list()
self.mne.new_annotation_labels = list()
self.mne.annotation_segment_colors = dict()
self.mne.annotation_hover_line = None
self.mne.draggable_annotations = False
# lines
self.mne.event_lines = list()
self.mne.event_texts = list()
self.mne.vline_visible = False
# decim
self.mne.decim_times = None
self.mne.decim_data = None
# scalings
if hasattr(self.mne, "butterfly"):
self.mne.scale_factor = 0.5 if self.mne.butterfly else 1.0
self.mne.scalebars = dict()
self.mne.scalebar_texts = dict()
# ancillary child figures
self.mne.child_figs = list()
self.mne.fig_help = None
self.mne.fig_proj = None
self.mne.fig_histogram = None
self.mne.fig_selection = None
self.mne.fig_annotation = None
# extra attributes for epochs
if self.mne.is_epochs:
# add epoch boundaries & center epoch numbers between boundaries
self.mne.midpoints = (
np.convolve(self.mne.boundary_times, np.ones(2), mode="valid") / 2
)
# initialize picks and projectors
self._update_picks()
if not self.mne.instance_type == "ica":
self._update_projector()
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# ANNOTATIONS
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _get_annotation_labels(self):
"""Get the unique labels in the raw object and added in the UI."""
return sorted(
set(self.mne.inst.annotations.description)
| set(self.mne.new_annotation_labels)
)
def _setup_annotation_colors(self):
"""Set up colors for annotations; init some annotation vars."""
segment_colors = getattr(self.mne, "annotation_segment_colors", dict())
labels = self._get_annotation_labels()
colors, red = _get_color_list(annotations=True)
color_cycle = cycle(colors)
for key, color in segment_colors.items():
if color != red and key in labels:
next(color_cycle)
for idx, key in enumerate(labels):
if key.lower().startswith("bad") or key.lower().startswith("edge"):
segment_colors[key] = red
elif key in segment_colors:
continue
else:
segment_colors[key] = next(color_cycle)
self.mne.annotation_segment_colors = segment_colors
# init a couple other annotation-related variables
self.mne.visible_annotations = {label: True for label in labels}
self.mne.show_hide_annotation_checkboxes = None
def _update_annotation_segments(self):
"""Update the array of annotation start/end times."""
self.mne.annotation_segments = np.array([])
if len(self.mne.inst.annotations):
annot_start = _sync_onset(self.mne.inst, self.mne.inst.annotations.onset)
durations = self.mne.inst.annotations.duration.copy()
durations[durations < 1 / self.mne.info["sfreq"]] = (
1 / self.mne.info["sfreq"]
)
annot_end = annot_start + durations
self.mne.annotation_segments = np.vstack((annot_start, annot_end)).T
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# PROJECTOR & BADS
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _update_projector(self):
"""Update the data after projectors (or bads) have changed."""
inds = np.where(self.mne.projs_on)[0] # doesn't include "active" projs
# copy projs from full list (self.mne.projs) to info object
with self.mne.info._unlock():
self.mne.info["projs"] = [deepcopy(self.mne.projs[ix]) for ix in inds]
# compute the projection operator
proj, wh_chs = _setup_plot_projector(
self.mne.info, self.mne.noise_cov, True, self.mne.use_noise_cov
)
self.mne.whitened_ch_names = list(wh_chs)
self.mne.projector = proj
def _toggle_bad_channel(self, idx):
"""Mark/unmark bad channels; `idx` is index of *visible* channels."""
pick = self.mne.picks[idx]
ch_name = self.mne.ch_names[pick]
# add/remove from bads list
bads = self.mne.info["bads"]
marked_bad = ch_name not in bads
if marked_bad:
bads.append(ch_name)
color = self.mne.ch_color_bad
else:
while ch_name in bads: # to make sure duplicates are removed
bads.remove(ch_name)
# Only mpl-backend has ch_colors
if hasattr(self.mne, "ch_colors"):
color = self.mne.ch_colors[idx]
else:
color = None
self.mne.info["bads"] = bads
self._update_projector()
return color, pick, marked_bad
def _toggle_bad_epoch(self, xtime):
epoch_num = self._get_epoch_num_from_time(xtime)
epoch_ix = self.mne.inst.selection.tolist().index(epoch_num)
if epoch_num in self.mne.bad_epochs:
self.mne.bad_epochs.remove(epoch_num)
color = "none"
else:
self.mne.bad_epochs.append(epoch_num)
self.mne.bad_epochs.sort()
color = self.mne.epoch_color_bad
return epoch_ix, color
def _toggle_whitening(self):
if self.mne.noise_cov is not None:
self.mne.use_noise_cov = not self.mne.use_noise_cov
self._update_projector()
self._update_yaxis_labels() # add/remove italics
self._redraw()
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# MANAGE TRACES
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _update_picks(self):
"""Compute which channel indices to show."""
if self.mne.butterfly and self.mne.ch_selections is not None:
selections_dict = self._make_butterfly_selections_dict()
self.mne.picks = np.concatenate(tuple(selections_dict.values()))
elif self.mne.butterfly:
self.mne.picks = self.mne.ch_order
else:
_slice = slice(self.mne.ch_start, self.mne.ch_start + self.mne.n_channels)
self.mne.picks = self.mne.ch_order[_slice]
self.mne.n_channels = len(self.mne.picks)
assert isinstance(self.mne.picks, np.ndarray)
assert self.mne.picks.dtype.kind == "i"
def _make_butterfly_selections_dict(self):
"""Make an altered copy of the selections dict for butterfly mode."""
from ..utils import _get_stim_channel
selections_dict = deepcopy(self.mne.ch_selections)
# remove potential duplicates
for selection_group in ("Vertex", "Custom"):
selections_dict.pop(selection_group, None)
# if present, remove stim channel from non-misc selection groups
stim_ch = _get_stim_channel(None, self.mne.info, raise_error=False)
if len(stim_ch):
stim_pick = self.mne.ch_names.tolist().index(stim_ch[0])
for _sel, _picks in selections_dict.items():
if _sel != "Misc":
stim_mask = np.in1d(_picks, [stim_pick], invert=True)
selections_dict[_sel] = np.array(_picks)[stim_mask]
return selections_dict
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# MANAGE DATA
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _get_start_stop(self):
# update time
start_sec = self.mne.t_start - self.mne.first_time
stop_sec = start_sec + self.mne.duration
if self.mne.is_epochs:
start, stop = np.round(
np.array([start_sec, stop_sec]) * self.mne.info["sfreq"]
).astype(int)
else:
start, stop = self.mne.inst.time_as_index((start_sec, stop_sec))
return start, stop
def _load_data(self, start=None, stop=None):
"""Retrieve the bit of data we need for plotting."""
if "raw" in (self.mne.instance_type, self.mne.ica_type):
# Add additional sample to cover the case sfreq!=1000
# when the shown time-range wouldn't correspond to duration anymore
if stop is None:
return self.mne.inst[:, start:]
else:
return self.mne.inst[:, start : stop + 2]
else:
# subtract one sample from tstart before searchsorted, to make sure
# we land on the left side of the boundary time (avoid precision
# errors)
ix_start = np.searchsorted(
self.mne.boundary_times, self.mne.t_start - self.mne.sampling_period
)
ix_stop = ix_start + self.mne.n_epochs
item = slice(ix_start, ix_stop)
data = np.concatenate(self.mne.inst.get_data(item=item), axis=-1)
times = np.arange(start, stop) / self.mne.info["sfreq"]
return data, times
def _apply_filter(self, data, start, stop, picks):
"""Filter (with same defaults as raw.filter())."""
from ..filter import _overlap_add_filter, _iir_filter
starts, stops = self.mne.filter_bounds
mask = (starts < stop) & (stops > start)
starts = np.maximum(starts[mask], start) - start
stops = np.minimum(stops[mask], stop) - start
for _start, _stop in zip(starts, stops):
_picks = np.where(np.in1d(picks, self.mne.picks_data))[0]
if len(_picks) == 0:
break
this_data = data[_picks, _start:_stop]
if isinstance(self.mne.filter_coefs, np.ndarray): # FIR
this_data = _overlap_add_filter(
this_data, self.mne.filter_coefs, copy=False
)
else: # IIR
this_data = _iir_filter(
this_data, self.mne.filter_coefs, None, 1, False
)
data[_picks, _start:_stop] = this_data
def _process_data(self, data, start, stop, picks, thread=None):
"""Update self.mne.data after user interaction."""
# apply projectors
if self.mne.projector is not None:
# thread is the loading-thread only available in Qt-backend
if thread:
thread.processText.emit("Applying Projectors...")
data = self.mne.projector @ data
# get only the channels we're displaying
data = data[picks]
# remove DC
if self.mne.remove_dc:
if thread:
thread.processText.emit("Removing DC...")
data -= np.nanmean(data, axis=1, keepdims=True)
# apply filter
if self.mne.filter_coefs is not None:
if thread:
thread.processText.emit("Apply Filter...")
self._apply_filter(data, start, stop, picks)
# scale the data for display in a 1-vertical-axis-unit slot
if thread:
thread.processText.emit("Scale Data...")
this_names = self.mne.ch_names[picks]
this_types = self.mne.ch_types[picks]
stims = this_types == "stim"
white = np.logical_and(
np.in1d(this_names, self.mne.whitened_ch_names),
np.in1d(this_names, self.mne.info["bads"], invert=True),
)
norms = np.vectorize(self.mne.scalings.__getitem__)(this_types)
norms[stims] = data[stims].max(axis=-1)
norms[white] = self.mne.scalings["whitened"]
norms[norms == 0] = 1
data /= 2 * norms[:, np.newaxis]
return data
def _update_data(self):
start, stop = self._get_start_stop()
# get the data
data, times = self._load_data(start, stop)
# process the data
data = self._process_data(data, start, stop, self.mne.picks)
# set the data as attributes
self.mne.data = data
self.mne.times = times
def _get_epoch_num_from_time(self, time):
epoch_nums = self.mne.inst.selection
return epoch_nums[np.searchsorted(self.mne.boundary_times[1:], time)]
def _redraw(self, update_data=True, annotations=False):
"""Redraws backend if necessary."""
if update_data:
self._update_data()
self._draw_traces()
if annotations and not self.mne.is_epochs:
self._draw_annotations()
def _close(self, event):
"""Handle close events (via keypress or window [x])."""
from matplotlib.pyplot import close
logger.debug(f"Closing {self.mne.instance_type} browser...")
# write out bad epochs (after converting epoch numbers to indices)
if self.mne.instance_type == "epochs":
bad_ixs = np.in1d(self.mne.inst.selection, self.mne.bad_epochs).nonzero()[0]
self.mne.inst.drop(bad_ixs)
logger.info(
"The following epochs were marked as bad "
"and are dropped:\n"
f"{self.mne.bad_epochs}"
)
# write bad channels back to instance (don't do this for proj;
# proj checkboxes are for viz only and shouldn't modify the instance)
if self.mne.instance_type in ("raw", "epochs"):
self.mne.inst.info["bads"] = self.mne.info["bads"]
logger.info(
f"Channels marked as bad:\n" f"{self.mne.info['bads'] or 'none'}"
)
# ICA excludes
elif self.mne.instance_type == "ica":
self.mne.ica.exclude = [
self.mne.ica._ica_names.index(ch) for ch in self.mne.info["bads"]
]
# write window size to config
str_size = ",".join([str(i) for i in self._get_size()])
set_config("MNE_BROWSE_RAW_SIZE", str_size, set_env=False)
# Clean up child figures (don't pop(), child figs remove themselves)
while len(self.mne.child_figs):
fig = self.mne.child_figs[-1]
close(fig)
self._close_event(fig)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# CHILD FIGURES
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@abstractmethod
def _new_child_figure(self, fig_name, **kwargs):
pass
def _create_ch_context_fig(self, idx):
"""Show context figure; idx is index of **visible** channels."""
inst = self.mne.instance_type
pick = self.mne.picks[idx]
if inst == "raw":
fig = self._create_ch_location_fig(pick)
elif inst == "ica":
fig = self._create_ica_properties_fig(pick)
else:
fig = self._create_epoch_image_fig(pick)
return fig
def _create_ch_location_fig(self, pick):
"""Show channel location figure."""
from .utils import _channel_type_prettyprint, plot_sensors
ch_name = self.mne.ch_names[pick]
ch_type = self.mne.ch_types[pick]
if ch_type not in _DATA_CH_TYPES_SPLIT:
return
# create figure and axes
title = f"Location of {ch_name}"
fig = self._new_child_figure(figsize=(4, 4), fig_name=None, window_title=title)
fig.suptitle(title)
ax = fig.add_subplot(111)
title = f"{ch_name} position ({_channel_type_prettyprint[ch_type]})"
_ = plot_sensors(
self.mne.info,
ch_type=ch_type,
axes=ax,
title=title,
kind="select",
show=False,
)
# highlight desired channel & disable interactivity
inds = np.in1d(fig.lasso.ch_names, [ch_name])
fig.lasso.disconnect()
fig.lasso.alpha_other = 0.3
fig.lasso.linewidth_selected = 3
fig.lasso.style_sensors(inds)
return fig
def _create_ica_properties_fig(self, idx):
"""Show ICA properties for the selected component."""
from mne.viz.ica import (
_create_properties_layout,
_prepare_data_ica_properties,
_fast_plot_ica_properties,
)
ch_name = self.mne.ch_names[idx]
if ch_name not in self.mne.ica._ica_names: # for EOG chans: do nothing
return
pick = self.mne.ica._ica_names.index(ch_name)
title = f"{ch_name} properties"
fig = self._new_child_figure(figsize=(7, 6), fig_name=None, window_title=title)
fig.suptitle(title)
fig, axes = _create_properties_layout(fig=fig)
if not hasattr(self.mne, "data_ica_properties"):
# Precompute epoch sources only once
self.mne.data_ica_properties = _prepare_data_ica_properties(
self.mne.ica_inst, self.mne.ica
)
_fast_plot_ica_properties(
self.mne.ica,
self.mne.ica_inst,
picks=pick,
axes=axes,
precomputed_data=self.mne.data_ica_properties,
show=False,
)
return fig
def _create_epoch_image_fig(self, pick):
"""Show epochs image for the selected channel."""
from matplotlib.gridspec import GridSpec
from mne.viz import plot_epochs_image
ch_name = self.mne.ch_names[pick]
title = f"Epochs image ({ch_name})"
fig = self._new_child_figure(figsize=(6, 4), fig_name=None, window_title=title)
fig.suptitle = title
gs = GridSpec(nrows=3, ncols=10)
fig.add_subplot(gs[:2, :9])
fig.add_subplot(gs[2, :9])
fig.add_subplot(gs[:2, 9])
plot_epochs_image(self.mne.inst, picks=pick, fig=fig, show=False)
return fig
def _create_epoch_histogram(self):
"""Create peak-to-peak histogram of channel amplitudes."""
epochs = self.mne.inst
data = OrderedDict()
ptp = np.ptp(epochs.get_data(), axis=2)
for ch_type in ("eeg", "mag", "grad"):
if ch_type in epochs:
data[ch_type] = ptp.T[self.mne.ch_types == ch_type].ravel()
units = _handle_default("units")
titles = _handle_default("titles")
colors = _handle_default("color")
scalings = _handle_default("scalings")
title = "Histogram of peak-to-peak amplitudes"
figsize = (4, 1 + 1.5 * len(data))
fig = self._new_child_figure(
figsize=figsize, fig_name="fig_histogram", window_title=title
)
for ix, (_ch_type, _data) in enumerate(data.items()):
ax = fig.add_subplot(len(data), 1, ix + 1)
ax.set(title=titles[_ch_type], xlabel=units[_ch_type], ylabel="Count")
# set histogram bin range based on rejection thresholds
reject = None
_range = None
if epochs.reject is not None and _ch_type in epochs.reject:
reject = epochs.reject[_ch_type] * scalings[_ch_type]
_range = (0.0, reject * 1.1)
# plot it
ax.hist(
_data * scalings[_ch_type],
bins=100,
color=colors[_ch_type],
range=_range,
)
if reject is not None:
ax.plot((reject, reject), (0, ax.get_ylim()[1]), color="r")
# finalize
fig.suptitle(title, y=0.99)
if hasattr(fig, "_inch_to_rel"):
kwargs = dict(
bottom=fig._inch_to_rel(0.5, horiz=False),
top=1 - fig._inch_to_rel(0.5, horiz=False),
left=fig._inch_to_rel(0.75),
right=1 - fig._inch_to_rel(0.25),
)
else:
kwargs = dict()
fig.subplots_adjust(hspace=0.7, **kwargs)
self.mne.fig_histogram = fig
return fig
def _close_event(self, fig):
"""Look at _close_event in mne.fixes.py for why this exists."""
pass
def fake_keypress(self, key, fig=None): # noqa: D400
"""Pass a fake keypress to the figure.
Parameters
----------
key : str
The key to fake (e.g., ``'a'``).
fig : instance of Figure
The figure to pass the keypress to.
"""
return self._fake_keypress(key, fig=fig)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# TEST METHODS
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@abstractmethod
def _get_size(self):
pass
@abstractmethod
def _fake_keypress(self, key, fig):
pass
@abstractmethod
def _fake_click(self, point, fig, axis, xform, button, kind):
pass
@abstractmethod
def _click_ch_name(self, ch_index, button):
pass
@abstractmethod
def _resize_by_factor(self, factor):
pass
@abstractmethod
def _get_ticklabels(self, orientation):
pass
@abstractmethod
def _update_yaxis_labels(self):
pass
def _load_backend(backend_name):
global backend
if backend_name == "matplotlib":
backend = importlib.import_module(name="._mpl_figure", package="mne.viz")
else:
from mne_qt_browser import _pg_figure as backend
logger.info(f"Using {backend_name} as 2D backend.")
return backend
def _get_browser(show, block, **kwargs):
"""Instantiate a new MNE browse-style figure."""
from .utils import _get_figsize_from_config
figsize = kwargs.setdefault("figsize", _get_figsize_from_config())
if figsize is None or np.any(np.array(figsize) < 8):
kwargs["figsize"] = (8, 8)
kwargs["splash"] = True if show else False
if kwargs.get("theme", None) is None:
kwargs["theme"] = get_config("MNE_BROWSER_THEME", "auto")
if kwargs.get("overview_mode", None) is None:
kwargs["overview_mode"] = get_config("MNE_BROWSER_OVERVIEW_MODE", "channels")
# Initialize browser backend
backend_name = get_browser_backend()
# Check mne-qt-browser compatibility
if backend_name == "qt":
import mne_qt_browser
from .. import BaseEpochs
from ..fixes import _compare_version
is_ica = kwargs.get("ica", False)
is_epochs = isinstance(kwargs.get("inst", False), BaseEpochs)
not_compat = _compare_version(mne_qt_browser.__version__, "<", "0.2.0")
inst_str = "ICA" if is_ica else "Epochs"
if not_compat and (is_ica or is_epochs):
logger.info(
f'You set the browser-backend to "qt" but your'
f" current version {mne_qt_browser.__version__}"
f" of mne-qt-browser is too low for {inst_str}."
f"Update with pip or conda."
f"Defaults to matplotlib."
)
with use_browser_backend("matplotlib"):
# Initialize Browser
fig = backend._init_browser(**kwargs)
_show_browser(show=show, block=block, fig=fig)
return fig
# Initialize Browser
fig = backend._init_browser(**kwargs)
_show_browser(show=show, block=block, fig=fig)
return fig
def _check_browser_backend_name(backend_name):
_validate_type(backend_name, str, "backend_name")
backend_name = backend_name.lower()
backend_name = "qt" if backend_name == "pyqtgraph" else backend_name
_check_option("backend_name", backend_name, VALID_BROWSE_BACKENDS)
return backend_name
@verbose
def set_browser_backend(backend_name, verbose=None):
"""Set the 2D browser backend for MNE.
The backend will be set as specified and operations will use
that backend.
Parameters
----------
backend_name : str
The 2D browser backend to select. See Notes for the capabilities
of each backend (``'qt'``, ``'matplotlib'``). The ``'qt'`` browser
requires `mne-qt-browser
<https://github.com/mne-tools/mne-qt-browser>`__.
%(verbose)s
Returns
-------
old_backend_name : str | None
The old backend that was in use.
Notes
-----
This table shows the capabilities of each backend ("✓" for full support,
and "-" for partial support):
.. table::
:widths: auto
+--------------------------------------+------------+----+
| **2D browser function:** | matplotlib | qt |
+======================================+============+====+
| :func:`plot_raw` | ✓ | ✓ |
+--------------------------------------+------------+----+
| :func:`plot_epochs` | ✓ | ✓ |
+--------------------------------------+------------+----+
| :func:`plot_ica_sources` | ✓ | ✓ |
+--------------------------------------+------------+----+
+--------------------------------------+------------+----+
| **Feature:** |
+--------------------------------------+------------+----+
| Show Events | ✓ | ✓ |
+--------------------------------------+------------+----+
| Add/Edit/Remove Annotations | ✓ | ✓ |
+--------------------------------------+------------+----+
| Toggle Projections | ✓ | ✓ |
+--------------------------------------+------------+----+
| Butterfly Mode | ✓ | ✓ |
+--------------------------------------+------------+----+
| Selection Mode | ✓ | ✓ |
+--------------------------------------+------------+----+
| Smooth Scrolling | | ✓ |
+--------------------------------------+------------+----+
| Overview-Bar (with Z-Score-Mode) | | ✓ |
+--------------------------------------+------------+----+
.. versionadded:: 0.24
"""
global MNE_BROWSER_BACKEND
old_backend_name = MNE_BROWSER_BACKEND
backend_name = _check_browser_backend_name(backend_name)
if MNE_BROWSER_BACKEND != backend_name:
_load_backend(backend_name)
MNE_BROWSER_BACKEND = backend_name
return old_backend_name
def _init_browser_backend():
global MNE_BROWSER_BACKEND
# check if MNE_BROWSER_BACKEND is not None and valid or get it from config
loaded_backend = MNE_BROWSER_BACKEND or get_config(
key="MNE_BROWSER_BACKEND", default=None
)
if loaded_backend is not None:
set_browser_backend(loaded_backend)
return MNE_BROWSER_BACKEND
else:
errors = dict()
# Try import of valid browser backends
for name in VALID_BROWSE_BACKENDS:
try:
_load_backend(name)
except ImportError as exc:
errors[name] = str(exc)
else:
MNE_BROWSER_BACKEND = name
break
else:
raise RuntimeError(
"Could not load any valid 2D backend:\n"
+ "\n".join(f"{key}: {val}" for key, val in errors.items())
)
return MNE_BROWSER_BACKEND
def get_browser_backend():
"""Return the 2D backend currently used.
Returns
-------
backend_used : str | None
The 2D browser backend currently in use. If no backend is found,
returns ``None``.
"""
try:
backend_name = _init_browser_backend()
except RuntimeError as exc:
backend_name = None
logger.info(str(exc))
return backend_name
@contextmanager
def use_browser_backend(backend_name):
"""Create a 2D browser visualization context using the designated backend.
See :func:`mne.viz.set_browser_backend` for more details on the available
2D browser backends and their capabilities.
Parameters
----------
backend_name : {'qt', 'matplotlib'}
The 2D browser backend to use in the context.
"""
old_backend = set_browser_backend(backend_name)
try:
yield backend
finally:
if old_backend is not None:
try:
set_browser_backend(old_backend)
except Exception:
pass