Skip to content

Instantly share code, notes, and snippets.

@dfm
dfm / contour.py
Created March 9, 2012 21:12
How to plot nice 2d density plots of samples in python
import numpy as np
import scipy.special as sp
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.cm as cm
import matplotlib.pyplot as pl
def reshist2d(x, y, *args, **kwargs):
ax = kwargs.pop('ax', pl.gca())
extent = kwargs.pop('extent', [[x.min(), x.max()], [y.min(), y.max()]])
@dfm
dfm / triplot.py
Created March 30, 2012 14:45
Triangle plots
__all__ = ["triplot"]
import itertools
import matplotlib.pyplot as pl
from matplotlib.ticker import MaxNLocator
import numpy as np
def triplot(samples, titles=None, sfactor=0.1):
@dfm
dfm / booboos.sh
Created May 25, 2012 05:46
Count the number of occurrences of "FIXME", "TODO", etc. in the current git repository
function count_code_booboos() {
GIT_DIR=$(git rev-parse --git-dir 2> /dev/null) || return
NOTES_PATTERN="FIXME|TODO|MAGIC"
NOTES_RES=$(echo $(ack -hi --ignore-dir="venv" --ignore-dir="build" \
"\b$NOTES_PATTERN\b" "$(dirname $GIT_DIR)" 2>/dev/null))
for NM in "FIXME" "MAGIC" "TODO"
do
num=$(echo $(echo $NOTES_RES | ack -i "\b$NM\b" | wc -l))
if [ $num != 0 ]
then
@dfm
dfm / gaussfit.py
Created July 11, 2012 12:16
Fitting a Gaussian + constant background in Python
import numpy as np
import scipy.optimize as op
# x, y, yerr = ... read in your data
def chi(p):
sig2 = p[2] ** 2
m = p[0] * np.exp(-0.5 * (x - p[1]) ** 2 / sig2) / np.sqrt(2 * np.pi * sig2) + p[3]
return (y - m) / yerr
@dfm
dfm / data.dat
Created July 20, 2012 14:55
Code from Python lunch at MPIA
0.29012 4.69497 0.58980
0.39687 4.53831 0.61233
0.58085 4.58511 0.72283
0.79724 3.88045 0.58649
0.96492 3.28931 0.59474
1.44055 2.63219 0.52092
2.00743 1.76821 0.57815
2.71773 0.46981 0.64042
2.73360 0.32956 0.50058
3.04199 -0.22867 0.63828
@dfm
dfm / _chi2.c
Created August 3, 2012 13:41
How to wrap C code in Python
#include <Python.h>
#include <numpy/arrayobject.h>
#include "chi2.h"
/* Docstrings */
static char module_docstring[] =
"This module provides an interface for calculating chi-squared using C.";
static char chi2_docstring[] =
"Calculate the chi-squared of some data given a model.";
@dfm
dfm / interest.py
Created August 10, 2012 21:30
Implied interest in the ligbit2 repo
from __future__ import print_function
import json
from collections import defaultdict
import time
from datetime import datetime
import requests
import numpy as np
@dfm
dfm / weiß.py
Created August 12, 2012 18:59
Argh...
>>> str("weiß")
'wei\xc3\x9f'
>>> str(u"weiß")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xdf' in position 3: ordinal not in range(128)
@dfm
dfm / Makefile
Created August 13, 2012 19:51
ctypes chi2 example by @jrwren
libchi2.so: chi2.o
gcc -shared -Wl,-soname,libchi2.so.1 -o libchi2.so.1.0.0 chi2.o -lc
chi2.o: chi2.c
gcc -fPIC -g -c -Wall chi2.c
@dfm
dfm / triangle.py
Created September 6, 2012 18:47
Triangle plots v2.0
__all__ = ["hist2d", "error_ellipse", "corner"]
import numpy as np
import scipy.special as sp
import matplotlib.pyplot as pl
from matplotlib.ticker import MaxNLocator
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.patches import Ellipse
import matplotlib.cm as cm