Skip to content

Instantly share code, notes, and snippets.

@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])
@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 / 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 / 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 / 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 / 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 / interval.py
Created December 9, 2022 12:25
Check whether two intervals overlap
def interval_overlap(alo, ahi, blo, bhi):
# interval a: alo to ahi
# interval b: blo to bhi
return (blo < ahi and bhi > alo) or (alo < bhi and ahi > blo)
@JohannesBuchner
JohannesBuchner / Makefile
Last active November 22, 2022 17:14
Starting point for new make files
# run with: $ make
# or make -j4 to run in parallel
.PHONY: all help # rules that do not correspond to a output file
.SUFFIXES: # disable built-in rules
.SECONDARY: # do not delete intermediate products
# first rule is default
all: mytarget
@JohannesBuchner
JohannesBuchner / package.py
Last active September 21, 2022 08:56
Prepare arxiv latex submission tarball (package all files into one directory with bib, figure files, etc).
"""Package a paper written in latex for arxiv.
Rationale
---------
You may have figures and bibliography included from somewhere else in your file system with absolute paths.
This script makes a subdirectory package-mylatexfile.tex/ which contains the latex file, figures, .bib, input files referenced in the tex file
in the subdirectory, pdflatex mylatexfile.tex should work and not touch any files outside the subdirectory.
the subdirectory can then by tarred and uploaded to arxiv.
@JohannesBuchner
JohannesBuchner / wstatrebin.py
Last active August 19, 2022 07:32
The bias of rebinning to a minimum of bmin=1 background counts with ftgrouppha, then using wstat to estimate background contribution
import matplotlib.pyplot as plt
import numpy as np
def rebin(
Nbins = 40,
minimum = 0.1,
):
bins = np.linspace(0, 1, Nbins)
lam = minimum + 0 * bins
c = np.random.poisson(lam)