Repository: mne-tools/mne-python

Issue:
ENH: Add evoked.get_data
In #9241 there are lines like:
```
evoked_eeg_interp.data[evoked_eeg_interp.ch_names.index('EEG 004'), :]
```
We should have `evoked.get_data` like we have for epochs and raw so this can just become:
```
evoked_eeg_interp.get_data('EEG 004')
```
and become more API-consistent.


Discussion:
+1

On Sat, Apr 3, 2021 at 4:24 PM Eric Larson ***@***.***> wrote:

> In #9241 <https://github.com/mne-tools/mne-python/pull/9241> there are
> lines like:
>
> evoked_eeg_interp.data[evoked_eeg_interp.ch_names.index('EEG 004'), :]
>
> We should have evoked.get_data like we have for epochs and raw so this
> can just become:
>
> evoked.get_data('EEG 004')
>
> and become more API-consistent.
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/mne-tools/mne-python/issues/9244>, or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AABHKHERT224TGOTLDKV653TG4QLNANCNFSM42KJIY2A>
> .
>


Resolving patch:
diff --git a/mne/evoked.py b/mne/evoked.py
--- a/mne/evoked.py
+++ b/mne/evoked.py
@@ -158,6 +158,27 @@ def data(self, data):
         """Set the data matrix."""
         self._data = data
 
+    @fill_doc
+    def get_data(self, picks=None):
+        """Get evoked data as 2D array.
+
+        Parameters
+        ----------
+        %(picks_all)s
+
+        Returns
+        -------
+        data : ndarray, shape (n_channels, n_times)
+            A view on evoked data.
+
+        Notes
+        -----
+        .. versionadded:: 0.24
+        """
+        picks = _picks_to_idx(self.info, picks, "all", exclude=())
+        data = self.data[picks, :]
+        return data
+
     @verbose
     def apply_function(self, fun, picks=None, dtype=None, n_jobs=1,
                        verbose=None, **kwargs):


Test patch:
diff --git a/mne/channels/tests/test_interpolation.py b/mne/channels/tests/test_interpolation.py
--- a/mne/channels/tests/test_interpolation.py
+++ b/mne/channels/tests/test_interpolation.py
@@ -110,14 +110,8 @@ def test_interpolation_eeg(offset, avg_proj, ctol, atol, method):
     evoked_eeg_interp = evoked_eeg_2_bads.interpolate_bads(
         origin=(0., 0., 0.), exclude=['EEG 004'], **kw)
     assert evoked_eeg_interp.info['bads'] == ['EEG 004']
-    assert np.all(
-        evoked_eeg_interp.data[evoked_eeg_interp.ch_names.index('EEG 004'), :]
-        == 1e10
-    )
-    assert np.all(
-        evoked_eeg_interp.data[evoked_eeg_interp.ch_names.index('EEG 012'), :]
-        != 1e10
-    )
+    assert np.all(evoked_eeg_interp.get_data('EEG 004') == 1e10)
+    assert np.all(evoked_eeg_interp.get_data('EEG 012') != 1e10)
 
     # Now test without exclude parameter
     evoked_eeg_bad.info['bads'] = ['EEG 012']
diff --git a/mne/io/tests/test_reference.py b/mne/io/tests/test_reference.py
--- a/mne/io/tests/test_reference.py
+++ b/mne/io/tests/test_reference.py
@@ -661,9 +661,8 @@ def _check_bipolar(raw_test, ch_a, ch_b):
     # check if reference channels have been kept correctly.
     assert (len(raw_test.ch_names) == len(a_channels) + len(ch_names))
     for idx, ch_label in enumerate(ch_names):
-        manual_ch = raw_data[idx, :]
-        assert_array_equal(
-            raw_test._data[raw_test.ch_names.index(ch_label), :], manual_ch)
+        manual_ch = raw_data[np.newaxis, idx]
+        assert_array_equal(raw_test.get_data(ch_label), manual_ch)
 
     # test bipolars with a channel in both list (anode & cathode).
     raw_test = set_bipolar_reference(
diff --git a/mne/tests/test_evoked.py b/mne/tests/test_evoked.py
--- a/mne/tests/test_evoked.py
+++ b/mne/tests/test_evoked.py
@@ -30,6 +30,20 @@
 event_name = op.join(base_dir, 'test-eve.fif')
 
 
+def test_get_data():
+    """Test the get_data method for Evoked."""
+    evoked = read_evokeds(fname, 0)
+    d1 = evoked.get_data()
+    d2 = evoked.data
+    np.testing.assert_array_equal(d1, d2)
+
+    eeg_idxs = np.array([i == "eeg" for i in evoked.get_channel_types()])
+    np.testing.assert_array_equal(
+        evoked.data[eeg_idxs],
+        evoked.get_data(picks="eeg")
+    )
+
+
 def test_decim():
     """Test evoked decimation."""
     rng = np.random.RandomState(0)