Skip to content

Instantly share code, notes, and snippets.

@dkirkby
dkirkby / plt_to_numpy.py
Created August 12, 2024 00:59
Capture matplotlib output to numpy array
def plt_to_numpy_demo(width=15, height=11, dpi=32, offscreen=True):
if offscreen:
# Switch to Agg backend to prevent display
backend = matplotlib.get_backend()
plt.close('all')
plt.switch_backend('agg')
# Create figure with exactly width x height pixels
fig = plt.figure(figsize=(width/dpi, height/dpi), dpi=dpi, frameon=False)
ax = fig.add_subplot()
@dkirkby
dkirkby / schema.csv
Last active October 24, 2024 13:23
Dump column names for all tables to CSV
import fpoffline.db
DB = fpoffline.db.DB()
dbname = 'telemetry'
schema = DB.query(f"SELECT table_name, column_name FROM information_schema.columns WHERE table_schema = '{dbname}' order by table_name asc", maxrows=5000)
schema.to_csv(f'{dbname}-schema.csv', index=None)
@dkirkby
dkirkby / get_contour_levels.py
Created July 3, 2024 12:58
Compute contour levels
def get_contour_levels(data, nsigmas=(2,1)):
assert data.ndim == 2
assert np.all(data >= 0)
assert np.sum(data) <= 1 + 1e-6
assert np.all(np.asarray(nsigmas) > 0)
assert np.all(np.diff(nsigmas) < 0)
# Convert sigmas to confidence levels
CL = scipy.stats.chi2.cdf(x=np.asarray(nsigmas) ** 2, df=1)
@dkirkby
dkirkby / jfind.sh
Created June 27, 2024 16:57
Find jupyter notebooks containing a string
find . -name "*.ipynb" -not -path "*checkpoint*" -print0 | xargs -0 fgrep -l coordinates
# https://developer.apple.com/metal/jax/
conda create -n apple-ml python=3.10 pip ipython jupyter jupyterlab ipykernel numpy wheel scipy pandas matplotlib
conda activate apple-ml
python -m pip install ml-dtypes==0.2.0 jax-metal
# Successfully installed jax-0.4.11 jax-metal-0.0.4 jaxlib-0.4.11 ml-dtypes-0.2.0 opt-einsum-3.3.0
## This installs a newer version of jaxlib that is incompatible with jax-metal ??
##python -m pip install -q -U flax
@dkirkby
dkirkby / jupyter_search.bash
Created August 10, 2023 22:46
search within jupyter notebooks
# exclude directories with specific names
find . -type d \( -name 'code' -o -name '.ipynb_checkpoints' \) -prune -o -name '*.ipynb' -type f -exec fgrep 'desietc' {} +
@dkirkby
dkirkby / fits2df.py
Created August 10, 2023 21:23
Create pandas dataframe from FITS table
# Need to convert from big-endian to little-endian to avoid this error:
# ValueError: Big-endian buffer not supported on little-endian compiler
data = fitsio.read(path).byteswap().newbyteorder()
df = pd.DataFrame(data)
  • Update version in setup.cfg, usually by remove .dev
  • Check that CHANGES.rst describes all changes since the last version
  • Replace unreleased with today's YYYY-MM-DD in CHANGES.rst
  • Commit and tag
git add .
git commit -m 'prepare for release'
git tag v0.15
  • Bump version in setup.cfg and append .dev
@dkirkby
dkirkby / gitdebug.txt
Last active September 15, 2021 18:21
Debug github ssh connection problems
# https://docs.github.com/en/github/authenticating-to-github/troubleshooting-ssh
# https://selleo.com/til/posts/m8eirl36cu-debugging-git-pushssh-hanging
% cat ~/bin/sshverbose.sh
#!/bin/bash
ssh -vvv "$@"
% GIT_SSH=~/bin/sshverbose.sh git pull

My minimal git config:

[user]
	name = David Kirkby
	email = [email protected]
[alias]
	ls = log --graph --pretty=format:'%C(blue)%h%Creset%C(red bold)%d%Creset %C(black)%s%Creset %C(green)(by %an %ar)%Creset' --all

This should normally be in ~/.gitconfig but if using a shared account (at KPNO for example), put it somewhere else then set GIT_CONFIG in your shell env to point to it.