In [1]:
#simulations infrastructure
import splat
import wisps.simulations as wispsim
from wisps.utils.tools import distance
from astropy.coordinates import SkyCoord

import astropy.units as u
import numpy as np
import matplotlib.pyplot as plt
import wisps
import pandas as pd

import seaborn as sns
%matplotlib inline
import splat.photometry as sphot
import splat.core as spl
import splat.empirical as spe
import matplotlib as mpl
from tqdm import tqdm

#matplotlib defaults
mpl.rcParams['grid.color'] = 'k'
mpl.rcParams['grid.linestyle'] = '--'
mpl.rcParams['grid.linewidth'] = 0.1
mpl.rcParams['figure.figsize'] = [6.0, 3.0]
mpl.rcParams['figure.dpi'] = 80
mpl.rcParams['savefig.dpi'] = 100
mpl.rcParams['font.size'] = 18
mpl.rcParams['legend.fontsize'] = 'large'
mpl.rcParams['figure.titlesize'] = 'large'

from astropy import stats as astrostats

In [2]:
def fit_polynomial(x, y, n=2):
    #sigma clipping
    scatter=np.nanstd(y)
    index=np.where((y<3.0*scatter) | (y>3.0*scatter))[0]
    p = np.poly1d(np.polyfit(x[index], y[index], n))
    return p

def dropnans(x):
    return [~np.isnan(x)]

Generate a spectral type, HST mags relation

Using spex templates/standards


In [3]:
splat.initializeStandards()

In [4]:
#1.visualize dupuy relation
sp_grid=np.arange(20, 38)
js = [spe.typeToMag(splat.typeToNum(x),'2MASS J',set='dupuy') for x in sp_grid]
hs= [spe.typeToMag(splat.typeToNum(x),'2MASS H',set='dupuy') for x in sp_grid]

In [5]:
#transform dupuy relation into HST mags
def get_abs_hst_mag(spt, flt):
    #using splat filtermag
    sp = splat.STDS_DWARF_SPEX[spt]
    sp.fluxCalibrate('2MASS J',float(sp.j_2mass))
    mag, mag_unc = splat.filterMag(sp, flt)
    #calculate the mag of the standard in J and H
    magj, mag_unck = splat.filterMag(sp,'2MASS J')
    magh, mag_unck = splat.filterMag(sp,'2MASS H')
    #calculate the offset between HST filters and 2mass filters
    offsetj=magj-mag
    offset2=magh-mag
    return [splat.filterMag(sp, 'NICMOS {}'.format(k)) for k in ['F110W', 'F140W', 'F160W']]

In [6]:
abs_hstmags=[get_abs_hst_mag(splat.typeToNum(x)) for x in  sp_grid]

Generate apparent HST mags

Using candidates?


In [7]:
ds=np.logspace(-1, 4, 1000)

In [8]:
def get_app_hst_mag(dist, absmag):
    ##returns apparent HST mag given distance and absolute mag
    return np.log10(dist-1)*5+absmag

In [9]:
app_hstmags=[]
for m in np.array(abs_hstmags)[:,:, 0]:
    app_hstmags.append([get_app_hst_mag(ds, x) for x in  m])

Visualization


In [10]:
fig, ax=plt.subplots(ncols=5, sharey=True, figsize=(13, 3))
ax[0].plot(np.array(js)[:,0], sp_grid)
ax[1].plot(np.array(hs)[:,0], sp_grid)

ax[0].set_xlabel('abs J')
ax[1].set_xlabel('abs H')
ax[0].set_ylabel('SpT')

ax[2].plot(np.array(abs_hstmags)[:,:, 0][:,0], sp_grid, '.')
ax[3].plot(np.array(abs_hstmags)[:,:, 0][:,1], sp_grid, '.')
ax[4].plot(np.array(abs_hstmags)[:,:, 0][:,2], sp_grid, '.')

ax[2].set_xlabel('abs F110W')
ax[3].set_xlabel('abs F140W')
ax[4].set_xlabel('abs F160W')



p4=fit_polynomial( sp_grid, np.array(abs_hstmags)[:,:, 0][:,0], n=3)
ax[2].plot(p4(sp_grid), sp_grid)


p5=fit_polynomial(sp_grid, np.array(abs_hstmags)[:,:, 0][:,1], n=3)
ax[3].plot(p5(sp_grid), sp_grid)

