-
Notifications
You must be signed in to change notification settings - Fork 7
/
mcmc_sdss_demo.py
60 lines (44 loc) · 1.8 KB
/
mcmc_sdss_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import numpy as np
import matplotlib.pyplot as plt
from celeste import *
from mcmc_utils import *
from mcmc_transitions import *
im = FitsImage('r')
# MCMC step
rand = np.random.RandomState(seed=89) # http://xkcd.com/221/
from scipy.misc import imsave
srcs = np.array([])
imsave("orig_image.png", im.nelec)
srcs = initializeSources(srcs, im, percentile = 95)
imsave("init_image.png", gen_model_image(srcs, im))
createImageDifference("initialize_im_diff.png", im.nelec, gen_model_image(srcs, im).copy())
print srcs.size
Niters = 1
logprobs = np.zeros(Niters + 1)
logprobs[0] = celeste_likelihood(srcs, im)
for it in xrange(Niters):
# Gibbs sampling for brightness for each catalog entry
# http://andrewgelman.com/2011/12/07/neutral-noninformative-and-informative-conjugate-beta-and-gamma-prior-distributions/
im1 = gen_model_image(srcs, im).copy()
srcs = gibbsSampleBrightnesses(srcs, im, aPrior=1./3, bPrior=0.1, eta=1, rand=rand)
im2 = gen_model_image(srcs, im).copy()
createImageDifference("gibbs_im_diff_%d.png" % it, im1, im2)
#for i in xrange(len(srcs)):
# TODO: randomize order
# sliceSampleSourceSingleAxis(srcs, im, i, 0, rand=rand)
# sliceSampleSourceSingleAxis(srcs, im, i, 1, rand=rand)
im3 = gen_model_image(srcs, im).copy()
createImageDifference("slice_im_diff_%d.png" % it, im2, im3)
if rand.rand() > 0.5:
srcs = splitStar(srcs, im, rand=rand)
else:
srcs = mergeStar(srcs, im, rand=rand)
if rand.rand() > 0.5:
srcs = birthStar(srcs, im, rand=rand)
else:
srcs = deathStar(srcs, im, rand=rand)
logprobs[it+1] = logprob = celeste_likelihood(srcs, im)
print "After iteration %s: %s, cat len %s" % (it, logprob, len(srcs))
print "Brightnesses:", [src.b for src in srcs]
plt.plot(logprobs)
plt.show()