示例#1
0
def compute_noise_cov(subject, hcp_path, noise_cov_fname=''):
    if noise_cov_fname == '':
        noise_cov_fname = meg.NOISE_COV.format(cond='empty_room')
    if op.isfile(noise_cov_fname):
        noise_cov = mne.read_cov(noise_cov_fname)
        return noise_cov
    utils.make_dir(utils.get_parent_fol(noise_cov_fname))

    raw_noise = hcp.read_raw(subject=subject,
                             hcp_path=hcp_path,
                             data_type='noise_empty_room')
    raw_noise.load_data()
    # apply ref channel correction and drop ref channels
    preproc.apply_ref_correction(raw_noise)
    raw_noise.filter(0.50,
                     None,
                     method='iir',
                     iir_params=dict(order=4, ftype='butter'),
                     n_jobs=1)
    raw_noise.filter(None,
                     60,
                     method='iir',
                     iir_params=dict(order=4, ftype='butter'),
                     n_jobs=1)
    ##############################################################################
    # Note that using the empty room noise covariance will inflate the SNR of the
    # evkoked and renders comparisons  to `baseline` rather uninformative.
    noise_cov = mne.compute_raw_covariance(raw_noise, method='empirical')
    noise_cov.save(noise_cov_fname)
    return noise_cov
def test_apply_ref_correction():
    """Test reference correction."""
    raw = hcp.read_raw(data_type='rest', run_index=0, **hcp_params)
    # raw.crop(0, 10).load_data()
    raw.load_data()
    # XXX terrible hack to have more samples.
    # The test files are too short.
    raw.append(raw.copy())
    meg_picks = mne.pick_types(raw.info, meg=True, ref_meg=False)
    orig = raw[meg_picks[0]][0][0]
    apply_ref_correction(raw)
    proc = raw[meg_picks[0]][0][0]
    assert_true(np.linalg.norm(orig) > np.linalg.norm(proc))
示例#3
0
def test_apply_ref_correction():
    """Test reference correction."""
    raw = hcp.read_raw(data_type='rest', run_index=0, **hcp_params)
    # raw.crop(0, 10).load_data()
    raw.load_data()
    # XXX terrible hack to have more samples.
    # The test files are too short.
    raw.append(raw.copy())
    meg_picks = mne.pick_types(raw.info, meg=True, ref_meg=False)
    orig = raw[meg_picks[0]][0][0]
    apply_ref_correction(raw)
    proc = raw[meg_picks[0]][0][0]
    assert_true(np.linalg.norm(orig) > np.linalg.norm(proc))
示例#4
0
文件: comp_fun.py 项目: wronk/rsn
def preproc_annot_filter(subj_fold, raw, hcp_params):
    """Helper to annotate bad segments and apply Butterworth freq filtering"""

    # apply ref channel correction
    preproc.apply_ref_correction(raw)

    # construct MNE annotations
    annots = hcp.read_annot(**hcp_params)
    bad_seg = (annots['segments']['all']) / raw.info['sfreq']
    annotations = mne.Annotations(bad_seg[:, 0],
                                  (bad_seg[:, 1] - bad_seg[:, 0]),
                                  description='bad')

    raw.annotations = annotations

    # Read all bad channels (concatenated from all runs)
    bad_ch_file = op.join(hcp_path, subj_fold, 'unprocessed', 'MEG',
                          'prebad_chans.txt')
    with open(bad_ch_file, 'rb') as prebad_file:
        prebad_chs = prebad_file.readlines()
    raw.info['bads'].extend([ch.strip() for ch in prebad_chs])  # remove '\n'

    #raw.info['bads'].extend(annots['channels']['all'])
    #print('Bad channels added: %s' % annots['channels']['all'])

    # Band-pass filter (by default, only operates on MEG/EEG channels)
    # Note: MNE complains on Python 2.7
    raw.filter(filt_params['lp'],
               filt_params['hp'],
               method=filt_params['method'],
               filter_length=filt_params['filter_length'],
               l_trans_bandwidth='auto',
               h_trans_bandwidth='auto',
               phase=filt_params['phase'],
               n_jobs=-1)

    return raw, annots
示例#5
0
        info_from=dict(data_type=data_type, run_index=run_index))
    fwd = src_outputs['fwd']
    del src_outputs
    #=================================================================
    # just using baseline to compute the noise cov
    # after this, using empty room noise
    #noise_cov = mne.compute_covariance(hcp_epochs, tmax=-0.5,method=['shrunk', 'empirical'])
    #=================================================================
    # Now we can compute the noise covariance. For this purpose we will apply
    # the same filtering as was used for the computations of the ERF
    raw_noise = hcp.read_raw(subject=subject,
                             hcp_path=hcp_path,
                             data_type='noise_empty_room')
    raw_noise.load_data()
    # apply ref channel correction and drop ref channels
    preproc.apply_ref_correction(raw_noise)

    # Note: MNE complains on Python 2.7
    raw_noise.filter(0.50,
                     None,
                     method='iir',
                     iir_params=dict(order=4, ftype='butter'),
                     n_jobs=1)
    raw_noise.filter(None,
                     60,
                     method='iir',
                     iir_params=dict(order=4, ftype='butter'),
                     n_jobs=1)
    #Note that using the empty room noise covariance will inflate the SNR of the
    #evkoked and renders comparisons  to `baseline` rather uninformative.
    noise_cov = mne.compute_raw_covariance(raw_noise, method='empirical')
    src_params=dict(add_dist=False),
    info_from=dict(data_type=data_type, run_index=run_index))

fwd = src_outputs['fwd']

##############################################################################
# Now we can compute the noise covariance. For this purpose we will apply
# the same filtering as was used for the computations of the ERF in the first
# place. See also :ref:`tut_reproduce_erf`.

raw_noise = hcp.read_raw(subject=subject, hcp_path=hcp_path,
                         data_type='noise_empty_room')
raw_noise.load_data()

# apply ref channel correction and drop ref channels
preproc.apply_ref_correction(raw_noise)

# Note: MNE complains on Python 2.7
raw_noise.filter(0.50, None, method='iir',
                 iir_params=dict(order=4, ftype='butter'), n_jobs=1)
raw_noise.filter(None, 60, method='iir',
                 iir_params=dict(order=4, ftype='butter'), n_jobs=1)

##############################################################################
# Note that using the empty room noise covariance will inflate the SNR of the
# evkoked and renders comparisons  to `baseline` rather uninformative.
noise_cov = mne.compute_raw_covariance(raw_noise, method='empirical')


##############################################################################
# Now we assemble the inverse operator, project the data and show the results
示例#7
0
# put single channel aside for comparison later
chan1 = raw[meg_picks[0]][0]

# add some plotting parameter
decim_fit = 100  # we lean a purely spatial model, we don't need all samples
decim_show = 10  # we can make plotting faster
n_fft = 2**15  # let's use long windows to see low frequencies

# we put aside the time series for later plotting
x_meg = raw[meg_picks][0][:, ::decim_show].mean(0)
x_meg_ref = raw[ref_picks][0][:, ::decim_show].mean(0)

###############################################################################
# Now we apply the ref correction (in place).

apply_ref_correction(raw)

###############################################################################
# That was the easiest part! Let's now plot everything.

plt.figure(figsize=(9, 6))
plot_psd(x_meg, Fs=raw.info['sfreq'], NFFT=n_fft, label='MEG', color='black')
plot_psd(x_meg_ref,
         Fs=raw.info['sfreq'],
         NFFT=n_fft,
         label='MEG-REF',
         color='red')
plot_psd(raw[meg_picks][0][:, ::decim_show].mean(0),
         Fs=raw.info['sfreq'],
         NFFT=n_fft,
         label='MEG-corrected',
# put single channel aside for comparison later
chan1 = raw[meg_picks[0]][0]

# add some plotting parameter
decim_fit = 100  # we lean a purely spatial model, we don't need all samples
decim_show = 10  # we can make plotting faster
n_fft = 2 ** 15  # let's use long windows to see low frequencies

# we put aside the time series for later plotting
x_meg = raw[meg_picks][0][:, ::decim_show].mean(0)
x_meg_ref = raw[ref_picks][0][:, ::decim_show].mean(0)

###############################################################################
# Now we apply the ref correction (in place).

apply_ref_correction(raw)

###############################################################################
# That was the easiest part! Let's now plot everything.

plt.figure(figsize=(9, 6))
plot_psd(x_meg, Fs=raw.info['sfreq'], NFFT=n_fft, label='MEG', color='black')
plot_psd(x_meg_ref, Fs=raw.info['sfreq'], NFFT=n_fft, label='MEG-REF',
         color='red')
plot_psd(raw[meg_picks][0][:, ::decim_show].mean(0), Fs=raw.info['sfreq'],
         NFFT=n_fft, label='MEG-corrected', color='orange')

plt.legend()
plt.xticks(np.log10([0.1, 1, 10, 100]), [0.1, 1, 10, 100])
plt.xlim(np.log10([0.1, 300]))
plt.xlabel('log10(frequency) [Hz]')