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)
# pairs are defined in fsaverage space, map them to the source space of the current subject fsaverage_to_subj = conpy.utils.get_morph_src_mapping( fs_src, fwd_tan['src'], indices=True, subjects_dir=mri_dir)[0] pairs = [[fsaverage_to_subj[v] for v in pairs[0]], [fsaverage_to_subj[v] for v in pairs[1]]] for cond in exp_conds: print("Calculations for Condition: ", cond) for freq, vals in freqs.items(): print("Calculations for Frequency: ", freq) csd = read_csd("{dir}nc_{meg}-csd_{cond}_{freq}.h5".format( dir=meg_dir, meg=meg, cond=cond, freq=freq)) csd = csd.mean() csd = pick_channels_csd(csd, fwd_tan['info']['ch_names']) con = conpy.dics_connectivity(vertex_pairs=pairs, fwd=fwd_tan, data_csd=csd, reg=0.05, n_jobs=8) con.save("{dir}nc_{meg}_{cond}_{freq}-connectivity.h5".format( dir=meg_dir, meg=meg, cond=cond, freq=freq)) # for the baseline conditions: print("Running Baseline Conditions") fwd_r = mne.read_forward_solution( "{dir}nc_{meg}_from-fs_ico4_bas-r-fwd.fif".format(dir=meg_dir, meg=meg)) # convert the forward model to one that defines two orthogonal dipoles at each source, that are tangential to a sphere fwd_tan = conpy.forward_to_tangential(fwd_r) # get pairs for connectivity calculation pairs = np.load("{}NEMO_ico4_connectivity_pairs.npy".format(meg_dir)) # pairs are defined in fsaverage space, map them to the source space of the current subject
# ----------------------- # Let's try to estimate coherence between the two sources. One way to do it is # to compute "one-to-all" connectivity. We pick one point on the cortex where # we know (or assume) that there is an active source. Then, we compute # coherence between all points on the cortex and this reference point to see # which parts of the cortex have a signal that is coherent with our reference # source. Hopefully, we'll find the other source. # Our reference point is the point with maximum power (roughly the location of # our second simulated source signal). ref_point = np.argmax(power.data) # Compute one-to-all coherence between each point and the reference point. pairs = one_to_all_connectivity_pairs(fwd, ref_point) fwd_tan = forward_to_tangential(fwd) con = dics_connectivity(pairs, fwd_tan, csd_signal, reg=1) ############################################################################### # To plot the result, we transform the :class:`conpy.VertexConnectivity` object # to an :class:`mne.SourceEstimate` object, where the "signal" at each point is # the coherence value. one_to_all = con.make_stc('sum', weight_by_degree=True) # Plot the coherence values on the cortex brain = one_to_all.plot('sample', subjects_dir=subjects_dir, hemi='both', figure=3, size=400) # Indicate the true locations of the source activity on the plot (in white)
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)