def test_random_permutation(): """Test random permutation function.""" n_samples = 10 random_state = 42 python_randperm = random_permutation(n_samples, random_state) # matlab output when we execute rng(42), randperm(10) matlab_randperm = np.array([7, 6, 5, 1, 4, 9, 10, 3, 8, 2]) assert_array_equal(python_randperm, matlab_randperm - 1)
def generate_data_for_comparing_against_eeglab_infomax(ch_type, random_state): """Generate data.""" data_dir = op.join(testing.data_path(download=False), 'MEG', 'sample') raw_fname = op.join(data_dir, 'sample_audvis_trunc_raw.fif') raw = read_raw_fif(raw_fname, preload=True) if ch_type == 'eeg': picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads') else: picks = pick_types(raw.info, meg=ch_type, eeg=False, exclude='bads') # select a small number of channels for the test number_of_channels_to_use = 5 idx_perm = random_permutation(picks.shape[0], random_state) picks = picks[idx_perm[:number_of_channels_to_use]] raw.filter(1, 45, picks=picks, filter_length='10s', l_trans_bandwidth=0.5, h_trans_bandwidth=0.5, phase='zero-double', fir_window='hann') # use the old way X = raw[picks, :][0][:, ::20] # Subtract the mean mean_X = X.mean(axis=1) X -= mean_X[:, None] # pre_whitening: z-score X /= np.std(X) T = X.shape[1] cov_X = np.dot(X, X.T) / T # Let's whiten the data U, D, _ = svd(cov_X) W = np.dot(U, U.T / np.sqrt(D)[:, None]) Y = np.dot(W, X) return Y
def generate_data_for_comparing_against_eeglab_infomax(ch_type, random_state): """Generate data.""" data_dir = op.join(testing.data_path(download=False), 'MEG', 'sample') raw_fname = op.join(data_dir, 'sample_audvis_trunc_raw.fif') raw = read_raw_fif(raw_fname, preload=True, add_eeg_ref=False) if ch_type == 'eeg': picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads') else: picks = pick_types(raw.info, meg=ch_type, eeg=False, exclude='bads') # select a small number of channels for the test number_of_channels_to_use = 5 idx_perm = random_permutation(picks.shape[0], random_state) picks = picks[idx_perm[:number_of_channels_to_use]] with warnings.catch_warnings(record=True): # deprecated params raw.filter(1, 45, picks=picks) # Eventually we will need to add these, but for now having none of # them is a nice deprecation sanity check. # filter_length='10s', # l_trans_bandwidth=0.5, h_trans_bandwidth=0.5, # phase='zero-double', fir_window='hann') # use the old way X = raw[picks, :][0][:, ::20] # Subtract the mean mean_X = X.mean(axis=1) X -= mean_X[:, None] # pre_whitening: z-score X /= np.std(X) T = X.shape[1] cov_X = np.dot(X, X.T) / T # Let's whiten the data U, D, _ = svd(cov_X) W = np.dot(U, U.T / np.sqrt(D)[:, None]) Y = np.dot(W, X) return Y
def generate_data_for_comparing_against_eeglab_infomax(ch_type, random_state): data_dir = op.join(testing.data_path(download=False), 'MEG', 'sample') raw_fname = op.join(data_dir, 'sample_audvis_trunc_raw.fif') raw = Raw(raw_fname, preload=True) if ch_type == 'eeg': picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads') else: picks = pick_types(raw.info, meg=ch_type, eeg=False, exclude='bads') # select a small number of channels for the test number_of_channels_to_use = 5 idx_perm = random_permutation(picks.shape[0], random_state) picks = picks[idx_perm[:number_of_channels_to_use]] with warnings.catch_warnings(record=True): # deprecated params raw.filter(1, 45, picks=picks) # Eventually we will need to add these, but for now having none of # them is a nice deprecation sanity check. # filter_length='10s', # l_trans_bandwidth=0.5, h_trans_bandwidth=0.5, # phase='zero-double') # use the old way X = raw[picks, :][0][:, ::20] # Subtract the mean mean_X = X.mean(axis=1) X -= mean_X[:, None] # pre_whitening: z-score X /= np.std(X) T = X.shape[1] cov_X = np.dot(X, X.T) / T # Let's whiten the data U, D, _ = svd(cov_X) W = np.dot(U, U.T / np.sqrt(D)[:, None]) Y = np.dot(W, X) return Y
def generate_data_for_comparing_against_eeglab_infomax(ch_type, random_state): data_dir = op.join(testing.data_path(download=False), 'MEG', 'sample') raw_fname = op.join(data_dir, 'sample_audvis_trunc_raw.fif') raw = Raw(raw_fname, preload=True) if ch_type == 'eeg': picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads') else: picks = pick_types(raw.info, meg=ch_type, eeg=False, exclude='bads') # select a small number of channels for the test number_of_channels_to_use = 5 idx_perm = random_permutation(picks.shape[0], random_state) picks = picks[idx_perm[:number_of_channels_to_use]] raw.filter(1, 45, n_jobs=2) X = raw[picks, :][0][:, ::20] # Substract the mean mean_X = X.mean(axis=1) X -= mean_X[:, None] # pre_whitening: z-score X /= np.std(X) T = X.shape[1] cov_X = np.dot(X, X.T) / T # Let's whiten the data U, D, _ = svd(cov_X) W = np.dot(U, U.T / np.sqrt(D)[:, None]) Y = np.dot(W, X) return Y