Skip to content

Instantly share code, notes, and snippets.

@JohannesBuchner
JohannesBuchner / run_processes_when_idle.sh
Created May 12, 2020 08:56
Stop/resume processes when the user is active/inactive in Linux/Xorg
#!/bin/bash
# how to identify the processes to STOP/CONT
# this is any part of process command line
PROCESS="myscript.py"
# run this in one terminal:
# it tries to resume the processes every so often
while sleep 10m; do
pgrep -f "${PROCESS}"|xargs -rt kill -CONT
@JohannesBuchner
JohannesBuchner / poissonerr.py
Last active February 23, 2023 16:53
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
JohannesBuchner / mcmc.py
Created August 5, 2019 09:58
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
JohannesBuchner / toh5.py
Created June 2, 2019 04:40
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)
else:
@JohannesBuchner
JohannesBuchner / fetchswiftspectrum.py
Last active March 15, 2019 12:21
Download Swift spectra
"""
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
"""
@JohannesBuchner
JohannesBuchner / makegaia.sh
Created February 27, 2019 09:57
Download GAIA data
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 January 19, 2019 16:23
script for releasing packages on PyPI and publishing sphinx documentation on github pages
#!/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 May 25, 2022 17:10
Arxiv submission tool
# 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 December 2, 2018 23:52
Draw random numbers from broken powerlaw (broken code ATM)
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 April 6, 2018 15:05
Make maps with uncertainties
# 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