In [1]:
import splat
import wisps
from astropy.io import fits, ascii
import matplotlib.pyplot as plt
import glob
import pandas as pd
import numpy as np

%matplotlib inline

In [2]:
data='/Users/caganze/research/wisps/data/manjavacas/final_data_journal/*.csv'

In [3]:
splat.initiateStandards()

In [4]:
spectra=[]
for f in glob.glob(data):
    try:
        d=ascii.read(f).to_pandas()

        s=wisps.Spectrum(wave=d.col1, flux=d.col2, noise=d.col3)
        s._filename=f.split('/')[-1].split('.csv')[0]
        spectra.append(s)
    except:
        pass


Warning: normalization range [1.15, 1.65] is outside range of spectrum wave array: [1.1515, 1.624]

Warning: normalization range [1.15, 1.65] is outside range of spectrum wave array: [1.1625, 1.608]

Warning: normalization range [1.15, 1.65] is outside range of spectrum wave array: [1.1315, 1.6325]

Warning: normalization range [1.15, 1.65] is outside range of spectrum wave array: [1.1565, 1.6067]

Warning: normalization range [1.15, 1.65] is outside range of spectrum wave array: [1.125, 1.625]

Warning: normalization range [1.15, 1.65] is outside range of spectrum wave array: [1.142, 1.632]

Warning: normalization range [1.15, 1.65] is outside range of spectrum wave array: [1.1315, 1.5765]

Warning: normalization range [1.15, 1.65] is outside range of spectrum wave array: [1.1425, 1.6325]

Warning: normalization range [1.15, 1.65] is outside range of spectrum wave array: [1.127, 1.6475]

In [5]:
indices=pd.DataFrame([s.indices for s in spectra])
snrs=pd.DataFrame([s.snr for s in spectra])
ftests=pd.DataFrame.from_records([{'spex_chi': s.spex_chi, 'line_chi':s.line_chi, 'spt':s.spectral_type, 'f_test':s.f_test} for s in spectra])

In [6]:
df=pd.DataFrame()
for k in indices.columns: df[k]=indices[k]
for k in ftests.columns: df[k]=ftests[k]
for k in snrs.columns: df[k]=snrs[k]
df['name']=[s.filename for s in spectra]
#df['spectra']=spectra

In [7]:
df.to_pickle(wisps.LIBRARIES+'/manjavacas.pkl')

In [8]:
for s in spectra:
    s.splat_spectrum.plot(ylabel=s.filename)


No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.

In [9]:
#ghjkla
#spex=wisps.spex_sample_ids(stype='spex_sample',  from_file=False)
#spexids=pd.DataFrame([x for x in spex['Indices']])
#for k in spexids.columns: spex[k]=spexids[k]
#ref=wisps.Annotator.reformat_table(spexids)
df['spectra']=spectra
df=wisps.Annotator.reformat_table(df)

In [10]:
f=df[(df['CH_4/H-Cont'] <0.5) & (df['H-cont/J-Cont'] >0.6)].spectra.apply(lambda x: x.splat_spectrum.plot(ylabel=x.filename))


No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.

In [11]:
np.vstack(df.spt.values)[:,0]


Out[11]:
array([38., 33., 32., 25., 26., 39., 37., 40., 25., 30., 39., 35., 38.,
       35., 32., 40., 27., 32., 32., 36., 36., 39., 40., 14., 25., 40.,
       27., 27., 34., 25., 36., 40., 41., 31., 27., 28., 39., 40., 34.,
       40., 34., 31., 27., 26., 16., 27., 39., 40., 39., 28., 40., 40.,
       28., 39., 17., 34., 13., 40., 28., 38., 29., 35., 32., 29., 32.,
       23., 36., 36., 29., 38., 13., 25., 27.])

In [12]:
ydwarfs=df[np.vstack(df.spt.values)[:,0]>37]
#df[(df['CH_4/H-Cont'] <0.5) & (df['H-cont/J-Cont'] >0.6)]

In [13]:
schn='/Users/caganze/research/wisps/data/schneider/*.txt'

In [14]:
schntb=pd.read_csv('/Users/caganze/research/wisps/data/schneider2015.txt', delimiter=' ').drop(columns='Unnamed: 14')

In [15]:
def get_shortname(n):
    return splat.designationToShortName(n).replace('J', 'WISE')

In [16]:
schntb['shortname']=schntb.Name.apply(get_shortname)

In [17]:
schntb


