In [1]:
%matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter as FSF
from matplotlib.ticker import MaxNLocator
from matplotlib.ticker import MultipleLocator
import numpy as np
from IPython.html import widgets # Widget definitions
from IPython.display import display, clear_output, HTML # Used to display widgets in the notebook
from IPython.html.widgets import interact, interactive, fixed


Using matplotlib backend: Qt4Agg

[ -13.37354867, 5188.75488375, 5.79597804], [ -13.58059031, 5151.82322042, 6.15930413], [ -13.38718559, 5142.85381703, 7.06802524], [ -13.76186598, 5202.25227836, 7.45325824], [ -13.64347323, 5150.74527037, 6.67246923], [ -13.70379713, 5169.07086766, 6.47508788], [ -13.7420562, 5198.61903481, 5.48643032], [ -13.7557276, 5170.68109, 5.07069537], [ -13.4095891, 5235.30766, 4.82242126], [ -13.70503714, 5187.83335919, 5.30911125], [ -13.74135283, 5234.57274185, 5.49249104]


In [2]:
from StellarSpectra.model import Model
from StellarSpectra.spectrum import DataSpectrum
from StellarSpectra.grid_tools import TRES, SPEX, HDF5Interface
from StellarSpectra.utils import saveall
from StellarSpectra import utils
import scipy.sparse as sp

myDataSpectrum = DataSpectrum.open("../../data/WASP14/WASP14-2009-06-14.hdf5", orders=np.array([21, 22, 23]))

myInstrument = TRES()
# myInstrument = SPEX()

myHDF5Interface = HDF5Interface("../../libraries/PHOENIX_TRES_F.hdf5")
myErrorHDF5Interface = HDF5Interface("../../libraries/PHOENIX_TRES_F.hdf5")

#Load a model using the JSON file

#Update this:
myModel = Model.from_json("WASP14_PHOENIX_model0_final.json", myDataSpectrum, myInstrument, myHDF5Interface, myErrorHDF5Interface)
#old
#myModel = Model.from_json("../WASP14_22_model_final_region.json", myDataSpectrum, myInstrument, myHDF5Interface)


Determine Chunk Log: Wl is 16384
Determine Chunk Log: Wl is 16384
Creating OrderModel 0
Creating OrderModel 1
Creating OrderModel 2
creating region 
INFO:RegionCovarianceMatrix 5079.124974339364:Created region
INFO:CovarianceMatrix:Added region 0 to self.RegionList
INFO:RegionCovarianceMatrix 5131.414999139933:Created region
INFO:CovarianceMatrix:Added region 1 to self.RegionList
 0 0 {'loga': -13.710794529751722, 'mu': 5079.124974339364, 'sigma': 6.349081439780744}
creating region  1 1 {'loga': -14.071784383691893, 'mu': 5131.414999139933, 'sigma': 4.304118876593128}
creating region 
INFO:RegionCovarianceMatrix 5107.330784496594:Created region
INFO:CovarianceMatrix:Added region 2 to self.RegionList
INFO:RegionCovarianceMatrix 5090.696075164306:Created region
INFO:CovarianceMatrix:Added region 3 to self.RegionList
 2 2 {'loga': -13.71910158338199, 'mu': 5107.330784496594, 'sigma': 5.311750349661101}
creating region  3 3 {'loga': -13.857671823844235, 'mu': 5090.696075164306, 'sigma': 5.613780194752862}
creating region 
INFO:RegionCovarianceMatrix 5097.448664827108:Created region
INFO:CovarianceMatrix:Added region 4 to self.RegionList
INFO:RegionCovarianceMatrix 5098.637657762301:Created region
INFO:CovarianceMatrix:Added region 5 to self.RegionList
 4 4 {'loga': -13.615911783955768, 'mu': 5097.448664827108, 'sigma': 6.025081460071304}
creating region  5 5 {'loga': -14.04344529590619, 'mu': 5098.637657762301, 'sigma': 6.997549988781763}
creating region 
INFO:RegionCovarianceMatrix 5126.729544821372:Created region
INFO:CovarianceMatrix:Added region 6 to self.RegionList
INFO:RegionCovarianceMatrix 5125.001369849194:Created region
INFO:CovarianceMatrix:Added region 7 to self.RegionList
 6 6 {'loga': -13.718016616432767, 'mu': 5126.729544821372, 'sigma': 8.936958925561834}
creating region  7 7 {'loga': -13.360596381236995, 'mu': 5125.001369849194, 'sigma': 3.680661223587719}
creating region 
INFO:RegionCovarianceMatrix 5072.009280039171:Created region
INFO:CovarianceMatrix:Added region 8 to self.RegionList
INFO:RegionCovarianceMatrix 5151.771846354639:Created region
INFO:CovarianceMatrix:Added region 9 to self.RegionList
 8 8 {'loga': -13.374644468004876, 'mu': 5072.009280039171, 'sigma': 5.175276322877656}
creating region  9 9 {'loga': -13.537480137770286, 'mu': 5151.771846354639, 'sigma': 5.046608236980869}
creating region 
INFO:RegionCovarianceMatrix 5075.072983392498:Created region
INFO:CovarianceMatrix:Added region 10 to self.RegionList
INFO:RegionCovarianceMatrix 5150.777604758331:Created region
INFO:CovarianceMatrix:Added region 11 to self.RegionList
 10 10 {'loga': -13.715697006595521, 'mu': 5075.072983392498, 'sigma': 3.9833669211397202}
creating region  11 11 {'loga': -13.984849450684091, 'mu': 5150.777604758331, 'sigma': 5.785022773256455}
creating region 
INFO:RegionCovarianceMatrix 5083.390627066195:Created region
INFO:CovarianceMatrix:Added region 12 to self.RegionList
 12 12 {'loga': -13.43988952730681, 'mu': 5083.390627066195, 'sigma': 9.905672052407887}
creating region 
INFO:RegionCovarianceMatrix 5188.748015034012:Created region
INFO:CovarianceMatrix:Added region 0 to self.RegionList
INFO:RegionCovarianceMatrix 5202.301294215613:Created region
INFO:CovarianceMatrix:Added region 1 to self.RegionList
 0 0 {'loga': -13.364522403262285, 'mu': 5188.748015034012, 'sigma': 6.409848875871397}
creating region  1 1 {'loga': -13.683361465291451, 'mu': 5202.301294215613, 'sigma': 7.015446911773385}
creating region 
INFO:RegionCovarianceMatrix 5198.620642582554:Created region
INFO:CovarianceMatrix:Added region 2 to self.RegionList
INFO:RegionCovarianceMatrix 5169.060257546352:Created region
INFO:CovarianceMatrix:Added region 3 to self.RegionList
 2 2 {'loga': -13.655238673474527, 'mu': 5198.620642582554, 'sigma': 5.367470875984932}
creating region  3 3 {'loga': -13.69125660580706, 'mu': 5169.060257546352, 'sigma': 8.088305972571215}
creating region 
INFO:RegionCovarianceMatrix 5187.853342356894:Created region
INFO:CovarianceMatrix:Added region 4 to self.RegionList
INFO:RegionCovarianceMatrix 5151.831677512655:Created region
INFO:CovarianceMatrix:Added region 5 to self.RegionList
 4 4 {'loga': -13.723910712609335, 'mu': 5187.853342356894, 'sigma': 5.3224050305133}
creating region  5 5 {'loga': -13.679139103906817, 'mu': 5151.831677512655, 'sigma': 6.102108083061945}
creating region 
INFO:RegionCovarianceMatrix 5142.8537755999905:Created region
INFO:CovarianceMatrix:Added region 6 to self.RegionList
INFO:RegionCovarianceMatrix 5234.586027428319:Created region
INFO:CovarianceMatrix:Added region 7 to self.RegionList
 6 6 {'loga': -13.453816547701932, 'mu': 5142.8537755999905, 'sigma': 6.9845715026635}
creating region  7 7 {'loga': -13.72228938577596, 'mu': 5234.586027428319, 'sigma': 5.5012881441472}
creating region 
INFO:RegionCovarianceMatrix 5258.7576555302485:Created region
INFO:CovarianceMatrix:Added region 0 to self.RegionList
INFO:RegionCovarianceMatrix 5245.548671666723:Created region
INFO:CovarianceMatrix:Added region 1 to self.RegionList
 0 0 {'loga': -13.27662319352174, 'mu': 5258.7576555302485, 'sigma': 11.39738429371532}
creating region  1 1 {'loga': -13.27999849281383, 'mu': 5245.548671666723, 'sigma': 5.28401664684793}
creating region 
INFO:RegionCovarianceMatrix 5292.5215020861415:Created region
INFO:CovarianceMatrix:Added region 2 to self.RegionList
INFO:RegionCovarianceMatrix 5268.322855066006:Created region
INFO:CovarianceMatrix:Added region 3 to self.RegionList
 2 2 {'loga': -13.799520625636886, 'mu': 5292.5215020861415, 'sigma': 3.4655108185602868}
creating region  3 3 {'loga': -13.526624291660271, 'mu': 5268.322855066006, 'sigma': 4.243892046645223}
creating region 
INFO:RegionCovarianceMatrix 5236.126714866654:Created region
INFO:CovarianceMatrix:Added region 4 to self.RegionList
INFO:RegionCovarianceMatrix 5269.288173692989:Created region
INFO:CovarianceMatrix:Added region 5 to self.RegionList
 4 4 {'loga': -13.80029888914934, 'mu': 5236.126714866654, 'sigma': 7.23357590421515}
creating region  5 5 {'loga': -13.609796207853215, 'mu': 5269.288173692989, 'sigma': 6.256397698544023}
creating region 
INFO:RegionCovarianceMatrix 5264.728995790121:Created region
INFO:CovarianceMatrix:Added region 6 to self.RegionList
INFO:RegionCovarianceMatrix 5250.5653215616385:Created region
INFO:CovarianceMatrix:Added region 7 to self.RegionList
 6 6 {'loga': -13.855887266629432, 'mu': 5264.728995790121, 'sigma': 5.094022366957161}
creating region  7 7 {'loga': -13.730145834900604, 'mu': 5250.5653215616385, 'sigma': 5.556475512722488}
creating region 
INFO:RegionCovarianceMatrix 5275.914723861688:Created region
INFO:CovarianceMatrix:Added region 8 to self.RegionList
INFO:RegionCovarianceMatrix 5283.987551541256:Created region
INFO:CovarianceMatrix:Added region 9 to self.RegionList
 8 8 {'loga': -13.590268823748854, 'mu': 5275.914723861688, 'sigma': 10.014971435497314}
creating region  9 9 {'loga': -13.640411981639126, 'mu': 5283.987551541256, 'sigma': 4.967901329845103}
creating region 
INFO:RegionCovarianceMatrix 5260.583372324041:Created region
INFO:CovarianceMatrix:Added region 10 to self.RegionList
INFO:RegionCovarianceMatrix 5272.156405134155:Created region
INFO:CovarianceMatrix:Added region 11 to self.RegionList
 10 10 {'loga': -13.48518553209724, 'mu': 5260.583372324041, 'sigma': 6.974545561131224}
creating region  11 11 {'loga': -13.63341431359278, 'mu': 5272.156405134155, 'sigma': 5.588190622722968}

In [4]:
myOrderModel = myModel.OrderModels[1]
model_flux = myOrderModel.get_spectrum()

In [5]:
spec = myModel.get_data()
wl = spec.wls[1]
fl = spec.fls[1]

model_fl = myOrderModel.get_spectrum()
residuals = fl - model_fl

mask = spec.masks[1]
cov = myModel.OrderModels[1].get_Cov().todense()

In [6]:
draws = np.load("PHOENIX_draws.npy").T

In [9]:
utils.visualize_draws(draws, num=50)

In [7]:
#Colorbrewer bands
s3 = '#fee6ce'
s2 = '#fdae6b'
s1 = '#e6550d'

In [9]:
fig, ax = plt.subplots(nrows=2, figsize=(7,3), sharex=True)
l0, = ax[0].plot(wl, fl*1e13, "b")
l1, = ax[0].plot(wl, model_flux*1e13, "r")

ax[0].annotate("data", (0.86, 0.1), xycoords="axes fraction", ha="right", color="b", size=9)
ax[0].annotate("model", (0.97, 0.1), xycoords="axes fraction", ha="right", color="r", size=9)

#ax[0].legend([l0, l1], ["data", "model"], loc="lower right", ncol=2, prop={'size':10})

#Jump again and now plot the envelope of the draws
min_spec, max_spec = utils.std_envelope(draws)
ax[1].fill_between(wl, 3e13*min_spec, 3e13*max_spec, zorder=0, color=s3)
ax[1].fill_between(wl, 2e13*min_spec, 2e13*max_spec, zorder=0, color=s2)
ax[1].fill_between(wl, 1e13*min_spec, 1e13*max_spec, zorder=0, color=s1)

ax[1].plot(wl, residuals*1e13, "w", lw=1.8, zorder=1, ls="steps-mid")
l2, = ax[1].plot(wl, residuals*1e13, "k", zorder=1, ls="steps-mid")

ax[1].set_xlabel(r"$\lambda$ [\AA]")

ax[1].xaxis.set_major_formatter(FSF("%.0f"))
ax[1].set_xlim(np.min(wl), np.max(wl))
scale = 1.2 * np.max(np.abs(residuals)) * 1e13
ax[1].set_ylim(-scale, scale)

ax[1].annotate("residuals", (0.97, 0.8), xycoords="axes fraction", ha="right", color="k", size=9)
#ax[1].legend([l2], ["residuals"], loc="upper right", ncol=2, prop={'size':10})

ax[0].yaxis.set_major_locator(MaxNLocator(nbins=6, prune='lower'))   
ax[1].yaxis.set_major_locator(MaxNLocator(nbins=6, prune='upper'))  

fig.text(0.01, 0.83, r"$f_\lambda\, \times 10^{-13}\, \quad [\textrm{erg}\;\textrm{cm}^{-2}\;\textrm{s}^{-1}\;\textrm{\AA}^{-1}]$", rotation="vertical")
fig.subplots_adjust(hspace=0, left=0.1, right=0.9, bottom=0.17, top=0.93)

saveall(fig, "../../plots/residuals_PHOENIX_logg")
plt.show()

In [ ]: