Skip to content

Instantly share code, notes, and snippets.

@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)
@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 / mplrecorder.py
Last active April 21, 2022 19:19
Intercept all matplotlib calls and store each figure's data into json files, with labels as keys.
"""
Intercept all matplotlib calls and store each figure's data
into json files, with labels as keys.
Usage:
Just replace:
import matplotlib.pyplot as plt
with:
from mplrecorder import plt
@JohannesBuchner
JohannesBuchner / logdeduplicator.py
Created April 6, 2022 10:11
Strips duplicated and repeated lines from stdin (such as a log output). Can also handles multiline repeats, up to a configurable memory limit.
import os
import sys
max_memory = int(os.environ.get('MAX_MEMORY', '10'))
recent_lines = []
for line in sys.stdin:
if line not in recent_lines:
sys.stdout.write(line)
@JohannesBuchner
JohannesBuchner / shrink-folder.sh
Created March 18, 2022 23:05
Delete files until folder is smaller than 1GB
find $FOLDER -maxdepth 1 -type f -printf '%s\t%p\n' |
{ S=0; while read s l; do
((S+=s)); [[ $S -gt 1000000000 ]] && rm -v "$l";
done; }
@JohannesBuchner
JohannesBuchner / supertar.sh
Created September 22, 2021 12:15
Better tar file compression by sorting similar files together
# Compression can be improved when files with the same or similar content
# are next to each other in the file list.
#
# This command sorts by reversed filenames, which places files
# together by file extension, filename and path, in that order.
# identify all files
find mypath/ -type f |
rev | sort | rev |
tar --no-recursion --files-from=- -cvzf myarchive.tar.gz
@JohannesBuchner
JohannesBuchner / joss_make_latex.sh
Created January 23, 2021 22:30
Make LaTeX and PDF from JOSS markdown papers
#!/bin/bash
# you need to install:
# pip install openbases
# sudo apt install texlive-xetex pandoc pandoc-citeproc
PDF_INFILE=paper.md
PDF_LOGO=logo.png
PDF_OUTFILE=paper.pdf
TEX_OUTFILE=paper.tex