Skip to content

Instantly share code, notes, and snippets.

View jonahpearl's full-sized avatar

Jonah Pearl jonahpearl

View GitHub Profile
@jonahpearl
jonahpearl / gist:38cd74926c225a8b4ef4be3a4e349bc1
Created April 15, 2023 14:14
Vec to angle and angle to vec for numpy
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.
@jonahpearl
jonahpearl / 2_to_4_tabs_ipynb.sh
Last active March 20, 2023 19:18
Re-indent directory of ipynb's
#!/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%.*}";
@jonahpearl
jonahpearl / vid_from_mpl_plots.py
Created December 16, 2021 16:19
Template for video from mpl plots
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')
@jonahpearl
jonahpearl / scatter_categorical_fixedLegend.txt
Last active December 22, 2021 21:50
Matplotlib scatter with categorical colors and a custom legend
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],