This demonstrates all the steps in my candidate selection before conducting visual inspection


In [1]:
import numpy as np
import splat
import wisps.data_analysis as wispd
from wisps.data_analysis import selection_criteria as sel_crt
import shapey
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import wisps

import matplotlib as mpl

from tqdm import tqdm
import random
import matplotlib.pyplot as plt
%matplotlib inline

In [2]:
import pandas as pd
df=pd.read_pickle(wisps.OUTPUT_FILES+'/stdspex_sample.pkl')

In [3]:
df2=pd.read_pickle(wisps.LIBRARIES+'/spex_data_set_table.pkl')

In [4]:
df2.columns


Out[4]:
Index(['H_2O-1/J-Cont', 'H_2O-2/H_2O-1', 'H-cont/H_2O-1', 'CH_4/H_2O-1',
       'H_2O-2/J-Cont', 'H-cont/J-Cont', 'CH_4/J-Cont', 'H-cont/H_2O-2',
       'CH_4/H_2O-2', 'CH_4/H-Cont', 'H_2O-1+H_2O-2/J-Cont',
       'H_2O-1+H_2O-2/H-Cont', 'H_2O-1+CH_4/J-Cont', 'H_2O-2+CH_4/J-Cont',
       'H_2O-1+CH_4/H-Cont', 'H_2O-2+CH_4/H-Cont', 'snr1', 'snr2', 'snr3',
       'snr4', 'shortname', 'data_file', 'metallicity_class',
       'gravity_class_nir', 'sbinary', 'h_2mass', 'j_2mass', 'f_test',
       'line_chi', 'spex_chi', 'spt'],
      dtype='object')

In [5]:
#withnans=wisps.datasets['highsnr']
#wihtoutnans=
alldata=pd.read_hdf(wisps.LIBRARIES+'/master_dataset.hdf',  key='new')

In [6]:
sf=pd.read_pickle(wisps.OUTPUT_FILES+'/selection_function.pkl') #the simulated spectral data

In [7]:
simulated_data=pd.DataFrame.from_records(pd.DataFrame(sf).values.flatten())

In [8]:
simulated_data=wisps.Annotator.reformat_table(simulated_data)

In [9]:
star_ids=alldata[alldata['class_star'] !=0]

In [10]:
stars=wisps.Annotator.reformat_table(star_ids).reset_index(drop=True)

In [11]:
spex=wisps.Annotator.reformat_table(wisps.datasets['spex'])

for k in wisps.INDEX_NAMES: fig, ax=plt.subplots()

#plt.hist(spex[k].apply(np.log10).replace((np.inf, -np.inf), np.nan).dropna(), bins=32, histtype='step', normed=True)
plt.hist(comb[k].apply(np.log10).replace((np.inf, -np.inf), np.nan).dropna(), bins=32, histtype='step', normed=True)
plt.hist(st[k].apply(np.log10).replace((np.inf, -np.inf), np.nan).dropna(), bins=32, histtype='step', normed=True, color='k')

ax.set_title(r'$'+str(k.split(' ')[0])+'$', fontsize=18)

In [12]:
cands=pd.read_hdf(wisps.OUTPUT_FILES+'/true_spectra_cands.hdf')

In [13]:
cands=wisps.Annotator.reformat_table(cands)

In [14]:
cands=cands[(cands.spt >=17.) | ((cands.spt >=16))]

In [15]:
dt=stars[(stars.f_test< 0.4) &(stars.snr2>=1.)].reset_index(drop=True)

In [16]:
dt['spt']=(dt['spt']).apply(wisps.make_spt_number).apply(float)

In [17]:
gbhio=sel_crt.save_criteria(conts=dt)


(2452, 33) (113, 33)
100%|██████████| 120/120 [06:34<00:00,  3.29s/it]

In [18]:
crts=sel_crt.crts_from_file()

In [19]:
contamns=pd.DataFrame([ x.contamination for x in crts.values()])
compls=pd.DataFrame([ x.completeness for x in crts.values()])

In [20]:
compls.min()


Out[20]:
L0-L5        0.968981
L5-T0        0.943284
M7-L0        0.955314
T0-T5        0.945946
T5-T9        0.910112
subdwarfs    0.929204
trash        0.951456
Y dwarfs     0.818182
dtype: float64

In [21]:
contamns.index=[x for x in crts.keys()]
compls.index=[x for x in crts.keys()]

In [22]:
1/100


Out[22]:
0.01

In [23]:
contamns.style.apply(lambda x: ["background-color: #7FDBFF" 
                          if (i >= 0 and (v < 0.1 
                                          and v > 0. )) 
                          else "" for i, v in enumerate(x)], axis = 1)


