def task_dipole(): """Step 5: Dipole source estimate (golden standard)""" for subject in subjects: yield dict( name=str(subject), file_dep=[ fname.epochs(subject=subject), fname.fwd(subject=subject), '05_dipole.py' ], targets=[fname.ecd(subject=subject)], actions=[f'ipython 05_dipole.py {subject:d}'], )
def task_dics(): """Step 7: DICS source estimate""" for subject in subjects: yield dict( name=str(subject), file_dep=[ fname.epochs_long(subject=subject), fname.fwd(subject=subject), fname.ecd(subject=subject), '07_dics.py' ], targets=[ fname.stc_dics(subject=subject), fname.nii_dics(subject=subject) ], actions=[f'ipython 07_dics.py {subject:d}'], )
def task_lcmv(): """Step 6: LCMV source estimate""" for subject in subjects: yield dict( name=str(subject), file_dep=[ fname.epochs(subject=subject), fname.fwd(subject=subject), fname.ecd(subject=subject), '06_lcmv.py' ], targets=[ fname.stc_lcmv(subject=subject), fname.nii_lcmv(subject=subject) ], actions=[f'ipython 06_lcmv.py {subject:d}'], )
def task_mne(): """Step 8: MNE source estimate""" for subject in subjects: yield dict( name=str(subject), file_dep=[ fname.epochs(subject=subject), fname.fwd(subject=subject), fname.ecd(subject=subject), '08_mne.py' ], targets=[ fname.stc_mne(subject=subject), fname.nii_mne(subject=subject) ], actions=[f'ipython 08_mne.py {subject:d}'], )
import numpy as np from config import fname # Handle command line arguments parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('subject', metavar='sub###', type=int, help='The subject to process') args = parser.parse_args() subject = args.subject print('Processing subject:', subject) epochs = mne.read_epochs(fname.epochs(subject=subject)) noise_cov = mne.compute_covariance(epochs, tmin=-0.2, tmax=0, method='shrunk') fwd = mne.read_forward_solution(fname.fwd(subject=subject)) inv = mne.minimum_norm.make_inverse_operator(epochs.info, fwd, noise_cov, depth=None) stc = mne.minimum_norm.apply_inverse(epochs.average(), inv) stc.save(fname.stc_mne(subject=subject)) # Find the time point that corresponds to the best dipole fit dip = mne.read_dipole(fname.ecd(subject=subject)) peak_time = dip[int(np.argmax(dip.gof))].times[0] stc_peak = abs(stc.copy().crop(peak_time, peak_time).mean()) stc_peak.save(fname.stc_mne(subject=subject)) stc_peak.save_as_volume(fname.nii_mne(subject=subject), src=fwd['src']) fig = stc.plot(initial_time=peak_time, subject=fname.subject_id(subject=subject), subjects_dir=fname.subjects_dir, src=fwd['src'], clim=dict(kind='percent', lims=[99.9, 99.95, 100])) with mne.open_report(fname.report(subject=subject)) as report: report.add_figs_to_section(fig, f'MNE Source estimate at {peak_time}', 'Source level', replace=True) report.save(fname.report_html(subject=subject), overwrite=True, open_browser=False)
# Find the slope of the onset evoked = epochs.average().crop(0.03, 0.05) _, mag_peak = evoked.get_peak('mag') _, grad_peak = evoked.get_peak('grad') peak_time = (mag_peak + grad_peak) / 2 evoked = epochs.average().crop(peak_time - 0.005, peak_time + 0.005) print(evoked) dip, res = mne.fit_dipole(evoked, noise_cov, bem, trans, n_jobs=n_jobs, verbose=True) dip = dip[int(np.argmax(dip.gof))] dip.save(fname.ecd(subject=subject), overwrite=True) # Plot the result in 3D brain with the MRI image using Nilearn mri_pos = mne.head_to_mri(dip.pos, mri_head_t=trans, subject=fname.subject_id(subject=subject), subjects_dir=fname.subjects_dir) t1_fname = op.join(fname.subjects_dir, fname.subject_id(subject=subject), 'mri', 'T1.mgz') fig = plt.figure() plot_anat(t1_fname, cut_coords=mri_pos[np.argmax(dip.gof)], title='Dipole loc.', figure=fig) with mne.open_report(fname.report(subject=subject)) as report: report.add_figs_to_section(fig,