def make_fig2(save=False): data_params = {} data_params[ 'path'] = '/research/vibs/Pedro/datasets/motorimagery/BCI-competitions/BCI-IV/2a/' data_params['session'] = 1 data_params['task'] = 1 data_params['tparams'] = [1.25, 3.75] data_params['fparams'] = [8.0, 35.0] data_params['subject'] = 5 X, yworst = get_data(data_params) X = X[(yworst == 1) | (yworst == 2)] yworst = yworst[(yworst == 1) | (yworst == 2)] covs = Covariances().fit_transform(X) uworst, lworst = get_diffusionEmbedding(points=covs, distance=distance_riemann) data_params['subject'] = 1 X, ybest = get_data(data_params) X = X[(ybest == 1) | (ybest == 2)] ybest = ybest[(ybest == 1) | (ybest == 2)] covs = Covariances().fit_transform(X) ubest, lbest = get_diffusionEmbedding(points=covs, distance=distance_riemann) fig = plt.figure(figsize=(10.5, 5)) plt.subplots_adjust(wspace=0.020, hspace=0.025) plt.subplot(1, 2, 1) colorst = [['b', 'r'][int(t)] for t in (yworst - 2)] plt.scatter(uworst[:, 1], uworst[:, 2], color=colorst, s=44) plt.xlabel(r'$\phi_1$', fontsize=26) plt.ylabel(r'$\phi_2$', fontsize=26) plt.xticks([]) plt.yticks([]) ttl = plt.title('Worst subject', fontsize=20) ttl.set_position([.5, 1.025]) ax = plt.subplot(1, 2, 2) colorst = [['b', 'r'][int(t)] for t in (ybest - 2)] plt.scatter(ubest[:, 1], ubest[:, 2], color=colorst, s=44) plt.xlabel(r'$\phi_1$', fontsize=26) plt.ylabel(r'$\phi_2$', fontsize=26) ax.yaxis.set_label_position("right") plt.xticks([]) plt.yticks([]) ttl = plt.title('Best subject', fontsize=20) ttl.set_position([.5, 1.025]) if save: name = 'figure2' savefigure(name) return [uworst, lworst], [ubest, lbest]
y = [] for _ in range(nt / 2): x.append(gen_sig(ns, condition=2)) y.append(1) for _ in range(nt / 2): x.append(gen_sig(ns, condition=4)) y.append(2) x = np.stack(x, axis=0) #%% import sys sys.path.append('../diffusion/') from diffusionmap import get_diffusionEmbedding u_spectrum, _ = get_diffusionEmbedding(x, distance_spectrum) covs = Covariances().fit_transform(x) u_covariance, _ = get_diffusionEmbedding(covs, distance_riemann) #%% plt.figure(figsize=(13, 6)) plt.subplot(1, 2, 1) for i in range(nt): plt.scatter(u_spectrum[i, 1], u_spectrum[i, 2], color=['b', 'r'][y[i] - 1]) plt.subplot(1, 2, 2) for i in range(nt): plt.scatter(u_covariance[i, 1], u_covariance[i, 2], color=['b', 'r'][y[i] - 1])
points = points[idx] theta = theta[idx] return points, theta def get_color(x): # note that x goes from 0 to 1 colormap = plt.cm.gist_rainbow return colormap(x) Npoints = 150 distance = distance_euc points, theta = gen_data(Npoints) fig = plt.figure(figsize=(16, 7)) ax = fig.add_subplot(121, projection='3d') for i, point in enumerate(points): thetan = theta[i] / max(theta) ax.scatter(point[0], point[1], point[2], color=get_color(thetan)) ax = fig.add_subplot(122) u, s = get_diffusionEmbedding(points, distance, alpha=1.0) for i in range(Npoints): thetan = theta[i] / max(theta) ax.scatter(s[1] * u[i, 1], s[2] * u[i, 2], color=get_color(thetan)) ax.axis('equal')
fs = struct['freq'] fini = 4.0 fend = 40.0 b, a = signal.butter(5, [fini / (fs / 2), fend / (fs / 2)], btype='bandpass') for xt in X: f, pxx = welch(xt, fs=fs) xt = signal.filtfilt(b, a, xt) #%% from pyriemann.estimation import Covariances covs = Covariances(estimator='oas').fit_transform(X) from diffusionmap import get_diffusionEmbedding u, l = get_diffusionEmbedding(points=covs, distance=distance_riemann) #%% fig = plt.figure(figsize=(11, 9.6)) ax = fig.add_subplot(111, projection='3d') green = Color("green") colors = list(green.range_to(Color("red"), int(np.max(lat) + 1))) for i in range(len(covs)): ax.scatter(u[i, 1], u[i, 2], u[i, 3], color=colors[int(lat[i])].get_rgb(), s=44) ax.set_xlim(-3, +3)
filepaths = glob.glob(path + '*.jpg') X = [] for filepath in filepaths: I = imread(filepath, flatten=True) I = I[::4, ::4] X.append(I) X = np.stack(X) #%% def distance_image(IX, IY): return np.linalg.norm(IX-IY) from diffusionmap import get_diffusionEmbedding u,l = get_diffusionEmbedding(points=X, distance=distance_image) #%% for ifig in range(len(filepaths)): fig = plt.figure(figsize=(23.8,9.4), facecolor='white') plt.subplot(1,2,1) for ui in u: plt.scatter(ui[1], ui[2], color='k', s=25) plt.scatter(u[ifig,1], u[ifig,2], s=160, color='r') plt.xlim(-2,2) plt.ylim(-2,3) plt.subplot(1,2,2)
def make_fig3(save=False): data_params = {} data_params[ 'path'] = '/research/vibs/Pedro/datasets/motorimagery/BCI-competitions/BCI-IV/2a/' data_params['task'] = 1 data_params['tparams'] = [1.25, 3.75] data_params['fparams'] = [8.0, 35.0] data_params['subject'] = 1 data_params['session'] = 1 X1, y1 = get_data(data_params) X1 = X1[(y1 == 1) | (y1 == 2)] y1 = y1[(y1 == 1) | (y1 == 2)] data_params['session'] = 2 X2, y2 = get_data(data_params) X2 = X2[(y2 == 1) | (y2 == 2)] y2 = y2[(y2 == 1) | (y2 == 2)] X2 = np.delete(X2, (37), axis=0) # delete bad trial y2 = np.delete(y2, (37), axis=0) # delete bad trial y = np.concatenate((y1, y2), axis=0) covs = Covariances().fit_transform(X1) u1, l1 = get_diffusionEmbedding(points=covs, distance=distance_riemann) covs = Covariances().fit_transform(X2) u2, l2 = get_diffusionEmbedding(points=covs, distance=distance_riemann) covs = Covariances().fit_transform(np.concatenate((X1, X2), axis=0)) u, l = get_diffusionEmbedding(points=covs, distance=distance_riemann) fig1 = plt.figure(figsize=(15.5, 5)) plt.subplots_adjust(wspace=0.020, hspace=0.025) ax1 = plt.subplot(1, 3, 1) colorst = [['b', 'r'][int(t)] for t in (y1 - 2)] for i, ui in enumerate(u1): plt.scatter(ui[1], ui[2], color=colorst[i], s=44) ax1.set_xticks([]) ax1.set_yticks([]) plt.xlabel(r'$\phi_1$', fontsize=26) plt.ylabel(r'$\phi_2$', fontsize=26) ttl = plt.title('Session 1', fontsize=24) ttl.set_position([.5, 1.025]) ax2 = plt.subplot(1, 3, 2) colorst = [['b', 'r'][int(t)] for t in (y2 - 2)] for i, ui in enumerate(u2): plt.scatter(ui[1], ui[2], color=colorst[i], s=44) ax2.set_xticks([]) ax2.set_yticks([]) plt.xlabel(r'$\phi_1$', fontsize=26) ttl = plt.title('Session 2', fontsize=24) ttl.set_position([.5, 1.025]) ax3 = plt.subplot(1, 3, 3) colorst = [['b', 'r'][int(t)] for t in (y - 2)] markerst = ['o' for _ in range(len(y1))] + ['*' for _ in range(len(y2))] for i, ui in enumerate(u): plt.scatter(ui[1], ui[2], color=colorst[i], s=44, marker=markerst[i]) ax3.set_xticks([]) ax3.set_yticks([]) plt.xlabel(r'$\phi_1$', fontsize=26) plt.ylabel(r'$\phi_2$', fontsize=26) ax3.yaxis.set_label_position("right") ttl = plt.title('Both sessions', fontsize=24) ttl.set_position([.5, 1.025]) if save: name = 'figure3a' savefigure(name) fig2 = plt.figure(figsize=(9, 8)) ax = fig2.add_subplot(111, projection='3d') colorst = [['b', 'r'][int(t)] for t in (y - 2)] markerst = ['o' for _ in range(len(y1))] + ['*' for _ in range(len(y2))] for i, ui in enumerate(u): ax.scatter(ui[1], ui[2], ui[3], color=colorst[i], s=44, marker=markerst[i]) ax.set_xlabel(r'$\phi_1$', fontsize=26) ax.set_ylabel(r'$\phi_2$', fontsize=26) ax.set_zlabel(r'$\phi_3$', fontsize=26) ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([]) ttl = plt.title('Both sessions', fontsize=24) ttl.set_position([0.50, 0.975]) if save: name = 'figure3b' savefigure(name)
# plt.ylim(0,20) # ax = plt.gca() #% Sliding window for covariances and diffusion embedding L = 250 nt,nc,ns = X.shape covm = [] for w in tqdm(gen_windows(L, ns, step=32)): xw = X[:,:,w] covs = Covariances().fit_transform(xw) covm.append(mean_riemann(covs)) print 'getting the diffusion embedding' covm = np.stack(covm) u,l = get_diffusionEmbedding(covm, distance_riemann, alpha=1.0, tdiff=0) filepath = 'trajectory_real_bci-iv_subject' + str(subject) + '.pkl' embedding = [u,l] joblib.dump(embedding, filepath) #%% Visualization
state0 = np.array([+1, +1, +1]) state1 = np.array([-1, -1, -1]) sigma = 0.50 state0_pertub = np.array( [state0 + sigma * np.random.randn(3) for _ in range(Npoints / 2)]) state1_pertub = np.array( [state1 + sigma * np.random.randn(3) for _ in range(Npoints / 2)]) points = np.concatenate((state0_pertub, state1_pertub), axis=0) theta = np.zeros(Npoints) theta[Npoints / 2:] = 1 return points, theta Npoints = 200 distance = distance_euc points, theta = gen_data(Npoints) u, s = get_diffusionEmbedding(points, distance) fig = plt.figure(figsize=(18, 7)) colorst = [['b', 'r'][int(t)] for t in theta] ax = fig.add_subplot(121, projection='3d') ax.scatter(points[:, 0], points[:, 1], points[:, 2], color=colorst) ax = fig.add_subplot(122) ax.scatter(range(Npoints), u[:, 1], color=colorst)