def test_find_ch_connectivity(): """Test computing the connectivity matrix.""" data_path = testing.data_path() raw = read_raw_fif(raw_fname, preload=True) sizes = {'mag': 828, 'grad': 1700, 'eeg': 386} nchans = {'mag': 102, 'grad': 204, 'eeg': 60} for ch_type in ['mag', 'grad', 'eeg']: conn, ch_names = find_ch_connectivity(raw.info, ch_type) # Silly test for checking the number of neighbors. assert_equal(conn.getnnz(), sizes[ch_type]) assert_equal(len(ch_names), nchans[ch_type]) pytest.raises(ValueError, find_ch_connectivity, raw.info, None) # Test computing the conn matrix with gradiometers. conn, ch_names = _compute_ch_connectivity(raw.info, 'grad') assert_equal(conn.getnnz(), 2680) # Test ch_type=None. raw.pick_types(meg='mag') find_ch_connectivity(raw.info, None) bti_fname = op.join(data_path, 'BTi', 'erm_HFH', 'c,rfDC') bti_config_name = op.join(data_path, 'BTi', 'erm_HFH', 'config') raw = read_raw_bti(bti_fname, bti_config_name, None) _, ch_names = find_ch_connectivity(raw.info, 'mag') assert 'A1' in ch_names ctf_fname = op.join(data_path, 'CTF', 'testdata_ctf_short.ds') raw = read_raw_ctf(ctf_fname) _, ch_names = find_ch_connectivity(raw.info, 'mag') assert 'MLC11' in ch_names pytest.raises(ValueError, find_ch_connectivity, raw.info, 'eog')
def test_find_ch_connectivity(): """Test computing the connectivity matrix.""" data_path = testing.data_path() raw = read_raw_fif(raw_fname, preload=True) sizes = {'mag': 828, 'grad': 1700, 'eeg': 386} nchans = {'mag': 102, 'grad': 204, 'eeg': 60} for ch_type in ['mag', 'grad', 'eeg']: conn, ch_names = find_ch_connectivity(raw.info, ch_type) # Silly test for checking the number of neighbors. assert_equal(conn.getnnz(), sizes[ch_type]) assert_equal(len(ch_names), nchans[ch_type]) assert_raises(ValueError, find_ch_connectivity, raw.info, None) # Test computing the conn matrix with gradiometers. conn, ch_names = _compute_ch_connectivity(raw.info, 'grad') assert_equal(conn.getnnz(), 2680) # Test ch_type=None. raw.pick_types(meg='mag') find_ch_connectivity(raw.info, None) bti_fname = op.join(data_path, 'BTi', 'erm_HFH', 'c,rfDC') bti_config_name = op.join(data_path, 'BTi', 'erm_HFH', 'config') raw = read_raw_bti(bti_fname, bti_config_name, None) _, ch_names = find_ch_connectivity(raw.info, 'mag') assert_true('A1' in ch_names) ctf_fname = op.join(data_path, 'CTF', 'testdata_ctf_short.ds') raw = read_raw_ctf(ctf_fname) _, ch_names = find_ch_connectivity(raw.info, 'mag') assert_true('MLC11' in ch_names) assert_raises(ValueError, find_ch_connectivity, raw.info, 'eog')
def con_matrix(epochs: mne.Epochs, freqs_mean: list, draw: bool = False) -> tuple: """ Computes a priori channel connectivity across space and frequencies. Arguments: epochs: one participant Epochs object; contains channel information. freqs_mean: list of frequencies in frequency-band-of-interest used by MNE for power or coherence spectral density calculation. draw: option to plot the connectivity matrices, boolean. Returns: ch_con, ch_con_freq: - ch_con: connectivity matrix between channels along space based on their position, scipy.sparse.csr_matrix of shape (n_channels, n_channels). - ch_con_freq: connectivity matrix between channels along space and frequencies, scipy.sparse.csr_matrix of shape (n_channels*len(freqs_mean), n_channels*len(freqs_mean)). """ # creating channel-to-channel connectivity matrix in space ch_con, ch_names_con = find_ch_connectivity(epochs.info, ch_type='eeg') ch_con_arr = ch_con.toarray() # duplicating the array 'freqs_mean' or 'freqs' times (PSD or CSD) # to take channel connectivity across neighboring frequencies into # account l_freq = len(freqs_mean) init = np.zeros((l_freq * len(ch_names_con), l_freq * len(ch_names_con))) for i in range(0, l_freq * len(ch_names_con)): for p in range(0, l_freq * len(ch_names_con)): if (p // len(ch_names_con) == i // len(ch_names_con)) or ( p // len(ch_names_con) == i // len(ch_names_con) + 1) or (p // len(ch_names_con) == i // len(ch_names_con) - 1): init[i][p] = 1 ch_con_mult = np.tile(ch_con_arr, (l_freq, l_freq)) ch_con_freq = np.multiply(init, ch_con_mult) if draw: plt.figure() # visualizing the matrix and transforming it into array plt.subplot(1, 2, 1) plt.spy(ch_con) plt.title("Connectivity matrix") plt.subplot(1, 2, 2) plt.spy(ch_con_freq) plt.title("Meta-connectivity matrix") con_matrixTuple = namedtuple('con_matrix', ['ch_con', 'ch_con_freq']) return con_matrixTuple(ch_con=ch_con, ch_con_freq=ch_con_freq)
def collect_data(dat0_files,condname,tmin,tmax,ismulti): nfiles0 = len(dat0_files) evokeds0 = [] # find the path to the data files if len(dat0_files) == 1 or ismulti: filepath = config.epoch_path else: filepath = config.evoked_path for dat_idx, dat in enumerate(dat0_files): dat_file = op.join(filepath, dat + '.fif') print("Loading subject: %s" % dat_file) # load the files first if nfiles0 != 1: ev_dat0 = mne.read_evokeds(dat_file,condition=condname) else: ev_dat0 = mne.read_epochs(dat_file) # We use the first dataset to create empty variables we fill in subsequently if dat_idx == 0: if nfiles0 != 1: dat0_orig = np.zeros((nfiles0,len(ev_dat0.times),ev_dat0.info['nchan'])) else: # we store all trials for single subject stats dat0_orig = ev_dat0[condname].get_data() # find electrode neighbourhoods if len(ev_dat0.ch_names) == 1: connectivity = None else: connectivity = find_ch_connectivity(ev_dat0.info, ch_type='eeg') connectivity = connectivity[0] # store the data for later plotting evokeds0.append(ev_dat0.copy()) if nfiles0 != 1: dat0_orig[dat_idx] = np.transpose(ev_dat0.data) # select time-window of interest ev_dat0_crop = ev_dat0.crop(tmin=tmin, tmax=tmax) if dat_idx == 0: # create empty matrix to store the data that will actually be analysed dat0 = np.zeros((dat0_orig.shape[0],ev_dat0_crop.data.shape[1],dat0_orig.shape[2])) dat0[dat_idx] = np.transpose(ev_dat0_crop.data) else: # select time-window of interest ev_dat0_crop = ev_dat0[condname].crop(tmin=tmin, tmax=tmax) dat0 = np.transpose(ev_dat0_crop.get_data(), (0, 2, 1)) return dat0, evokeds0, connectivity
reject = dict(mag=4e-12, eog=150e-6) epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=None, reject=reject, preload=True) epochs.drop_channels(['EOG 061']) epochs.equalize_event_counts(event_id) X = [epochs[k].get_data() for k in event_id] # as 3D matrix X = [np.transpose(x, (0, 2, 1)) for x in X] # transpose for clustering ############################################################################### # Find the FieldTrip neighbor definition to setup sensor connectivity # ------------------------------------------------------------------- connectivity, ch_names = find_ch_connectivity(epochs.info, ch_type='mag') print(type(connectivity)) # it's a sparse matrix! plt.imshow(connectivity.toarray(), cmap='gray', origin='lower', interpolation='nearest') plt.xlabel('{} Magnetometers'.format(len(ch_names))) plt.ylabel('{} Magnetometers'.format(len(ch_names))) plt.title('Between-sensor adjacency') ############################################################################### # Compute permutation statistic # ----------------------------- # # How does it work? We use clustering to `bind` together features which are # similar. Our features are the magnetic fields measured over our sensor
x2, y2, z2 = sens_loc[nodes[1]] lines = mlab.plot3d( [x1, x2], [y1, y2], [z1, z2], [val, val], vmin=vmin, vmax=vmax, tube_radius=0.0002, #color features colormap='blue-red') lines.module_manager.scalar_lut_manager.reverse_lut = True ##Calculate connectivity for single cap from mne.channels import find_ch_connectivity x, y = find_ch_connectivity(epochsS1.info, ch_type="eeg") from scipy.sparse import csr_matrix A = csr_matrix(x.toarray()) ##Plot two caps connectivity def capTest(x): if x >= 31: out = x - 31 else: out = x return (out) #Brain areas
tmax=tmax, df=len(epochs.events) - 2, t_val=t, p=p) print(report.format(**format_dict)) ############################################################################## # Absent specific hypotheses, we can also conduct an exploratory # mass-univariate analysis at all sensors and time points. This requires # correcting for multiple tests. # MNE offers various methods for this; amongst them, cluster-based permutation # methods allow deriving power from the spatio-temoral correlation structure # of the data. Here, we use TFCE. # Calculate statistical thresholds con = find_ch_connectivity(epochs.info, "eeg") # Extract data: transpose because the cluster test requires channels to be last # In this case, inference is done over items. In the same manner, we could # also conduct the test over, e.g., subjects. X = [ long_words.get_data().transpose(0, 2, 1), short_words.get_data().transpose(0, 2, 1) ] tfce = dict(start=.2, step=.2) t_obs, clusters, cluster_pv, h0 = spatio_temporal_cluster_test( X, tfce, n_permutations=100) # a more standard number would be 1000+ significant_points = cluster_pv.reshape(t_obs.shape).T < .05 print(str(significant_points.sum()) + " points selected by TFCE ...")
# display results format_dict = dict(elec=elec, tmin=tmin, tmax=tmax, df=len(epochs.events) - 2, t_val=t, p=p) print(report.format(**format_dict)) ############################################################################## # Absent specific hypotheses, we can also conduct an exploratory # mass-univariate analysis at all sensors and time points. This requires # correcting for multiple tests. # MNE offers various methods for this; amongst them, cluster-based permutation # methods allow deriving power from the spatio-temoral correlation structure # of the data. Here, we use TFCE. # Calculate statistical thresholds con = find_ch_connectivity(epochs.info, "eeg") # Extract data: transpose because the cluster test requires channels to be last # In this case, inference is done over items. In the same manner, we could # also conduct the test over, e.g., subjects. X = [long_words.get_data().transpose(0, 2, 1), short_words.get_data().transpose(0, 2, 1)] tfce = dict(start=.2, step=.2) t_obs, clusters, cluster_pv, h0 = spatio_temporal_cluster_test( X, tfce, n_permutations=100) # a more standard number would be 1000+ significant_points = cluster_pv.reshape(t_obs.shape).T < .05 print(str(significant_points.sum()) + " points selected by TFCE ...") ############################################################################## # The results of these mass univariate analyses can be visualised by plotting
g2 = np.swapaxes( np.array([[ y_df.query( 'metric_type=="{}" & subj_id=="{}" & channel=="{}"'.format( metric_type, s, ch))['env'].values for ch in CHANNELS ] for s in y_df.query('fb_type=="FBMock"')['subj_id'].unique()]), 2, 1) from mne.stats import spatio_temporal_cluster_test from mne import create_info from mne.channels import read_montage, find_ch_connectivity cnk = find_ch_connectivity( create_info(CHANNELS, 250, 'eeg', read_montage('standard_1005')), 'eeg')[0] t_obs, clusters, cluster_pv, h0 = spatio_temporal_cluster_test( [g1, g2], 138, stat_fun=rankstat, tail=1, connectivity=cnk) cluster_pv good_cluster_inds = np.where(cluster_pv < 0.05)[0] for i_clu, clu_idx in enumerate(good_cluster_inds[:10]): # unpack cluster information, get unique indices time_inds, space_inds = np.squeeze(clusters[clu_idx]) ch_inds = np.unique(space_inds) time_inds = np.unique(time_inds) # get topography for F stat
# compute bootstrap confidence interval for phase-coherence betas and t-values # set random state for replication random_state = 42 random = np.random.RandomState(random_state) # number of random samples boot = 2000 # place holders for bootstrap samples cluster_H0 = np.zeros(boot) f_H0 = np.zeros(boot) # setup connectivity n_tests = betas.shape[1] connectivity, ch_names = find_ch_connectivity(epochs_info, ch_type='eeg') connectivity = _setup_connectivity(connectivity, n_tests, n_times) # threshond for clustering threshold = 100. # run bootstrap for regression coefficients for i in range(boot): # extract random subjects from overall sample resampled_subjects = random.choice(range(betas.shape[0]), betas.shape[0], replace=True) # resampled betas resampled_betas = betas[resampled_subjects, :] # compute standard error of bootstrap sample
'../syncpipeline/FINS_data/enobio32.locs') info = mne.create_info(ch_names=montage.ch_names, sfreq=500, ch_types='eeg') epo1 = mne.EpochsArray(data=np.empty((36, 32, 501)), info=info) epo1.set_montage(montage) epo2 = epo1.copy() mne.epochs.equalize_epoch_counts([epo1, epo2]) sampling_rate = epo1.info['sfreq'] #Hz # concatenate two datasets epo_real = utils.merge(epoch_S1=epo1, epoch_S2=epo2) # setting up parameters n_chan = len(epo_real.ch_names) # get channel locations con, _ = find_ch_connectivity(epo1.info, 'eeg') con = con.toarray() N = int(n_chan / 2) A11 = 1 * np.ones((N, N)) A12 = 0 * np.ones((N, N)) A21 = 0 * np.ones((N, N)) A22 = 1 * np.ones((N, N)) # A11 = con # A22 = con W = np.block([[A11, A12], [A21, A22]]) W = 0.2 * W # simulation params frequency_mean = 10.