Calculating intra- and inter-night gaps for several cadences
In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import healpy as hp
import lsst.sims.maf.slicers as slicers
import lsst.sims.maf.metrics as metrics
import lsst.sims.maf.metricBundles as metricBundles
import lsst.sims.maf.db as db
import lsst.sims.maf.plots as plots
import shutil
In [2]:
import matplotlib as mpl
mpl.rcParams['figure.autolayout'] = False
In [3]:
surveyDuration=10
if surveyDuration < 10:
year = 3
sqlconstraint = 'night between %f and %f' % ((365.25*year,365.25*(year+1)))
else:
sqlconstraint = ''
if False:
if len(sqlconstraint):
sqlconstraint += ' and filter = "r"'
else:
sqlconstraint = 'filter = "r"'
In [4]:
# Pick a slicer
slicer = slicers.HealpixSlicer(nside=16)
# Configure some metrics
metricList = []
# Intra-night gaps
m1 = metrics.IntraNightGapsMetric()
metricList.append(m1)
# Inter-night gaps
m2 = metrics.InterNightGapsMetric()
metricList.append(m2)
# Gap histogram
m3 = metrics.AveGapMetric()
metricList.append(m3)
# summaryMetrics
summaryMetrics = [metrics.MinMetric(), metrics.MeanMetric(), metrics.MaxMetric(),
metrics.MedianMetric(), metrics.RmsMetric(),
metrics.PercentileMetric(percentile=25), metrics.PercentileMetric(percentile=75)]
# suppress angular power spectrum plots
plotFuncs = [plots.HealpixSkyMap(), plots.HealpixHistogram()]
In [5]:
# Choose the opsim runs and do them all!
runs = ['minion_1016', # baseline cadence
'enigma_1282', # NEO with triples
'kraken_1043', # no visit pairs
'minion_1020'] # PS-like
outDir = 'gaps'
# resultsdb will contain multiple runs with various values if we change sqlconstraints, etc. Instead, delete so
# we have a clean directory
shutil.rmtree('./'+outDir+'/')
bDictDict = {}
for runName in runs:
print runName
opsdb = db.OpsimDatabase(runName + '_sqlite.db')
resultsDb = db.ResultsDb(outDir=outDir)
# build the bundleDict
bDict={}
for i,metric in enumerate(metricList):
bDict[i] = metricBundles.MetricBundle(metric, slicer, sqlconstraint,
runName=runName, summaryMetrics=summaryMetrics, plotFuncs=plotFuncs)
bgroup = metricBundles.MetricBundleGroup(bDict, opsdb, outDir=outDir, resultsDb=resultsDb)
bgroup.runAll()
bgroup.plotAll(closefigs=False)
bDictDict[runName] = bDict
In [34]:
metric_names = []
for runName in runs:
print runName
bDict = bDictDict[runName]
for key in sorted(bDict):
bDict[key].computeSummaryStats(resultsDb=resultsDb)
print bDict[key].metric.name, bDict[key].summaryValues
metric_names.append(bDict[key].metric.name)
print
metric_names=set(metric_names)
In [35]:
metric_names
Out[35]:
In [29]:
# these take awhile to run, so save them!
#import pickle
#output = open(outDir+'/bDictDict.pkl','wb')
#pickle.dump(bDictDict,output)
#output.close()
In [38]:
colors = {'minion_1016':'black', # baseline cadence
'enigma_1282':'blue', # NEO with triples
'kraken_1043':'green', # no visit pairs
'minion_1020':'red'} # PS-like
In [39]:
def plot_metric_byruns(metricName, bDictDict, cumulative=False):
# loop over runs to make a new bundle list with only the right metrics
new_bundle = []
for runName in runs:
bDict = bDictDict[runName]
for key in sorted(bDict):
bDict[key].computeSummaryStats(resultsDb=resultsDb)
if bDict[key].metric.name == metricName:
new_bundle.append(bDict[key])
# Set up the plotHandler.
ph = plots.PlotHandler(outDir=outDir, resultsDb=resultsDb)
# Instantiate the healpix histogram plotter, since we'll use it a lot.
healpixhist = plots.HealpixHistogram()
for bundle in new_bundle:
bundle.setPlotDict({'cumulative': cumulative,'color':colors[bundle.runName]})
ph.setMetricBundles(new_bundle)
# Add min/max values to the plots, which will be used for the combo histogram for nvisits.
#ph.setPlotDicts(nvisitsPlotRanges)
ph.plot(plotFunc=healpixhist)
fname = metric
plt.savefig(outDir+'/'+ "".join(metricName.split())+'.pdf')
In [40]:
for metricName in metric_names:
plot_metric_byruns(metricName,bDictDict)
In [11]:
rundb = db.ResultsDb(database=outDir+'/resultsDb_sqlite.db')
In [12]:
import pandas as pd
In [13]:
simDataName = [m[0] for m in rundb.session.query(db.MetricRow.simDataName).all()]
info = rundb.getMetricDisplayInfo()
dfi = pd.DataFrame(info)
dfi['simDataName'] = pd.Series(simDataName,index=dfi.index)
dfi.head()
Out[13]:
In [14]:
stats = rundb.getSummaryStats()
dfs = pd.DataFrame(stats)
dfs.head()
Out[14]:
In [15]:
df = pd.merge(dfs[['metricId','summaryName','summaryValue']],dfi,on='metricId')
df.head()
Out[15]:
In [16]:
print set(df['simDataName'])
print set(df['sqlConstraint'])
print set(df['metricName'])
In [17]:
for simName in set(df['simDataName']):
print simName
w = (df['simDataName'] == simName) & (df['sqlConstraint'] == 'night between 1095.750000 and 1461.000000 and filter = "r"') & (df['summaryName'] == 'Median')
print df[w][['metricName','summaryValue']]
In [ ]: