def make_dipole_volume(stc, src): """Find the peak in a distributed source estimate and make a dipole out of it for volume source space data""" stc = abs(stc).mean() max_idx = stc.get_peak(vert_as_index=True)[0] max_vertno = stc.get_peak()[0] pos = src[0]['rr'][max_vertno] dip = mne.Dipole(stc.times, pos, stc.data[max_idx], [1., 0., 0.], 1) return dip
def make_dipole(stc, src): """Find the peak in a distributed source estimate and make a dipole out of it""" stc = abs(stc).mean() max_idx = stc.get_peak(vert_as_index=True)[0] max_vertno = stc.get_peak()[0] max_hemi = int(max_idx < len(stc.vertices[0])) pos = src[max_hemi]['rr'][max_vertno] dip = mne.Dipole(stc.times, pos, stc.data[max_idx], [1., 0., 0.], 1) return dip
def gen_forward_solution(pos, bem, info, trans, verbose=True): # NOTE: # Both, dipole amplitude and orientation, are set to arbitrary values # here: They are merely required to instantiate a Dipole object, which # we then use to conveniently retrieve a forward solution via # `make_forward_dipole()`. This forward solution is returned in "fixed" # orientation. We then convert it back to "free" orientation mode before # returning the forward object. amplitude = np.array([1]).reshape(1, ) # Arbitraty amplitude. ori = np.array([1., 1., 1.]).reshape(1, 3) # Arbitrary orientation. ori /= np.linalg.norm(ori) pos = pos.reshape(1, 3) gof = np.array([100]).reshape(1, ) dip = mne.Dipole(times=[0], pos=pos, ori=ori, amplitude=amplitude, gof=gof) fwd, _ = mne.make_forward_dipole(dip, bem=bem, info=info, trans=trans, verbose=verbose) fwd = mne.convert_forward_solution(fwd, force_fixed=False, verbose=verbose) return fwd
print('mean(abs amplitude error) = %0.1f nAm' % (np.mean(np.abs(amps)), )) ax3.bar(event_id, amps) ax3.set_xlabel('Dipole index') ax3.set_ylabel('Amplitude error (nAm)') fig.tight_layout() plt.show() # %% # Let's plot the positions and the orientations of the actual and the estimated # dipoles actual_amp = np.ones(len(dip)) # misc amp to create Dipole instance actual_gof = np.ones(len(dip)) # misc GOF to create Dipole instance dip_true = \ mne.Dipole(dip.times, actual_pos, actual_amp, actual_ori, actual_gof) fig = mne.viz.plot_alignment(evoked.info, bem=sphere, surfaces='inner_skull', coord_frame='head', meg='helmet', show_axes=True) # Plot the position and the orientation of the actual dipole fig = mne.viz.plot_dipole_locations(dipoles=dip_true, mode='arrow', subject=subject, color=(0., 0., 0.), fig=fig)
# :func:`mne.setup_source_space` function. Dipoles are placed along fixed # intervals on the cortex, determined by the ``spacing`` parameter. The source # space does not define the orientation for these dipoles. lh = fwd['src'][0] # Visualize the left hemisphere verts = lh['rr'] # The vertices of the source space tris = lh['tris'] # Groups of three vertices that form triangles dip_pos = lh['rr'][lh['vertno']] # The position of the dipoles dip_ori = lh['nn'][lh['vertno']] dip_len = len(dip_pos) dip_times = [0] white = (1.0, 1.0, 1.0) # RGB values for a white color actual_amp = np.ones(dip_len) # misc amp to create Dipole instance actual_gof = np.ones(dip_len) # misc GOF to create Dipole instance dipoles = mne.Dipole(dip_times, dip_pos, actual_amp, dip_ori, actual_gof) trans = mne.read_trans(trans_fname) fig = mne.viz.create_3d_figure(size=(600, 400), bgcolor=white) coord_frame = 'mri' # Plot the cortex mne.viz.plot_alignment( subject=subject, subjects_dir=subjects_dir, trans=trans, surfaces='white', coord_frame=coord_frame, fig=fig) # Mark the position of the dipoles with small red dots mne.viz.plot_dipole_locations( dipoles=dipoles, trans=trans, mode='sphere', subject=subject, subjects_dir=subjects_dir, coord_frame=coord_frame, scale=7e-4, fig=fig)
ori[ii] = dip.ori[0] ############################################################################### # Compute localisation errors actual_pos = 0.01 * np.array([[0.16, 1.61, 5.13], [0.17, 1.35, 4.15], [0.16, 1.05, 3.19], [0.13, 0.80, 2.26]]) actual_pos = np.dot(actual_pos, [[0, 1, 0], [-1, 0, 0], [0, 0, 1]]) errors = 1e3 * np.linalg.norm(actual_pos - pos, axis=1) print("errors (mm) : %s" % errors) ############################################################################### # Plot the dipoles in 3D: actual_amp = np.ones(len(dip)) # misc amp to create Dipole instance actual_gof = np.ones(len(dip)) # misc GOF to create Dipole instance dip = mne.Dipole(dip.times, pos, actual_amp, ori, actual_gof) dip_true = mne.Dipole(dip.times, actual_pos, actual_amp, ori, actual_gof) fig = mne.viz.plot_alignment(evoked.info, bem=sphere, surfaces=[]) # Plot the position of the actual dipole fig = mne.viz.plot_dipole_locations(dipoles=dip_true, mode='sphere', color=(1., 0., 0.), fig=fig) # Plot the position of the estimated dipole fig = mne.viz.plot_dipole_locations(dipoles=dip, mode='sphere', color=(1., 1., 0.), fig=fig)