Skip to content

Instantly share code, notes, and snippets.

View MilesCranmer's full-sized avatar

Miles Cranmer MilesCranmer

View GitHub Profile
@MilesCranmer
MilesCranmer / nobels_by_country.md
Last active June 18, 2017 11:55
Nobel prizes by country, per capita and per scientist, post-1985 and post-1970

Rankings

First, let's look at just physics.

Since this is a lot less data to work with, we'll take prizes from 1970 onwards.

Country Nobels in Physics 2016 population (millions) Nobels per million
Denmark 2 5.7 0.350877193
@MilesCranmer
MilesCranmer / tricks.md
Last active June 21, 2017 03:35
Programming tricks

Favorite tricks

(These are ranked in order of favourites)

Getting includes automatically in C

Set up ctags:

apt-get install exuberant-ctags
@MilesCranmer
MilesCranmer / word_count_changes.md
Last active February 22, 2024 23:17
Accurate word count changes in git, useful for tracking changes on a paper in overleaf

Words changed in a git repo

This should work generally, but I use this to track the number of words changed in a (LaTeX) paper with a version history in git (and which Overleaf uses by default).

This is a tricky thing to deal with for many reasons.

Show the added words, deleted words, words on duplicate lines on every commit in the last day (bash):

@MilesCranmer
MilesCranmer / numpy_cffi_arrays.py
Created January 29, 2018 00:18 — forked from arjones6/numpy_cffi_arrays.py
Passing multidimensional numpy arrays to C using cffi.
import numpy
from cffi import FFI
ffi = FFI()
ffi.cdef("""
void single_test(double *x, int n);
void multi_test(double **x, int n, int m);
""")
C = ffi.dlopen("./simple.so")
@MilesCranmer
MilesCranmer / plotdf.py
Last active November 18, 2018 02:36
Plot only 98th percentile of data with pandas with subsample
def plotdf(df, x, y, c=None, sample=10000, alpha=0.5, color=None,
cmap='viridis', clabel=None, xlabel=None, ylabel=None,
noise_df=None, percentile=0.98, use_noise_df_for_framing=False,
noise_alpha=0.1):
cols_to_get = [x, y]
if c is not None:
cols_to_get.append(c)
df = df[cols_to_get]
@MilesCranmer
MilesCranmer / plotdf.py
Created November 13, 2018 16:30
Plot only 99th percentile of data with pandas.
def plotdf(df, x, y, sample=10000):
df = df[[x, y]].sample(sample).copy()
for col in [x, y]:
df = df[(df[col] > df[col].quantile(0.01)) & (df[col] < df[col].quantile(0.99))]
df.plot(
x, y,
linestyle='', marker='.',
markersize=0.1)
@MilesCranmer
MilesCranmer / simple_detex.vim
Last active December 22, 2018 04:44
Remove latex code so can feed paper into grammarly.com
" 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
@MilesCranmer
MilesCranmer / build_vim_with_conda.sh
Created May 14, 2019 17:21
Build vim in a conda environment
#!/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 / use_pymc3_like_emcee.py
Last active October 2, 2019 02:37
Use PyMC3 like emcee
##########################################
#emcee example:
##########################################
import numpy as np
import emcee
# Number of samples for each chain.
N_samp = 1000
def log_prob(x):
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.,