Skip to content

Instantly share code, notes, and snippets.

@dkirkby
dkirkby / chisq_calc.py
Last active May 21, 2020 15:23
Chi-square probability calculations
import scipy.stats
def nsigmas_to_CL(nsigmas=1., D=1):
return scipy.stats.chi2.cdf(x=nsigmas ** 2, df=D)
def CL_to_nsigmas(CL=0.95, D=1):
return np.sqrt(scipy.stats.chi2.ppf(q=CL, df=D))
# A "1-sigma" error bar contains ~68% of a 1D Gaussian, but only ~39% in 2D.
nsigmas_to_CL(nsigmas=1, D=[1, 2])
@dkirkby
dkirkby / unshuffle.py
Last active May 19, 2020 21:13
Unshuffle
# Calculate the indexing of B that equals A, assuming that A is a shuffle of B and all elements are unique.
def unshuffle(A, B):
assert (A.shape == B.shape) and np.array_equal(np.unique(A), np.unique(B))
sorter = np.argsort(B)
return sorter[np.searchsorted(B, A, sorter=sorter)]
N = 10
A = np.random.uniform(size=N)
B = np.random.choice(A, N, replace=False)
assert np.array_equal(A, B[unshuffle(A, B)])
@dkirkby
dkirkby / .bashrc
Last active November 14, 2022 23:57
Login environment shell customizations
# Add these to ~/.bash_aliases, creating the file (if necessary)
# then add these lines to your ~/.bashrc (if necessary)
#
# if [ -f ~/.bash_aliases ]; then
# . ~/.bash_aliases
# fi
# OS X 12 uses zsh so these go into .zshrc instead
alias last='history | fgrep'
def animate(files, save='animation.gif', interval=500, dpi=64):
images = []
fig, ax = None, None
for f in files:
imgdata = plt.imread(f)
if fig is None:
ny, nx = imgdata.shape[:2]
fig = plt.figure(figsize=(nx / dpi, ny / dpi), dpi=dpi, frameon=False)
ax = plt.axes((0, 0, 1, 1))
ax.axis('off')
@dkirkby
dkirkby / grade.py
Last active December 21, 2020 23:44
import collections
def grade(file, offset,
breaks=collections.OrderedDict(
{'A+': 96.5, 'A': 93.5, 'A-': 90.0,
'B+': 86.5, 'B': 83.5, 'B-': 80.0,
'C+': 76.5, 'C': 73.5, 'C-': 70.0,
'D+': 66.5, 'D': 63.5, 'D-': 60.0, 'F': 0})):
stats = collections.OrderedDict({k: 0 for k in breaks})
values = []
@dkirkby
dkirkby / add_top_axis.py
Created November 26, 2019 17:49
Add a top axis to a matplotlib graph
def add_top_axis(ax, bottom_grid, top_grid, ticks, fmt='%s', title=None, color='k',
grid=False, gridopts=dict(ls=':', alpha=0.25)):
scale = ax.get_xscale()
if scale not in ('linear', 'log'):
raise RuntimeError('xscale "{0}" not yet supported.'.format(scale))
top = ax.twiny()
sign = -1 if top_grid[-1] < top_grid[0] else +1
tick_bottom_coord = np.interp(ticks, top_grid[::sign], bottom_grid[::sign])
if scale == 'linear':
tick_loc = (tick_bottom_coord - bottom_grid[0]) / (bottom_grid[-1] - bottom_grid[0])
# Reuse the automatically selected color from a plot
line2d = plt.plot([0, 1], [0, 1], ':')
plt.scatter([0, 1], [0, 1], c=line2d[0].get_color())
# Reuse the automatically selected color from a scatter
pathcol = plt.scatter([0, 1], [0, 1])
plt.plot([0, 1], [0, 1], c=pathcol.get_fc()[0], ls=':')
# Use the j-th default color
c = plt.rcParams['axes.prop_cycle'].by_key()['color'][j]
## DESI env using python 3.12
conda create -n desi312 python=3.12 pip ipython jupyter jupyterlab ipykernel numpy scipy pandas matplotlib pyyaml requests psycopg2 sqalchemy astropy skimage
conda activate desi
conda install -c conda-forge fitsio
pip install SQLAlchemy
# https://docs.nersc.gov/services/jupyter/how-to-guides/#how-to-use-a-conda-environment-as-a-python-kernel
python -m ipykernel install --user --name desi312 --display-name DESI312
# Install local packages
import time, sys
import IPython.display
class ProgressBar(object):
"""Replace existing contents of the current output cell with a progress bar.
"""
def __init__(self, maxval=1., label='Progress', width=40):
self.maxval = maxval
self.label = label
self.width = width
# Get the latest name using tab completion or check sys.prefix from within jupyterhub
#module load python/3.8-anaconda-2020.11
module load python/3.9-anaconda-2021.11
# Create a new env. Only list the pkgs you need since they will be downloaded into ~/.conda/pkgs/
#conda create -n desi pip ipython jupyter ipykernel numpy scipy matplotlib pyyaml
conda create -n desi39 pip ipython jupyter ipykernel numpy scipy matplotlib pyyaml astropy pandas
# conda activate <env> *was* not supported at NERSC yet but is now
conda activate desi39