Skip to content

Instantly share code, notes, and snippets.

# 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)}.'
@dkirkby
dkirkby / MJD.py
Last active March 21, 2021 14:34
Convert between MJD and datetime
def mjd_to_date(mjd, utc_offset=-7):
"""Convert an MJD value to a datetime using the specified UTC offset in hours.
The default utc_offset of -7 corresponds to local time at Kitt Peak.
Use :func:`date_to_mjd` to invert this calculation.
"""
return datetime.datetime(2019, 1, 1) + datetime.timedelta(days=mjd - 58484.0, hours=utc_offset)
def date_to_mjd(date, utc_offset=-7):
@dkirkby
dkirkby / multiproc.py
Last active February 12, 2021 17:34
Demo of multiprocessing with shared numpy arrays
# Test parallel processing with shared memory.
import time
import multiprocessing
import multiprocessing.shared_memory # needs python >= 3.8
import numpy as np
class MultiDemo(object):
def __init__(self, names=['Bobby', 'Mo', 'Mane'], size=4):