Out[23]:
L0-L5 L5-T0 M7-L0 T0-T5 T5-T9 subdwarfs trash Y dwarfs
H_2O-1/J-Cont H_2O-2/H_2O-1 0.097745 0.119754 0.074187 0.001834 0.000085 0.148445 0.154757 0.000213
H_2O-1/J-Cont H-cont/H_2O-1 0.204732 0.191652 0.123507 0.073078 0.009384 0.162193 0.166415 0.006341
H_2O-1/J-Cont CH_4/H_2O-1 0.207646 0.197893 0.130019 0.065130 0.000654 0.178842 0.179140 0.002588
H_2O-1/J-Cont H_2O-2/J-Cont 0.083892 0.120145 0.078825 0.002178 0.000057 0.191967 0.164866 0.000199
H_2O-1/J-Cont H-cont/J-Cont 0.196277 0.165618 0.105483 0.065259 0.000911 0.122961 0.143670 0.002690
H_2O-1/J-Cont CH_4/J-Cont 0.202696 0.179538 0.107020 0.080133 0.000541 0.133864 0.154687 0.000327
H_2O-1/J-Cont H-cont/H_2O-2 0.127481 0.168427 0.170050 0.102509 0.012116 0.383809 0.214926 0.007019
H_2O-1/J-Cont CH_4/H_2O-2 0.121599 0.141446 0.170404 0.070289 0.012073 0.375020 0.221415 0.006976
H_2O-1/J-Cont CH_4/H-Cont 0.076654 0.114710 0.096671 0.090179 0.000826 0.173368 0.116803 0.000128
H_2O-1/J-Cont H_2O-1+H_2O-2/J-Cont 0.107228 0.151828 0.115816 0.021255 0.000313 0.290506 0.206295 0.000213
H_2O-1/J-Cont H_2O-1+H_2O-2/H-Cont 0.134042 0.077357 0.163259 0.033553 0.001862 0.284449 0.198376 0.000299
H_2O-1/J-Cont H_2O-1+CH_4/J-Cont 0.201973 0.187287 0.111863 0.093210 0.001322 0.199571 0.143824 0.000455
H_2O-1/J-Cont H_2O-2+CH_4/J-Cont 0.115190 0.123081 0.124758 0.094631 0.005744 0.160345 0.151516 0.000256
H_2O-1/J-Cont H_2O-1+CH_4/H-Cont 0.185530 0.192918 0.087591 0.035612 0.000100 0.146603 0.141151 0.000085
H_2O-1/J-Cont H_2O-2+CH_4/H-Cont 0.086092 0.102550 0.115491 0.038682 0.000085 0.275374 0.158915 0.000014
H_2O-2/H_2O-1 H-cont/H_2O-1 0.693120 0.241765 0.269375 0.017032 0.142827 0.251233 0.282270 0.982399
H_2O-2/H_2O-1 CH_4/H_2O-1 0.692509 0.274038 0.238807 0.018696 0.122269 0.248276 0.285980 0.980138
H_2O-2/H_2O-1 H_2O-2/J-Cont 0.472017 0.477426 0.214495 0.006191 0.002719 0.286672 0.258931 0.007117
H_2O-2/H_2O-1 H-cont/J-Cont 0.643120 0.533298 0.209539 0.055509 0.015898 0.149774 0.197782 0.464254
H_2O-2/H_2O-1 CH_4/J-Cont 0.662463 0.545240 0.200658 0.072902 0.004640 0.154101 0.210365 0.014233
H_2O-2/H_2O-1 H-cont/H_2O-2 0.583792 0.611042 0.255414 0.090363 0.141887 0.322001 0.261464 0.980894
H_2O-2/H_2O-1 CH_4/H_2O-2 0.548519 0.423235 0.254940 0.080866 0.139963 0.317284 0.270273 0.975726
H_2O-2/H_2O-1 CH_4/H-Cont 0.268853 0.383163 0.151611 0.076880 0.004342 0.149675 0.143353 0.007190
H_2O-2/H_2O-1 H_2O-1+H_2O-2/J-Cont 0.226936 0.185137 0.099876 0.004052 0.000085 0.228784 0.178086 0.002389
H_2O-2/H_2O-1 H_2O-1+H_2O-2/H-Cont 0.293245 0.085204 0.197833 0.006469 0.002346 0.242433 0.255157 0.008687
H_2O-2/H_2O-1 H_2O-1+CH_4/J-Cont 0.562776 0.522001 0.108449 0.042040 0.000398 0.141291 0.129733 0.002673
H_2O-2/H_2O-1 H_2O-2+CH_4/J-Cont 0.323258 0.221150 0.236092 0.027368 0.002275 0.210544 0.227618 0.004223
H_2O-2/H_2O-1 H_2O-1+CH_4/H-Cont 0.645170 0.680354 0.219687 0.061444 0.001566 0.214222 0.216684 0.003302
H_2O-2/H_2O-1 H_2O-2+CH_4/H-Cont 0.285592 0.346412 0.161859 0.055211 0.000626 0.232574 0.187899 0.000940
H-cont/H_2O-1 CH_4/H_2O-1 0.636308 0.471004 0.382572 0.373943 0.010919 0.358517 0.349375 0.989977
H-cont/H_2O-1 H_2O-2/J-Cont 0.155242 0.095847 0.114720 0.002306 0.000313 0.301133 0.230849 0.007102
H-cont/H_2O-1 H-cont/J-Cont 0.569849 0.348838 0.257746 0.202750 0.000882 0.215745 0.266258 0.465592
H-cont/H_2O-1 CH_4/J-Cont 0.597888 0.397928 0.247171 0.286896 0.000313 0.207134 0.248666 0.014532
H-cont/H_2O-1 H-cont/H_2O-2 0.414784 0.423127 0.276527 0.412734 0.073449 0.422216 0.308746 0.996654
H-cont/H_2O-1 CH_4/H_2O-2 0.364849 0.283969 0.271697 0.310920 0.073021 0.409923 0.297836 0.991458
H-cont/H_2O-1 CH_4/H-Cont 0.231310 0.264098 0.176697 0.388887 0.002819 0.180042 0.146543 0.007346
H-cont/H_2O-1 H_2O-1+H_2O-2/J-Cont 0.135590 0.103260 0.096237 0.014686 0.000100 0.269005 0.179010 0.002374
H-cont/H_2O-1 H_2O-1+H_2O-2/H-Cont 0.449308 0.151798 0.403244 0.031719 0.005701 0.403969 0.398738 0.008530
H-cont/H_2O-1 H_2O-1+CH_4/J-Cont 0.489358 0.282241 0.111222 0.253480 0.000668 0.161338 0.138277 0.003000
H-cont/H_2O-1 H_2O-2+CH_4/J-Cont 0.538678 0.365640 0.427058 0.410737 0.012596 0.376957 0.356199 0.004635
H-cont/H_2O-1 H_2O-1+CH_4/H-Cont 0.510098 0.285672 0.161275 0.064248 0.000043 0.207347 0.224327 0.003288
H-cont/H_2O-1 H_2O-2+CH_4/H-Cont 0.282531 0.221697 0.194063 0.180524 0.000313 0.298192 0.213667 0.000940
CH_4/H_2O-1 H_2O-2/J-Cont 0.127074 0.070625 0.111902 0.002790 0.000413 0.297148 0.241339 0.006661
CH_4/H_2O-1 H-cont/J-Cont 0.466090 0.265802 0.213482 0.312245 0.024965 0.188332 0.241933 0.466332
CH_4/H_2O-1 CH_4/J-Cont 0.510540 0.303733 0.272449 0.288162 0.000470 0.254088 0.319504 0.014503
CH_4/H_2O-1 H-cont/H_2O-2 0.329589 0.266618 0.297114 0.575962 0.774783 0.450206 0.316932 0.988838
CH_4/H_2O-1 CH_4/H_2O-2 0.340362 0.220772 0.299146 0.131819 0.774587 0.454670 0.358414 0.984866
CH_4/H_2O-1 CH_4/H-Cont 0.217287 0.219693 0.187189 0.484617 0.002648 0.194906 0.169137 0.007318
CH_4/H_2O-1 H_2O-1+H_2O-2/J-Cont 0.126974 0.086398 0.101753 0.009255 0.000085 0.278516 0.195061 0.002303
CH_4/H_2O-1 H_2O-1+H_2O-2/H-Cont 0.341044 0.125880 0.355361 0.027695 0.000142 0.389226 0.356569 0.006583
CH_4/H_2O-1 H_2O-1+CH_4/J-Cont 0.406927 0.207160 0.114293 0.146395 0.000455 0.182678 0.157016 0.002971
CH_4/H_2O-1 H_2O-2+CH_4/J-Cont 0.325049 0.204402 0.295364 0.281004 0.002559 0.288113 0.259721 0.004592
CH_4/H_2O-1 H_2O-1+CH_4/H-Cont 0.419334 0.193797 0.147881 0.021862 0.000057 0.225594 0.255298 0.003302
CH_4/H_2O-1 H_2O-2+CH_4/H-Cont 0.218479 0.127036 0.199601 0.069789 0.000128 0.326324 0.218109 0.000940
H_2O-2/J-Cont H-cont/J-Cont 0.428094 0.198440 0.178812 0.001993 0.000100 0.199550 0.261365 0.000299
H_2O-2/J-Cont CH_4/J-Cont 0.431239 0.181061 0.159014 0.004398 0.001181 0.194241 0.265422 0.001879
H_2O-2/J-Cont H-cont/H_2O-2 0.406176 0.492682 0.205900 0.006022 0.001865 0.365514 0.281927 0.005026
H_2O-2/J-Cont CH_4/H_2O-2 0.400347 0.461880 0.209542 0.004086 0.001481 0.359700 0.294338 0.002563
H_2O-2/J-Cont CH_4/H-Cont 0.179376 0.217845 0.112959 0.005125 0.000057 0.166021 0.160155 0.000085
H_2O-2/J-Cont H_2O-1+H_2O-2/J-Cont 0.349426 0.397250 0.195366 0.002320 0.000114 0.342125 0.283071 0.000427
H_2O-2/J-Cont H_2O-1+H_2O-2/H-Cont 0.199621 0.091434 0.134148 0.001808 0.000470 0.225711 0.235475 0.001722
H_2O-2/J-Cont H_2O-1+CH_4/J-Cont 0.397663 0.356942 0.122719 0.003188 0.000100 0.216844 0.185915 0.000285
H_2O-2/J-Cont H_2O-2+CH_4/J-Cont 0.168493 0.134134 0.129352 0.002348 0.000171 0.184249 0.197387 0.000384
H_2O-2/J-Cont H_2O-1+CH_4/H-Cont 0.439210 0.457670 0.244840 0.005295 0.001352 0.335350 0.345939 0.002391
H_2O-2/J-Cont H_2O-2+CH_4/H-Cont 0.223394 0.339213 0.130013 0.004684 0.000071 0.256268 0.205627 0.000100
H-cont/J-Cont CH_4/J-Cont 0.795300 0.722342 0.271652 0.522538 0.001736 0.194480 0.283964 0.004426
H-cont/J-Cont H-cont/H_2O-2 0.376059 0.390880 0.167827 0.522601 0.053005 0.276385 0.281325 0.433890
H-cont/J-Cont CH_4/H_2O-2 0.319576 0.216415 0.191671 0.280866 0.052976 0.268052 0.270031 0.433685
H-cont/J-Cont CH_4/H-Cont 0.314483 0.433819 0.149176 0.470607 0.001652 0.121129 0.135210 0.003574
H-cont/J-Cont H_2O-1+H_2O-2/J-Cont 0.169971 0.101425 0.128966 0.019015 0.000157 0.225864 0.200344 0.000555
H-cont/J-Cont H_2O-1+H_2O-2/H-Cont 0.203689 0.080004 0.182212 0.014774 0.000157 0.225850 0.285942 0.000541
H-cont/J-Cont H_2O-1+CH_4/J-Cont 0.712236 0.538166 0.145561 0.387395 0.000683 0.169302 0.168277 0.001053
H-cont/J-Cont H_2O-2+CH_4/J-Cont 0.374699 0.270841 0.239713 0.245961 0.000669 0.184304 0.226718 0.000911
H-cont/J-Cont H_2O-1+CH_4/H-Cont 0.776997 0.654578 0.260935 0.170996 0.000100 0.226633 0.278256 0.000242
H-cont/J-Cont H_2O-2+CH_4/H-Cont 0.296982 0.250470 0.122679 0.133186 0.000114 0.187714 0.190433 0.000199
CH_4/J-Cont H-cont/H_2O-2 0.409915 0.369510 0.205014 0.681786 0.009553 0.275516 0.295932 0.013297
CH_4/J-Cont CH_4/H_2O-2 0.348776 0.230951 0.181150 0.441358 0.009553 0.279655 0.337671 0.013240
CH_4/J-Cont CH_4/H-Cont 0.316761 0.488062 0.147269 0.663207 0.005609 0.115762 0.155284 0.007062
CH_4/J-Cont H_2O-1+H_2O-2/J-Cont 0.144764 0.093824 0.120355 0.033661 0.000185 0.220442 0.209539 0.000185
CH_4/J-Cont H_2O-1+H_2O-2/H-Cont 0.266058 0.111217 0.213652 0.045532 0.000114 0.212129 0.279438 0.000085
CH_4/J-Cont H_2O-1+CH_4/J-Cont 0.674675 0.527961 0.154301 0.607452 0.000897 0.180077 0.198636 0.000640
CH_4/J-Cont H_2O-2+CH_4/J-Cont 0.343757 0.266528 0.212983 0.417313 0.000982 0.146871 0.199334 0.000285
CH_4/J-Cont H_2O-1+CH_4/H-Cont 0.744858 0.666505 0.260052 0.374400 0.000868 0.247598 0.355285 0.002348
CH_4/J-Cont H_2O-2+CH_4/H-Cont 0.284539 0.235663 0.151623 0.285777 0.000185 0.197423 0.200641 0.000157
H-cont/H_2O-2 CH_4/H_2O-2 0.281994 0.169066 0.537593 0.015661 0.996056 0.732029 0.506257 0.993052
H-cont/H_2O-2 CH_4/H-Cont 0.095959 0.063982 0.236339 0.014878 0.016942 0.306586 0.212606 0.007361
H-cont/H_2O-2 H_2O-1+H_2O-2/J-Cont 0.110779 0.074033 0.227395 0.003403 0.002136 0.447330 0.270690 0.002420
H-cont/H_2O-2 H_2O-1+H_2O-2/H-Cont 0.237347 0.113569 0.400903 0.010137 0.016116 0.489500 0.434474 0.007304
H-cont/H_2O-2 H_2O-1+CH_4/J-Cont 0.203021 0.094150 0.163826 0.004499 0.005795 0.265593 0.194792 0.003033
H-cont/H_2O-2 H_2O-2+CH_4/J-Cont 0.126981 0.080696 0.282948 0.012443 0.032304 0.259229 0.269323 0.004641
H-cont/H_2O-2 H_2O-1+CH_4/H-Cont 0.230214 0.127977 0.206196 0.004314 0.001879 0.245348 0.238714 0.003033
H-cont/H_2O-2 H_2O-2+CH_4/H-Cont 0.224049 0.146158 0.426743 0.016885 0.001495 0.675551 0.432096 0.000911
CH_4/H_2O-2 CH_4/H-Cont 0.105611 0.066630 0.236962 0.011205 0.016472 0.344082 0.256424 0.007361
CH_4/H_2O-2 H_2O-1+H_2O-2/J-Cont 0.103360 0.058927 0.197053 0.002990 0.002050 0.484097 0.281592 0.002321
CH_4/H_2O-2 H_2O-1+H_2O-2/H-Cont 0.184254 0.067753 0.311147 0.010236 0.014764 0.494092 0.385450 0.006079
CH_4/H_2O-2 H_2O-1+CH_4/J-Cont 0.167753 0.061959 0.130837 0.003175 0.005709 0.283599 0.210037 0.002976
CH_4/H_2O-2 H_2O-2+CH_4/J-Cont 0.122993 0.048477 0.247423 0.010692 0.030666 0.294077 0.252705 0.004442
CH_4/H_2O-2 H_2O-1+CH_4/H-Cont 0.189849 0.087315 0.157674 0.002919 0.001894 0.259069 0.255026 0.002933
CH_4/H_2O-2 H_2O-2+CH_4/H-Cont 0.140305 0.081734 0.261190 0.011375 0.001452 0.560649 0.312329 0.000925
CH_4/H-Cont H_2O-1+H_2O-2/J-Cont 0.085337 0.089765 0.083956 0.026623 0.000100 0.290252 0.150814 0.000057
CH_4/H-Cont H_2O-1+H_2O-2/H-Cont 0.185680 0.076738 0.177067 0.064281 0.000769 0.259058 0.178733 0.000128
CH_4/H-Cont H_2O-1+CH_4/J-Cont 0.343869 0.385612 0.084711 0.452811 0.000769 0.169407 0.117485 0.000242
CH_4/H-Cont H_2O-2+CH_4/J-Cont 0.216262 0.183189 0.176839 0.509105 0.004712 0.176910 0.140691 0.000413
CH_4/H-Cont H_2O-1+CH_4/H-Cont 0.355856 0.427441 0.108302 0.209926 0.000185 0.157847 0.141303 0.000142
CH_4/H-Cont H_2O-2+CH_4/H-Cont 0.117399 0.096827 0.157163 0.266675 0.000413 0.371617 0.198180 0.000285
H_2O-1+H_2O-2/J-Cont H_2O-1+H_2O-2/H-Cont 0.173564 0.101099 0.141277 0.011289 0.001223 0.232296 0.220837 0.001607
H_2O-1+H_2O-2/J-Cont H_2O-1+CH_4/J-Cont 0.261427 0.139230 0.143523 0.016279 0.000725 0.298165 0.229168 0.000796
H_2O-1+H_2O-2/J-Cont H_2O-2+CH_4/J-Cont 0.119752 0.107369 0.111534 0.009938 0.001066 0.150163 0.166427 0.000682
H_2O-1+H_2O-2/J-Cont H_2O-1+CH_4/H-Cont 0.262529 0.218050 0.149746 0.011073 0.000100 0.230561 0.217324 0.000228
H_2O-1+H_2O-2/J-Cont H_2O-2+CH_4/H-Cont 0.139450 0.162400 0.120601 0.010009 0.000043 0.294661 0.199345 0.000014
H_2O-1+H_2O-2/H-Cont H_2O-1+CH_4/J-Cont 0.301975 0.106587 0.126136 0.045310 0.001976 0.229737 0.167792 0.000711
H_2O-1+H_2O-2/H-Cont H_2O-2+CH_4/J-Cont 0.213088 0.072664 0.397856 0.058774 0.010578 0.429888 0.399633 0.001479
H_2O-1+H_2O-2/H-Cont H_2O-1+CH_4/H-Cont 0.297884 0.118946 0.141420 0.015457 0.000057 0.223573 0.233308 0.000043
H_2O-1+H_2O-2/H-Cont H_2O-2+CH_4/H-Cont 0.265732 0.145700 0.277349 0.058457 0.000370 0.471298 0.285436 0.000142
H_2O-1+CH_4/J-Cont H_2O-2+CH_4/J-Cont 0.289762 0.234173 0.116326 0.132761 0.003114 0.124230 0.125780 0.002076
H_2O-1+CH_4/J-Cont H_2O-1+CH_4/H-Cont 0.707625 0.665808 0.151482 0.122205 0.000399 0.187150 0.179066 0.000498
H_2O-1+CH_4/J-Cont H_2O-2+CH_4/H-Cont 0.273220 0.275740 0.112543 0.077548 0.000470 0.223633 0.150541 0.000327
H_2O-2+CH_4/J-Cont H_2O-1+CH_4/H-Cont 0.406240 0.270698 0.153617 0.115131 0.000228 0.186538 0.184660 0.000228
H_2O-2+CH_4/J-Cont H_2O-2+CH_4/H-Cont 0.234339 0.234339 0.196313 0.307574 0.000968 0.304841 0.214849 0.000313
H_2O-1+CH_4/H-Cont H_2O-2+CH_4/H-Cont 0.316031 0.351110 0.134040 0.060763 0.000327 0.216002 0.179385 0.000626

