def test_dics_connectivity(): """ Test dics_connectivity function""" source_vertno1 = 146374 # Somewhere on the frontal lobe source_vertno2 = 33830 # Load restricted forwards fwd, fwd_fixed = _load_restricted_forward(source_vertno1, source_vertno2) csd = _simulate_data(fwd_fixed, source_vertno1, source_vertno2) # Take subset of pairs to make calculations faster pairs = all_to_all_connectivity_pairs(fwd, 0.05) # CSD not averaged with pytest.raises(ValueError): con = dics_connectivity(pairs, fwd, csd) csd = csd.mean() # Fixed forward with pytest.raises(ValueError): dics_connectivity(pairs, fwd_fixed, csd) # Incorrect pairs with pytest.raises(ValueError): dics_connectivity([[0, 1, 3], [2, 4]], fwd, csd) con = dics_connectivity(pairs, fwd, csd, reg=1) max_ind = np.argmax(con.data) vertices = np.concatenate(con.vertices) assert len(con.data) == len(pairs[0]) assert con.n_sources == fwd['nsource'] assert vertices[pairs[0][max_ind]] == source_vertno1 assert vertices[pairs[1][max_ind]] == source_vertno2 # Check result is the same with tangential fwd_tan = forward_to_tangential(fwd) con2 = dics_connectivity(pairs, fwd_tan, csd, reg=1) assert_array_equal(con2.data, con.data)
def test_all_to_all_connectivity_pairs(): fwd = _load_forward() # Test using forward total_vertices = len(fwd['source_rr']) # All should pass pairs = all_to_all_connectivity_pairs(fwd, min_dist=0) assert max(pairs[0].max(), pairs[1].max()) == total_vertices - 1 # None should pass pairs = all_to_all_connectivity_pairs(fwd, min_dist=1) assert pairs[0].size == 0 assert pairs[1].size == 0 # Test using SourceSpaces # All should pass pairs = all_to_all_connectivity_pairs(fwd['src'], min_dist=0) assert max(pairs[0].max(), pairs[1].max()) == total_vertices - 1 # None should pass pairs = all_to_all_connectivity_pairs(fwd['src'], min_dist=1) assert pairs[0].size == 0 assert pairs[1].size == 0 # Incorrect input with pytest.raises(ValueError): all_to_all_connectivity_pairs([], min_dist=1)
# Save a visualization of the restricted forward operator to the subject's # HTML report with mne.open_report(fname.report(subject=subject)) as report: fig = mne.viz.plot_alignment(fwd['info'], trans=fname.trans(subject=subject), src=fwd_r['src'], meg='sensors', surfaces='white') fig.scene.background = (1, 1, 1) # white g = fig.children[-1].children[0].children[0].glyph.glyph g.scale_factor = 0.008 mlab.view(135, 120, 0.3, [0.01, 0.015, 0.058]) report.add_figs_to_section([fig], ['Selected sources'], section='Source-level', replace=True) report.save(fname.report_html(subject=subject), overwrite=True, open_browser=False) # Compute vertex pairs for which to compute connectivity # (distances are based on the MRI of the first subject). print('Computing connectivity pairs for all subjects...') pairs = conpy.all_to_all_connectivity_pairs(fwd1, min_dist=min_pair_dist) # Store the pairs in fsaverage space subj1_to_fsaverage = conpy.utils.get_morph_src_mapping( fsaverage, fwd1['src'], indices=True, subjects_dir=fname.subjects_dir)[1] pairs = [[subj1_to_fsaverage[v] for v in pairs[0]], [subj1_to_fsaverage[v] for v in pairs[1]]] np.save(fname.pairs, pairs)
# going to use a forward model that defines less source points. # Load an oct-4 forward model that has only a few source points fwd_lim = mne.read_forward_solution(fwd_lim_fname) # Restrict the forward model to gradiometers only fwd_lim = mne.pick_types_forward(fwd_lim, meg='grad', eeg=False, exclude='bads') fwd_lim = forward_to_tangential(fwd_lim) ############################################################################### # To reduce the number of coherence computations even further, we're going to # ignore all connections that span less than 5 centimeters. pairs = all_to_all_connectivity_pairs(fwd_lim, min_dist=0.05) print('There are now %d connectivity pairs' % len(pairs[0])) ############################################################################### # Compute coherence between all defined pairs using DICS. This connectivity # estimate will be dominated by local field spread effects, unless we make a # contrast between two conditions. con_noise = dics_connectivity(pairs, fwd_lim, csd_noise, reg=1) con_signal = dics_connectivity(pairs, fwd_lim, csd_signal, reg=1) con_contrast = con_signal - con_noise # Create a cortical map, where the "signal" at each point is the sum of the # coherence of all outgoing and incoming connections from and to that point. all_to_all = con_contrast.make_stc('sum', weight_by_degree=True) brain = all_to_all.plot('sample',
import conpy, mne # Import required Python modules # Read and convert a forward model to one that defines two orthogonal dipoles # at each source, that are tangential to a sphere. fwd = mne.read_forward_solution('sub002-fwd.fif') # Read forward model fwd_tan = conpy.forward_to_tangential(fwd_r) # Convert to tangential model # Pairs for which to compute connectivity. Use a distance threshold of 4 cm. pairs = conpy.all_to_all_connectivity_pairs(fwd_tan, min_dist=0.04) # Load CSD matrix csd = conpy.read_csd('sub002-csd-face.h5') # Read CSD for 'face' condition csd = csd.mean(fmin=31, fmax=40) # Obtain CSD for frequency band 31-40 Hz. # Compute source connectivity using DICS. Try 50 orientations for each source # point to find the orientation that maximizes coherence. con = conpy.dics_connectivity(pairs, fwd_tan, csd, reg=0.05, n_angles=50)