# Setup for reading the raw data raw = fiff.Raw(raw_fname, verbose=False) events = mne.find_events(raw, stim_channel='STI 014') inverse_operator = read_inverse_operator(fname_inv) raw.info['bads'] = ['MEG 2443', 'EEG 053'] # picks MEG gradiometers picks = fiff.pick_types(raw.info, meg=True, eeg=False, eog=True, stim=False, exclude='bads') tmin, tmax = 0, 120 # use the first 120s of data fmin, fmax = 4, 100 # look at frequencies between 4 and 100Hz NFFT = 2048 # the FFT size (NFFT). Ideally a power of 2 label = mne.read_label(fname_label) stc = compute_source_psd(raw, inverse_operator, lambda2=1. / 9., method="dSPM", tmin=tmin, tmax=tmax, fmin=fmin, fmax=fmax, pick_ori="normal", NFFT=NFFT, label=label) stc.save('psd_dSPM') ############################################################################### # View PSD of sources in label import matplotlib.pyplot as plt plt.plot(1e3 * stc.times, stc.data.T) plt.xlabel('Frequency (Hz)') plt.ylabel('PSD (dB)') plt.title('Source Power Spectrum (PSD)') plt.show()
eeg=False, eog=True, stim=False, exclude='bads') tmin, tmax = 0, 120 # use the first 120s of data fmin, fmax = 4, 100 # look at frequencies between 4 and 100Hz n_fft = 2048 # the FFT size (n_fft). Ideally a power of 2 label = mne.read_label(fname_label) stc = compute_source_psd(raw, inverse_operator, lambda2=1. / 9., method="dSPM", tmin=tmin, tmax=tmax, fmin=fmin, fmax=fmax, pick_ori="normal", n_fft=n_fft, label=label, dB=True) stc.save('psd_dSPM') ############################################################################### # View PSD of sources in label plt.plot(1e3 * stc.times, stc.data.T) plt.xlabel('Frequency (Hz)') plt.ylabel('PSD (dB)') plt.title('Source Power Spectrum (PSD)') plt.show()
eeg=False, eog=True, stim=False, exclude='bads') tmin, tmax = 0, 120 # use the first 120s of data fmin, fmax = 4, 100 # look at frequencies between 4 and 100Hz NFFT = 2048 # the FFT size (NFFT). Ideally a power of 2 label = mne.read_label(fname_label) stc = compute_source_psd(raw, inverse_operator, lambda2=1. / 9., method="dSPM", tmin=tmin, tmax=tmax, fmin=fmin, fmax=fmax, pick_normal=True, NFFT=NFFT, label=label) stc.save('psd_dSPM') ############################################################################### # View PSD of sources in label import pylab as pl pl.plot(1e3 * stc.times, stc.data.T) pl.xlabel('Frequency (Hz)') pl.ylabel('PSD (dB)') pl.title('Source Power Spectrum (PSD)')
def intra(subj_list, fmin, fmax): ''' Performs main process, including generation of inverse solution and PSD computation. ''' for subj in subj_list: print('Now beginning intra processing on ' + subj + '...\n') * 5 # Set function parameters fname_raw = data_path + subj[:5] + '/' + subj fname_fwd = data_path + subj[:5] + '/' + subj[:-4] + '-ico-4-fwd.fif' # Load data raw = fiff.Raw(fname_raw) forward_meg = mne.read_forward_solution(fname_fwd) # Estimate noise covariance from the raw data precov = mne.compute_raw_data_covariance(raw, reject=dict(eog=150e-6)) write_cov(data_path + subj[:5] + '/' + subj[:-4] + '-cov.fif', precov) # Find events from raw file events = mne.find_events(raw, stim_channel='STI 014') # Write events to file mne.write_events(data_path + subj[:5] + '/' + subj[:-4] + '-eve.fif', events) # Set up pick list: include = [] exclude = raw.info['bads'] picks = fiff.pick_types(raw.info, meg=True, eeg=False, stim=True, eog=True, include=include, exclude=exclude) # Read epochs and remove bad epochs epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=(None, 0), preload=True, reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6)) # Average epochs to produce an evoked dataset, then write to disk evoked = epochs.average() evoked.save(data_path + subj[:5] + '/' + subj[:-4] + '-ave.fif') # Regularize noise cov cov = mne.cov.regularize(precov, evoked.info, grad=0.05, mag=0.05, eeg=0.1, proj=True) # Restrict forward solution as necessary for MEG restricted_fwd = mne.fiff.pick_types_forward(forward_meg, meg=True, eeg=False) # Make inverse operator info = evoked.info inverse_operator = make_inverse_operator(info, restricted_fwd, cov, loose=None, depth=0.8) # Pull data for averaging later epc_array = epochs.get_data() # Compute the inverse solution inv = apply_inverse(evoked, inverse_operator, lambda2, "dSPM", pick_normal=False) inv.save(data_path + subj[:5] + '/' + subj[:-4] + '-inv.fif') # picks MEG gradiometers picks = fiff.pick_types(raw.info, meg=True, eeg=False, eog=True, stim=False, exclude=exclude) # Compute source power spectral density and save to file psd = compute_source_psd(raw, inverse_operator, method='dSPM', lambda2=lambda2, fmin=fmin, fmax=fmax, NFFT=2048) psd.save(data_path + subj[:5] + '/' + subj[:-4] + '-psd.fif')
trans, src=src, bem=bem, eeg=False, verbose=True) # Compute and apply inverse to PSD estimated using multitaper + Welch noise_cov = mne.compute_raw_covariance(raw_erm, n_jobs=18) inverse_operator = make_inverse_operator(raw.info, forward=fwd, noise_cov=noise_cov, verbose=True) stc_psd, evoked_psd = compute_source_psd(raw, inverse_operator, method='MNE', dB=False, return_sensor=True, low_bias=True, adaptive=True, n_jobs=config.N_JOBS, n_fft=n_fft, verbose=True) stc_psds.append(stc_psd) evoked_psds.append(evoked_psd) grand_ave_stc /= len(stc_psds) grand_ave_evo = mne.grand_average(evoked_psds) topos = dict() stcs = dict() topo_norm = grand_ave_evo.data.sum(axis=1, keepdims=True) stc_norm = grand_ave_stc.sum() # Normalize each sensor/source by the total power across freqs for band, limits in defaults.bands.items(): data = evoked_psd.copy().crop(*limits).data.sum(axis=1, keepdims=True)