Skip to content

Instantly share code, notes, and snippets.


Miles Cranmer MilesCranmer

View GitHub Profile
def acos2(num, denom, disamb):
cosine = num/denom
return torch.where((cosine > -1) & (cosine < 1.),
torch.acos(cosine) * torch.where(disamb < 0.0, -1, 1),
torch.where(cosine <= -1, np.pi, 0.0)
def coord_transform(x):
# Assumes in CoM frame
MilesCranmer /
Created Mar 3, 2021
Trick to step backwards if a NaN occurs

Before training:

last = model.state_dict()

Inside training loop, after computing loss:

if torch.isnan(loss).sum().item():
MilesCranmer / mwe.jl
Last active Jan 27, 2021
Implementing SymbolicUtils.jl interface for SymbolicRegression.jl
View mwe.jl
using SymbolicUtils
mutable struct Node
#Holds operators, variables, constants in a tree
degree::Integer #0 for constant/variable, 1 for cos/sin, 2 for +/* etc.
val::Union{Float32, Integer, Nothing} #Either const value, or enumerates variable
constant::Bool #false if variable
op::Integer #enumerates operator (separately for degree=1,2)
l::Union{Node, Nothing}
r::Union{Node, Nothing}
MilesCranmer /
Created Jan 5, 2021
Generic einops operation that performs a repeat, rearrange, or reduce based on indices
# 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
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 /
Last active Oct 17, 2020
Analytic Approximation of log(1+erf(x)) for x in [-10, -5]. Uses PySR:, mpmath:
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 / matrix_box_notation.tex
Last active Feb 9, 2021
Draw boxes for matrices in equation
View matrix_box_notation.tex
%Make sure to have \usepackage{tikz}
% - for grid
% - 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 / .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
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
call plug#begin('~/.vim/plugged')
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
# REF:
# Daily script to navigate to a directory of plain text files,
# add files to git repo, calculate key stats, store stats to csv
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_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.,