Calculate the time gap between three consecutive visits in each filter, and generate a set of combined histograms with all filters. Derived from sims_maf_contrib/tutorials/Plotting Examples.ipynb
In [1]:
# Import modules.
import matplotlib.pyplot as plt
%matplotlib inline
import lsst.sims.maf.db as db
import lsst.sims.maf.metrics as metrics
import lsst.sims.maf.slicers as slicers
import lsst.sims.maf.plots as plots
from lsst.sims.maf.metricBundles import MetricBundle, MetricBundleGroup, makeBundlesDictFromList
from mafContrib import kConsecutiveGapMetric
In [2]:
# Connect to databases.
runName = 'minion_1016'
opsdb = db.OpsimDatabase(runName + '_sqlite.db')
outDir = 'allfilters_test'
resultsDb = db.ResultsDb(outDir=outDir)
Set up and run non-dithered metric bundles. Use a lower value of nside to make the notebook run faster, although at lower spatial resolution.
In [3]:
nside = 16
# Set up metrics, slicer and summaryMetrics.
m1 = kConsecutiveGapMetric(k=2)
m2 = metrics.AveGapMetric()
slicer = slicers.HealpixSlicer(nside=nside)
summaryMetrics = [metrics.MinMetric(), metrics.MeanMetric(), metrics.MaxMetric(),
metrics.MedianMetric(), metrics.RmsMetric(),
metrics.PercentileMetric(percentile=25), metrics.PercentileMetric(percentile=75)]
# And I'll set a plotDict for the nvisits and coadded depth, because otherwise the DD fields throw the
# scale in the plots into too wide a range.
# (we could also generate plots, see this effect, then set the dict and regenerate the plots)
#nvisitsPlotRanges = {'xMin':0, 'xMax':300, 'colorMin':0, 'colorMax':300, 'binsize':5}
#coaddPlotRanges = {'xMin':24, 'xMax':28, 'colorMin':24, 'colorMax':28, 'binsize':0.02}
In [4]:
filterlist = ['u', 'g', 'r', 'i', 'z', 'y']
filterorder = {'u':0, 'g':1, 'r':2, 'i':3, 'z':4, 'y':5}
In [5]:
# Create metricBundles for each filter.
# For ease of access later, I want to make a dictionary with 'kgap[filter]' first.
kgap = {}
avegap = {}
for f in filterlist:
sqlconstraint = 'filter = "%s"' %(f)
# Add displayDict stuff that's useful for showMaf to put things in "nice" order.
displayDict = {'subgroup':'Undithered', 'order':filterorder[f], 'group':'kgap'}
kgap[f] = MetricBundle(m1, slicer, sqlconstraint=sqlconstraint, runName=runName,
summaryMetrics=summaryMetrics, #plotDict=nvisitsPlotRanges,
displayDict=displayDict)
displayDict['group'] = 'AveGap'
avegap[f] = MetricBundle(m2, slicer, sqlconstraint=sqlconstraint, runName=runName,
summaryMetrics=summaryMetrics, #plotDict=nvisitsPlotRanges,
displayDict=displayDict)
blistAll = []
for f in filterlist:
blistAll.append(kgap[f])
blistAll.append(avegap[f])
bdict = makeBundlesDictFromList(blistAll)
# Set the metricBundleGroup up with all metricBundles, in all filters.
bgroup = MetricBundleGroup(bdict, opsdb, outDir=outDir, resultsDb=resultsDb)
bgroup.runAll()
bgroup.writeAll()
bgroup.plotAll()
In [6]:
print 'Kgap --'
for f in filterlist:
print kgap[f].summaryValues
print 'Avegap --'
for f in filterlist:
print avegap[f].summaryValues
Now let's try to combine the histograms.
In [7]:
# Set more complicated plot labels directly in the bundles.
for f in filterlist:
kgap[f].setPlotDict({'label':'%s %1.f/%.1f/%1.f' %(f, kgap[f].summaryValues['25th%ile'],
kgap[f].summaryValues['Median'],
kgap[f].summaryValues['75th%ile'])})
In [8]:
# 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()
ph.setMetricBundles(kgap)
# Add min/max values to the plots, which will be used for the combo histogram for nvisits.
#ph.setPlotDicts(nvisitsPlotRanges)
ph.plot(plotFunc=healpixhist)
Out[8]:
In [ ]: