Downsampling raw data from EEGlab set format

I would like to downsample by half my data to speed up computation. I am trying to use the mne.filter.resample() method, though I get a weird error:

rawDown = mne.filter.resample(raw, down=2, npad='auto')

Traceback (most recent call last):

  File "<ipython-input-5-82bba6e4fb03>", line 1, in <module>
    rawDown = mne.filter.resample(raw, down=2, npad='auto')

  File "<decorator-gen-118>", line 24, in resample

  File "/opt/anaconda3/lib/python3.8/site-packages/mne/filter.py", line 1443, in resample
    x = _check_filterable(x, 'resampled')

  File "/opt/anaconda3/lib/python3.8/site-packages/mne/filter.py", line 1377, in _check_filterable
    x = np.asanyarray(x)

  File "/opt/anaconda3/lib/python3.8/site-packages/numpy/core/_asarray.py", line 138, in asanyarray
    return array(a, dtype, copy=False, order=order, subok=True)

  File "/opt/anaconda3/lib/python3.8/site-packages/mne/io/base.py", line 770, in __getitem__
    sel, start, stop = self._parse_get_set_params(item)

  File "/opt/anaconda3/lib/python3.8/site-packages/mne/io/base.py", line 705, in _parse_get_set_params
    sel = _picks_to_idx(self.info, item[0])

  File "/opt/anaconda3/lib/python3.8/site-packages/mne/io/pick.py", line 1032, in _picks_to_idx
    raise ValueError('All picks must be < n_channels (%d), got %r'

ValueError: All picks must be < n_channels (256), got 256

I don’t understand this – is it not the method I need? Thanks!

Hello @robpetrosino, actually I’ve never used mne.filter.resample() – instead I’d always go with Raw.resample(). Mind checking this out to see if it works for you?

raw_downsampled = raw.resample(sfreq=raw.info['sfreq']/2)

That one worked!! Is it somewhere in the API? I couldn’t find it anywhere – if there isn’t, so you may want to add it.

Thanks!!

I’m not sure what you’re asking? I included a link to the documentation in my response, is that what you’re looking for?

Glad to hear this solved the issue for you!

1 Like

I was just saying – without your message, I could not have found the method in the API page one can access from the MNE webpage, so I was wondering where one could have found it without asking you guys here. (Also, the search engine of the website does not work for me for some reason).

I always go to the documentation of the data type I’m working with – in your case, Raw – and check out the available attributes and methods. Or I simply use the search function, which unfortunately seems to be broken for you (actually, is that still the case? We deployed a fix yesterday I believe…)

1 Like

Thank you for the suggestion. And yes – now the search engine works again!

1 Like

I have notice a potential bug in the resample() method. I have noticed that that, after resampling raw data together with event onsets, the usual attributes are no longer available. The helper of the function advises to call the events argument to ensure that event triggers are resampled together with the data and to avoid jitter on the event onsets.

rawDown1 = raw.resample(sfreq=raw.info['sfreq']/2, events=events)

rawDown1.info
Traceback (most recent call last):

  File "<ipython-input-22-a1b523d4836c>", line 1, in <module>
    rawDown1.info

AttributeError: 'tuple' object has no attribute 'info'

If the events argument of the function is not called, the attributes are still available.

rawDown2 = raw.resample(sfreq=raw.info['sfreq']/2)

rawDown2.info
Out[24]: 
<Info | 8 non-empty values
 bads: []
 ch_names: E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, ...
 chs: 256 EEG
 custom_ref_applied: False
 dig: 259 items (3 Cardinal, 256 EEG)
 highpass: 0.0 Hz
 lowpass: 125.0 Hz
 meas_date: unspecified
 nchan: 256
 projs: []
 sfreq: 250.0 Hz

I checked the type of the two different objects and it looks like that the presence/absence of the event argument changes the type:

type(rawDown1)
Out[36]: tuple

type(rawDown2)
Out[37]: mne.io.eeglab.eeglab.RawEEGLAB

Why is that the case?

If you pass events, Raw.resample() will return a tuple of the resampled raw data and the respective events. Also there was a mistake in my solution above: Raw.resample() works in-place, so you should run it on a copy.

Putting this all together, the correct approach for you would be:

raw_rs, events_rs = raw.copy().resample(sfreq=raw.info['sfreq']/2,
                                        events=events))
1 Like

Got it, now I understand – thanks!