In [108]:
%load_ext autoreload
%autoreload 2
%matplotlib inline
import numpy as np
import acqstattools as ast
import matplotlib.pyplot as plt
import astropy as ap
from datetime import datetime as dt
from astropy.time import Time
import warnings
warnings.filterwarnings('ignore')
In [109]:
# Loading the estimates from the trained model
acqstats = 'acqstats_bp6_draws_trained_2011.5_to_2014.5.pkl'
betas = ast.loadacqstats(acqstats, description=True)
#Loading historical star data from file
acq_data = np.load('data/acq_table.npy')
#Adding fields required for analysis
acq_data = ast.add_column(acq_data, 'tstart_jyear' , np.zeros(len(acq_data)))
acq_data = ast.add_column(acq_data, 'tstart_quarter' , np.zeros(len(acq_data)))
acq_data = ast.add_column(acq_data, 'mag_floor' , np.zeros(len(acq_data)))
acq_data = ast.add_column(acq_data, 'year' , np.zeros(len(acq_data)))
acq_data = ast.add_column(acq_data, 'failure' , np.zeros(len(acq_data)))
acq_data['tstart_jyear'] = Time(acq_data['tstart'], format='cxcsec').jyear
acq_data['year'] = np.floor(acq_data.tstart_jyear)
acq_data['mag_floor'] = np.floor(acq_data['mag'])
acq_data['failure'][acq_data['obc_id']=="NOID"] = 1.
#Removing Bad Stars
bad_obsids = [
# Venus
2411,2414,6395,7306,7307,7308,7309,7311,7312,7313,7314,7315,7317,7318,7406,583,
7310,9741,9742,9743,9744,9745,9746,9747,9749,9752,9753,9748,7316,15292,16499,
16500,16501,16503,16504,16505,16506,16502,
# multi-obi catalogs
943,897,60879,2042,60881,800,1900,2365,906,2010,3182,2547,380,3057,2077,60880,
2783,1578,1561,4175,3764
]
for badid in bad_obsids:
acq_data = acq_data[acq_data['obsid']!=badid]
In [110]:
# Creating star catalogs from historical information.
range13_14 = ast.subset_range_tstart_jyear(acq_data, 2011.0, 2014.0)
uniqueobsids = np.unique(range13_14['obsid'])
obs2013_2014 = {}
for i in uniqueobsids:
# Script only handles star catalogs with 8 stars presently.
if len(range13_14[range13_14['obsid']==int(i)]) != 8:
print "Skipped {}, Only {} Observations".format(i, len(range13_14[range13_14['obsid']==int(i)]))
else:
obs_dict = {}
for obs in range13_14[range13_14['obsid']==int(i)]:
obs_dict.update({obs['agasc_id']:{'mag':obs['mag'],'warm_pix':obs['warm_pix'],
'failure':obs['failure']}})
obs2013_2014.update({i:obs_dict})
In [111]:
#Binning the historical data
zeros = []
ones = []
twos = []
threes = []
fours = []
for obs in obs2013_2014:
failcount = 0
for agasc in obs2013_2014[obs]:
failcount += obs2013_2014[obs][agasc]['failure']
if failcount > 4:
print failcount
else:
acqevent = ast.acqPredictCatalog(obs2013_2014[obs], betas, summary=False)
if failcount == 0:
zeros.append(acqevent)
if failcount == 1:
ones.append(acqevent)
if failcount == 2:
twos.append(acqevent)
if failcount == 3:
threes.append(acqevent)
if failcount == 4:
fours.append(acqevent)
In [112]:
nbins = np.arange(0,8.1,0.1)
F = plt.figure()
plt.subplot(5,1,1)
p1 = plt.hist([acqevent.expectedfailures for acqevent in zeros],
bins=nbins, color='r', alpha=0.5, label="Zero Failed")
lgd = plt.legend()
plt.xlim((0,4))
plt.xlabel('Predicted Expected Number of Failures')
plt.subplot(5,1,2)
p1 = plt.hist([acqevent.expectedfailures for acqevent in ones],
bins=nbins, color='b', alpha=0.5, label="One Failed")
lgd = plt.legend()
plt.xlim((0,4))
plt.xlabel('Predicted Expected Number of Failures')
plt.subplot(5,1,3)
p1 = plt.hist([acqevent.expectedfailures for acqevent in twos],
bins=nbins, color='g', alpha=0.5, label="Two Failed")
lgd = plt.legend()
plt.xlim((0,4))
plt.xlabel('Predicted Expected Number of Failures')
plt.subplot(5,1,4)
p1 = plt.hist([acqevent.expectedfailures for acqevent in threes],
bins=nbins, color='purple', alpha=0.5, label="Three Failed")
lgd = plt.legend()
plt.xlim((0,4))
plt.xlabel('Predicted Expected Number of Failures')
plt.subplot(5,1,5)
p1 = plt.hist([acqevent.expectedfailures for acqevent in fours],
bins=nbins, color='orange', alpha=0.5, label="Four Failed")
lgd = plt.legend()
plt.xlim((0,4))
plt.xlabel('Predicted Expected Number of Failures')
F.set_size_inches(15,8)
plt.show()
In [113]:
nbins = np.arange(0,8.1,0.1)
F = plt.figure()
plt.subplot(5,1,1)
p1 = plt.hist([acqevent.stddev for acqevent in zeros],
bins=nbins, color='r', alpha=0.5, label="Zero Failed")
lgd = plt.legend()
plt.xlim((0,2))
plt.xlabel('Predicted Expected Number of Failures')
plt.subplot(5,1,2)
p1 = plt.hist([acqevent.stddev for acqevent in ones],
bins=nbins, color='b', alpha=0.5, label="One Failed")
lgd = plt.legend()
plt.xlim((0,2))
plt.xlabel('Predicted Expected Number of Failures')
plt.subplot(5,1,3)
p1 = plt.hist([acqevent.stddev for acqevent in twos],
bins=nbins, color='g', alpha=0.5, label="Two Failed")
lgd = plt.legend()
plt.xlim((0,2))
plt.xlabel('Predicted Expected Number of Failures')
plt.subplot(5,1,4)
p1 = plt.hist([acqevent.stddev for acqevent in threes],
bins=nbins, color='purple', alpha=0.5, label="Three Failed")
lgd = plt.legend()
plt.xlim((0,2))
plt.xlabel('Predicted Standard Deviation of Failures')
plt.subplot(5,1,5)
p1 = plt.hist([acqevent.stddev for acqevent in fours],
bins=nbins, color='orange', alpha=0.5, label="Four Failed")
lgd = plt.legend()
plt.xlim((0,2))
plt.xlabel('Predicted Standard Deviation of Failures')
F.set_size_inches(10,6)
plt.show()
In [114]:
nbins = np.arange(0,8.1,0.1)
F = plt.figure()
plt.subplot(5,1,1)
p1 = plt.hist([acqevent.expectedfailures + acqevent.stddev for acqevent in zeros],
bins=nbins, color='r', alpha=0.5, label="Zero Failed")
lgd = plt.legend()
plt.xlim((0,4))
plt.xlabel('Predicted Expected Number of Failures + One Standard Deviation')
plt.subplot(5,1,2)
p1 = plt.hist([acqevent.expectedfailures + acqevent.stddev for acqevent in ones],
bins=nbins, color='b', alpha=0.5, label="One Failed")
lgd = plt.legend()
plt.xlim((0,4))
plt.xlabel('Predicted Expected Number of Failures + One Standard Deviation')
plt.subplot(5,1,3)
p1 = plt.hist([acqevent.expectedfailures + acqevent.stddev for acqevent in twos],
bins=nbins, color='g', alpha=0.5, label="Two Failed")
lgd = plt.legend()
plt.xlim((0,4))
plt.xlabel('Predicted Expected Number of Failures + One Standard Deviation')
plt.subplot(5,1,4)
p1 = plt.hist([acqevent.expectedfailures + acqevent.stddev for acqevent in threes],
bins=nbins, color='purple', alpha=0.5, label="Three Failed")
lgd = plt.legend()
plt.xlim((0,4))
plt.xlabel('Predicted Expected Number of Failures + One Standard Deviation')
plt.subplot(5,1,5)
p1 = plt.hist([acqevent.expectedfailures + acqevent.stddev for acqevent in fours],
bins=nbins, color='orange', alpha=0.5, label="Four Failed")
lgd = plt.legend()
plt.xlim((0,4))
plt.xlabel('Predicted Expected Number of Failures + One Standard Deviation')
F.set_size_inches(10,6)
plt.show()
In [115]:
zero_expfailed, binedges = np.histogram([acqevent.expectedfailures for acqevent in zeros], bins=np.arange(0,4.1,0.1))
ones_expfailed, binedges = np.histogram([acqevent.expectedfailures for acqevent in ones], bins=np.arange(0,4.1,0.1))
twos_expfailed, binedges = np.histogram([acqevent.expectedfailures for acqevent in twos], bins=np.arange(0,4.1,0.1))
threes_expfailed, binedges = np.histogram([acqevent.expectedfailures for acqevent in threes], bins=np.arange(0,4.1,0.1))
fours_expfailed, binedges = np.histogram([acqevent.expectedfailures for acqevent in fours], bins=np.arange(0,4.1,0.1))
totalcounts = zero_expfailed + ones_expfailed + twos_expfailed + threes_expfailed + fours_expfailed
zero_percent = zero_expfailed / totalcounts.astype(float)
one_percent = ones_expfailed / totalcounts.astype(float)
two_percent = twos_expfailed / totalcounts.astype(float)
three_percent = threes_expfailed / totalcounts.astype(float)
four_percent = fours_expfailed / totalcounts.astype(float)
from matplotlib import gridspec
F = plt.figure(figsize=(12, 6))
gs = gridspec.GridSpec(2, 1, height_ratios=[3, 2])
ax0 = plt.subplot(gs[0])
ax0.bar(binedges[:-1],zero_percent, width=0.1, alpha=0.5, color='b')
ax0.bar(binedges[:-1],one_percent, width=0.1, alpha=0.5, color='r', bottom=zero_percent)
ax0.bar(binedges[:-1],two_percent, width=0.1, alpha=0.5, color='g', bottom=zero_percent + one_percent)
ax0.bar(binedges[:-1],three_percent, width=0.1, alpha=0.5, color='purple', bottom=zero_percent + one_percent+ two_percent)
ax0.bar(binedges[:-1],four_percent, width=0.1, alpha=0.5, color='orange', bottom= zero_percent + one_percent+ two_percent+three_percent)
plt.xlabel('Predicted Expected Number of Failures')
plt.ylabel('Proportion Failed')
plt.xlim(0,3.5)
plt.legend(('Zero Failed', 'One Failed', 'Two Failed',
'Three Failed', 'Four Failed'))
plt.title('Assessing Model Performance with Historical Data - Expected Number of Failures')
ax1 = plt.subplot(gs[1])
ax1.plot(binedges[1:]-0.05, totalcounts)
ax1.set_yscale('log')
plt.xlim((0,3.5))
plt.ylabel('Log Total Observed Counts')
plt.xlabel('Predicted Expected Number of Failures')
plt.show()
In [116]:
zero_expfailed, binedges = np.histogram([acqevent.expectedfailures + acqevent.stddev for acqevent in zeros], bins=np.arange(0,4.1,0.1))
ones_expfailed, binedges = np.histogram([acqevent.expectedfailures + acqevent.stddev for acqevent in ones], bins=np.arange(0,4.1,0.1))
twos_expfailed, binedges = np.histogram([acqevent.expectedfailures + acqevent.stddev for acqevent in twos], bins=np.arange(0,4.1,0.1))
threes_expfailed, binedges = np.histogram([acqevent.expectedfailures + acqevent.stddev for acqevent in threes], bins=np.arange(0,4.1,0.1))
fours_expfailed, binedges = np.histogram([acqevent.expectedfailures + acqevent.stddev for acqevent in fours], bins=np.arange(0,4.1,0.1))
totalcounts = zero_expfailed + ones_expfailed + twos_expfailed + threes_expfailed + fours_expfailed
zero_percent = zero_expfailed / totalcounts.astype(float)
one_percent = ones_expfailed / totalcounts.astype(float)
two_percent = twos_expfailed / totalcounts.astype(float)
three_percent = threes_expfailed / totalcounts.astype(float)
four_percent = fours_expfailed / totalcounts.astype(float)
from matplotlib import gridspec
F = plt.figure(figsize=(12, 6))
gs = gridspec.GridSpec(2, 1, height_ratios=[3, 2])
ax0 = plt.subplot(gs[0])
ax0.bar(binedges[:-1],zero_percent, width=0.1, alpha=0.5, color='b')
ax0.bar(binedges[:-1],one_percent, width=0.1, alpha=0.5, color='r', bottom=zero_percent)
ax0.bar(binedges[:-1],two_percent, width=0.1, alpha=0.5, color='g', bottom=zero_percent + one_percent)
ax0.bar(binedges[:-1],three_percent, width=0.1, alpha=0.5, color='purple', bottom=zero_percent + one_percent+ two_percent)
ax0.bar(binedges[:-1],four_percent, width=0.1, alpha=0.5, color='orange', bottom= zero_percent + one_percent+ two_percent+three_percent)
plt.xlabel('Predicted Expected Number of Failures + One Standard Deviation')
plt.ylabel('Proportion Failed')
plt.xlim(0,4.5)
plt.legend(('Zero Failed', 'One Failed', 'Two Failed',
'Three Failed', 'Four Failed'))
plt.title('Assessing Model Performance with Historical Data - Expected Number of Failures + One Standard Deviation ')
ax1 = plt.subplot(gs[1])
ax1.plot(binedges[1:]-0.05, totalcounts)
ax1.set_yscale('log')
plt.xlim((0,4.5))
plt.ylabel('Log Total Observed Counts')
plt.xlabel('Predicted Expected Number of Failures + One Standard Deviation')
plt.show()
In [117]:
starcat = {'43648032':{'mag':10.3237543106, 'warm_pix':0.157814292736},
'43649080':{'mag':6.99063110352, 'warm_pix':0.157814292736},
'43650048':{'mag':10.0143651962, 'warm_pix':0.157814292736},
'43650856':{'mag':9.42395305634, 'warm_pix':0.157814292736},
'43652384':{'mag':10.1900663376, 'warm_pix':0.157814292736},
'43656176':{'mag':8.27156066895, 'warm_pix':0.157814292736},
'43656696':{'mag':6.34437942505, 'warm_pix':0.157814292736},
'43656184':{'mag':10.3632602692, 'warm_pix':0.157814292736}}
test = ast.acqPredictCatalog(starcat, betas)
test.starpredictions[0].summary()
In [117]:
In [118]:
# ObsID
starcatGood = {"525609504":{'mag':9.241, 'warm_pix':0.0803082109881},
"525735456":{'mag':7.206, 'warm_pix':0.0803082109881},
"525736400":{'mag':9.146, 'warm_pix':0.0803082109881},
"525732232":{'mag':9.117, 'warm_pix':0.0803082109881},
"525732528":{'mag':9.318, 'warm_pix':0.0803082109881},
"525731944":{'mag':9.478, 'warm_pix':0.0803082109881},
"525734296":{'mag':9.484, 'warm_pix':0.0803082109881},
"525739240":{'mag':9.558, 'warm_pix':0.0803082109881}}
test = ast.acqPredictCatalog(starcatGood, betas)
In [119]:
starcat = {'813832200':{'mag':9.61997318268, 'warm_pix':0.146424291198, 'obc_id':"ID"},
'813833224':{'mag':9.67224121094, 'warm_pix':0.146424291198, 'obc_id':"ID"},
'813836688':{'mag':8.68381690979, 'warm_pix':0.146424291198, 'obc_id':"ID"},
'813836992':{'mag':10.4075469971, 'warm_pix':0.146424291198, 'obc_id':"NOID"},
'813957656':{'mag':9.94153213501, 'warm_pix':0.146424291198, 'obc_id':"NOID"},
'813834008':{'mag':10.3945388794, 'warm_pix':0.146424291198, 'obc_id':"ID"},
'813837240':{'mag':10.4958143234, 'warm_pix':0.146424291198, 'obc_id':"NOID"},
'813960160':{'mag':10.7895717621, 'warm_pix':0.146424291198, 'obc_id':"ID"}}
test = ast.acqPredictCatalog(starcat, betas)
# for star in starcat:
# ast.acqPredict1(starcat[star]['mag'], starcat[star]['warm_pix'], betas, agasc=int(star)).summary()
In [120]:
starcat = {
'257557024':{'mag':8.13523864746, 'warm_pix':0.133249026193, 'obc_id':'ID'},
'257557392':{'mag':10.4494104385, 'warm_pix':0.133249026193, 'obc_id':'ID'},
'257558960':{'mag':8.00120258331, 'warm_pix':0.133249026193, 'obc_id':'ID'},
'257560064':{'mag':8.50957202911, 'warm_pix':0.133249026193, 'obc_id':'ID'},
'257562816':{'mag':9.9642906189, 'warm_pix':0.133249026193, 'obc_id':'NOID'},
'257563488':{'mag':10.1029891968, 'warm_pix':0.133249026193, 'obc_id':'ID'},
'257563520':{'mag':9.97522926331, 'warm_pix':0.133249026193, 'obc_id':'ID'},
'257565080':{'mag':8.83695888519, 'warm_pix':0.133249026193, 'obc_id':'ID'}}
test = ast.acqPredictCatalog(starcat, betas)
In [121]:
starcat = {
'1197749776':{'mag':9.82180118561, 'warm_pix':0.146037858242, 'obc_id':'ID'},
'1198191400':{'mag':8.35087585449, 'warm_pix':0.146037858242, 'obc_id':'ID'},
'1198192176':{'mag':9.49861240387, 'warm_pix':0.146037858242, 'obc_id':'ID'},
'1198192408':{'mag':9.44103050232, 'warm_pix':0.146037858242, 'obc_id':'ID'},
'1197740448':{'mag':10.0938825607, 'warm_pix':0.146037858242, 'obc_id':'NOID'},
'1197750848':{'mag':10.1972503662, 'warm_pix':0.146037858242, 'obc_id':'ID'},
'1198192088':{'mag':9.18758773804, 'warm_pix':0.146037858242, 'obc_id':'ID'}
}
test = ast.acqPredictCatalog(starcat, betas)
In [121]:
In [121]:
In [121]:
In [121]: