Skip to content

Instantly share code, notes, and snippets.

@adrn
adrn / gist:3106258
Created July 13, 2012 17:50
Python pipeline function sketch
import os
import cPickle as pickle
def some_function(another_function, overwrite=False, *args, **kwargs):
if overwrite:
os.remove("somefilename.pickle")
if not os.path.exists("somefilename.pickle"):
# analysis / make object
object = another_function(*args, **kwargs)
@adrn
adrn / columbia_astropy.md
Created October 10, 2012 20:41
What do people at Columbia want in AstroPy?
  • read_ascii()
    • Probably already included in astropy.ascii
  • ascii_template()
    • Probably already included in astropy.ascii
  • ntostr() (from Erin Sheldon's sdsstools)
    • It looks like this is just pure Python...
  • helcorr.pro
    • This will be included in the coordinates subpackage (astropy.coordinates)
  • glactc.pro
  • This will be included in the coordinates subpackage (astropy.coordinates)
@adrn
adrn / density_contour.py
Created November 1, 2012 14:35
Make a 2D density contour plot with matplotlib
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as so
def find_confidence_interval(x, pdf, confidence_level):
return pdf[pdf > x].sum() - confidence_level
def density_contour(xdata, ydata, nbins_x, nbins_y, ax=None, **contour_kwargs):
""" Create a density contour plot.
# coding: utf-8
import os
import random
__author__ = "adrn <adrn@astro.columbia.edu>"
def simple_data_bend(input_file, output_file, grayscale=True):
# Open file in binary mode
f = open(input_file, "rb")
data = f.read()
from astropy.table import Table
from astropy.io import ascii
IDs = [1, 3, 6, 9, 100]
names = ["cat", "bat", "rat", "mat", "gnat"]
scores = [83, 51, 77, 81, 92]
t = Table([IDs, names, scores], names=["ID", "name", "score"])
ascii.write(t, delimiter=",")
@adrn
adrn / emcee_mpi_hotfoot.py
Last active September 25, 2023 06:01
run emcee with MPI on Hotfoot
"""
- Hotfoot is Columbia's shared Astronomy-Statistics supercomputing
cluster.
- Emcee is a kick-ass MCMC black-box for sampling probability
distributions & parameter estimation.
- MPI is a standardized message passing system designed for supporting
and running parallel code.
Emcee is an implementation of an affine-invariant, ensemble sampler --
key words here being *ensemple sampler*. The algorithm runs an ensemble
"""
Make a figure to visualize using MCMC (in particular, the Python package
emcee) to infer 4 parameters from a parametrized model of the Milky Way's
dark matter halo by using tracer stars from the Sagittarius Stream.
If you're unfamiliar with the jargon here (walkers, Sgr, etc.), check out:
- Law & Majewski 2010
http://iopscience.iop.org/0004-637X/714/1/229/pdf/apj_714_1_229.pdf
- emcee
http://dan.iel.fm/emcee/
@adrn
adrn / scatterplotmatrix.py
Created April 3, 2013 13:25
Make a scatter-plot matrix with matplotlib
# coding: utf-8
""" Create a scatter-plot matrix using Matplotlib. """
from __future__ import division, print_function
__author__ = "adrn <adrn@astro.columbia.edu>"
# Standard library
import os, sys
@adrn
adrn / labeled_histogram.py
Created April 29, 2013 00:46
Make a histogram with labeled bars
import numpy as np
import matplotlib.pyplot as plt
def label_bars(rects):
# attach some text labels to the bars
for rect in rects:
height = rect.get_height()
plt.text(rect.get_x()+rect.get_width()/2.,
height, str(int(height)),
ha='center', va='bottom')
@adrn
adrn / sgr_example.py
Last active December 17, 2015 01:19
Transform to and from Sagittarius coordinates in Python
import astropy.units as u
from astropy.coordinates import ICRSCoordinates
from streams.coordinates import SgrCoordinates, OrphanCoordinates
icrs = ICRSCoordinates(15.34167, 1.53412, unit=(u.hour, u.degree))
sgr = icrs.transform_to(SgrCoordinates)
print(sgr.Lambda, sgr.Beta)
orp = sgr.transform_to(OrphanCoordinates)
print(orp.Lambda, orp.Beta)