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
JohannesBuchner / poissonerr.py
Last active Aug 12, 2019
Obtain correct error bars from observed counts (fraction in sample, detected events, etc.)
View poissonerr.py
"""
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
JohannesBuchner / mcmc.py
Created Aug 5, 2019
Minimalistic MCMC implementation
View mcmc.py
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
JohannesBuchner / toh5.py
Created Jun 2, 2019
Translate a csv/npy/csv.gz integer file to efficiently compressed HDF5
View toh5.py
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)
else:
View fetchswiftspectrum.py
"""
Download custom spectrum from http://www.swift.ac.uk/, programmatically
How to use:
$ python fetchswiftspectrum.py grbid timeslicespec
$ wget -nc --continue $(cat customspec.url)/a.tar.gz
"""
View makegaia.sh
wget -nc http://cdn.gea.esac.esa.int/Gaia/gdr2/gaia_source/csv/
grep -Eo GaiaSource_.*.csv.gz index.html | while read i; do
if [ -e $i ]; then
true
else
wget "http://cdn.gea.esac.esa.int/Gaia/gdr2/gaia_source/csv/$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
fi
@JohannesBuchner
JohannesBuchner / publish.sh
Created Jan 19, 2019
script for releasing packages on PyPI and publishing sphinx documentation on github pages
View publish.sh
#!/bin/bash
# Author: Johannes Buchner (C) 2013
# tool for publishing sphinx documentation on github
# and releasing packages on PyPI
case "$1" in
doc)
# see https://help.github.com/articles/creating-project-pages-manually
make -C doc/ html &&
git checkout gh-pages &&
@JohannesBuchner
JohannesBuchner / arxiv-submit.sh
Last active Jan 10, 2019
Arxiv submission tool
View arxiv-submit.sh
# To submit just at the right time (deadline 14:00 EST),
#
# How to use:
# - Make sure your clock is synced to https://arxiv.org/localtime
# - 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
JohannesBuchner / sample_broken_powerlaw.py
Last active Dec 2, 2018
Draw random numbers from broken powerlaw (broken code ATM)
View sample_broken_powerlaw.py
import numpy
# Sampling from a broken powerlaw or powerlaw segments
# Reference material:
# http://mathworld.wolfram.com/RandomNumber.html
# 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
JohannesBuchner / uncertaincolors.py
Created Apr 6, 2018
Make maps with uncertainties
View uncertaincolors.py
# based on http://spatial-analyst.net/wiki/index.php?title=Uncertainty_visualization
import numpy
import colorsys
from matplotlib.colors import hsv_to_rgb
def to_rgb(value, error):
z = value
f1 = -90 - z*300
@JohannesBuchner
JohannesBuchner / smf-fit.py
Created May 16, 2017
Fitting of stellar mass function with only two, uncertain data points
View smf-fit.py
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
# http://adsabs.harvard.edu/abs/2015ApJ...802...89B
Nsamples = 1000
You can’t perform that action at this time.