Skip to content

Instantly share code, notes, and snippets.

Avatar

Miles Cranmer MilesCranmer

View GitHub Profile
@MilesCranmer
MilesCranmer / einop.py
Created Jan 5, 2021
Generic einops operation that performs a repeat, rearrange, or reduce based on indices
View einop.py
# Copy this into your code. Call with, e.g., einop(x, 'i j -> j', reduction='mean')
import functools
import einops as _einops
from einops.parsing import ParsedExpression
@functools.lru_cache(256)
def _match_einop(pattern: str, reduction=None, **axes_lengths: int):
"""Find the corresponding operation matching the pattern"""
left, rght = pattern.split('->')
left = ParsedExpression(left)
@MilesCranmer
MilesCranmer / analytic_approximation.py
Last active Oct 17, 2020
Analytic Approximation of log(1+erf(x)) for x in [-10, -5]. Uses PySR: https://pysr.readthedocs.io/, mpmath: http://mpmath.org/
View analytic_approximation.py
import numpy as np
from mpmath import mp, mpmathify
from pysr import *
#Set precision to 200 decimal places:
mp.dps = 200
x = np.linspace(-10, -5, num=300)
#High precision calculation:
@MilesCranmer
MilesCranmer / matrix_box_notation.tex
Last active Oct 24, 2020
Draw boxes for matrices in equation
View matrix_box_notation.tex
%Make sure to have \usepackage{tikz}
%https://tex.stackexchange.com/a/45815/140440 - for grid
%https://tex.stackexchange.com/a/381175/140440 - for alignment in equation
% This function draws a matrix.
\newcommand{\mat}[2]{% cols, rows
\vcenter{\hbox{ %Vertical alignment
\begin{tikzpicture}[scale=0.3, align=center]
@MilesCranmer
MilesCranmer / .vimrc
Created Jul 6, 2020 — forked from yk/.vimrc
vimrc july 2020
View .vimrc
set nocompatible " be iMproved, required
let g:python3_host_prog = '/usr/local/opt/python@3.8/bin/python3.8'
if empty(glob('~/.vim/autoload/plug.vim'))
silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif
call plug#begin('~/.vim/plugged')
@MilesCranmer
MilesCranmer / writings-tracking-with-git.bash
Created Jun 11, 2020 — forked from markwk/writings-tracking-with-git.bash
My Writings Tracker with Git: A bash script for tracking my writings in plain tex files, calculate stats like word count, hashtags and new files, store stats to csv and commit to git.
View writings-tracking-with-git.bash
#!/bin/bash
##################################
#
# THE ARCHIVE TRACKER
#
# REF: https://gist.github.com/markwk/c85a8a72bc8c03d0f510262bb5219a34/
#
# INTRODUCTION:
# Daily script to navigate to a directory of plain text files,
# add files to git repo, calculate key stats, store stats to csv
@MilesCranmer
MilesCranmer / pretty_plots
Created Jan 28, 2020
Pretty plots taken from Neural Tangents notebook
View pretty_plots
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('pdf', 'svg')
import matplotlib
import seaborn as sns
sns.set(font_scale=1.3)
sns.set_style("darkgrid", {"axes.facecolor": ".95"})
import matplotlib.pyplot as plt
View vanden_berk.dat
Title: Composite Quasar Spectra From the Sloan Digital Sky Survey
Authors: Vanden Berk D.E., Richards G.T., Bauer A., Strauss M.A.,
Schneider D.P., Heckman T.M., York D.G., Hall P.B., Fan X., Knapp G.R.,
Anderson S.F., Annis J., Bahcall N.A., Bernardi M., Briggs J.W.,
Brinkmann J., Brunner R., Burles S., Carey L., Castander F.J.,
Connolly A.J., Crocker J.H., Csabai I., Doi M., Finkbeiner D.,
Friedman S., Frieman J.A., Fukugita M., Gunn J.E., Hennessy G.S.,
Ivezic Z., Kent S., Kunszt P.Z., Lamb D.Q., Leger R.F., Long D.C.,
Loveday J., Lupton R.H., Meiksin A., Merelli A., Munn J.A.,
Newberg H.J., Newcomb M., Nichol R.C., Owen R., Pier J.R., Pope A.,
View use_pymc3_like_emcee.py
##########################################
#emcee example:
##########################################
import numpy as np
import emcee
# Number of samples for each chain.
N_samp = 1000
def log_prob(x):
@MilesCranmer
MilesCranmer / build_vim_with_conda.sh
Created May 14, 2019
Build vim in a conda environment
View build_vim_with_conda.sh
#!/bin/bash
#This builds vim with python + python3 support in a conda environment. No sudo required! Replace the directories to your own.
vi_cv_path_python=/mnt/home/mcranmer/miniconda3/envs/py27/bin/python \
vi_cv_path_python3=/mnt/home/mcranmer/miniconda3/envs/main/bin/python \
./configure \
--with-features=huge \
--enable-multibyte \
--enable-rubyinterp=yes \
@MilesCranmer
MilesCranmer / simple_detex.vim
Last active Dec 22, 2018
Remove latex code so can feed paper into grammarly.com
View simple_detex.vim
" Delete up to begin{document}, and from end{document} to before the bibliography.
:%s/\\cite{[a-zA-Z0-9_]\+}/REF/g
:%s/\\citealt{[a-zA-Z0-9_]\+}/REF/g
:%s/\\cref{[a-zA-Z0-9_:]\+}/REF/g
:%s/\\caption{//g
:%s/\$\$.\{-}\$\$/REF/g
:%s/\$.\{-}\$/REF/g
:%s/\\[a-zA-Z_]\+{.*}/ /g
:%s/\\[a-zA-Z_]\+/ /g
You can’t perform that action at this time.