Out[17]:
Name Spec Y_MKO Y_MKO_er J_MKO J_MKO_er H_MKO H_MKO_er F105W F105W_er F125W F125W_er F140W F140W_eR shortname
0 J032504.52-504403.0 T8 19.980 0.027 18.935 0.024 19.423 0.027 20.601 0.028 19.547 0.023 19.223 0.022 WISE0325-5044
1 J033515.07+431044.7 T9 20.166 0.029 19.467 0.023 19.938 0.031 20.939 0.033 20.137 0.025 19.785 0.023 WISE0335+4310
2 J035000.31-565830.5 Y1 NaN NaN 22.178 0.073 22.263 0.135 NaN NaN 22.951 0.114 22.431 0.073 WISE0350-5658
3 J035934.07-540154.8 Y0 NaN NaN 21.566 0.046 22.028 0.112 NaN NaN 22.258 0.062 21.789 0.045 WISE0359-5401
4 J040443.50-642030.0 T9 20.328 0.032 19.647 0.025 19.970 0.033 21.063 0.037 20.293 0.028 19.893 0.024 WISE0404-6420
5 J041022.75+150247.9 Y0 NaN NaN 19.325 0.024 19.897 0.038 NaN NaN 19.997 0.025 19.643 0.024 WISE0410+1502
6 J053516.87-750024.6 Y1 22.701 0.070 22.132 0.071 NaN NaN 23.581 0.138 22.876 0.102 NaN NaN WISE0535-7500
7 J064723.24-623235.4 Y1 22.870 0.076 22.854 0.066 23.306 0.166 23.833 0.117 23.683 0.098 23.204 0.066 WISE0647-6232
8 J073444.03-715743.8 Y0 20.870 0.041 20.354 0.029 21.069 0.071 21.675 0.051 21.045 0.035 20.726 0.030 WISE0734-7157
9 J082507.37+280548.2 Y0.5 22.566 0.053 22.401 0.050 22.965 0.139 23.409 0.073 23.015 0.062 22.731 0.051 WISE0825+2805
10 J094306.00+360723.3 T9.5 NaN NaN 19.766 0.025 20.315 0.038 NaN NaN 20.444 0.027 20.092 0.025 WISE0943+3607
11 J120604.25+840110.5 Y0 20.875 0.036 20.472 0.030 21.061 0.062 21.819 0.050 21.171 0.036 20.798 0.029 WISE1206+8401
12 J140518.32+553421.3 Y0.5 21.333 0.057 21.061 0.035 21.501 0.073 22.193 0.076 21.730 0.044 21.375 0.035 WISE1405+5534
13 J154151.65-225024.9 Y1 21.671 0.037 21.631 0.064 22.085 0.170 22.724 0.083 22.443 0.097 22.138 0.078 WISE1541-2250
14 J154214.00+223005.2 T9.5 20.461 0.028 19.937 0.026 20.520 0.045 21.273 0.034 20.586 0.027 20.235 0.025 WISE1542+2230
15 J163940.84-684739.4 Y0pec 20.833 0.023 20.626 0.023 20.746 0.029 21.674 0.025 21.252 0.024 20.824 0.024 WISE1639-6847
16 J173835.52+273258.8 Y0 NaN NaN 19.546 0.023 20.246 0.031 NaN NaN 20.223 0.023 19.923 0.023 WISE1738+2732
17 J205628.88+145953.6 Y0 NaN NaN 19.129 0.022 19.643 0.026 NaN NaN 19.811 0.023 19.479 0.022 WISE2056+1459
18 J220905.75+271143.6 Y0 22.954 0.071 22.859 0.128 22.389 0.152 23.508 0.110 23.355 0.156 22.877 0.105 WISE2209+2711
19 J221216.27-693121.6 T9 20.282 0.023 19.737 0.024 20.225 0.036 21.043 0.027 20.378 0.026 20.047 0.024 WISE2212-6931
20 J222055.34-362817.5 Y0 20.899 0.034 20.447 0.025 20.858 0.035 21.783 0.042 21.131 0.027 20.749 0.024 WISE2220-3628
21 J235402.79+024014.1 Y1 NaN NaN 23.068 0.199 22.882 0.300 NaN NaN 24.124 0.393 23.292 0.179 WISE2354+0240

In [18]:
spectra_schn=[]
shortnams=[]
for f in glob.glob(schn):
    d=ascii.read(f).to_pandas()
    shortname=(f.split('/')[-1]).split('.txt')[0]
    s=wisps.Spectrum(wave=d.col1, flux=d.col2, noise=d.col3)
    s._filename=shortname
    spectra_schn.append(s)
    shortnams.append((f.split('/')[-1]).split('.txt')[0])

In [19]:
indices=pd.DataFrame([s.indices for s in spectra_schn])
snrs=pd.DataFrame([s.snr for s in spectra_schn])
fs=pd.DataFrame.from_records([{'spex_chi': s.spex_chi,
                               'line_chi':s.line_chi, 'spt':s.spectral_type, 'f_test':s.f_test} for s in spectra_schn])

In [20]:
df=pd.DataFrame()
df['name']=[s.filename.replace('.txt', '') for s in spectra_schn ]
df['shortname']=shortnams

In [21]:
new_df=df.join(indices).join(snrs).join(fs)

In [22]:
nf_df=new_df.merge(schntb)

In [23]:
#nf_df['spectra']=spectra_schn

In [24]:
nf_df.to_pickle(wisps.LIBRARIES+'/schneider.pkl')

In [25]:
dfnnn=wisps.Annotator.reformat_table(nf_df[wisps.INDEX_NAMES])

In [26]:
import wisps
import numpy as np
import splat as spl

In [27]:
nan=np.nan

In [28]:
distances=wisps.distance({'F110W': (nan, nan), 'F160W': (19.614, 0.004), 'F140W': (nan, nan)}, 20, 1)

In [29]:
ds=np.array([distances[k] for k in distances.keys() if ('dist'  in k) and ('dist_er'  not in k)])
ers=np.array([distances[k] for k in distances.keys() if 'dist_er'  in k])

#distance is the weighted mean and std 
nans=np.isnan(ds)
val, unc=spl.weightedMeanVar(ds[~nans], ers[~nans])

In [30]:
val, (unc**2+(ers[~nans]**2).sum())**0.5


Out[30]:
(425.84689326645037, 118.74618432930191)

In [31]:
distances


Out[31]:
{'distF110W': nan,
 'dist_erF110W': nan,
 'distF160W': 425.84689326645037,
 'dist_erF160W': 118.74618432930191,
 'distF140W': nan,
 'dist_erF140W': nan}

In [ ]: