Skip to content

Instantly share code, notes, and snippets.

@joezuntz
joezuntz / split_sdss_jpeg.py
Created May 24, 2011 13:40
Split an SDSS jpeg into component g,r,i filter images plotted in red,green,blue respectively
import PIL.Image
import matplotlib
matplotlib.use("Agg")
import numpy as np
import pylab as pl
def make_color_maps():
x=np.arange(0.,1.,0.01)
z=np.zeros_like(x)
R=list(zip(x,z,z))
@joezuntz
joezuntz / plot.py
Last active September 26, 2015 01:38
Short Python command-line plotting code
#!/usr/bin/env python
print "This script has been moved to https://github.com/joezuntz/plot"
@joezuntz
joezuntz / PymcGraphExample.py
Created February 1, 2012 17:33
Example of using pymc to display and fit a graphical model
from numpy import sqrt,exp,log,pi
import numpy as np
import pymc
#Example script showing how to fit
#Generate some fake data to fit.
#Use fixed mean and variance
true_mu = 2.4
@joezuntz
joezuntz / pymc-problem-am-db.py
Created March 1, 2012 11:24
Problem loading AdaptiveMetropolis sampler state from db
import os
import pymc
database_file = "mc.pickle"
number_samples = 1000
a = pymc.Normal("a",mu=0, tau=1)
b = pymc.Normal("b",mu=0, tau=1)
mu = a+b
c = pymc.Normal("c", mu=mu, tau=1, observed=True, value=0.5)
@joezuntz
joezuntz / gist:3380452
Last active September 25, 2017 11:29
Programming maxims for new phd students
Here are some short programming maxims for new science PhD students, to correct the most egregious errors made by people
trained in science but not in programming. Some from http://www.theexclusive.org/2012/08/principles-of-research-code.html and http://arkitus.com/PRML/
- There is no tax on variable name length - make them as long as you like.
- People will come to your code not knowing your nomenclature.
- Don't write programs, write functions.
@joezuntz
joezuntz / gist:4118179
Created November 20, 2012 14:12
Python script to get lat/lon of all towns in England
#Needs Python Wikipedia Robot Framework
import wikipedia
import sys
p=wikipedia.Page('en','List_of_towns_in_England')
town_pages=p.linkedPages()[7:-9]
class LatLonError(ValueError):
pass
@joezuntz
joezuntz / gist:4177285
Created November 30, 2012 17:41
Emcee bugfix patch
diff --git a/emcee/utils.py b/emcee/utils.py
index c5685d3..0aa63c7 100644
--- a/emcee/utils.py
+++ b/emcee/utils.py
@@ -163,19 +163,25 @@ if MPI is not None:
F = _function_wrapper(function)
# Tell all the workers what function to use.
+ requests = []
+
@joezuntz
joezuntz / gist:4181972
Created December 1, 2012 12:24
Emcee patch to cache objective function for mpi pool
diff --git a/emcee/utils.py b/emcee/utils.py
index ca5fc30..0f33d2a 100644
--- a/emcee/utils.py
+++ b/emcee/utils.py
@@ -160,17 +160,23 @@ if MPI is not None:
self.wait()
return
- F = _function_wrapper(function)
+ if function is not self.function:
@joezuntz
joezuntz / point_in_polygon
Created January 22, 2013 14:08
C function to check if a point is in an arbitrary polygon. I can't vouch for this or even recall where I got it!
int point_in_polygon(int n, float *vertx, float *verty, float testx, float testy)
{
int i, j, c = 0;
for (i = 0, j=n-1; i < n; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
@joezuntz
joezuntz / sample_schechter.py
Last active April 1, 2020 02:57
Sampling a Schechter distribution in python
import numpy as np
def simulate_schechter_distribution(self, alpha, L_star, L_min, N):
"""
Generate N samples from a Schechter distribution, which is like a gamma distribution
but with a negative alpha parameter and cut off on the left somewhere above zero so that
it converges.
If you pass in stupid enough parameters then it will get stuck in a loop forever, and it
will be all your own fault.