Skip to content

Instantly share code, notes, and snippets.

@astrofrog
astrofrog / async_plotting.py
Created December 10, 2011 00:11
Asynchronous Plotting in Matplotlib: rather than call savefig directly, add plots to an asynchronous queue to avoid holding up the main program. Makes use of multiple processes to speed up the writing out. Suggestions welcome!
import time
import multiprocessing as mp
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@astrofrog
astrofrog / raster_axes.py
Created June 9, 2011 16:21
How to plot a million points in matplotlib
import numpy as np
import numpy.ma as ma
import matplotlib.pyplot as plt
import matplotlib.colors as colors
def make_colormap(color):
r, g, b = colors.colorConverter.to_rgb(color)
# Remove all but the latest N versions from wheels on Anaconda.org
# Usefult to avoid building up too many files when uploading
# developer wheels.
from binstar_client.utils import get_server_api
KEEP_N_LATEST = 10
api = get_server_api(token=<TOKEN>)
package = api.package("astropy", "astropy")
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@astrofrog
astrofrog / test_oo.py
Created February 13, 2011 18:48
Diagnosing Memory Leaks in Matplotlib
# Object-oriented API
#
# Memory usage (iteration, object count, memory size)
# 100 5637 1562216
# 200 5529 1491528
# 300 5422 1426264
# 400 5758 1587376
# 500 5422 1426288
# 600 5416 1440456
# 700 5610 1515056
@astrofrog
astrofrog / qsub.py
Created May 2, 2013 11:39
Submitting jobs via qsub in Python
import os
import random
import string
import tempfile
import subprocess
def random_id(length=8):
return ''.join(random.sample(string.ascii_letters + string.digits, length))
TEMPLATE_SERIAL = """
@astrofrog
astrofrog / merge_structured_arrays.py
Created April 29, 2012 19:32
Merge Numpy structured arrays
import numpy as np
def merge_structured_arrays(array1, array2):
n1 = len(array1)
n2 = len(array2)
array_out = array1.copy()
array_out.resize(n1 + n2)
array_out[n1:] = array2
return array_out
@astrofrog
astrofrog / convert_2mass.py
Created February 22, 2011 22:24
Convert 2MASS ALTAS images into MJy/sr (and subtract background)
# Convert 2MASS ATLAS images into MJy/sr, and subtract background value
# estimated by 2MASS pipeline.
#
# Example:
#
# >>> atlas_to_MJysr('glimpse_k_test.fits', 'glimpse_k_cal.fits')
#
import pyfits
import pywcs
def sample_dask_array_chunks(array, n_chunks):
"""
Return an 1-d array which contains the data values from n_chunks randomly
sampled from the chunks in the array (without replacement)
"""
# Find the indices of the chunks to extract
indices = [np.random.randint(dimsize, size=n_chunks) for dimsize in array.numblocks]
# Determine the boundaries of chunks along each dimension