Skip to content

Instantly share code, notes, and snippets.

@mattpitkin
mattpitkin / colourfillviolin.md
Last active February 20, 2024 16:31
Violin plot with gradient colour fill

In answer to this StackOverflow question, here is a method (based heavily on this answer) to create a violin plot with a gradient colour fill based on the density of the samples (i.e., width of the violin):

from matplotlib import pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch
import matplotlib as mpl
@mattpitkin
mattpitkin / examplenetworkgrid.py
Last active May 22, 2023 11:55
A network plot of 2x2 grids
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
fig, ax = plt.subplots()
def draw_2x2_grid(ax, xy, bwidth=0.25, bheight=0.25, numbers=[]):
for i in range(2):

How to typeset a value and its uncertainty in HTML (without a CSS stylesheet)

If you want to quote a value and a plus/minus uncertainty range in HTML, using the <sup> and <sub> tags leaves the values offset, e.g.:

2.3<sup>+1.2</sup><sub>-0.5</sub>

gives:

@mattpitkin
mattpitkin / dtmf.py
Last active January 10, 2023 22:23
DTMF keypad
"""
GUI keypad that plays DTMF (Dual-tone multi-frequency) signals.
https://en.wikipedia.org/wiki/Dual-tone_multi-frequency_signaling
"""
import sys
from functools import partial
from string import ascii_uppercase
from threading import Thread, Event
@mattpitkin
mattpitkin / sunrise_sunset.py
Last active March 14, 2023 16:27
Plot sunrise and sunset times over a year
"""
Plot the sun rise, sunset and length of daylight hours over a year
(from a given location). This uses the astropy and astroplan packages.
"""
from matplotlib import pyplot as plt
import numpy as np
from astropy.time import Time, TimeDelta
from astropy import units as u
@mattpitkin
mattpitkin / my_pubs.py
Last active June 5, 2019 20:48
Get my publication from ADS
import ads
query = 'pubdate:[2003-01 TO 9999-12] author:("Pitkin, M")'
maxpubs = 500
papers = ads.SearchQuery(q=query, rows=maxpubs, property='article')
titleexclusions = ["Erratum", "ERRATUM", "Publisher's Note"]
to_retrieve = set()
for paper in papers:
@mattpitkin
mattpitkin / time_delay.py
Last active January 24, 2019 14:26
Get time delays using PINT
"""
Get solar system barycentring time delays using PINT and lalsuite.
(see the example at https://github.com/nanograv/PINT/blob/master/examples/TimingModel_composition.ipynb)
"""
import subprocess as sp
# create a fake pulsar (units must be TDB as PINT can't do TCB yet!)
parcontent = """PSRJ J0123+4501
RAJ 01:23:00.0
# script to try and get the phase residuals for B1913+16 using TEMPO2
cd ${HOME}
mkdir PSRB1913+16
cd PSRB1913+16
# get B1913+16 TOAs and Tempo input file from https://zenodo.org/record/54764#.Wt-fQ3XwZ8d
wget --no-parent -r --accept "1913.*" https://zenodo.org/record/54764/
# move downloaded files
@mattpitkin
mattpitkin / README.md
Last active March 15, 2023 10:52
Singularity & Docker in jupyter

Use Singularity and Docker to run a kernel in a jupyter notebook

This is an extension to this post about creating a kernel in a Jupyter notebook that runs a Singularity container.

Download Singularity (see here).

Create a Singularity file, e.g., (making sure to install the ipykernel module in it):

Bootstrap: docker
@mattpitkin
mattpitkin / swigtesting.md
Last active March 15, 2023 10:55
Adding python functionality to a SWIG wrapped structure

Adding python functionality to a SWIG-wrapped structure

This is a little example of adding some python-object-like functionality to a C structure that is SWIG-wrapped for use in python. What I've done mainly comes from this StackOverflow question, and the very useful answer, but is written here to remind me.

Say you have some C code containing a structure like this:

/* testswig.h file */
#include <stdlib.h>
#include <stdio.h>