In [24]:
crts['H_2O-1/J-Cont H_2O-2/H_2O-1']


Out[24]:
index-index space of H_2O-1/J-Cont H_2O-2/H_2O-1

In [25]:
#

In [26]:
to_use={'H_2O-1/J-Cont H_2O-1+CH_4/H-Cont':'L0-L5',
        'H_2O-1+H_2O-2/J-Cont H_2O-1+H_2O-2/H-Cont':'L5-T0' ,
        'H_2O-1/J-Cont H_2O-2/J-Cont':'M7-L0',
        'H_2O-1+H_2O-2/J-Cont H_2O-2+CH_4/J-Cont': 'T0-T5',
        'H_2O-2/J-Cont H_2O-1+CH_4/H-Cont': 'T5-T9',
        'H_2O-1+CH_4/J-Cont H_2O-2+CH_4/J-Cont': 'Y dwarfs',
        'H_2O-1/J-Cont H-cont/J-Cont': 'subdwarfs'}

In [ ]:


In [27]:
len(to_use)


Out[27]:
7

In [28]:
def box_parameters(idx, spt_range):
    bs=idx.shapes
    b=[x for x in bs if x.shape_name==spt_range][0]
    print ('{}  {} m: {} b: {} s:{}, comp : {}, cont: {}'.format(spt_range, idx,  round(b.coeffs[0], 2), round(b.coeffs[1], 2), round(b.scatter, 2), round(idx.completeness[spt_range], 2),  round(idx.contamination[spt_range], 3)))

