Explore event-related dynamics for specific frequency bands

  • MNE version: 0.24.0
  • operating system: Windows 10

Hi,
I want to show the ECoG recording (Three channels from three animals) I have taken with bipolar electrodes as a specific band frequency as in the link time_frequency_global_field_power . The data does not have epochs and events. But I applied mne.make_fixed_length_events / epoch codes. I converted the raw data to the fif file and tried to apply the codes as in the link/below.

 import os.path as op

import numpy as np
import matplotlib.pyplot as plt

import mne


# let's explore some frequency bands
iter_freqs = [
    ('Theta', 4, 7),
    ('Alpha', 8, 12),
    ('Beta', 13, 25),
    ('Gamma', 30, 45)]


# set epoching parameters
event_id, tmin, tmax = 1, -1., 3.
baseline = None

# get the header to extract events
raw = mne.io.read_raw_fif('C:\\Users\Mehmet\Desktop\sczf.fif')
events = mne.make_fixed_length_events(raw)

frequency_map = list()

for band, fmin, fmax in iter_freqs:
    # (re)load the data to save memory
    raw = mne.io.read_raw_fif('C:\\Users\Mehmet\Desktop\sczf.fif')
    raw.pick_types(meg='grad', eog=False, ecog=True)  # we just look at gradiometers
    raw.load_data()

    # bandpass filter
    raw.filter(fmin, fmax, n_jobs=1,  # use more jobs to speed up.
               l_trans_bandwidth=1,  # make sure filter params are the same
               h_trans_bandwidth=1)  # in each band and skip "auto" option.

    # epoch
    epochs = mne.make_fixed_length_epochs (raw)
    # remove evoked response
    epochs.subtract_evoked()

    # get analytic signal (envelope)
    epochs.apply_hilbert(envelope=True)
    frequency_map.append(((band, fmin, fmax), epochs.average()))
    del epochs
del raw


# Helper function for plotting spread
def stat_fun(x):
    """Return sum of squares."""
    return np.sum(x ** 2, axis=0)


# Plot
fig, axes = plt.subplots(4, 1, figsize=(10, 7), sharex=True, sharey=True)
colors = plt.get_cmap('winter_r')(np.linspace(0, 1, 4))
for ((freq_name, fmin, fmax), average), color, ax in zip(
        frequency_map, colors, axes.ravel()[::-1]):
    times = average.times * 1e3
    gfp = np.sum(average.data ** 2, axis=0)
    gfp = mne.baseline.rescale(gfp, times, baseline=(None, 0))
    ax.plot(times, gfp, label=freq_name, color=color, linewidth=2.5)
    ax.axhline(0, linestyle='--', color='grey', linewidth=2)
    ci_low, ci_up = bootstrap_confidence_interval(average.data, random_state=0,
                                                  stat_fun=stat_fun)
    ci_low = rescale(ci_low, average.times, baseline=(None, 0))
    ci_up = rescale(ci_up, average.times, baseline=(None, 0))
    ax.fill_between(times, gfp + ci_up, gfp - ci_low, color=color, alpha=0.3)
    ax.grid(True)
    ax.set_ylabel('GFP')
    ax.annotate('%s (%d-%dHz)' % (freq_name, fmin, fmax),
                xy=(0.95, 0.8),
                horizontalalignment='right',
                xycoords='axes fraction')
    ax.set_xlim(-1000, 3000)

axes.ravel()[-1].set_xlabel('Time [ms]')

However, I got the error “No channels match the selection”. I ran the codes step by step and gives this error in the following line of code.

for band, fmin, fmax in iter_freqs:
    # (re)load the data to save memory
    raw = mne.io.read_raw_fif('C:\\Users\Mehmet\Desktop\sczf.fif')
    **raw.pick_types(meg='grad', eog=False, ecog=True)**  # we just look at gradiometers
    raw.load_data()

I would be glad if you can help.

Hello, have you tried setting meg=False, since you don’t have any MEG channels in your data?

1 Like

Hello, @richard, Yes, I tried.

Can you please share the full traceback (i.e., the entire block of error messages you see)?

C:\Users\Mehmet\AppData\Local\Temp/ipykernel_7940/2386507011.py:20: RuntimeWarning: This filename (C:\Users\Mehmet\Desktop\sczf.fif) does not conform to MNE naming conventions. All raw files should end with raw.fif, raw_sss.fif, raw_tsss.fif, _meg.fif, _eeg.fif, _ieeg.fif, raw.fif.gz, raw_sss.fif.gz, raw_tsss.fif.gz, _meg.fif.gz, _eeg.fif.gz or _ieeg.fif.gz
  raw = mne.io.read_raw_fif('C:\\Users\Mehmet\Desktop\sczf.fif')
Traceback (most recent call last):

  File "C:\Users\Mehmet\AppData\Local\Temp/ipykernel_7940/2386507011.py", line 21, in <module>
    raw.pick_types(meg=False, eog=False, ecog=True)  # we just look at gradiometers

  File "<decorator-gen-44>", line 12, in pick_types

  File "C:\Users\Mehmet\anaconda3\lib\site-packages\mne\channels\channels.py", line 678, in pick_types
    self._pick_drop_channels(idx)

  File "<decorator-gen-46>", line 12, in _pick_drop_channels

  File "C:\Users\Mehmet\anaconda3\lib\site-packages\mne\channels\channels.py", line 856, in _pick_drop_channels
    pick_info(self.info, idx, copy=False)

  File "<decorator-gen-9>", line 12, in pick_info

  File "C:\Users\Mehmet\anaconda3\lib\site-packages\mne\io\pick.py", line 538, in pick_info
    raise ValueError('No channels match the selection.')

ValueError: No channels match the selection.

Thank you! Can you please share the output of:

print(raw.get_channel_types())

Thank you very much for your interest.

Output:

print(raw.get_channel_types())
['eeg', 'eeg', 'eeg']

I ran the codes (Just edited line) according to the “eeg=True”. This time I got this error.

  File "C:\Users\Mehmet\AppData\Local\Temp/ipykernel_7940/3542700214.py", line 19
    """Return sum of squares."""
    ^
IndentationError: expected an indented block

When I ran all the codes again, I got this error.

RuntimeError: By default, MNE does not load data into main memory to conserve resources. inst.apply_hilbert requires epochs data to be loaded. Use preload=True (or string) in the constructor or epochs.load_data().

You have the source of your issue right there: There are no ecog channels in your data, only eeg channels. Hence, raw.pick_types(ecog=True) fails.

I added preload=True. I got this graph.