def dip_source_loc(evoked, cov, fs_subj, study_path, plot=False): evo_crop = evoked.copy().crop(-0.003, 0.003) subj = fs_subj.strip('_an') if fs_subj.find('_an') > 0 else fs_subj img_type = 'anony' if fs_subj.find('_an') > 0 else 'orig' trans_fname = op.join(study_path, 'source_stim', subj, 'source_files', img_type, '%s_fid-trans.fif' % fs_subj) bem_fname = op.join(subjects_dir, fs_subj, 'bem', '%s-bem-sol.fif' % fs_subj) cond = evoked.info['description'] stim_coords = find_stim_coords(cond, subj, study_path) dip, res = mne.fit_dipole(evo_crop, cov, bem_fname, trans_fname, min_dist=10, n_jobs=3) import mne.transforms as tra from scipy import linalg trans = mne.read_trans(trans_fname) stim_point = stim_coords['surf'] # get point for plot in mm #dip.pos[np.argmax(dip.gof)] = tra.apply_trans(surf_to_head, stim_coords['surf_ori']/1e3) # check stim loc (apply affine in m) dip_surf = tra.apply_trans(trans['trans'], dip.pos[np.argmax(dip.gof)]) * 1e3 # transform from head to surface dist_surf = euclidean(dip_surf, stim_point) # compute distance in surface space print(dist_surf) if plot: # Plot the result in 3D brain with the MRI image. fig = plt.figure() ax = fig.add_subplot(111, projection='3d') dip.plot_locations(trans_fname, fs_subj, subjects_dir, mode='orthoview', coord_frame='mri', ax=ax, show_all=True, idx='gof') ax.scatter(stim_point[0], stim_point[1], stim_point[2]) ax.plot([stim_point[0], -128], [stim_point[1], stim_point[1]], [stim_point[2], stim_point[2]], color='g') ax.plot([stim_point[0], stim_point[0]], [stim_point[1], -128], [stim_point[2], stim_point[2]], color='g') ax.plot([stim_point[0], stim_point[0]], [stim_point[1], stim_point[1]], [stim_point[2], -128], color='g') ax.text2D(0.05, 0.90, 'distance: %i mm \nstim coords = %0.1f %0.1f %0.1f' % (dist_surf, stim_point[0], stim_point[1], stim_point[2]), transform=ax.transAxes) red_patch = mpatches.Patch(color='red') green_patch = mpatches.Patch(color='green') fig.legend(handles=[red_patch, green_patch], labels=['dipole', 'electrode']) fig.savefig(op.join(study_path, 'source_stim', subj, 'figures', 'dipole', '%s_dip_15mm.png' % cond)) plt.close() plot_dipole_amplitudes(dip) plot_dipole_locations(dip, trans, subj, subjects_dir=subjects_dir) return dist_surf
# Crop to remove edges for dip in dipoles: dip.crop(tmin=-0.05, tmax=0.3) evoked.crop(tmin=-0.05, tmax=0.3) residual.crop(tmin=-0.05, tmax=0.3) ############################################################################### # Plot dipole activations plot_dipole_amplitudes(dipoles) ############################################################################### # Plot location of the strongest dipole with MRI slices idx = np.argmax([np.max(np.abs(dip.amplitude)) for dip in dipoles]) plot_dipole_locations(dipoles[idx], forward['mri_head_t'], 'sample', subjects_dir=subjects_dir, mode='orthoview', idx='amplitude') # # Plot dipole locations of all dipoles with MRI slices # for dip in dipoles: # plot_dipole_locations(dip, forward['mri_head_t'], 'sample', # subjects_dir=subjects_dir, mode='orthoview', # idx='amplitude') ############################################################################### # Show the evoked response and the residual for gradiometers ylim = dict(grad=[-120, 120]) evoked.pick_types(meg='grad', exclude='bads') evoked.plot(titles=dict(grad='Evoked Response: Gradiometers'), ylim=ylim, proj=True, time_unit='s')
condition = 'Right Auditory' evoked = mne.read_evokeds(evoked_fname, condition=condition, baseline=(None, 0)) # select N100 evoked.crop(tmin=0.05, tmax=0.15, verbose='error') # ignore baseline evoked.pick_types(meg=True, eeg=False) # Read the forward solution forward = mne.read_forward_solution(fwd_fname) # Read noise covariance matrix noise_cov = mne.read_cov(cov_fname) dipoles, residual = rap_music(evoked, forward, noise_cov, n_dipoles=2, return_residual=True, verbose=True) trans = forward['mri_head_t'] plot_dipole_locations(dipoles, trans, 'sample', subjects_dir=subjects_dir) plot_dipole_amplitudes(dipoles) # Plot the evoked data and the residual. evoked.plot(ylim=dict(grad=[-300, 300], mag=[-800, 800], eeg=[-6, 8]), time_unit='s') residual.plot(ylim=dict(grad=[-300, 300], mag=[-800, 800], eeg=[-6, 8]), time_unit='s')
# Compute (ir)MxNE inverse solution with dipole output dipoles, residual = mixed_norm( evoked, forward, cov, alpha, loose=loose, depth=depth, maxit=3000, tol=1e-4, active_set_size=10, debias=True, weights=stc_dspm, weights_min=8., n_mxne_iter=n_mxne_iter, return_residual=True, return_as_dipoles=True) ############################################################################### # Plot dipole activations plot_dipole_amplitudes(dipoles) # Plot dipole location of the strongest dipole with MRI slices idx = np.argmax([np.max(np.abs(dip.amplitude)) for dip in dipoles]) plot_dipole_locations(dipoles[idx], forward['mri_head_t'], 'sample', subjects_dir=subjects_dir, mode='orthoview', idx='amplitude') # # Plot dipole locations of all dipoles with MRI slices # for dip in dipoles: # plot_dipole_locations(dip, forward['mri_head_t'], 'sample', # subjects_dir=subjects_dir, mode='orthoview', # idx='amplitude') ############################################################################### # Plot residual ylim = dict(eeg=[-10, 10], grad=[-400, 400], mag=[-600, 600]) evoked.pick_types(meg=True, eeg=True, exclude='bads') evoked.plot(ylim=ylim, proj=True, time_unit='s') residual.pick_types(meg=True, eeg=True, exclude='bads') residual.plot(ylim=ylim, proj=True, time_unit='s')
subjects_dir = data_path + '/subjects' fwd_fname = data_path + '/MEG/sample/sample_audvis-meg-eeg-oct-6-fwd.fif' evoked_fname = data_path + '/MEG/sample/sample_audvis-ave.fif' cov_fname = data_path + '/MEG/sample/sample_audvis-cov.fif' # Read the evoked response and crop it condition = 'Right Auditory' evoked = mne.read_evokeds(evoked_fname, condition=condition, baseline=(None, 0)) evoked.crop(tmin=0.05, tmax=0.15) # select N100 evoked.pick_types(meg=True, eeg=False) # Read the forward solution forward = mne.read_forward_solution(fwd_fname) # Read noise covariance matrix noise_cov = mne.read_cov(cov_fname) dipoles, residual = rap_music(evoked, forward, noise_cov, n_dipoles=2, return_residual=True, verbose=True) trans = forward['mri_head_t'] plot_dipole_locations(dipoles, trans, 'sample', subjects_dir=subjects_dir) plot_dipole_amplitudes(dipoles) # Plot the evoked data and the residual. evoked.plot(ylim=dict(grad=[-300, 300], mag=[-800, 800], eeg=[-6, 8]), time_unit='s') residual.plot(ylim=dict(grad=[-300, 300], mag=[-800, 800], eeg=[-6, 8]), time_unit='s')
def dipole_source_loc(evoked, cov, fs_subj, study_path, plot=False): evo_crop = evoked.copy().crop(-0.005, 0.005) subj = fs_subj.strip('_an') if fs_subj.find('_an') > 0 else fs_subj img_type = 'anony' if fs_subj.find('_an') > 0 else 'orig' seeg_ch_info = pd.read_csv( op.join(study_path, 'physio_data', subj, 'chan_info', '%s_seeg_ch_info.csv' % subj)) # ori_to_an_fname = op.join(study_path, 'freesurfer_subjects', subj, 'mri', 'ori_to_an_trans.lta') # mri to mri # ori_to_an_trans = np.genfromtxt(ori_to_an_fname, skip_header=8, skip_footer=18) trans_fname = op.join(study_path, 'source_stim', subj, 'source_files', img_type, '%s_static-trans.fif' % fs_subj) bem_fname = op.join(subjects_dir, fs_subj, 'bem', '%s-bem-sol.fif' % fs_subj) cond = eeg_epo.info['description'] stim_coords = find_stim_coords(cond, subj, study_path) # Fit a dipole dip = mne.fit_dipole(evo_crop, cov, bem_fname, trans_fname, min_dist=5, n_jobs=2)[0] from mne.beamformer import rap_music dip = rap_music(evo_crop, fwd, cov, n_dipoles=1, return_residual=True, verbose=True)[0][0] import mne.transforms as tra from scipy import linalg trans = mne.read_trans(trans_fname) surf_to_head = linalg.inv(trans['trans']) # stim_point = tra.apply_trans(surf_to_head, stim_coords['surf']/1e3) stim_point = stim_coords['surf_ori'] # get point for plot in mm #dip.pos[np.argmax(dip.gof)] = tra.apply_trans(surf_to_head, stim_coords['surf_ori']/1e3) # check stim loc (apply affine in m) #stim_point = tra.apply_trans(ori_to_an_trans, stim_point) dip_surf = tra.apply_trans(trans['trans'], dip.pos[np.argmax( dip.gof)]) * 1e3 # transform from head to surface dist_surf = euclidean(dip_surf, stim_point) # compute distance in surface space print(dist_surf) if plot: # Plot the result in 3D brain with the MRI image. fig = plt.figure() ax = fig.add_subplot(111, projection='3d') dip.plot_locations(trans_fname, fs_subj, subjects_dir, mode='orthoview', coord_frame='mri', ax=ax, show_all=True, idx='gof') ax.scatter(stim_point[0], stim_point[1], stim_point[2]) ax.plot([stim_point[0], -128], [stim_point[1], stim_point[1]], [stim_point[2], stim_point[2]], color='g') ax.plot([stim_point[0], stim_point[0]], [stim_point[1], -128], [stim_point[2], stim_point[2]], color='g') ax.plot([stim_point[0], stim_point[0]], [stim_point[1], stim_point[1]], [stim_point[2], -128], color='g') ax.text2D(0.05, 0.90, 'distance: %i mm \nstim coords = %0.1f %0.1f %0.1f' % (dist_surf, stim_point[0], stim_point[1], stim_point[2]), transform=ax.transAxes) red_patch = mpatches.Patch(color='red') green_patch = mpatches.Patch(color='green') fig.legend(handles=[red_patch, green_patch], labels=['dipole', 'electrode']) fig.savefig( op.join(study_path, 'source_stim', subj, 'figures', 'dipole', '%s_dip_15mm.png' % cond)) plt.close() from mne.viz import plot_dipole_locations, plot_dipole_amplitudes plot_dipole_amplitudes(dip) plot_dipole_locations(dip, trans, subj, subjects_dir=subjects_dir) return dist_surf