Skip to content

Instantly share code, notes, and snippets.

Johannes Buchner JohannesBuchner

Block or report user

Report or block JohannesBuchner

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
JohannesBuchner /
Last active Aug 12, 2019
Obtain correct error bars from observed counts (fraction in sample, detected events, etc.)
If you ever made a plot of "fractions" or "rates" with symmetric error
bars, like in the plot shown, this mini-tutorial is for you. Here is how
to compute correct error bars, so that uncertainties in the fractions
do not go below 0% or above 100%.
If you have a histogram for
instance, and you detected k objects in a given bin. What is the rate
underlying at quantiles q=0.1, 0.5(median), 0.9?
JohannesBuchner /
Created Aug 5, 2019
Minimalistic MCMC implementation
import numpy as np
def mcmc(logfunction, x0, nsteps, sigma_p):
samples = np.empty((nsteps, len(x0)))
logL0 = logfunction(x0)
naccepts = 0
for i in range(nsteps):
x1 = np.random.normal(x0, sigma_p)
logL1 = logfunction(x1)
if logL1 - logL0 > np.log(np.random.uniform()):
JohannesBuchner /
Created Jun 2, 2019
Translate a csv/npy/csv.gz integer file to efficiently compressed HDF5
import sys
import numpy
import h5py
filename = sys.argv[1]
outfilename = filename.replace('.npy', '').replace('.gz', '').replace('.csv', '') + '.h5'
if filename.endswith('.npy'):
print('loading NPY...')
data = numpy.load(filename)
Download custom spectrum from, programmatically
How to use:
$ python grbid timeslicespec
$ wget -nc --continue $(cat customspec.url)/a.tar.gz
wget -nc
grep -Eo GaiaSource_.*.csv.gz index.html | while read i; do
if [ -e $i ]; then
wget "$i" -O - | zcat | LC_ALL=C awk -F, '{print $2,$6,$8,sqrt($7**2+$9**2),$48}' | tail -n +2 | gzip > $i.tmp && mv $i.tmp $i
JohannesBuchner /
Created Jan 19, 2019
script for releasing packages on PyPI and publishing sphinx documentation on github pages
# Author: Johannes Buchner (C) 2013
# tool for publishing sphinx documentation on github
# and releasing packages on PyPI
case "$1" in
# see
make -C doc/ html &&
git checkout gh-pages &&
JohannesBuchner /
Last active Jan 10, 2019
Arxiv submission tool
# To submit just at the right time (deadline 14:00 EST),
# How to use:
# - Make sure your clock is synced to
# - Prepare arxiv submission up to last page, move cursor over submission button
# - execute below function which will produce a left-button mouse click at 14:00
# - if too early, unsubmit, fix clock, try again the next day
# Hopefully one day arxiv will randomize their submission ordering.
JohannesBuchner /
Last active Dec 2, 2018
Draw random numbers from broken powerlaw (broken code ATM)
import numpy
# Sampling from a broken powerlaw or powerlaw segments
# Reference material:
# The strategy is to draw from each powerlaw segment, and make sure the proportions are right based on the segment integrals
# This code does not work correctly, improvements are welcome
JohannesBuchner /
Created Apr 6, 2018
Make maps with uncertainties
# based on
import numpy
import colorsys
from matplotlib.colors import hsv_to_rgb
def to_rgb(value, error):
z = value
f1 = -90 - z*300
JohannesBuchner /
Created May 16, 2017
Fitting of stellar mass function with only two, uncertain data points
import matplotlib.pyplot as plt
import numpy
from numpy import log, exp
import scipy.misc
import scipy.optimize
# see Buchner et al (2015), Appendix A, for details on the method
Nsamples = 1000
You can’t perform that action at this time.