In [29]:
spex_df=wisps.Annotator.reformat_table(wisps.datasets['spex']).reset_index(drop=True)
manj=wisps.Annotator.reformat_table(wisps.datasets['manjavacas']).reset_index(drop=True)
schn=wisps.Annotator.reformat_table(wisps.datasets['schneider']).reset_index(drop=True)

In [30]:
ydwarfs=(manj[manj['spt'].apply(wisps.make_spt_number)>38].append(schn)).reset_index(drop=True)

In [31]:
spex_df['spt']=np.vstack(spex_df['spt'].values)[:,0]
manj['spt']=np.vstack(manj['spt'].values)[:,0]
schn['spt']=np.vstack(schn['spt'].values)[:,0]

In [32]:
cands_dff=cands.reset_index(drop=True)

In [33]:
import pickle
#save the random forest
output_file=wisps.OUTPUT_FILES+'/best_indices_to_use.pkl'
with open(output_file, 'wb') as file:
    pickle.dump(to_use,file)

In [34]:
#dt

In [35]:
fp={}

In [36]:
cands_dff.grism_id=cands_dff.grism_id.apply(lambda x: x.lower())

In [37]:
stars.grism_id=stars.grism_id.apply(lambda x: x.lower())

In [38]:
cx=cands_dff[cands_dff.grism_id.isin(stars.grism_id)]

In [39]:
cy=stars[stars.grism_id.isin(cx.grism_id)]

In [40]:
idx=crts['H_2O-1/J-Cont H_2O-2/H_2O-1']

In [41]:
cands_dff['spt']=np.vstack(cands_dff['spt'].values)

In [42]:
cands_indices=pd.DataFrame.from_records([x.indices for x in cands_dff.spectra])

In [43]:
for k in wisps.INDEX_NAMES:
    cands_dff[str(k)]=cands_indices[str(k)]

In [44]:
cands_dff=wisps.Annotator.reformat_table(cands_dff)

In [45]:
simulated_data=(simulated_data[simulated_data.snr1>3.]).reset_index(drop=True)

In [46]:
simulated_data['spt']=np.vstack(simulated_data.spt_new.values)[:,0]

In [47]:
def plot_index_box(index_name, box_name, ax):
    
    #get the index and the box
    idx=crts[index_name]
    bx=[x for x in idx.shapes if x.shape_name==box_name][0]
    
    xkey=idx.xkey
    ykey=idx.ykey

    to_use_df=spex_df
    if box_name.lower()=='y dwarfs':
        to_use_df=ydwarfs
    if box_name.lower()=='subdwarfs':
        to_use_df=wisps.Annotator.reformat_table(idx.subdwarfs)
        to_use_df['spt']=17

    ax.scatter(dt[xkey].apply(float).values, dt[ykey].apply(float).values, marker='o', s=10., facecolors='none', edgecolors='#AAAAAA')
    
    
    cands_slctd, cands_bools=bx._select(np.array([cands_dff[xkey].values,cands_dff[ykey].values]))
   
    trash_slctd, trsh_bools=bx._select(np.array([dt[xkey].values, dt[ykey].values]))
    
    simul_slctd, simul_bools=bx._select(np.array([simulated_data[xkey].values, simulated_data[ykey].values]))
    
    
    cands_in_that_class_bool=(cands_dff[ cands_bools]).spt.apply(lambda x: wisps.is_in_that_classification(x, box_name))
    
    spexs_slctd_in_that_class_bool= (to_use_df).spt.apply(lambda x: wisps.is_in_that_classification(x, box_name))
    
    simulated_in_that_class_bool=(simulated_data[simul_bools]).spt.apply(lambda x: wisps.is_in_that_classification(x, box_name))
    
    if box_name.lower()=='subdwarfs':
         spexs_slctd_in_that_class_bool=np.ones(len(to_use_df), dtype=bool)
    
    
    
    
    cands_in_that_class=np.array([cands_slctd[0][cands_in_that_class_bool], cands_slctd[1][cands_in_that_class_bool]])
    
    simulated_in_that_class= np.array([simul_slctd[0][simulated_in_that_class_bool], simul_slctd[1][simulated_in_that_class_bool]])
    
    spexs_slctd_in_that_class=np.array([to_use_df[xkey][spexs_slctd_in_that_class_bool], to_use_df[ykey][spexs_slctd_in_that_class_bool]])
    

    #ax.scatter( simulated_in_that_class[0],    simulated_in_that_class[1], facecolors='none', s=10, 
    #           edgecolors='#001f3f', label='simulated')
    
    ax.scatter(spexs_slctd_in_that_class[0], spexs_slctd_in_that_class[1], facecolors='#0074D9',  label='SpeX', s=10.)
    
    ax.scatter(cands_dff[xkey], cands_dff[ykey], marker='x', facecolors='#111111', s=10.)
    
    
    ax.scatter( cands_in_that_class[0],    cands_in_that_class[1], marker='x', facecolors='#FF851B', s=10, 
               edgecolors='#2ECC40', label='candidates')
    
    
    
    
    bx.color='None'
    bx.alpha=1.
    bx.linewidth=1
    bx.linestyle='-'
    bx.edgecolor='#111111'
    bx.plot(ax=ax, only_shape=True, highlight=False)
    
    
    ax.set_xlabel(r'$'+str(idx.name.split(' ')[0])+'$', fontsize=15)
    ax.set_ylabel(r'$'+str(idx.name.split(' ')[1])+'$', fontsize=15)
    
    ax.set_title(box_name, fontsize=18)
    
    xbuffer=np.nanstd(to_use_df[[xkey,ykey]])
    
    ax.set_xlim([ bx.xrange[0]-0.5*abs(np.ptp(bx.xrange)), bx.xrange[1]+0.5*abs(np.ptp(bx.xrange))])
    ax.set_ylim([ bx.yrange[0]-0.5*abs(np.ptp(bx.yrange)), bx.yrange[1]+0.5*abs(np.ptp(bx.yrange))])
    
    ax.minorticks_on()
    

    if (trash_slctd.shape[1])==0:
        fprate=0.0
    else:
        fprate=(trash_slctd.shape[1]- cands_slctd.shape[1])/trash_slctd.shape[1]
        if box_name.lower()=='subdwarfs':
                fprate=1.
    
    fp[box_name]=  fprate
    
    plt.tight_layout()
    
    return {str(box_name): bx}

