How do I process raw data from edf?

  • MNE version: 1.2.2
  • operating system: macOS M1Pro

Hello.

I am trying to process EEG files recorded in EDF files using MNEPython.
I can read the files using “mne.io.read_raw_edf()”, but I don’t know how to filter or rename the channels.
Is there a function to edit as well as read?

Sorry for the rudimentary question.

Thank you in advance for your help.

Hello,

Please look through the tutorials, especially the introductory tutorials.
https://mne.tools/dev/auto_tutorials/index.html#introductory-tutorials

from mne.io import read_raw_edf

raw  = read_raw_edf(...)
raw.rename_channels(...)
raw.filter(...)

Best,
Mathieu

Thank you for your reply.

I tried the following as advised, but I got an error.

Below is the code I ran.

l_freq = 1
h_freq = 20
ch_names = ["FP1","FP2","F3","F4","C3","C4","P3","P4","O1","O2","F7","F8","T3","T4","T5","T6","Fz","Cz","Pz"]

data = mne.io.read_raw_edf("./0629.edf")

data.rename_channels(ch_names)
data.filter(l_freq, h_freq)
print(data.info)

This is the error for changing the channel.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/var/folders/h7/03ld7ngj5wj0p_fbv23g528c0000gn/T/ipykernel_64428/2163577672.py in 
---> 10 data.rename_channels(ch_names)
     11 # data.filter(l_freq, h_freq)
     12 print(data.info)

 in rename_channels(self, mapping, allow_duplicates, verbose)

/opt/anaconda3/lib/python3.9/site-packages/mne/channels/channels.py in rename_channels(self, mapping, allow_duplicates, verbose)
    412 
    413         ch_names_orig = list(self.info['ch_names'])
--> 414         rename_channels(self.info, mapping, allow_duplicates)
    415 
    416         # Update self._orig_units for Raw

 in rename_channels(info, mapping, allow_duplicates, verbose)

/opt/anaconda3/lib/python3.9/site-packages/mne/channels/channels.py in rename_channels(info, mapping, allow_duplicates, verbose)
   1094                      for ci, ch_name in enumerate(ch_names)]
   1095     else:
-> 1096         raise ValueError('mapping must be callable or dict, not %s'
   1097                          % (type(mapping),))
   1098 

ValueError: mapping must be callable or dict, not 

Next is the error for the filter.

RuntimeError                              Traceback (most recent call last)
/var/folders/h7/03ld7ngj5wj0p_fbv23g528c0000gn/T/ipykernel_64428/2415822929.py in 
        10 # data.rename_channels(ch_names)
---> 11 data.filter(l_freq = 1, h_freq = 20)
     12 print(data.info)
     13 

/opt/anaconda3/lib/python3.9/site-packages/mne/io/base.py in filter(self, l_freq, h_freq, picks, filter_length, l_trans_bandwidth, h_trans_bandwidth, n_jobs, method, iir_params, phase, fir_window, fir_design, skip_by_annotation, pad, verbose)
    975                skip_by_annotation=('edge', 'bad_acq_skip'),
    976                pad='reflect_limited', verbose=None):  # noqa: D102
--> 977         return super().filter(
    978             l_freq, h_freq, picks, filter_length, l_trans_bandwidth,
    979             h_trans_bandwidth, n_jobs=n_jobs, method=method,

 in filter(self, l_freq, h_freq, picks, filter_length, l_trans_bandwidth, h_trans_bandwidth, n_jobs, method, iir_params, phase, fir_window, fir_design, skip_by_annotation, pad, verbose)

/opt/anaconda3/lib/python3.9/site-packages/mne/filter.py in filter(self, l_freq, h_freq, picks, filter_length, l_trans_bandwidth, h_trans_bandwidth, n_jobs, method, iir_params, phase, fir_window, fir_design, skip_by_annotation, pad, verbose)
   2004         """
   2005         from .io.base import BaseRaw
-> 2006         _check_preload(self, 'inst.filter')
   2007         if pad is None and method != 'iir':
   2008             pad = 'edge'
...
--> 283             raise RuntimeError(
    284                 "By default, MNE does not load data into main memory to "
    285                 "conserve resources. " + msg + ' requires %s data to be '

RuntimeError: By default, MNE does not load data into main memory to conserve resources. inst.filter requires raw data to be loaded. Use preload=True (or string) in the constructor or raw.load_data().
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

Thank you in advance for your help.

Hello,

Please have a bare minimum look through the documentation, I’m sure you can find the information you need in there. You can find an example about changing channel names and types in the second introductory tutorial: The Raw data structure: continuous data — MNE 1.4.2 documentation

The Raw object and all its associated methods is documented in the API Reference page, specifically, here.

The rename_channels method is documented here. The input is a dictionary or a callable, as the error is also mentioning, thus you should provide a dictionary mapping the old channel name to the new channel name.

You also have a search function through the website (magnifying glass at the top right or Ctrl+K shortcut) which will get you to the pages you need.

Best,
Mathieu

1 Like

Thank you for your reply.

As you pointed out, when I created a dict including before the change and executed it, it worked.
Thank you for your help!

Regarding the filter, is it possible to change the highpass and lowpass of the raw data itself?

Regarding the filter, is it possible to change the highpass and lowpass of the raw data itself?

I do not understand this question. Filtering will alter the underlying data array of shape (n_channels, n_samples).

Mathieu