Skip to content

Instantly share code, notes, and snippets.

@mattpitkin
mattpitkin / plotoutside.py
Last active May 18, 2017 15:44
Example for producing a contour plot and also plotting the points outside the outer contour
import numpy as np
from matplotlib import pyplot as pl
x = np.random.randn(5000)
y = 0.2*np.random.randn(5000) + 3
xbins = 20
ybins = 20
H, xedges, yedges = np.histogram2d(x, y, bins=(xbins, ybins))
@mattpitkin
mattpitkin / gsl_integrate_kde.pyx
Last active May 22, 2017 09:48
Using GSL integration functions in Cython when integrating a KDE function obtained from samples
"""
Example of integration of a function f(x) (for which a set of samples and integral estimate exist
e.g. through use of the Nested Sampling algorithm) multiplied by some other function g(x). NOTE: one
could use the expectatation value for this, but in some cases where there is sparse sampling over
the full allowed range of the function can cause problems.
The samples are passed through a KDE and then the kde object is passed to the GSL CQUAD
integration function
This requires the Cython, numpy, scikit-learn (v0.18 or greater) and the GSL library.
@mattpitkin
mattpitkin / roqupdatetest.py
Last active December 22, 2017 15:30
Testing ROQ update
from __future__ import print_function, division
import numpy as np
from scipy.signal import fftconvolve
import subprocess as sp
import os
import sys
import gzip
import h5py
from time import time
@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>
@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
# 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 / 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
@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 / 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 / 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