In [48]:
fig, ax=plt.subplots(nrows=4, ncols=2, figsize=(12, 16))

bxs=[]
for idx, k in enumerate(to_use.keys()):
    b=plot_index_box( k, to_use[k], np.concatenate(ax)[idx])
    bxs.append(b)
plt.tight_layout()
fig.delaxes(np.concatenate(ax)[-1])
plt.savefig(wisps.OUTPUT_FIGURES+'/index_index_plots.jpeg', bbox_inches='tight')



In [49]:
to_use


Out[49]:
{'H_2O-1/J-Cont H_2O-1+CH_4/H-Cont': 'L0-L5',
 'H_2O-1+H_2O-2/J-Cont H_2O-1+H_2O-2/H-Cont': 'L5-T0',
 'H_2O-1/J-Cont H_2O-2/J-Cont': 'M7-L0',
 'H_2O-1+H_2O-2/J-Cont H_2O-2+CH_4/J-Cont': 'T0-T5',
 'H_2O-2/J-Cont H_2O-1+CH_4/H-Cont': 'T5-T9',
 'H_2O-1+CH_4/J-Cont H_2O-2+CH_4/J-Cont': 'Y dwarfs',
 'H_2O-1/J-Cont H-cont/J-Cont': 'subdwarfs'}

In [ ]:


