Instantly share code, notes, and snippets.

# Jonah Pearl jonahpearl

Created April 15, 2023 14:14
Vec to angle and angle to vec for numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 def vec_to_angle(v, degrees=False): """Convert any vector to its radian equivalent (0-2pi). If degrees, convert output to degrees. """ a = np.arctan2(v[:,1], v[:,0]) # returns -180 to 180 a[a<0] += 2*np.pi # convert to 0-360 if degrees: a = a / np.pi * 180 return a def angle_to_vec(a, degrees=False): """Convert an angle to a unit vector. If degrees, convert back to radians before computing.
Last active March 20, 2023 19:18
Re-indent directory of ipynb's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 #!/bin/zsh # Will convert all files in current dir, recursively. Could be modified to take args but I'm lazy! for filename in ./**/*.ipynb; do # Split the file name extension="\${filename##*.}"; filename_noext="\${filename%.*}";
Created December 16, 2021 16:19
Template for video from mpl plots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 import matplotlib.pyplot as plt import numpy as np import imageio as iio def my_plt_plotting_func(data): plt.plot(data) # can be as extensive as you want, as long as it just makes a single pyplot fig def write_frame(data, vid): my_plt_plotting_func(data) plt.savefig('current_frame.tiff')
Last active December 22, 2021 21:50
Matplotlib scatter with categorical colors and a custom legend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 import numpy as np import matplotlib.pyplot as plt from matplotlib.lines import Line2D x = np.arange(0, 2*np.pi, 0.1) y = np.sin(x) clusters = np.random.randint(0,5, size=(x.shape[0],)) colors = plt.rcParams['axes.prop_cycle'].by_key()['color'] plt.scatter(x, y, c=[colors[c] for c in clusters]) plt.legend(handles=[Line2D([0],