Repository: mne-tools/mne-python

Issue:
MNE Viz Raw Browser crashes due to NameError: name '_qt_init_icons' is not defined
#### Describe the bug
Freshly installed MNE Version 1.0.2 for MacOS M1 / Apple Silicon (using Anaconda) causes the following error when trying to call `raw.plot()` if the MacOS theme option is set to dark mode:

```sh
Traceback (most recent call last):
  File "/Users/tarek/Documents/Development/BA/eeg-mne/topomap.py", line 27, in <module>
    raw.plot(block=True )
  File "/Users/tarek/opt/anaconda3/envs/mne/lib/python3.10/site-packages/mne/io/base.py", line 1550, in plot
    return plot_raw(self, events, duration, start, n_channels, bgcolor,
  File "<decorator-gen-182>", line 12, in plot_raw
  File "/Users/tarek/opt/anaconda3/envs/mne/lib/python3.10/site-packages/mne/viz/raw.py", line 356, in plot_raw
    fig = _get_browser(show=show, block=block, **params)
  File "/Users/tarek/opt/anaconda3/envs/mne/lib/python3.10/site-packages/mne/viz/_figure.py", line 654, in _get_browser
    fig = backend._init_browser(**kwargs)
  File "/Users/tarek/opt/anaconda3/envs/mne/lib/python3.10/site-packages/mne_qt_browser/_pg_figure.py", line 4748, in _init_browser
    browser = MNEQtBrowser(**kwargs)
  File "/Users/tarek/opt/anaconda3/envs/mne/lib/python3.10/site-packages/mne_qt_browser/_pg_figure.py", line 221, in func
    meth(self, *args, **kwargs)
  File "/Users/tarek/opt/anaconda3/envs/mne/lib/python3.10/site-packages/mne_qt_browser/_pg_figure.py", line 2713, in __init__
    stylesheet = _qt_get_stylesheet(getattr(self.mne, 'theme', 'auto'))
  File "/Users/tarek/opt/anaconda3/envs/mne/lib/python3.10/site-packages/mne/viz/backends/_utils.py", line 235, in _qt_get_stylesheet
    icons_path = _qt_init_icons()
NameError: name '_qt_init_icons' is not defined
```
Due to this error, the raw browser can't open.

#### Steps to reproduce
Independent of the data set used, calling the `raw.plot()` method on MacOS with the OS theme set to `dark` will cause the described error, crashing the raw browser.

#### Expected results
The raw browser should open itself on method call `raw.plot()` as it is working when the MacOS theme is set to `light`.

#### Additional information
This code causes the error (`/mne/viz/backends/_utils.py:235`):
```python
def _qt_get_stylesheet(theme):
    from ...fixes import _compare_version
    from ...utils import logger, warn, _validate_type
    _validate_type(theme, ('path-like',), 'theme')
    theme = str(theme)
    system_theme = None
    if theme == 'auto':
        theme = system_theme = _qt_detect_theme()
    if theme in ('dark', 'light'):
        if system_theme is None:
            system_theme = _qt_detect_theme()
        if sys.platform == 'darwin' and theme == system_theme:
            from qtpy import QtCore
            try:
                qt_version = QtCore.__version__  # PySide
            except AttributeError:
                qt_version = QtCore.QT_VERSION_STR  # PyQt
            if theme == 'dark' and _compare_version(qt_version, '<', '5.13'):
                # Taken using "Digital Color Meter" on macOS 12.2.1 looking at
                # Meld, and also adapting (MIT-licensed)
                # https://github.com/ColinDuquesnoy/QDarkStyleSheet/blob/master/qdarkstyle/dark/style.qss  # noqa: E501
                # Something around rgb(51, 51, 51) worked as the bgcolor here,
                # but it's easy enough just to set it transparent and inherit
                # the bgcolor of the window (which is the same). We also take
                # the separator images from QDarkStyle (MIT).
                icons_path = _qt_init_icons() # <--- NameError: name '_qt_init_icons' is not defined
                stylesheet = """\
...
```

```
mne.sys_info()
Platform:         macOS-12.2-x86_64-i386-64bit
Python:           3.10.4 | packaged by conda-forge | (main, Mar 24 2022, 17:45:10) [Clang 12.0.1 ]
Executable:       /Users/tarek/opt/anaconda3/envs/mne/bin/python
CPU:              i386: 8 cores
Memory:           16.0 GB
mne:              1.0.2
numpy:            1.21.6 {blas=NO_ATLAS_INFO, lapack=lapack}
scipy:            1.8.0
matplotlib:       3.5.2 {backend=module://backend_interagg}
sklearn:          1.0.2
numba:            0.55.1
nibabel:          3.2.2
nilearn:          0.9.1
dipy:             1.5.0
cupy:             Not found
pandas:           1.4.2
pyvista:          0.34.0 {OpenGL 4.1 Metal - 76.3 via Apple M1 Pro}
pyvistaqt:        0.9.0
ipyvtklink:       0.2.2
```


#### Quick Fix: How To 😉 
1. Set your MacOS theme to `light` to avoid getting into the erroneous `if theme == 'dark'` clause.
2. Done ;)



Discussion:

Resolving patch:
diff --git a/mne/preprocessing/_csd.py b/mne/preprocessing/_csd.py
--- a/mne/preprocessing/_csd.py
+++ b/mne/preprocessing/_csd.py
@@ -172,4 +172,12 @@ def compute_current_source_density(inst, sphere='auto', lambda2=1e-5,
     for pick in picks:
         inst.info['chs'][pick].update(coil_type=FIFF.FIFFV_COIL_EEG_CSD,
                                       unit=FIFF.FIFF_UNIT_V_M2)
+
+    # Remove rejection thresholds for EEG
+    if isinstance(inst, BaseEpochs):
+        if inst.reject and 'eeg' in inst.reject:
+            del inst.reject['eeg']
+        if inst.flat and 'eeg' in inst.flat:
+            del inst.flat['eeg']
+
     return inst
diff --git a/mne/viz/backends/_utils.py b/mne/viz/backends/_utils.py
--- a/mne/viz/backends/_utils.py
+++ b/mne/viz/backends/_utils.py
@@ -232,7 +232,6 @@ def _qt_get_stylesheet(theme):
                 # but it's easy enough just to set it transparent and inherit
                 # the bgcolor of the window (which is the same). We also take
                 # the separator images from QDarkStyle (MIT).
-                icons_path = _qt_init_icons()
                 stylesheet = """\
 QStatusBar {
   border: 1px solid rgb(76, 76, 75);
@@ -245,23 +244,7 @@ def _qt_get_stylesheet(theme):
   background-color: transparent;
   border-bottom: 1px solid rgb(99, 99, 99);
 }
-QToolBar::separator:horizontal {
-  width: 16px;
-  image: url("%(icons_path)s/toolbar_separator_horizontal@2x.png");
-}
-QToolBar::separator:vertical {
-  height: 16px;
-  image: url("%(icons_path)s/toolbar_separator_vertical@2x.png");
-}
-QToolBar::handle:horizontal {
-  width: 16px;
-  image: url("%(icons_path)s/toolbar_move_horizontal@2x.png");
-}
-QToolBar::handle:vertical {
-  height: 16px;
-  image: url("%(icons_path)s/toolbar_move_vertical@2x.png");
-}
-""" % dict(icons_path=icons_path)
+"""
             else:
                 stylesheet = ''
         else:


Test patch:
diff --git a/mne/beamformer/tests/test_lcmv.py b/mne/beamformer/tests/test_lcmv.py
--- a/mne/beamformer/tests/test_lcmv.py
+++ b/mne/beamformer/tests/test_lcmv.py
@@ -677,8 +677,8 @@ def test_localization_bias_fixed(bias_params_fixed, reg, weight_norm, use_cov,
         # no reg
         (0.00, 'vector', None, True, None, 23, 24, 0.96, 0.97),
         (0.00, 'vector', 'unit-noise-gain-invariant', True, None, 52, 54, 0.95, 0.96),  # noqa: E501
-        (0.00, 'vector', 'unit-noise-gain', True, None, 44, 46, 0.97, 0.98),
-        (0.00, 'vector', 'nai', True, None, 44, 46, 0.97, 0.98),
+        (0.00, 'vector', 'unit-noise-gain', True, None, 44, 48, 0.97, 0.99),
+        (0.00, 'vector', 'nai', True, None, 44, 48, 0.97, 0.99),
         (0.00, 'max-power', None, True, None, 14, 15, 0, 0),
         (0.00, 'max-power', 'unit-noise-gain-invariant', True, None, 35, 37, 0, 0),  # noqa: E501
         (0.00, 'max-power', 'unit-noise-gain', True, None, 35, 37, 0, 0),
diff --git a/mne/preprocessing/tests/test_csd.py b/mne/preprocessing/tests/test_csd.py
--- a/mne/preprocessing/tests/test_csd.py
+++ b/mne/preprocessing/tests/test_csd.py
@@ -17,7 +17,8 @@
 from scipy import linalg
 
 from mne.channels import make_dig_montage
-from mne import create_info, EvokedArray, pick_types, Epochs
+from mne import (create_info, EvokedArray, pick_types, Epochs, find_events,
+                 read_epochs)
 from mne.io import read_raw_fif, RawArray
 from mne.io.constants import FIFF
 from mne.utils import object_diff
@@ -183,3 +184,16 @@ def test_csd_fif():
         ch.update(coil_type=FIFF.FIFFV_COIL_EEG, unit=FIFF.FIFF_UNIT_V)
         raw_csd._data[pick] = raw._data[pick]
     assert object_diff(raw.info, raw_csd.info) == ''
+
+
+def test_csd_epochs(tmp_path):
+    """Test making epochs, saving to disk and loading."""
+    raw = read_raw_fif(raw_fname)
+    raw.pick_types(eeg=True, stim=True).load_data()
+    events = find_events(raw)
+    epochs = Epochs(raw, events, reject=dict(eeg=1e-4), preload=True)
+    epochs = compute_current_source_density(epochs)
+    epo_fname = tmp_path / 'test_csd_epo.fif'
+    epochs.save(epo_fname)
+    epochs2 = read_epochs(epo_fname, preload=True)
+    assert_allclose(epochs._data, epochs2._data)