In [ ]:


In [50]:
common=dt[dt.grism_id.isin(cands.grism_id)]

In [51]:
cands.columns


Out[51]:
Index(['grism_id', 'spectra', 'spt', 'distance', 'RA', 'DEC', 'snr1',
       'H_2O-1/J-Cont', 'H_2O-1/J-Cont_er', 'H_2O-2/H_2O-1',
       'H_2O-2/H_2O-1_er', 'H-cont/H_2O-1', 'H-cont/H_2O-1_er', 'CH_4/H_2O-1',
       'CH_4/H_2O-1_er', 'H_2O-2/J-Cont', 'H_2O-2/J-Cont_er', 'H-cont/J-Cont',
       'H-cont/J-Cont_er', 'CH_4/J-Cont', 'CH_4/J-Cont_er', 'H-cont/H_2O-2',
       'H-cont/H_2O-2_er', 'CH_4/H_2O-2', 'CH_4/H_2O-2_er', 'CH_4/H-Cont',
       'CH_4/H-Cont_er', 'H_2O-1+H_2O-2/J-Cont', 'H_2O-1+H_2O-2/J-Cont_er',
       'H_2O-1+H_2O-2/H-Cont', 'H_2O-1+H_2O-2/H-Cont_er', 'H_2O-1+CH_4/J-Cont',
       'H_2O-1+CH_4/J-Cont_er', 'H_2O-2+CH_4/J-Cont', 'H_2O-2+CH_4/J-Cont_er',
       'H_2O-1+CH_4/H-Cont', 'H_2O-1+CH_4/H-Cont_er', 'H_2O-2+CH_4/H-Cont',
       'H_2O-2+CH_4/H-Cont_er'],
      dtype='object')

In [52]:
fig, ax=plt.subplots(ncols=2, sharex=True, sharey=True, figsize=(10, 6))

#['H_2O-1+H_2O-2/J-Cont'], train_df_ucds['H_2O-1+CH_4/H-Cont'],

ax[0].scatter(simulated_data['H_2O-1+H_2O-2/J-Cont'],simulated_data['H_2O-1+CH_4/H-Cont'], 
            c=simulated_data.snr1.apply(np.log10), marker='o', alpha=0.1,label='simulated', cmap='viridis', s=1.)

ax[1].scatter(spex['H_2O-1+H_2O-2/J-Cont'],  spex['H_2O-1+CH_4/H-Cont'], label='original ', s=1.) 

ax[1].scatter(common['H_2O-1+H_2O-2/J-Cont'],  common['H_2O-1+CH_4/H-Cont'], facecolors='#FF851B',marker='o', s=20.) 
ax[1].scatter(cands['H_2O-1+H_2O-2/J-Cont'],  cands['H_2O-1+CH_4/H-Cont'], facecolors='#111111',marker='x', s=1.) 

plt.tight_layout()

ax[0].set_xlim([-0.1, 2.])
ax[0].set_ylim([-0.1, 2.5])



for a in ax:
    a.legend(fontsize=18)
    a.set_xlabel(r'$'+str('H_2O-1+H_2O-2/J-Cont'.split(' ')[0])+'$', fontsize=18)
    a.set_ylabel(r'$'+str('H_2O-1+CH_4/H-Cont'.split(' ')[0])+'$', fontsize=18)
    
    
plt.tight_layout()



In [53]:
cands.grism_id.to_csv('/users/caganze/desktop/true_brown_dwarfs.csv')

In [54]:
plt.hist(common.spt.values)
plt.hist(cands.spt.values)


Out[54]:
(array([228.,  13.,  10.,   8.,   7.,   5.,   4.,   0.,   4.,   3.]),
 array([16. , 18.2, 20.4, 22.6, 24.8, 27. , 29.2, 31.4, 33.6, 35.8, 38. ]),
 <a list of 10 Patch objects>)

In [55]:
bx_dict={}
for b in bxs:
    bx_dict.update(b)

In [56]:
#invert to use
inv_to_use = {v: k for k, v in to_use.items()}

In [57]:
inv_to_use


Out[57]:
{'L0-L5': 'H_2O-1/J-Cont H_2O-1+CH_4/H-Cont',
 'L5-T0': 'H_2O-1+H_2O-2/J-Cont H_2O-1+H_2O-2/H-Cont',
 'M7-L0': 'H_2O-1/J-Cont H_2O-2/J-Cont',
 'T0-T5': 'H_2O-1+H_2O-2/J-Cont H_2O-2+CH_4/J-Cont',
 'T5-T9': 'H_2O-2/J-Cont H_2O-1+CH_4/H-Cont',
 'Y dwarfs': 'H_2O-1+CH_4/J-Cont H_2O-2+CH_4/J-Cont',
 'subdwarfs': 'H_2O-1/J-Cont H-cont/J-Cont'}

In [58]:
dt.shape


Out[58]:
(70337, 57)

In [59]:
from tqdm import tqdm

In [60]:
ncandidates=[]
for spt_range in bx_dict.keys():
    idx_name=inv_to_use[spt_range]
    idx=crts[idx_name]
    s, bools=(bx_dict[spt_range])._select(np.array([dt[idx.xkey].values, dt[idx.ykey].values]))
    ncandidates.append(dt[bools])

In [61]:
candsss=(pd.concat(ncandidates).drop_duplicates(subset='grism_id'))

In [62]:
cands_dff.grism_id=cands_dff.grism_id.apply(lambda x: x.lower())

In [63]:
candsss.spt.plot(kind='hist')


Out[63]:
<matplotlib.axes._subplots.AxesSubplot at 0x1b210b6710>

In [64]:
len(candsss), len(candsss[candsss.grism_id.isin(cands_dff.grism_id) & (candsss.spt.apply(wisps.make_spt_number)>16)])


Out[64]:
(26175, 168)

In [65]:
candsss.grism_id.values


Out[65]:
array(['par321-00736', 'par321-10006', 'par321-00483', ...,
       'goodsn-13-g141_20032', 'goodsn-13-g141_17541',
       'goodsn-13-g141_20092'], dtype=object)

In [66]:
len(candsss[candsss.grism_id.isin(cands_dff.grism_id) & (candsss.spt.apply(wisps.make_spt_number)>=17)])


Out[66]:
168

In [67]:
candsss.to_pickle(wisps.OUTPUT_FILES+'/selected_by_indices.pkl')

In [68]:
def round_tuple(tpl, n=2):
    return round(tpl[0], n), round(tpl[1],n)

In [69]:
inv_to_use


Out[69]:
{'L0-L5': 'H_2O-1/J-Cont H_2O-1+CH_4/H-Cont',
 'L5-T0': 'H_2O-1+H_2O-2/J-Cont H_2O-1+H_2O-2/H-Cont',
 'M7-L0': 'H_2O-1/J-Cont H_2O-2/J-Cont',
 'T0-T5': 'H_2O-1+H_2O-2/J-Cont H_2O-2+CH_4/J-Cont',
 'T5-T9': 'H_2O-2/J-Cont H_2O-1+CH_4/H-Cont',
 'Y dwarfs': 'H_2O-1+CH_4/J-Cont H_2O-2+CH_4/J-Cont',
 'subdwarfs': 'H_2O-1/J-Cont H-cont/J-Cont'}

In [70]:
good_indices=[crts[x] for x in inv_to_use.values()]

In [71]:
for sindex, k in zip(good_indices, to_use.keys()):
   
    spt_range=to_use[k]
    bs=sindex.shapes
    bs=[x for x in bs if x.shape_name==spt_range]

    bx=bs[0]
    
    print (" {} & {} &  {} & {} & {} & {} & {} & {} & {}  & {} \\\ ".format(spt_range,sindex.xkey, sindex.ykey, 
                                                                           round_tuple(bx.vertices[0]), round_tuple(bx.vertices[1])
                                                                          , round_tuple(bx.vertices[2]), round_tuple(bx.vertices[3]),
                                                                          round(sindex.completeness[spt_range], 2), 
                                                                            round(sindex.contamination[spt_range], 3), 
                                                                           round(fp[spt_range], 2)))


 L0-L5 & H_2O-1/J-Cont &  H_2O-1+CH_4/H-Cont & (0.67, 2.15) & (0.93, 2.28) & (0.93, 0.53) & (0.67, 0.39) & 1.0 & 0.186  & 1.0 \\ 
 L5-T0 & H_2O-1+H_2O-2/J-Cont &  H_2O-1+H_2O-2/H-Cont & (0.75, 1.46) & (1.76, 2.0) & (1.76, 1.19) & (0.75, 0.65) & 0.97 & 0.101  & 1.0 \\ 
 M7-L0 & H_2O-1/J-Cont &  H_2O-2/J-Cont & (0.83, 0.72) & (1.07, 0.85) & (1.07, 0.54) & (0.83, 0.4) & 0.97 & 0.079  & 0.97 \\ 
 T0-T5 & H_2O-1+H_2O-2/J-Cont &  H_2O-2+CH_4/J-Cont & (0.26, 1.66) & (1.01, 2.76) & (1.01, 1.32) & (0.26, 0.22) & 0.98 & 0.01  & 0.98 \\ 
 T5-T9 & H_2O-2/J-Cont &  H_2O-1+CH_4/H-Cont & (-0.01, 0.2) & (0.07, 0.37) & (0.07, 0.12) & (-0.01, -0.05) & 0.98 & 0.001  & 0.97 \\ 
 Y dwarfs & H_2O-1+CH_4/J-Cont &  H_2O-2+CH_4/J-Cont & (-0.02, 0.25) & (0.31, 0.73) & (0.31, 0.19) & (-0.02, -0.29) & 0.91 & 0.002  & 0.98 \\ 
 subdwarfs & H_2O-1/J-Cont &  H-cont/J-Cont & (0.83, 0.71) & (1.14, 0.74) & (1.14, 0.51) & (0.83, 0.48) & 0.94 & 0.123  & 1.0 \\ 

In [72]:
wisps.get_multiple_sources


Out[72]:
<function wisps.data_analysis.photometry.get_multiple_sources(filenames, **kwargs)>

In [99]:
s=splat.getSpectrum(lucky=True)[0]


Retrieving 1 lucky file


In [100]:
fig, ax=plt.subplots(figsize=(10, 6))
s.normalize()
s.addNoise(10.)
s.toInstrument('SPEX-PRISM')
plt.step(s.wave, s.flux, label='spex', c='k')
plt.xlim([1.1, 1.7])

s.toInstrument('WFC3-G141')
plt.step(s.wave.value, s.flux.value+0.1, label='WCF3')

s.reset()
plt.legend(fontsize=18)


Out[100]:
<matplotlib.legend.Legend at 0x1b418ec150>

In [ ]: