[Mne_analysis] trouble filtering ECG channel data

Megan Schendel mschendel at mrn.org
Mon Apr 25 16:19:28 EDT 2016
Search archives:

Hi Jon/ all,

It seems to work if I modify the picks line to
     picks = mne.pick_types(raw.info, meg =False,  ecg = True)
and then include picks in my filter statements.

Thanks a bunch!
Megan

MEG Technician
The Mind Research Network
1101 Yale Blvd. NE
Albuquerque, New Mexico 87106
505-272-3304


On Mon, Apr 25, 2016 at 2:07 PM, Megan Schendel <mschendel at mrn.org> wrote:

> Hi Jon!
> Thanks for your help!
>
> I'm a bit confused on what to do with the picks = line.  I tried putting
> it before my if statement, and adding picks to my notch statement, but I
> got a different error.  I also tried adding picks = line inside the if
> statement, before I did raw.pick_channels, but I get the same error.
>
> It seems like using picks inside the filter might be redundant with
> creating a raw object with raw.pick_channels.... ? Or I'm not using
> something correctly.  I'd very much appreciate a little more help.
>
> Thanks again,
> Megan
>
> #
> # attempt one, set picks prior to if
> #
> In [8]: picks = mne.pick_types(raw.info, ecg = True)
>
> In [9]: if 'EEG063' in raw.ch_names:
>    ...:     raw_bits = raw.pick_channels(['EEG063'])
>    ...:     print ('Found EEG063 ')
>    ...:
>
> In [10]: if 'ECG063' in raw.ch_names:
>    ....:     raw_bits = raw.pick_channels(['ECG063'])
>    ....:     print ('Found ECG063 ')
>    ....:
> Found ECG063
>
> In [11]: raw_bits_orig = raw_bits.copy()
>
> In [12]: raw_bits.notch_filter(freqs = numpy.arange(60, 241, 60),
> picks=picks )
> # Nasty error, which I've pasted at the end of my code
>
> #
> # attempt 2, after I used io.Raw again to make sure I had what I started
> with
> # picks inside if, prior to raw.pick_channels
> #
> In [17]: if 'ECG063' in raw.ch_names:
>    ....:     picks = mne.pick_types(raw.info, ecg=True)
>    ....:     raw_bits = raw.pick_channels(['ECG063'])
>    ....:     print ('Found ECG063 ')
>    ....:
> Found ECG063
>
> In [18]: raw_bits_orig = raw_bits.copy()
>
> In [19]: raw_bits.notch_filter(freqs = numpy.arange(60, 241, 60),
> picks=picks )
>
> #
> # In both cases, the error I got:
> #
>
> ---------------------------------------------------------------------------
> IndexError                                Traceback (most recent call last)
> <ipython-input-19-59ffe03b1d2b> in <module>()
> ----> 1 raw_bits.notch_filter(freqs = numpy.arange(60, 241, 60),
> picks=picks )
>
> /export/research/analysis/human/jstephen/shared/programs/python/anaconda3/lib/python3.5/site-packages/mne/io/base.py
> in notch_filter(self, freqs, picks, filter_length, notch_widths,
> trans_bandwidth, n_jobs, method, iir_params, mt_bandwidth, p_value, verbose)
>
> /export/research/analysis/human/jstephen/shared/programs/python/anaconda3/lib/python3.5/site-packages/mne/utils.py
> in verbose(function, *args, **kwargs)
>     549         finally:
>     550             set_log_level(old_level)
> --> 551     return function(*args, **kwargs)
>     552
>     553
>
> /export/research/analysis/human/jstephen/shared/programs/python/anaconda3/lib/python3.5/site-packages/mne/io/base.py
> in notch_filter(self, freqs, picks, filter_length, notch_widths,
> trans_bandwidth, n_jobs, method, iir_params, mt_bandwidth, p_value, verbose)
>     945                                   method=method,
> iir_params=iir_params,
>     946                                   mt_bandwidth=mt_bandwidth,
> p_value=p_value,
> --> 947                                   picks=picks, n_jobs=n_jobs,
> copy=False)
>     948
>     949     @verbose
>
> /export/research/analysis/human/jstephen/shared/programs/python/anaconda3/lib/python3.5/site-packages/mne/filter.py
> in notch_filter(x, Fs, freqs, filter_length, notch_widths, trans_bandwidth,
> method, iir_params, mt_bandwidth, p_value, picks, n_jobs, copy, verbose)
>
> /export/research/analysis/human/jstephen/shared/programs/python/anaconda3/lib/python3.5/site-packages/mne/utils.py
> in verbose(function, *args, **kwargs)
>     549         finally:
>     550             set_log_level(old_level)
> --> 551     return function(*args, **kwargs)
>     552
>     553
>
> /export/research/analysis/human/jstephen/shared/programs/python/anaconda3/lib/python3.5/site-packages/mne/filter.py
> in notch_filter(x, Fs, freqs, filter_length, notch_widths, trans_bandwidth,
> method, iir_params, mt_bandwidth, p_value, picks, n_jobs, copy, verbose)
>    1105                  for freq, nw in zip(freqs, notch_widths)]
>    1106         xf = band_stop_filter(x, Fs, lows, highs, filter_length,
> tb_2, tb_2,
> -> 1107                               method, iir_params, picks, n_jobs,
> copy)
>    1108     elif method == 'spectrum_fit':
>    1109         xf = _mt_spectrum_proc(x, Fs, freqs, notch_widths,
> mt_bandwidth,
>
> /export/research/analysis/human/jstephen/shared/programs/python/anaconda3/lib/python3.5/site-packages/mne/filter.py
> in band_stop_filter(x, Fs, Fp1, Fp2, filter_length, l_trans_bandwidth,
> h_trans_bandwidth, method, iir_params, picks, n_jobs, copy, verbose)
>
> /export/research/analysis/human/jstephen/shared/programs/python/anaconda3/lib/python3.5/site-packages/mne/utils.py
> in verbose(function, *args, **kwargs)
>     549         finally:
>     550             set_log_level(old_level)
> --> 551     return function(*args, **kwargs)
>     552
>     553
>
> /export/research/analysis/human/jstephen/shared/programs/python/anaconda3/lib/python3.5/site-packages/mne/filter.py
> in band_stop_filter(x, Fs, Fp1, Fp2, filter_length, l_trans_bandwidth,
> h_trans_bandwidth, method, iir_params, picks, n_jobs, copy, verbose)
>     786         if np.any(np.abs(np.diff(gain, 2)) > 1):
>     787             raise ValueError('Stop bands are not sufficiently
> separated.')
> --> 788         xf = _filter(x, Fs, freq, gain, filter_length, picks,
> n_jobs, copy)
>     789     else:
>     790         for fp_1, fp_2, fs_1, fs_2 in zip(Fp1, Fp2, Fs1, Fs2):
>
> /export/research/analysis/human/jstephen/shared/programs/python/anaconda3/lib/python3.5/site-packages/mne/filter.py
> in _filter(x, Fs, freq, gain, filter_length, picks, n_jobs, copy)
>     372         h = firwin2(N, freq, gain, window='hann')
>     373         x = _overlap_add_filter(x, h, zero_phase=True, picks=picks,
> --> 374                                 n_jobs=n_jobs)
>     375
>     376     x.shape = orig_shape
>
> /export/research/analysis/human/jstephen/shared/programs/python/anaconda3/lib/python3.5/site-packages/mne/filter.py
> in _overlap_add_filter(x, h, n_fft, zero_phase, picks, n_jobs)
>     140     if n_jobs == 1:
>     141         for p in picks:
> --> 142             x[p] = _1d_overlap_filter(x[p], h_fft, n_h, n_edge,
> zero_phase,
>     143                                       cuda_dict)
>     144     else:
>
> IndexError: index 1 is out of bounds for axis 0 with size 1
>
>
>
>
> MEG Technician
> The Mind Research Network
> 1101 Yale Blvd. NE
> Albuquerque, New Mexico 87106
> 505-272-3304
>
>
> On Fri, Apr 22, 2016 at 2:24 PM, Jon Houck <jhouck at mrn.org> wrote:
>
>> Hi Megan,
>>
>> Notch_filter will only filter the MEG/EEG channels unless you specify
>> some channels using picks.  You could do something like:
>>
>> picks = mne.pick_types(raw.info, ecg=True)
>> raw.notch_filter(freqs = np.arange(60, 241, 60), picks=picks )
>>
>> which will then find and filter the ECG data.  I think if you added that
>> to your notch_filter and added the picks lines for EEG and ECG in your if
>> statement, you'd be good to go.
>>
>> Jon
>>
>>
>>
>>
>>
>>
>> On Fri, Apr 22, 2016 at 11:49 AM, Megan Schendel <mschendel at mrn.org>
>> wrote:
>>
>>> Hello all,
>>> I have written a little script that pulls the ECG channel data out of
>>> the fif file and writes a text file to pass to QRSTool (free external
>>> software).
>>>
>>> But I have some fif files with the ECG data on channel EEG063 and some
>>> on ECG063.  My script works on the channel labeled EEG063, but it fails for
>>> the ECG063.  Specifically, I use raw.pick to get the channel I want.  I can
>>> plot it fine, it returns type mne.io.fiff.raw.RawFIF, but when I try to
>>> filter using raw.notch_filter, I get a weird error.  See below for the
>>> relevant snippets of code and outputs.
>>>
>>> I just discovered the raw.set_channel_types method and wonder if that
>>> could help, but I'm having trouble getting the syntax right...
>>>
>>> (also, btw, mne.sys.info() gives me an error... so I can't tell what
>>> mne version I'm running.  I did attempt git pull master origin and I'm
>>> still having the problem with raw.filter.)
>>>
>>> Thanks much in advance,
>>> Megan
>>>
>>> [mschendel at XXXX ~]$ ipython
>>> Python 3.5.1 |Anaconda 2.5.0 (64-bit)| (default, Dec  7 2015, 11:16:01)
>>> In [1]: import mne
>>> In [2]: mne.sys.info()
>>>
>>> ---------------------------------------------------------------------------
>>> AttributeError                            Traceback (most recent call
>>> last)
>>> <ipython-input-2-aa9bd53b86f1> in <module>()
>>> ----> 1 mne.sys.info()
>>>
>>> AttributeError: module 'mne' has no attribute 'sys'
>>>
>>> #
>>> # Here is my io.Raw command... no trouble there...
>>> #
>>>
>>> In [21]: raw = io.Raw(infile, preload=True, verbose=None)
>>> # Cut all the io reporting...
>>>
>>> In [22]: if 'EEG063' in raw.ch_names:
>>>    ....:     raw_bits = raw.pick_channels(['EEG063'])
>>>    ....:     print ('Found EEG063 ')
>>>    ....: elif 'ECG063' in raw.ch_names:
>>>    ....:     raw_bits = raw.pick_channels(['ECG063'])
>>>    ....:     print ('Found ECG063 ')
>>>    ....:
>>> Found EEG063
>>>
>>> In [23]: raw.notch_filter(freqs = numpy.arange(60, 241, 60) )
>>>
>>> ---------------------------------------------------------------------------
>>> RuntimeError                              Traceback (most recent call
>>> last)
>>> <ipython-input-13-f5e5440dff87> in <module>()
>>> ----> 1 raw.notch_filter(freqs = numpy.arange(60, 241, 60) )
>>>
>>> /export/research/analysis/human/jstephen/shared/programs/python/anaconda3/lib/python3.5/site-packages/mne/io/base.py
>>> in notch_filter(self, freqs, picks, filter_length, notch_widths,
>>> trans_bandwidth, n_jobs, method, iir_params, mt_bandwidth, p_value, verbose)
>>>
>>> /export/research/analysis/human/jstephen/shared/programs/python/anaconda3/lib/python3.5/site-packages/mne/utils.py
>>> in verbose(function, *args, **kwargs)
>>>     549         finally:
>>>     550             set_log_level(old_level)
>>> --> 551     return function(*args, **kwargs)
>>>     552
>>>     553
>>>
>>> /export/research/analysis/human/jstephen/shared/programs/python/anaconda3/lib/python3.5/site-packages/mne/io/base.py
>>> in notch_filter(self, freqs, picks, filter_length, notch_widths,
>>> trans_bandwidth, n_jobs, method, iir_params, mt_bandwidth, p_value, verbose)
>>>     932             # let's be safe.
>>>     933             if len(picks) < 1:
>>> --> 934                 raise RuntimeError('Could not find any valid
>>> channels for '
>>>     935                                    'your Raw object. Please
>>> contact the '
>>>     936                                    'MNE-Python developers.')
>>>
>>> RuntimeError: Could not find any valid channels for your Raw object.
>>> Please contact the MNE-Python developers.
>>>
>>> #
>>> # Try to remap channel to EEG type...
>>> #
>>> In [30]: raw_bits.set_channel_types(mapping = {'ECG063': ‘eeg’})
>>>   File "<ipython-input-30-1c0e7f2a2c6d>", line 1
>>>     raw_bits.set_channel_types(mapping = {'ECG063': ‘eeg’})
>>>                                                         ^
>>> SyntaxError: invalid character in identifier
>>>
>>>
>>>
>>> #
>>> #   If I do the same with data that has channel labeled as EEG ...
>>> #
>>> In [5]: if 'EEG063' in raw.ch_names:
>>>     raw_bits = raw.pick_channels(['EEG063'])
>>>     print ('Found EEG063 ')
>>> elif 'ECG063' in raw.ch_names:
>>>     raw_bits = raw.pick_channels(['ECG063'])
>>>     print ('Found ECG063 ')
>>>    ...:
>>> Found EEG063
>>> In [6]: raw_bits.notch_filter(freqs = numpy.arange(60, 241, 60) )
>>>
>>> In [7]: raw_bits.notch_filter(freqs = numpy.arange(60, 241, 60) )
>>> #  no complaints...
>>>
>>>
>>>
>>> MEG Technician
>>> The Mind Research Network
>>> 1101 Yale Blvd. NE
>>> Albuquerque, New Mexico 87106
>>> 505-272-3304
>>>
>>>
>>> _______________________________________________
>>> Mne_analysis mailing list
>>> Mne_analysis at nmr.mgh.harvard.edu
>>> https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis
>>>
>>>
>>> The information in this e-mail is intended only for the person to whom
>>> it is
>>> addressed. If you believe this e-mail was sent to you in error and the
>>> e-mail
>>> contains patient information, please contact the Partners Compliance
>>> HelpLine at
>>> http://www.partners.org/complianceline . If the e-mail was sent to you
>>> in error
>>> but does not contain patient information, please contact the sender and
>>> properly
>>> dispose of the e-mail.
>>>
>>>
>>
>>
>> --
>> Jon M. Houck, Ph.D.
>> Assistant Professor of Translational Neuroscience
>> Mind Research Network
>> 1101 Yale Blvd. NE
>> Albuquerque, NM 87106
>>
>>
>> _______________________________________________
>> Mne_analysis mailing list
>> Mne_analysis at nmr.mgh.harvard.edu
>> https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis
>>
>>
>> The information in this e-mail is intended only for the person to whom it
>> is
>> addressed. If you believe this e-mail was sent to you in error and the
>> e-mail
>> contains patient information, please contact the Partners Compliance
>> HelpLine at
>> http://www.partners.org/complianceline . If the e-mail was sent to you
>> in error
>> but does not contain patient information, please contact the sender and
>> properly
>> dispose of the e-mail.
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20160425/71c9ce52/attachment-0001.html 


More information about the Mne_analysis mailing list