Filtering in MNE python

Hi all,

I've been trying to filter raw data in MNE python and am getting the error
message: AttributeError: 'Raw' object has no attribute 'ndim'. The
tutorial exercises have run perfectly for me, using both the sample data
and my own data, so the most likely cause is user error. The commands in
ipython are:

import numpy as np
from mne import fiff
from mne import filter
data_path =
'/usr/lib/python2.6/site-packages/mne-0.2.git-py2.6.egg/mne/datasets/sample/MNE-sample-data'
raw_fname = data_path + '/MEG/sample/sample_audvis_raw.fif'
raw = fiff.Raw(raw_fname)
raw = filter.band_pass_filter(raw, 1000, 50, 1, filter_length=2048)

What's the correct way to do this? From the error, I'd guess that I'm
either passing the wrong part the array to the filter, or skipping some
important step. I'm using mne-tools-mne-python-v0.2-70-g9a72ede in python
2.6, on a machine running 64-bit CentOS 6. The complete error message is
below.

Thanks for any suggestions,

Jon Houck

/usr/lib/python2.6/site-packages/mne-0.2.git-py2.6.egg/mne/filter.pyc in
band_pass_filter(x, Fs, Fp1, Fp2, filter_length)
    252
    253 xf = _filter(x, Fs, [0, Fs1, Fp1, Fp2, Fs2, Fs / 2], [0, 0, 1,
1, 0, 0],
--> 254 filter_length)
    255
    256 return xf

/usr/lib/python2.6/site-packages/mne-0.2.git-py2.6.egg/mne/filter.pyc in
_filter(x, Fs, freq, gain, filter_length)
    162 x filtered
    163 """
--> 164 assert x.ndim == 1
    165
    166 # normalize frequencies

AttributeError: 'Raw' object has no attribute 'ndim'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20120203/d332e3cc/attachment.html

Jon,

mne-python currently doesn't support direct filtering operations on Raw
objects; the filtering operations only support 1D ndarrays i.e., a
single time series, as input.

Using the latest mne-python version from github, you could do what you
want as follows:

raw = fiff.Raw(raw_fname, preload=True)
data, times = raw[:, :]
sfreq = float(raw.info['sfreq'])
data_filtered = numpy.array([filter.band_pass_filter(x, sfreq, 50, 1,
filter_length=2048) for x in data])
raw[:, :] = data_filtered

Note that for band_pass you need Fp1 < Fp2, so the above still won't
work (I assume it should be 1, 50, 1..50Hz).

Best,

Martin

Martin,

This was helpful, thanks. On the first pass I got a new error message,
"AttributeError: 'module' object has no attribute 'firwin2'.". Updating
from scipy 0.7.2 to numpy 1.6.1 and scipy 0.10.0 fixed this, and it works
perfectly now.

Regards,

Jon

Jon,

I'm glad you got it working. With the latest version of mne-python from
github you can now filter Raw objects directly.

Assuming you would like to apply a band-pass from 1 to 50Hz with a
filter length of 2048 to all MEG channels, you would use:

raw = fiff.Raw(raw_fname, preload=True)
picks = mne.fiff.pick_types(raw.info, meg=True)

raw.band_pass_filter(picks, 1.0, 50.0, filter_length=2048)

This will apply the band-pass to all channels in "picks". If you have a
fast computer with enough RAM and you can use

raw.band_pass_filter(picks, 1.0, 50.0, filter_length=2048, n_jobs=8)

which will run the filtering in parallel on 8 CPUs.

I hope this is helpful to you.

Best,

Martin