Skip to content

Commit

Permalink
Updating to object oriented plots
Browse files Browse the repository at this point in the history
  • Loading branch information
yonibrowning committed Aug 29, 2022
1 parent c72a176 commit 14ac7dc
Show file tree
Hide file tree
Showing 8 changed files with 1,139 additions and 18,145 deletions.
75 changes: 38 additions & 37 deletions Tutorials/1_Principal_Components_Analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"plt.plot(X[:, 0], X[:, 1], '.');\n",
"plt.ylim(-3,3);\n",
"plt.xlim(-3,3);\n",
"\n",
"plt.axis('equal');\n",
"# Note that this dataset has a mean of 0 in both dimensions. This will be important later. "
]
},
Expand Down Expand Up @@ -191,14 +191,15 @@
"outputs": [],
"source": [
"# Now Lets look at how these eigenvectors fall relative to our data\n",
"fig, ax = plt.subplots(figsize=(5,5))\n",
"#fig, ax = plt.subplots(figsize=(5,5))\n",
"\n",
"ax.plot(X[:, 0], X[:, 1], '.')\n",
"plt.plot(X[:, 0], X[:, 1], '.')\n",
"for value, vector in zip(evalues, evectors.T):\n",
" ax.plot([0, 3.*np.sqrt(value)*vector[0]], [0, 3.*np.sqrt(value)*vector[1]], '-k', lw=3);\n",
" plt.plot([0, 3.*np.sqrt(value)*vector[0]], [0, 3.*np.sqrt(value)*vector[1]], '-k', lw=3);\n",
"\n",
"ax.set_ylim(-3,3);\n",
"ax.set_xlim(-3,3);"
"plt.ylim(-3,3);\n",
"plt.xlim(-3,3);\n",
"plt.axis('equal');"
]
},
{
Expand Down Expand Up @@ -303,7 +304,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -361,15 +362,15 @@
"outputs": [],
"source": [
"# We can plot these data to verify that everything matches up.\n",
"fig, ax = plt.subplots(figsize=(5,5))\n",
"\n",
"ax.plot(X[:, 0], X[:, 1], 'o')\n",
"ax = plt.subplot(1,1,1)\n",
"ax.plot(X[:, 0], X[:, 1], '.')\n",
"for length, vector in zip(pca.explained_variance_, pca.components_):\n",
" v = vector * 3 * np.sqrt(length)\n",
" ax.plot([0, v[0]], [0, v[1]], '-k', lw=3);\n",
"\n",
"ax.set_ylim(-3,3);\n",
"ax.set_xlim(-3,3);"
"ax.set_xlim(-3,3);\n",
"ax.axis('equal');"
]
},
{
Expand All @@ -395,7 +396,7 @@
"outputs": [],
"source": [
"# This, too, should look a lot like the plot above.\n",
"plt.plot(X_project[:,0], X_project[:,1], 'o');\n",
"plt.plot(X_project[:,0], X_project[:,1], '.');\n",
"plt.xlabel(\"First principal component\", fontsize=14);\n",
"plt.ylabel(\"Second principal component\", fontsize=14);\n",
"plt.axis('equal');"
Expand Down Expand Up @@ -463,13 +464,13 @@
" # EDIT location where you mounted hard drive\n",
" data_root = \"/media/$USERNAME/Brain2022/\"\n",
"\n",
"data_root = '../Data'\n",
"#data_root = '../Data'\n",
"# get the cache location\n",
"cache_dir = os.path.join(data_root, data_dirname)\n",
"\n",
"cache = VisualBehaviorNeuropixelsProjectCache.from_s3_cache(cache_dir=cache_dir)\n",
"#cache = VisualBehaviorNeuropixelsProjectCache.from_local_cache(\n",
"# cache_dir=cache_dir, use_static_cache=use_static)"
"#cache = VisualBehaviorNeuropixelsProjectCache.from_s3_cache(cache_dir=cache_dir)\n",
"cache = VisualBehaviorNeuropixelsProjectCache.from_local_cache(\n",
" cache_dir=cache_dir, use_static_cache=use_static)"
]
},
{
Expand Down Expand Up @@ -575,7 +576,7 @@
"fig, ax = plt.subplots(figsize = (20,10))\n",
"\n",
"rng = int(30/dt)\n",
"cax = ax.pcolormesh(X[:,:rng],\n",
"cax = plt.pcolormesh(X[:,:rng],\n",
" vmin = 0, vmax = np.percentile(X[:,:rng], 99),\n",
" cmap = 'magma')\n",
"ax.set_xticks(np.arange(0,rng,50))\n",
Expand Down Expand Up @@ -696,20 +697,19 @@
"source": [
"# Plot some behavior relative to the \n",
"licks = np.histogram(session.licks,tme)[0]>0\n",
"plt.figure()\n",
"plt.scatter(proj[licks==0,0],proj[licks==0,1],label = 'no lick')\n",
"plt.scatter(proj[licks==1,0],proj[licks==1,1],label = 'lick')\n",
"plt.legend()\n",
"plt.xlabel('PC1')\n",
"plt.ylabel('PC2')\n",
"\n",
"plt.figure()\n",
"plt.hist(proj[licks==0,0],density=True,label = 'no licks',alpha = .7)\n",
"plt.hist(proj[licks==1,0],density=True,label = 'licks',alpha = .7)\n",
"\n",
"plt.xlabel('PC1')\n",
"plt.ylabel('Fraction of bins')\n",
"plt.legend()"
"fig,ax = plt.subplots(nrows = 2,ncols = 1,figsize = (7,10))\n",
"ax[0].scatter(proj[licks==0,0],proj[licks==0,1],label = 'no lick')\n",
"ax[0].scatter(proj[licks==1,0],proj[licks==1,1],label = 'lick')\n",
"ax[0].legend()\n",
"ax[0].set_xlabel('PC1')\n",
"ax[0].set_ylabel('PC2')\n",
"\n",
"ax[1].hist(proj[licks==0,0],density=True,label = 'no licks',alpha = .7)\n",
"ax[1].hist(proj[licks==1,0],density=True,label = 'licks',alpha = .7)\n",
"\n",
"ax[1].set_xlabel('PC1')\n",
"ax[1].set_ylabel('Fraction of bins')\n",
"ax[1].legend();"
]
},
{
Expand All @@ -720,12 +720,13 @@
"source": [
"# Running speed\n",
"running_speed= interp1d(session.running_speed.timestamps,session.running_speed.speed)(tme[:-1])\n",
"plt.figure()\n",
"plt.scatter(proj[:,0],proj[:,1],c = running_speed,marker= '.')\n",
"plt.xlabel('PC1')\n",
"plt.ylabel('PC2') \n",
"cbar = plt.colorbar()\n",
"cbar.ax.set_ylabel('Running speed')\n"
"fig,ax = plt.subplots(figsize = (7,5))\n",
"sct = ax.scatter(proj[:,0],proj[:,1],c = running_speed,marker= '.')\n",
"ax.set_xlabel('PC1')\n",
"ax.set_ylabel('PC2') \n",
"#cbar = sct.colorbar()\n",
"#cbar.ax.set_ylabel('Running speed')\n",
"cb = plt.colorbar(sct, pad=0.015, label='Running Speed')"
]
},
{
Expand Down
Loading

0 comments on commit 14ac7dc

Please sign in to comment.