Skip to content

random snippets

python

numpy

Display a Numpy array with some precision

x = np.random.random(10)

with np.printoptions(precision=2, suppress=True, linewidth=300):
    print(x)

Find sign changes

dx_traj = np.diff(x_traj)
sign_dx_traj = np.sign(dx_traj)
idxs_turning = np.r_[[0], np.where(sign_dx_traj[:-1] != sign_dx_traj[1:])[0] + 1, [len(x_traj)-1]]

pandas

Display a Pandas dataframe without truncation

from IPython.display import display

with pd.option_context('display.max_rows', None, 'display.max_columns', None):
    display(df)

matplotlib

Sample a continuous colormap for plotting

TODO: make a demo

cmap = mpl.colormaps.get('cool')
lognorm = mpl.colors.LogNorm(lamb_arr.min(), lamb_arr.max())

fig, ax = plt.subplots(figsize=(6,4))
for ilamb, lamb in enumerate(lamb_arr):
    ax.plot(theta_arr[ilamb], marker='.', linestyle='--', color=cmap(lognorm(lamb)))
ax.set(xlabel='index', ylabel='theta_arr', title='$\\theta$ grid, as function of $\\lambda$')
plt.colorbar(mpl.cm.ScalarMappable(norm=lognorm, cmap=cmap), ax=ax, label='$\\lambda$ [rad]')
plt.show()

3D plotting boilerplate

TODO

Pi multiple ticker

ax.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(lambda val, pos: f'{val/(math.pi):0g}$\\pi$' if val != np.pi else '$\\pi$' if val != 0 else '0'))
ax.yaxis.set_major_locator(mpl.ticker.MultipleLocator(base=math.pi/2))

Remove your spines

ax.spines.left.set_visible(False)
ax.spines.right.set_visible(False)
ax.spines.top.set_visible(False)
ax.spines.bottom.set_visible(False)

All colors

all_colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
colors = dict((a, all_colors[a%len(all_colors)]) for a in range(s[0]))

bash and command line tools

watch for changes without inotifywait

while true; do make; sleep 1; echo 'waiting for changes...'; watch -d -t -g "ls -lR src | sha1sum" 2>&1 > /dev/null; done

slides printout

pdfjam --frame true --nup 1x2 --a4paper inputfile.pdf

to make a booklet

pdfjam --booklet true --a4paper --landscape --outfile /dev/stdout inputfile.pdf - > outputfile.pdf

to make a zine

Adapted from equa.space

pdfjam --angle 180 inputfile.pdf '5-2' --a4paper --outfile /dev/stdout | pdfjam --landscape --a4paper --nup 4x2 --outfile /dev/stdout /dev/stdin - inputfile.pdf '6-8,1' > outfile.pdf

backup a folder .tar.gz over ssh

ssh kitty@remotepc 'tar -czf - important_folder' > "important_folder.bkp-$(date --iso-8601=seconds).tar.gz" 

podman tmp shell

podman run -it --rm --volume "$(PWD)/shared:/mnt/shared:Z" alpine bash