# @title Install depedencies !pip install umap-learn --quiet import numpy as np import matplotlib.pyplot as plt from scipy.stats import zscore from sklearn.decomposition import PCA from umap import UMAP #@title Data retrieval import os, requests fname = "stringer_orientations.npy" url = "https://osf.io/ny4ut/download" if not os.path.isfile(fname): try: r = requests.get(url) except requests.ConnectionError: print("!!! Failed to download data !!!") else: if r.status_code != requests.codes.ok: print("!!! Failed to download data !!!") else: with open(fname, "wb") as fid: fid.write(r.content) # @title Figure settings from matplotlib import rcParams rcParams['figure.figsize'] = [20, 4] rcParams['font.size'] = 15 rcParams['axes.spines.top'] = False rcParams['axes.spines.right'] = False rcParams['figure.autolayout'] = True # @title Data loading dat = np.load('stringer_orientations.npy', allow_pickle=True).item() print(dat.keys()) print(dat['sresp'].shape) print(len(dat['stat'])) # @title plot the 3D positions of all neurons from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm x, y, z = dat['xyz'] zunq, iunq = np.unique(z, return_inverse=True) xc = np.linspace(0.0, 1.0, len(zunq)) cmap = cm.get_cmap('jet')(xc) fig = plt.figure(figsize=(6, 6)) ax = fig.add_subplot(111, projection='3d') ax.scatter(x[::-1], y[::-1], z[::-1], 'o', s=4, c=cmap[iunq]) ax.set(xlabel='horizontal(um)', ylabel='vertical(um)', zlabel='depth (um)') plt.show() # @title Basic data properties using plot, hist and scatter ax = plt.subplot(1, 5, 1) plt.hist(dat['istim']) ax.set(xlabel='orientations', ylabel='# trials') ax = plt.subplot(1, 5, 2) plt.scatter(dat['istim'], dat['sresp'][1000], s=1) ax.set(xlabel='orientation', ylabel='neural response') ax = plt.subplot(1, 5, 3) plt.plot(dat['run'][:1000]) ax.set(xlabel='timepoints', ylabel='running') ax = plt.subplot(1, 5, 4) plt.scatter(dat['run'], dat['sresp'][20998], s=1) ax.set(xlabel='running', ylabel='neural response') plt.show() # @title take PCA after preparing data by z-score Z = zscore(dat['sresp'], axis=1) X = PCA(n_components=200).fit_transform(Z.T) # @title plot PCs as function of stimulus orientation for j in range(5): ax = plt.subplot(1, 5, j + 1) plt.scatter(dat['istim'], X[:, j], s=1) ax.set(xlabel='orientation', ylabel='PC%d'%j) plt.show() # @title run a manifold embedding algorithm (UMAP) in two or three dimensions. ncomp = 3 # try 2, then try 3 xinit = 3 * zscore(X[:, :ncomp], axis=0) embed = UMAP(n_components=ncomp, init=xinit, n_neighbors=25, metric='correlation', transform_seed=42).fit_transform(X) plt.figure(figsize=(8,8)) for i in range(ncomp): for j in range(ncomp): plt.subplot(ncomp,ncomp, j + ncomp*i + 1) if i == j: plt.scatter(dat['istim'], embed[:, i], s=1) else: plt.scatter(embed[:, j], embed[:, i], s=1, c=dat['istim'], cmap='hsv') plt.show() plt.figure(figsize=(8, 4)) plt.subplot(1, 2, 1) plt.scatter(embed[:, 0], embed[:, 2], s=4, c=dat['istim'], cmap='hsv') plt.title('colored by stimulus orientation') plt.subplot(1, 2, 2) plt.scatter(embed[:, 0], embed[:, 2], s=4, c=dat['run'], vmin=0, vmax=10) plt.title('colored by running speed') plt.show()