In [13]:
from astropy.io import fits
import pandas as pd
import numpy as np
from sqlalchemy import create_engine
%matplotlib inline
import matplotlib.pyplot as plt
from astropy.table import Table
In [44]:
import sncosmo
In [2]:
import analyzeSN as ans
In [3]:
SDSS_summary_file = ans.snanaSims.SnanaSims.snanadatafile(snanafileroot='SDSS_allCandidates+BOSS', location='/Users/rbiswas/data/SNDATA/lcmerge')
In [19]:
summary = Table(fits.open(SDSS_summary_file)[1].data)
In [66]:
SNIDs = map (lambda x: x.strip(), summary[summary['SNTYPE'] == 120]['SNID'])
In [33]:
sdss = ans.snanaSims.SnanaSims.fromSNANAfileroot(snanafileroot='SDSS_allCandidates+BOSS', location='/Users/rbiswas/data/SNDATA/lcmerge'
)
In [42]:
sne = ans.snanaSims.SnanaSims.fromSNANAfileroot(snanafileroot='SDSS_allCandidates+BOSS',
location='/Users/rbiswas/data/SNDATA/lcmerge',
snids=SNIDs)
In [43]:
sn0 = ans.snanaSims.SnanaSims.reformat_SNANASN(sne.snList[0], snanaBands=['u','g','r','i','z'], replacements=['sdssu','sdssg', 'sdssr', 'sdssi', 'sdssz'])
In [108]:
sn1 = ans.snanaSims.SnanaSims.reformat_SNANASN(sne.snList[1], snanaBands=['u','g','r','i','z'], replacements=['sdssu','sdssg', 'sdssr', 'sdssi', 'sdssz'])
In [72]:
sn0[:5]
Out[72]:
In [70]:
fig_data = sncosmo.plot_lc(sn0);
In [62]:
fig_data.savefig('SN_data.pdf')
In [63]:
print sn0.meta
In [141]:
modelz = sncosmo.Model(source='salt2')
modelz.set(z = sn0.meta['REDSHIFT_FINAL'])
fitted_resz, fitted_modelz = sncosmo.fit_lc(sn0, modelz, vparam_names=['t0', 'x0', 'x1', 'c'])
fig_modelz = sncosmo.plot_lc(sn0, fitted_modelz, show_pulls=True)
In [145]:
fitted_resz.parameters
Out[145]:
In [82]:
fig_modelz.savefig('SN_data_fit.pdf')
In [83]:
fitted_resz.errors
Out[83]:
In [74]:
mcmc_resz
Out[74]:
In [73]:
mcmc_resz, mcmc_modelz = sncosmo.mcmc_lc(sn0, modelz, vparam_names=['t0', 'x0', 'x1', 'c'])
In [75]:
import triangle
In [159]:
figure_mcmc = triangle.corner(mcmc_resz.samples, labels=mcmc_resz.vparam_names, range=4*[0.95],
show_titles=True, title_args={"fontsize": 12})
figure_mcmc.gca().annotate("mcmc sampling", xy=(0.5, 1.0), xycoords="figure fraction",
xytext=(0, -5), textcoords="offset points",
ha="center", va="top")
axes = figure_mcmc.axes
In [84]:
figure_mcmc.savefig('mcmc_fig.pdf')
In [80]:
fig_comp = sncosmo.plot_lc(sn0, model=[fitted_modelz, mcmc_modelz], model_label=['MaxL', 'MCMC'])
In [85]:
fig_comp.savefig('fig_comp.pdf')
In [147]:
nest_res, nest_model = sncosmo.nest_lc(sn0, modelz, vparam_names=['t0', 'x0', 'x1', 'c'],
bounds={'c':(-0.3, 0.3), 'x1':(-3.0, 3.0)}, guess_amplitude_bound=True, minsnr=3.0,
verbose=True)
In [162]:
figure_nest = triangle.corner(nest_res.samples, weights=nest_res.weights, labels=nest_res.vparam_names, range=4*[0.99],
bins=20)
# figure_nest.gca().annotate("nest sampling", xy=(0.5, 1.0), xycoords="figure fraction",
# xytext=(0, -5), textcoords="offset points",
# ha="center", va="top")
In [156]:
triangle.corner?
In [97]:
model = sncosmo.Model(source='salt2')
In [99]:
fitted_res, fitted_model = sncosmo.fit_lc(sn0, model, vparam_names=['z', 't0', 'x0', 'x1', 'c'], bounds={'z':(0., 1.2)})
fig_model = sncosmo.plot_lc(sn0, fitted_model, show_pulls=True)
In [100]:
print fitted_model
In [101]:
mcmc_res, mcmc_model = sncosmo.mcmc_lc(sn0, model, vparam_names=['z', 't0', 'x0', 'x1', 'c'], bounds={'z':(0., 1.2)})
fig_mcmc_model = sncosmo.plot_lc(sn0, mcmc_model, show_pulls=True)
In [102]:
print mcmc_model
In [104]:
mcmc_res
Out[104]:
In [105]:
figure_mcmc = triangle.corner(mcmc_res.samples, labels=mcmc_res.vparam_names,
show_titles=True, title_args={"fontsize": 12})
figure_mcmc.gca().annotate("mcmc sampling", xy=(0.5, 1.0), xycoords="figure fraction",
xytext=(0, -5), textcoords="offset points",
ha="center", va="top")
axes = figure_mcmc.axes
In [106]:
nest_res, nest_model = sncosmo.mcmc_lc(sn0, model, vparam_names=['z', 't0', 'x0', 'x1', 'c'], bounds={'z':(0., 1.2)})
fig_nest_model = sncosmo.plot_lc(sn0, nest_model, show_pulls=True)
In [113]:
nest_res_small, nest_model_small = sncosmo.mcmc_lc(sn0, model, vparam_names=['z', 't0', 'x0', 'x1', 'c'], bounds={'z':(0., 0.3)})
fig_nest_model_small = sncosmo.plot_lc(sn0, nest_model_small, show_pulls=True)
In [140]:
fitted_resz.parameters
Out[140]:
In [138]:
nest_res_small.parameters
Out[138]:
In [136]:
nest_res_small.errors
Out[136]:
In [130]:
#sn1.rename_column('ZP', 'zp')
sn1.rename_column('ZPSYS', 'zpsys')
In [131]:
sn1
Out[131]:
In [111]:
nest_res, nest_model = sncosmo.mcmc_lc(sn1, model, vparam_names=['z', 't0', 'x0', 'x1', 'c'], bounds={'z':(0., 0.3)})
fig_nest_model = sncosmo.plot_lc(sn1, nest_model, show_pulls=True)
In [125]:
sn1['zp']
In [132]:
sncosmo.chisq(sn1, nest_model)
In [118]:
type(sn1)
Out[118]:
In [119]:
from astropy.table import Table
In [121]:
isinstance(sn1, Table)
Out[121]:
In [123]:
help(sncosmo.chisq)
In [151]:
len(nest_res.vparam_names)
Out[151]:
In [152]:
nest_res
Out[152]:
In [ ]: