/
plot_results.py
executable file
·94 lines (79 loc) · 2.8 KB
/
plot_results.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
"""Plot results of nested sampling. FILE is the name of a
FITS format file containing the data. START and END define which lightcurves
in the file to analyze."""
import os
import sys
from optparse import OptionParser
import numpy as np
import fitsio
from astropy.io.misc import fnunpickle
import sncosmo
import matplotlib.pyplot as plt
from modeldefs import models
from conf import pikdir, plotdir
parser = OptionParser(usage='%prog FILE START END',
description=__doc__)
opts, args = parser.parse_args(sys.argv[1:])
if len(args) != 3:
parser.print_help()
exit()
datafilename, startcand, endcand = args
startcand = int(startcand)
endcand = int(endcand)
f = fitsio.FITS(datafilename)
# Initialize chisq dist
minchisq = []
minchisqtype = []
for meta in f[1][startcand:endcand]:
pikname = '{0}/{1}.pik'.format(pikdir, meta['snid'])
if not os.path.exists(pikname): continue
# Candidate results
candres = fnunpickle(pikname)
# Data for this candidate
data = f[2][meta['datastart']:meta['dataend']]
mask = data['status'] < 8
data = data[mask] # Trim data based on mask
# Loop over models, calculate chisq / dof
print "\n",meta['snid']
for name, res in candres.iteritems():
res.chisq = -2. * res.loglmax
res.chisqdof = res.chisq / res.ndof
print " {} : chisq/dof = {:7.1f}".format(name, res.chisqdof)
# Sort modelnames by chisqdof
modelnames = sorted(candres.keys(), key=lambda x: candres[x].chisqdof)
name = modelnames[0]
minchisq.append(candres[name].chisqdof)
minchisqtype.append(candres[name].type)
# Make plot for min chisq/dof
plotname = "{0}/{1}.png".format(plotdir, meta['snid'])
if not os.path.exists(plotname):
model = models[name]['model']
res = candres[name]
model.set(**res.param_dict)
idx = np.argmax(res.logl)
parameters = res.samples[idx]
model.set(**dict(zip(res.param_names, parameters)))
figtext = ("SNID = {}\n"
"model = {}\n"
"type={}\n"
"$\\chi^2$/dof = {:.1f}/{:d}"
.format(meta['snid'], name, res.type, res.chisq, res.ndof))
sncosmo.plot_lc(data, model, figtext=figtext, fname=plotname)
# Plot chisq distribution
minchisq = np.array(minchisq)
minchisqtype = np.array(minchisqtype)
minchisqsets = []
minchisqlabels = []
for t in np.unique(minchisqtype):
mask = minchisqtype == t
minchisqsets.append(minchisq[mask])
minchisqlabels.append(t)
plt.hist(minchisqsets, bins=40, range=(0, 20), histtype='barstacked',
rwidth=1.0, label=minchisqlabels)
plt.legend()
plt.xlabel("lowest $\chi^2$ / dof")
plt.ylabel("# Candidates")
plt.title("{:d} Candidate light curves".format(len(minchisq)))
plt.savefig("chisqdist.png")
f.close()