This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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)]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |