Skip to content

Instantly share code, notes, and snippets.

@JohannesBuchner
JohannesBuchner / procmanage.py
Last active March 26, 2023 18:28
Manage a group of processes, keeping only N running at a time.
#!/usr/bin/env python3
"""
Manage a group of processes, keeping only N running at a time.
Usage:
procmanage.py N STRING
Arguments:
N An integer representing the number of processes to send the CONT signal to.
STRING A string representing the full name to match against.
@JohannesBuchner
JohannesBuchner / stripmovies.py
Created April 4, 2023 10:11
Parses ODP libreoffice impress files in python, removing movies and animations.
#!/usr/bin/env python3
#
# SYNOPSIS: stripgif.py input.odp output.odp
#
#
import os
import sys
import zipfile
import lxml.etree as ET
@JohannesBuchner
JohannesBuchner / colorbar.py
Created April 19, 2023 09:31
Manual colorbar with range, shown as a small inset with label
import matplotlib as mpl
import matplotlib.pyplot as plt
# make a colormap that can be used to look up colors
norm = mpl.colors.Normalize(vmin=0, vmax=3)
zcmap = plt.cm.ScalarMappable(norm=norm, cmap=plt.cm.viridis)
# use it to plot stuff
plt.errorbar(
x=1, xerr=0.1, y=2, yerr=0.2,
@JohannesBuchner
JohannesBuchner / sbpl.py
Created September 5, 2023 10:48
Smoothly bending power law function of Ryde+98. The width of the bend can be controlled.
def sbpl(x, norm, lam1, lam2, x0, xbrk, Lambda):
"""Smoothly bending powerlaw
Parameterization from Ryde99
https://ui.adsabs.harvard.edu/abs/1999ApL%26C..39..281R/abstract
Parameters
----------
x: array of independent variable
xmax: only consider x values up to this value
@JohannesBuchner
JohannesBuchner / hdi.py
Last active March 19, 2024 17:23
Highest Density Interval from posterior samples
from getdist.mcsamples import MCSamples
import getdist.chains
def highest_density_interval_from_samples(xsamples, xlo=None, xhi=None, probability_level=0.68):
"""
Compute the highest density interval (HDI) from posterior samples.
Parameters
----------
xsamples : array_like
@JohannesBuchner
JohannesBuchner / fitscathealth.py
Created April 29, 2024 11:26
Health check for a FITS catalog: should have column descriptions, units and should not fail to load with astropy.
import warnings
import sys
import astropy.io.fits as pyfits
from astropy.table import Table
print("opening FITS file ...")
f = pyfits.open(sys.argv[1])
print("extensions: ", [e.name for e in f])