def test_fit_independence(get_covmats): n_trials, n_channels = 6, 3 covmats = get_covmats(n_trials, n_channels) embd = Embedding() embd.fit_transform(covmats) # retraining with different size should erase previous fit new_covmats = covmats[:, :-1, :-1] embd.fit_transform(new_covmats)
def test_embedding(metric, eps, get_covmats): """Test Embedding.""" n_trials, n_channels, n_comp = 6, 3, 2 covmats = get_covmats(n_trials, n_channels) embd = Embedding(metric=metric, n_components=n_comp, eps=eps) covembd = embd.fit_transform(covmats) assert covembd.shape == (n_trials, n_comp)
def xdawn_embedding(data, use_xdawn): """Perform embedding of EEG data in 2D Euclidean space with Laplacian Eigenmaps. Parameters ---------- data : dict A dictionary containing training and testing data Returns ------- array Embedded """ if use_xdawn: nfilter = 3 xdwn = XdawnCovariances(estimator='scm', nfilter=nfilter) covs = xdwn.fit(data['train_x'], data['train_y']).transform(data['test_x']) lapl = Embedding(metric='riemann', n_components=3) embd = lapl.fit_transform(covs) else: tangent_space = Pipeline([ ('cov_transform', Covariances(estimator='lwf')), ('tangent_space', TangentSpace(metric='riemann')) ]) t_space = tangent_space.fit(data['train_x'], data['train_y']).transform(data['test_x']) reducer = umap.UMAP(n_neighbors=30, min_dist=1, spread=2) embd = reducer.fit_transform(t_space) return embd
def plot_embedding(X, y=None, metric='riemann', title='Spectral embedding of covariances'): """Plot 2D embedding of covariance matrices using Diffusion maps.""" lapl = Embedding(n_components=2, metric=metric) embd = lapl.fit_transform(X) if y is None: y = np.ones(embd.shape[0]) fig, ax = plt.subplots(figsize=(7, 7), facecolor='white') for label in np.unique(y): idx = (y == label) ax.scatter(embd[idx, 0], embd[idx, 1], s=36) ax.set_xlabel(r'$\varphi_1$', fontsize=16) ax.set_ylabel(r'$\varphi_2$', fontsize=16) ax.set_title(title, fontsize=16) ax.grid(False) ax.set_xticks([-1.0, -0.5, 0.0, +0.5, 1.0]) ax.set_yticks([-1.0, -0.5, 0.0, +0.5, 1.0]) ax.legend(list(np.unique(y))) return fig
def xdawn_embedding(data): """Perform embedding of EEG data in 2D Euclidean space with Laplacian Eigenmaps. Parameters ---------- data : dict A dictionary containing training and testing data Returns ------- array Embedded """ nfilter = 3 xdwn = XdawnCovariances(estimator='scm', nfilter=nfilter) covs = xdwn.fit(data['train_x'], data['train_y']).transform(data['test_x']) lapl = Embedding(metric='riemann', n_components=3) embd = lapl.fit_transform(covs) return embd
mean=mean, sigma=sigma / 2, random_state=random_state) samples_3 = sample_gaussian_spd(n_matrices=n_matrices, mean=epsilon * mean, sigma=sigma, random_state=random_state) # Stack all of the samples into one data array for the embedding samples = np.concatenate([samples_1, samples_2, samples_3]) labels = np.array(n_matrices * [1] + n_matrices * [2] + n_matrices * [3]) ############################################################################### # Apply the spectral embedding over the SPD matrices lapl = Embedding(metric='riemann', n_components=2) embd = lapl.fit_transform(X=samples) ############################################################################### # Plot the results fig, ax = plt.subplots(figsize=(8, 6)) colors = {1: 'C0', 2: 'C1', 3: 'C2'} for i in range(len(samples)): ax.scatter(embd[i, 0], embd[i, 1], c=colors[labels[i]], s=50) ax.scatter([], [], c='C0', s=50, label=r'$\varepsilon = 1.00, \sigma = 1.00$') ax.scatter([], [], c='C1', s=50, label=r'$\varepsilon = 1.00, \sigma = 0.50$') ax.scatter([], [], c='C2', s=50, label=r'$\varepsilon = 4.00, \sigma = 1.00$') ax.set_xticks([-1, -0.5, 0, 0.5, 1.0]) ax.set_xticklabels([-1, -0.5, 0, 0.5, 1.0], fontsize=12) ax.set_yticks([-1, -0.5, 0, 0.5, 1.0])
picks=picks, baseline=None, preload=True, verbose=False) X = epochs.get_data() y = epochs.events[:, -1] ############################################################################### # Embedding the Xdawn covariance matrices with Laplacian Eigenmaps nfilter = 4 xdwn = XdawnCovariances(estimator='scm', nfilter=nfilter) split = train_test_split(X, y, train_size=0.25, random_state=42) Xtrain, Xtest, ytrain, ytest = split covs = xdwn.fit(Xtrain, ytrain).transform(Xtest) lapl = Embedding(metric='riemann', n_components=2) embd = lapl.fit_transform(covs) ############################################################################### # Plot the three first components of the embedded points fig, ax = plt.subplots(figsize=(7, 8), facecolor='white') for cond, label in event_id.items(): idx = (ytest == label) ax.scatter(embd[idx, 0], embd[idx, 1], s=36, label=cond) ax.set_xlabel(r'$\varphi_1$', fontsize=16) ax.set_ylabel(r'$\varphi_2$', fontsize=16) ax.set_title('Spectral embedding of ERP recordings', fontsize=16) ax.set_xticks([-1.0, -0.5, 0.0, +0.5, 1.0]) ax.set_yticks([-1.0, -0.5, 0.0, +0.5, 1.0])
verbose=False) X = epochs.get_data() y = epochs.events[:, -1] ############################################################################### # Embedding the Xdawn covariance matrices with Laplacian Eigenmaps nfilter = 4 xdwn = XdawnCovariances(estimator='scm', nfilter=nfilter) split = train_test_split(X, y, train_size=0.25, random_state=42) Xtrain, Xtest, ytrain, ytest = split covs = xdwn.fit(Xtrain, ytrain).transform(Xtest) lapl = Embedding(metric='riemann', n_components=2) embd = lapl.fit_transform(covs) ############################################################################### # Plot the three first components of the embedded points fig, ax = plt.subplots(figsize=(7, 8), facecolor='white') for cond, label in event_id.items(): idx = (ytest == label) ax.scatter(embd[idx, 0], embd[idx, 1], s=36, label=cond) ax.set_xlabel(r'$\varphi_1$', fontsize=16) ax.set_ylabel(r'$\varphi_2$', fontsize=16) ax.set_title('Spectral embedding of ERP recordings', fontsize=16) ax.set_xticks([-1.0, -0.5, 0.0, +0.5, 1.0]) ax.set_yticks([-1.0, -0.5, 0.0, +0.5, 1.0])