Skip to content

Instantly share code, notes, and snippets.

@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.

@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):
@dkirkby
dkirkby / plotGradient.py
Last active July 31, 2020 14:29
Plot a rectangle with a gradient using matplotlib
def plotGradient(ax, xy, width, height, color='r', cmap=None, gradient=lambda x, y: x, alpha=lambda x,y: x ** 2):
"""
Args (xy), width, height as the same as for plt.Rectangle.
The axis must already have something drawn so that its coordinate system is defined.
The gradient is calculated per pixel at the time this is called. This will no longer be true
if there are later changes to the axis limits, but the gradient should still be visually ok.
However, for the best quality, do not change the axis limits after drawing gradients.
"""
# Convert the plot dimensions from data coords to pixels.
xy = np.asarray(xy)
@dkirkby
dkirkby / plot_pixels.py
Last active July 2, 2020 15:00
Plot an image without any pixel resampling
def plot_pixels(D, zoom=1, dpi=64, x0=0, y0=0, **args):
ny, nx = D.shape
width, height = zoom * nx, zoom * ny
fig = plt.figure(figsize=(width / dpi, height / dpi), dpi=dpi, frameon=False)
ax = plt.axes((0, 0, 1, zoom * ny / height))
extent = [x0 - 0.5, x0 + nx - 0.5, y0 - 0.5, y0 + ny - 0.5]
ax.imshow(D, extent=extent, **args)
ax.axis('off')
return fig, ax
@dkirkby
dkirkby / histogram_equalize.py
Created June 30, 2020 20:10
Perform histogram equalization on numpy array data
def equalize(A, clip_percent=5):
"""Equalize the values of an array.
The returned array has values between 0-1 such that clip_percent
of the values are clipped symmetrically at 0 and 1, and the
histogram of values between 0 and 1 is flat. This is a non-linear
transformation and primarily useful for showing small variations
over a large dynamic range.
"""
A_flat = A.reshape(-1)
n = len(A_flat)
@dkirkby
dkirkby / numpy_where.py
Last active April 15, 2021 15:13
Select rows from structured numpy array using a condition expression
import io
import tokenize
def select(data, condition, as_mask=False):
"""Return rows in the structured numpy array (or astropy table) that satisfy a condition.
The condition is a string expression using column names to represent column values.
"""
# Get the valid identifiers.
names = data.dtype.names