p6=fit_polynomial( sp_grid, np.array(abs_hstmags)[:,:, 0][:,2], n=3)
ax[4].plot(p6(sp_grid), sp_grid)

plt.tight_layout()



In [11]:
fig, ax=plt.subplots(ncols=3, figsize=(12, 3), sharex=True)
for a in ax:
    a.invert_yaxis()
    a.set_xscale('log')
for i in np.arange(0, 18):
    ax[0].plot(ds, np.array(app_hstmags)[i][0], color='#0074D9', alpha=0.4)
    ax[1].plot(ds, np.array(app_hstmags)[i][1],  color='#0074D9', alpha=0.4)
    ax[2].plot(ds, np.array(app_hstmags)[i][2],  color='#0074D9', alpha=0.4)

ax[0].set_xlabel('distance (pc)')
ax[1].set_xlabel('distance (pc)')
ax[2].set_xlabel('distance (pc)')

ax[0].set_ylabel('F110W')
ax[1].set_ylabel('F140W')
ax[2].set_ylabel('F160W')
plt.tight_layout()


Using my sample to map to SNR and sigma_error on app mags


In [12]:
cands=wisps.datasets['candidates']

In [13]:
#some reformatting
obs_F110W=np.array(np.apply_along_axis(list, 0,cands.F110.str.replace('nan', '-999').apply(eval).values))
obs_F140W=np.array(np.apply_along_axis(list, 0,cands.F140.str.replace('nan', '-999').apply(eval).values))
obs_F160W=np.array(np.apply_along_axis(list, 0,cands.F160.str.replace('nan', '-999').apply(eval).values))

In [14]:
obs_F160W[obs_F160W < -90.0]=np.nan
obs_F140W[obs_F140W < -90.0]=np.nan
obs_F110W[obs_F110W < -90.0]=np.nan

In [15]:
snrs=cands.snr1.values

In [16]:
fig, ax=plt.subplots(ncols=3, figsize=(12, 3))

mags=np.linspace(10, 27, 100)

for a in ax:
    #a.invert_yaxis()
    a.set_xlim([15, 25])
    a.set_ylabel(r'$\sigma^2_{mag}$')
    a.set_yscale('log')
ax[0].plot(obs_F110W[:,0], obs_F110W[:,1], '.')
ax[1].plot(obs_F140W[:,0], obs_F140W[:,1], '.')
ax[2].plot(obs_F160W[:,0], obs_F160W[:,1], '.')

ax[0].set_xlabel('F110W')
ax[1].set_xlabel('F140W')
ax[2].set_xlabel('F160W')
plt.tight_layout()



In [38]:
fig, ax=plt.subplots(ncols=3, figsize=(12, 3))

for a in ax:
    a.set_ylabel(r'log SNR-J')
    a.set_xlim([15, 27])
    #a.set_ylim([0, 500])

ax[0].plot(obs_F110W[:,0], np.log10(snrs), '.')
ax[1].plot(obs_F140W[:,0], np.log10(snrs), '.')
ax[2].plot(obs_F160W[:,0], np.log10(snrs), '.')

nonnans110=dropnans(obs_F110W[:,0])
p0=fit_polynomial(obs_F110W[:,0][nonnans110], np.log10(snrs[nonnans110]), n=1)
ax[0].plot(mags, p0(mags))

nonnans140=dropnans(obs_F140W[:,0])
p1=fit_polynomial(obs_F140W[:,0][nonnans140], np.log10(snrs[nonnans140]), n=1)
ax[1].plot(mags, p1(mags))

nonnans160=dropnans(obs_F160W[:,0])
p2=fit_polynomial(obs_F160W[:,0][nonnans160], np.log10(snrs[nonnans160]), n=1)
ax[2].plot(mags, p2(mags))

ax[0].set_xlabel('F110W')
ax[1].set_xlabel('F140W')
ax[2].set_xlabel('F160W')
plt.tight_layout()

plt.savefig(wisps.OUTPUT_FIGURES+'/snr_fits.pdf')



In [39]:
polrealtions={'snr_F110W':p0, 'snr_F140W':p1, 'snr_F160W':p2, \
             'sp_F110W':p4, 'sp_F140W':p5, 'sp_F160W':p6}

In [40]:
import pickle
with open(wisps.OUTPUT_FILES+'/polynomial_relations.pkl', 'wb') as file:
    pickle.dump(polrealtions,file)

In [ ]: