Repository: mne-tools/mne-python

Issue:
apply_baseline fails with AssertionError
# Description

Calling `apply_baseline` after loading `Epochs` with `preload=False` fails with an AssertionError.

# MWE

```python
import os.path as op
from pathlib import Path

import mne

sample_dir = Path(mne.datasets.sample.data_path())
sample_fname = sample_dir / "MEG" / "sample" / "sample_audvis_raw.fif"
raw = mne.io.read_raw_fif(sample_fname, preload=True)
events = mne.find_events(raw)

epochs = mne.Epochs(raw, events, baseline=None, preload=False)
basepath = Path(op.expanduser("~")) / "Desktop"
fname = basepath / "test_baseline_epochs.fif"
epochs.save(fname, overwrite=True)
epochs = mne.read_epochs(fname, preload=False)
epochs.apply_baseline((None, 0))
```

# Expected output

- a normally applied baseline that gets active as soon as data is loaded
- ... or at least an error that tells me that applying a baseline is only possible with preloaded data

# Actual output

```python
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-1-fd06060ead84> in <module>
     14 epochs.save(fname, overwrite=True)
     15 epochs = mne.read_epochs(fname, preload=False)
---> 16 epochs.apply_baseline((None, 0))

<decorator-gen-234> in apply_baseline(self, baseline, verbose)

~/Desktop/bids/mne-python/mne/epochs.py in apply_baseline(self, baseline, verbose)
    723         else:  # logging happens in "rescale" in "if" branch
    724             logger.info(_log_rescale(baseline))
--> 725             assert self._do_baseline is True
    726         self.baseline = baseline
    727         return self

AssertionError: 
```

# MNE sys info

Click on details.

<details>

```
Platform:       Linux-5.4.0-91-generic-x86_64-with-glibc2.10
Python:         3.8.8 (default, Feb 24 2021, 21:46:12)  [GCC 7.3.0]
Executable:     /home/stefanappelhoff/miniconda3/envs/mne_bids/bin/python
CPU:            x86_64: 8 cores
Memory:         Unavailable (requires "psutil" package)
mne:            1.0.dev0
numpy:          1.21.2 {blas=openblas, lapack=openblas}
scipy:          1.6.1
matplotlib:     3.3.4 {backend=Qt5Agg}

sklearn:        0.24.1
numba:          Not found
nibabel:        3.2.1
nilearn:        0.7.1
dipy:           Not found
cupy:           Not found
pandas:         1.2.3
pyvista:        Not found
pyvistaqt:      Not found
ipyvtklink:     Not found
vtk:            Not found
PyQt5:          5.9.2
ipympl:         Not found
mne_qt_browser: Not found
pooch:          v1.5.1

```

</details>


Discussion:
Agreed this looks like a bug. @sappelhoff I assume since you're trying to graduate you'd rather someone else take a look? :)
> I assume since you're trying to graduate you'd rather someone else take a look? :)

yes please :heart: 

Resolving patch:
diff --git a/mne/epochs.py b/mne/epochs.py
--- a/mne/epochs.py
+++ b/mne/epochs.py
@@ -722,7 +722,10 @@ def apply_baseline(self, baseline=(None, 0), *, verbose=None):
             self._do_baseline = False
         else:  # logging happens in "rescale" in "if" branch
             logger.info(_log_rescale(baseline))
-            assert self._do_baseline is True
+            # For EpochsArray and Epochs, this is already True:
+            # assert self._do_baseline is True
+            # ... but for EpochsFIF it's not, so let's set it explicitly
+            self._do_baseline = True
         self.baseline = baseline
         return self
 


Test patch:
diff --git a/mne/tests/test_epochs.py b/mne/tests/test_epochs.py
--- a/mne/tests/test_epochs.py
+++ b/mne/tests/test_epochs.py
@@ -789,7 +789,7 @@ def test_rescale():
 
 
 @pytest.mark.parametrize('preload', (True, False))
-def test_epochs_baseline(preload):
+def test_epochs_baseline_basic(preload, tmp_path):
     """Test baseline and rescaling modes with and without preloading."""
     data = np.array([[2, 3], [2, 3]], float)
     info = create_info(2, 1000., ('eeg', 'misc'))
@@ -799,6 +799,7 @@ def test_epochs_baseline(preload):
     epochs = mne.Epochs(raw, events, None, 0, 1e-3, baseline=None,
                         preload=preload)
     epochs.drop_bad()
+    epochs_nobl = epochs.copy()
     epochs_data = epochs.get_data()
     assert epochs_data.shape == (1, 2, 2)
     expected = data.copy()
@@ -832,6 +833,19 @@ def test_epochs_baseline(preload):
     else:
         epochs.apply_baseline(None)
         assert epochs.baseline is None
+    # gh-10139
+    fname = tmp_path / 'test-epo.fif'
+    epochs.apply_baseline((None, None))
+    assert_allclose(epochs.get_data('eeg').mean(-1), 0, atol=1e-20)
+    assert epochs_nobl.baseline is None
+    for ep in (epochs, epochs_nobl):
+        ep.save(fname, overwrite=True)
+        ep = mne.read_epochs(fname, preload=preload)
+        ep.apply_baseline((0, 0))
+        assert_allclose(ep.get_data('eeg').mean(-1), 0.5, atol=1e-20)
+        ep.save(fname, overwrite=True)
+        ep = mne.read_epochs(fname, preload=preload)
+        assert_allclose(ep.get_data('eeg').mean(-1), 0.5, atol=1e-20)
 
 
 def test_epochs_bad_baseline():