Skip to content

Instantly share code, notes, and snippets.

@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 = dkirkby@uci.edu
[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.

@dkirkby
dkirkby / git-describe.py
Last active March 6, 2021 21:40
Get a git description of a package
# Put this anywhere within the package then call it at runtime to get a git description
# of the package version being used. This only works when the package is being imported
# from a directory checked out directly from github, which is generally not the case
# for pip- or conda-installed packages.
def git_describe():
"""Return a string describing the git origin of the package where this function is defined.
The result is usually <tag>-<n>-g<hash> where <tag> is the last tag, <n> is the number of
subsequent commits, and <hash> is the current git hash. When n=0, only <hash> is returned.
@dkirkby
dkirkby / floatingpoint.py
Last active March 6, 2021 18:45
Floating point representations and rounding
import struct
def decode_float(x):
"""Decode the fields of an IEEE 754 single or double precision float:
https://en.wikipedia.org/wiki/Single-precision_floating-point_format
https://en.wikipedia.org/wiki/Double-precision_floating-point_format
"""
if isinstance(x, np.floating):
bits = np.finfo(x.dtype).bits
assert bits in (32, 64), f'Unsupported numpy floating dtype: {type(x)}.'