def task_csd(): """Step 05: Compute cross-spectral density (CSD) matrices""" for subject in subjects: epo_fname = fname.epo(subject=subject) csd_fnames = [fname.csd(subject=subject, condition=cond) for cond in conditions + ['baseline']] yield dict( name=subject, task_dep=['epochs'], file_dep=[epo_fname, '05_csd.py'], targets=csd_fnames, actions=['python 05_csd.py %s' % subject], )
def task_figures(): """Make all figures. Each figure is a sub-task.""" # Make figure 1: plot of the CSD matrices. yield dict( name='csd', task_dep=['connectivity_stats'], file_dep=[fname.epo(subject=subjects[0]), fname.csd(subject=subjects[0], condition='face')], targets=['../paper/figures/csd.pdf'], actions=['python figure_csd.py'], ) # Make figure 2: plot of the source space and forward model. yield dict( name='forward', file_dep=[fname.fwd(subject=subjects[0]), fname.fwd_r(subject=subjects[0]), fname.trans(subject=subjects[0])], targets=['../paper/figures/forward1.png', '../paper/figures/forward2.png'], actions=['python figure_forward.py'], ) # Make figure 3: grand average power maps. file_dep = [fname.ga_power_hemi(condition=cond, hemi='lh') for cond in conditions] file_dep += [fname.ga_power_hemi(condition=cond, hemi='rh') for cond in conditions] targets = ['../paper/figures/power_face_lh.png', '../paper/figures/power_face_rh.png', '../paper/figures/power_scrambled_lh.png', '../paper/figures/power_scrambled_rh.png'] targets += ['../paper/figures/power_contrast_%s-%s-lh.png' % (freq[0], freq[1]) for freq in freq_bands] yield dict( name='power', file_dep=file_dep, targets=targets, actions=['python figure_power.py'], ) # Make figure 4: plot of the functional connectivity. yield dict( name='connectivity', file_dep=[fname.ga_con(condition='pruned'), fname.ga_con(condition='parcelled')], targets=['../paper/figures/degree_lh.png', '../paper/figures/degree_rh.png', '../paper/figures/squircle.pdf'], actions=['python figure_connectivity.py'], )
def task_connectivity(): """Step 10: Compute DICS connectivity.""" for subject in subjects: fwd_r_fname = fname.fwd_r(subject=subject) csd_fnames = [] con_fnames = [] for cond in conditions: csd_fnames.append(fname.csd(subject=subject, condition=cond)) con_fnames.append(fname.con(subject=subject, condition=cond)) yield dict( name=subject, task_dep=['power'], file_dep=[fwd_r_fname, fname.pairs, '10_connectivity.py'] + csd_fnames, targets=con_fnames, actions=['python 10_connectivity.py %s' % subject], )
def task_power(): """Step 09: Compute DICS power maps.""" for subject in subjects: fwd_r_fname = fname.fwd_r(subject=subject) csd_fnames = [] stc_fnames = [] for cond in conditions + ['baseline']: csd_fnames.append(fname.csd(subject=subject, condition=cond)) stc_fnames.append(fname.power_hemi(subject=subject, condition=cond, hemi='lh')) stc_fnames.append(fname.power_hemi(subject=subject, condition=cond, hemi='rh')) yield dict( name=subject, task_dep=['select_vertices'], file_dep=[fwd_r_fname, '09_power.py'] + csd_fnames, targets=stc_fnames, actions=['python 09_power.py %s' % subject], )
for condition in conditions: print('Condition:', condition) # Remove the mean during the time interval for which we compute the CSD epochs_baselined = epochs[condition].apply_baseline((csd_tmin, csd_tmax)) # Compute CSD for the desired time interval csd = csd_morlet(epochs_baselined, frequencies=frequencies, tmin=csd_tmin, tmax=csd_tmax, decim=20, n_jobs=n_jobs, verbose=True) # Save the CSD matrices csd.save(fname.csd(condition=condition, subject=subject)) report.add_figs_to_section(csd.plot(show=False), ['CSD for %s' % condition], section='Sensor-level') # Also compute the CSD for the baseline period (use all epochs for this, # regardless of condition). This way, we can compare the change in power caused # by the presentation of the stimulus. epochs = epochs.apply_baseline((-0.2, 0)) # Make sure data is zero-mean csd_baseline = csd_morlet(epochs, frequencies=frequencies, tmin=-0.2, tmax=0, decim=20, n_jobs=n_jobs, verbose=True)
from matplotlib import pyplot as plt import mne from mne.time_frequency import read_csd, pick_channels_csd from config import fname, subjects, freq_bands info = mne.io.read_info(fname.epo(subject=subjects[0])) grads = [info['ch_names'][ch] for ch in mne.pick_types(info, meg='grad')] csd = read_csd(fname.csd(subject=subjects[0], condition='face')) csd = pick_channels_csd(csd, grads) csd = csd.mean([f[0] for f in freq_bands], [f[1] for f in freq_bands]) # Plot theta, alpha, low beta csd[:3].plot(info, n_cols=3, show=False) plt.savefig('../paper/figures/csd1.pdf', bbox_inches='tight') # Plot high beta 1, high beta 2 and low gamma csd[3:].plot(info, n_cols=3, show=False) plt.savefig('../paper/figures/csd2.pdf', bbox_inches='tight')
# Read the forward model fwd = mne.read_forward_solution(fname.fwd(subject=subject)) # Read the info structure info = mne.io.read_info(fname.epo(subject=subject)) # Compute source power for all frequency bands and all conditions fmin = [f[0] for f in freq_bands] fmax = [f[1] for f in freq_bands] csds = dict() for condition in conditions + ['baseline']: print('Reading CSD matrix for condition:', condition) # Read the CSD matrix csds[condition] = read_csd(fname.csd(condition=condition, subject=subject)) # Average the CSDs of both conditions csd = csds['face'].copy() csd._data += csds['scrambled']._data csd._data /= 2 # Compute the DICS beamformer using the CSDs from both conditions, for all # frequency bands. filters = make_dics(info=info, forward=fwd, csd=csd.mean(fmin, fmax), reg=reg, pick_ori='max-power') stcs = dict() for condition in conditions + ['baseline']: print('Computing power for condition:', condition) stcs[condition], _ = apply_dics_csd(csds[condition].mean(fmin, fmax),