def setupMetrics(colmap, wholesurvey=False): metricList = [] captionList = [] # Number of filter changes (per slice - either whole survey or X nights) if wholesurvey: metricList.append(metrics.NChangesMetric(col=colmap['filter'], orderBy=colmap['mjd'], metricName='Total Filter Changes')) else: metricList.append(metrics.NChangesMetric(col=colmap['filter'], orderBy=colmap['mjd'], metricName='Filter Changes')) captionList.append('Total filter changes ') # Minimum time between filter changes metricList.append(metrics.MinTimeBetweenStatesMetric(changeCol=colmap['filter'], timeCol=colmap['mjd'])) captionList.append('Minimum time between filter changes ') # Number of filter changes faster than 10 minutes metricList.append(metrics.NStateChangesFasterThanMetric(changeCol=colmap['filter'], timeCol=colmap['mjd'], cutoff=10)) captionList.append('Number of filter changes faster than 10 minutes ') # Number of filter changes faster than 20 minutes metricList.append(metrics.NStateChangesFasterThanMetric(changeCol=colmap['filter'], timeCol=colmap['mjd'], cutoff=20)) captionList.append('Number of filter changes faster than 20 minutes ') # Maximum number of filter changes faster than 10 minutes within slice metricList.append(metrics.MaxStateChangesWithinMetric(changeCol=colmap['filter'], timeCol=colmap['mjd'], timespan=10)) captionList.append('Max number of filter changes within a window of 10 minutes ') # Maximum number of filter changes faster than 20 minutes within slice metricList.append(metrics.MaxStateChangesWithinMetric(changeCol=colmap['filter'], timeCol=colmap['mjd'], timespan=20)) captionList.append('Max number of filter changes within a window of 20 minutes ') return metricList, captionList
def testNChangesMetric(self): """ Test the NChanges metric. """ filters = np.array(['u', 'u', 'g', 'g', 'r']) visitTimes = np.arange(0, filters.size, 1) data = np.core.records.fromarrays( [visitTimes, filters], names=['observationStartMJD', 'filter']) metric = metrics.NChangesMetric() result = metric.run(data) self.assertEqual(result, 2) filters = np.array(['u', 'g', 'u', 'g', 'r']) data = np.core.records.fromarrays( [visitTimes, filters], names=['observationStartMJD', 'filter']) metric = metrics.NChangesMetric() result = metric.run(data) self.assertEqual(result, 4)
def makeBundleList(dbFile, night=1, nside=64, latCol='ditheredDec', lonCol='ditheredRA'): """ Make a bundleList of things to run """ # Construct sql queries for each filter and all filters filters = ['u', 'g', 'r', 'i', 'z', 'y'] sqls = ['night=%i and filter="%s"' % (night, f) for f in filters] sqls.append('night=%i' % night) bundleList = [] plotFuncs_lam = [plots.LambertSkyMap()] reg_slicer = slicers.HealpixSlicer(nside=nside, lonCol=lonCol, latCol=latCol, latLonDeg=False) altaz_slicer = slicers.HealpixSlicer(nside=nside, latCol='altitude', latLonDeg=False, lonCol='azimuth', useCache=False) unislicer = slicers.UniSlicer() for sql in sqls: # Number of exposures metric = metrics.CountMetric('expMJD', metricName='N visits') bundle = metricBundles.MetricBundle(metric, reg_slicer, sql) bundleList.append(bundle) metric = metrics.CountMetric('expMJD', metricName='N visits alt az') bundle = metricBundles.MetricBundle(metric, altaz_slicer, sql, plotFuncs=plotFuncs_lam) bundleList.append(bundle) metric = metrics.MeanMetric('expMJD', metricName='Mean Visit Time') bundle = metricBundles.MetricBundle(metric, reg_slicer, sql) bundleList.append(bundle) metric = metrics.MeanMetric('expMJD', metricName='Mean Visit Time alt az') bundle = metricBundles.MetricBundle(metric, altaz_slicer, sql, plotFuncs=plotFuncs_lam) bundleList.append(bundle) metric = metrics.CountMetric('expMJD', metricName='N_visits') bundle = metricBundles.MetricBundle(metric, unislicer, sql) bundleList.append(bundle) # Need pairs in window to get a map of how well it gathered SS pairs. # Moon phase. metric = metrics.NChangesMetric(col='filter', metricName='Filter Changes') bundle = metricBundles.MetricBundle(metric, unislicer, 'night=%i' % night) bundleList.append(bundle) metric = metrics.OpenShutterFractionMetric() bundle = metricBundles.MetricBundle(metric, unislicer, 'night=%i' % night) bundleList.append(bundle) metric = metrics.MeanMetric('slewTime') bundle = metricBundles.MetricBundle(metric, unislicer, 'night=%i' % night) bundleList.append(bundle) metric = metrics.MinMetric('slewTime') bundle = metricBundles.MetricBundle(metric, unislicer, 'night=%i' % night) bundleList.append(bundle) metric = metrics.MaxMetric('slewTime') bundle = metricBundles.MetricBundle(metric, unislicer, 'night=%i' % night) bundleList.append(bundle) # Make plots of the solar system pairs that were taken in the night metric = metrics.PairMetric() sql = 'night=%i and (filter ="r" or filter="g" or filter="i")' % night bundle = metricBundles.MetricBundle(metric, reg_slicer, sql) bundleList.append(bundle) metric = metrics.PairMetric(metricName='z Pairs') sql = 'night=%i and filter="z"' % night bundle = metricBundles.MetricBundle(metric, reg_slicer, sql) bundleList.append(bundle) # Plot up each visit metric = metrics.NightPointingMetric() slicer = slicers.UniSlicer() sql = sql = 'night=%i' % night plotFuncs = [plots.NightPointingPlotter()] bundle = metricBundles.MetricBundle(metric, slicer, sql, plotFuncs=plotFuncs) bundleList.append(bundle) return metricBundles.makeBundlesDictFromList(bundleList)
def makeBundleList(dbFile, night=1, nside=64, latCol='fieldDec', lonCol='fieldRA', notes=True, colmap=None): """ Make a bundleList of things to run """ if colmap is None: colmap = ColMapDict('opsimV4') mjdCol = 'observationStartMJD' altCol = 'altitude' azCol = 'azimuth' # Construct sql queries for each filter and all filters filters = ['u', 'g', 'r', 'i', 'z', 'y'] sqls = ['night=%i and filter="%s"' % (night, f) for f in filters] sqls.append('night=%i' % night) bundleList = [] plotFuncs_lam = [plots.LambertSkyMap()] # Hourglass hourslicer = slicers.HourglassSlicer() displayDict = {'group': 'Hourglass'} md = '' sql = 'night=%i' % night metric = metrics.HourglassMetric(nightCol=colmap['night'], mjdCol=colmap['mjd'], metricName='Hourglass') bundle = metricBundles.MetricBundle(metric, hourslicer, constraint=sql, metadata=md, displayDict=displayDict) bundleList.append(bundle) reg_slicer = slicers.HealpixSlicer(nside=nside, lonCol=lonCol, latCol=latCol, latLonDeg=True) altaz_slicer = slicers.HealpixSlicer(nside=nside, latCol=altCol, latLonDeg=True, lonCol=azCol, useCache=False) unislicer = slicers.UniSlicer() for sql in sqls: # Number of exposures metric = metrics.CountMetric(mjdCol, metricName='N visits') bundle = metricBundles.MetricBundle(metric, reg_slicer, sql) bundleList.append(bundle) metric = metrics.CountMetric(mjdCol, metricName='N visits alt az') bundle = metricBundles.MetricBundle(metric, altaz_slicer, sql, plotFuncs=plotFuncs_lam) bundleList.append(bundle) metric = metrics.MeanMetric(mjdCol, metricName='Mean Visit Time') bundle = metricBundles.MetricBundle(metric, reg_slicer, sql) bundleList.append(bundle) metric = metrics.MeanMetric(mjdCol, metricName='Mean Visit Time alt az') bundle = metricBundles.MetricBundle(metric, altaz_slicer, sql, plotFuncs=plotFuncs_lam) bundleList.append(bundle) metric = metrics.CountMetric(mjdCol, metricName='N_visits') bundle = metricBundles.MetricBundle(metric, unislicer, sql) bundleList.append(bundle) # Need pairs in window to get a map of how well it gathered SS pairs. # Moon phase. metric = metrics.NChangesMetric(col='filter', metricName='Filter Changes') bundle = metricBundles.MetricBundle(metric, unislicer, 'night=%i' % night) bundleList.append(bundle) metric = metrics.BruteOSFMetric() bundle = metricBundles.MetricBundle(metric, unislicer, 'night=%i' % night) bundleList.append(bundle) metric = metrics.MeanMetric('slewTime') bundle = metricBundles.MetricBundle(metric, unislicer, 'night=%i' % night) bundleList.append(bundle) metric = metrics.MinMetric('slewTime') bundle = metricBundles.MetricBundle(metric, unislicer, 'night=%i' % night) bundleList.append(bundle) metric = metrics.MaxMetric('slewTime') bundle = metricBundles.MetricBundle(metric, unislicer, 'night=%i' % night) bundleList.append(bundle) # Make plots of the solar system pairs that were taken in the night metric = metrics.PairMetric(mjdCol=mjdCol) sql = 'night=%i and (filter ="r" or filter="g" or filter="i")' % night bundle = metricBundles.MetricBundle(metric, reg_slicer, sql) bundleList.append(bundle) metric = metrics.PairMetric(mjdCol=mjdCol, metricName='z Pairs') sql = 'night=%i and filter="z"' % night bundle = metricBundles.MetricBundle(metric, reg_slicer, sql) bundleList.append(bundle) # Plot up each visit metric = metrics.NightPointingMetric(mjdCol=mjdCol) slicer = slicers.UniSlicer() sql = 'night=%i' % night plotFuncs = [plots.NightPointingPlotter()] bundle = metricBundles.MetricBundle(metric, slicer, sql, plotFuncs=plotFuncs) bundleList.append(bundle) # stats from the note column if notes: displayDict = {'group': 'Basic Stats', 'subgroup': 'Percent stats'} metric = metrics.StringCountMetric(col='note', percent=True, metricName='Percents') bundle = metricBundles.MetricBundle(metric, unislicer, sql, displayDict=displayDict) bundleList.append(bundle) displayDict['subgroup'] = 'Count Stats' metric = metrics.StringCountMetric(col='note', metricName='Counts') bundle = metricBundles.MetricBundle(metric, unislicer, sql, displayDict=displayDict) bundleList.append(bundle) return metricBundles.makeBundlesDictFromList(bundleList)
def glanceBatch(colmap=None, runName='opsim', nside=64, filternames=('u', 'g', 'r', 'i', 'z', 'y'), nyears=10, pairnside=32, sqlConstraint=None): """Generate a handy set of metrics that give a quick overview of how well a survey performed. This is a meta-set of other batches, to some extent. Parameters ---------- colmap : dict, opt A dictionary with a mapping of column names. Default will use OpsimV4 column names. run_name : str, opt The name of the simulated survey. Default is "opsim". nside : int, opt The nside for the healpix slicers. Default 64. filternames : list of str, opt The list of individual filters to use when running metrics. Default is ('u', 'g', 'r', 'i', 'z', 'y'). There is always an all-visits version of the metrics run as well. nyears : int (10) How many years to attempt to make hourglass plots for pairnside : int (32) nside to use for the pair fraction metric (it's slow, so nice to use lower resolution) sqlConstraint : str or None, opt Additional SQL constraint to apply to all metrics. Returns ------- metricBundleDict """ if isinstance(colmap, str): raise ValueError('colmap must be a dictionary, not a string') if colmap is None: colmap = ColMapDict('opsimV4') bundleList = [] if sqlConstraint is None: sqlC = '' else: sqlC = '(%s) and' % sqlConstraint sql_per_filt = [ '%s %s="%s"' % (sqlC, colmap['filter'], filtername) for filtername in filternames ] sql_per_and_all_filters = [sqlConstraint] + sql_per_filt standardStats = standardSummary() subsetPlots = [plots.HealpixSkyMap(), plots.HealpixHistogram()] # Super basic things displayDict = {'group': 'Basic Stats', 'order': 1} sql = sqlConstraint slicer = slicers.UniSlicer() # Length of Survey metric = metrics.FullRangeMetric(col=colmap['mjd'], metricName='Length of Survey (days)') bundle = metricBundles.MetricBundle(metric, slicer, sql, displayDict=displayDict) bundleList.append(bundle) # Total number of filter changes metric = metrics.NChangesMetric(col=colmap['filter'], orderBy=colmap['mjd']) bundle = metricBundles.MetricBundle(metric, slicer, sql, displayDict=displayDict) bundleList.append(bundle) # Total open shutter fraction metric = metrics.OpenShutterFractionMetric( slewTimeCol=colmap['slewtime'], expTimeCol=colmap['exptime'], visitTimeCol=colmap['visittime']) bundle = metricBundles.MetricBundle(metric, slicer, sql, displayDict=displayDict) bundleList.append(bundle) # Total effective exposure time metric = metrics.TeffMetric(m5Col=colmap['fiveSigmaDepth'], filterCol=colmap['filter'], normed=True) for sql in sql_per_and_all_filters: bundle = metricBundles.MetricBundle(metric, slicer, sql, displayDict=displayDict) bundleList.append(bundle) # Number of observations, all and each filter metric = metrics.CountMetric(col=colmap['mjd'], metricName='Number of Exposures') for sql in sql_per_and_all_filters: bundle = metricBundles.MetricBundle(metric, slicer, sql, displayDict=displayDict) bundleList.append(bundle) # The alt/az plots of all the pointings slicer = slicers.HealpixSlicer(nside=nside, latCol='zenithDistance', lonCol=colmap['az'], latLonDeg=colmap['raDecDeg'], useCache=False) stacker = stackers.ZenithDistStacker(altCol=colmap['alt'], degrees=colmap['raDecDeg']) metric = metrics.CountMetric(colmap['mjd'], metricName='Nvisits as function of Alt/Az') plotFuncs = [plots.LambertSkyMap()] for sql in sql_per_and_all_filters: bundle = metricBundles.MetricBundle(metric, slicer, sql, plotFuncs=plotFuncs, displayDict=displayDict, stackerList=[stacker]) bundleList.append(bundle) # Things to check per night # Open Shutter per night displayDict = {'group': 'Pointing Efficency', 'order': 2} slicer = slicers.OneDSlicer(sliceColName=colmap['night'], binsize=1) metric = metrics.OpenShutterFractionMetric( slewTimeCol=colmap['slewtime'], expTimeCol=colmap['exptime'], visitTimeCol=colmap['visittime']) sql = sqlConstraint bundle = metricBundles.MetricBundle(metric, slicer, sql, summaryMetrics=standardStats, displayDict=displayDict) bundleList.append(bundle) # Number of filter changes per night slicer = slicers.OneDSlicer(sliceColName=colmap['night'], binsize=1) metric = metrics.NChangesMetric(col=colmap['filter'], orderBy=colmap['mjd'], metricName='Filter Changes') bundle = metricBundles.MetricBundle(metric, slicer, sql, summaryMetrics=standardStats, displayDict=displayDict) bundleList.append(bundle) # A few basic maps # Number of observations, coadded depths displayDict = {'group': 'Basic Maps', 'order': 3} slicer = slicers.HealpixSlicer(nside=nside, latCol=colmap['dec'], lonCol=colmap['ra'], latLonDeg=colmap['raDecDeg']) metric = metrics.CountMetric(col=colmap['mjd']) plotDict = {'percentileClip': 95.} for sql in sql_per_and_all_filters: bundle = metricBundles.MetricBundle(metric, slicer, sql, summaryMetrics=standardStats, displayDict=displayDict, plotDict=plotDict) bundleList.append(bundle) metric = metrics.Coaddm5Metric(m5Col=colmap['fiveSigmaDepth']) for sql in sql_per_and_all_filters: bundle = metricBundles.MetricBundle(metric, slicer, sql, summaryMetrics=standardStats, displayDict=displayDict) bundleList.append(bundle) # Checking a few basic science things # Maybe check astrometry, observation pairs, SN plotDict = {'percentileClip': 95.} displayDict = {'group': 'Science', 'subgroup': 'Astrometry', 'order': 4} stackerList = [] stacker = stackers.ParallaxFactorStacker(raCol=colmap['ra'], decCol=colmap['dec'], degrees=colmap['raDecDeg'], dateCol=colmap['mjd']) stackerList.append(stacker) # Maybe parallax and proper motion, fraction of visits in a good pair for SS displayDict['caption'] = r'Parallax precision of an $r=20$ flat SED star' metric = metrics.ParallaxMetric(m5Col=colmap['fiveSigmaDepth'], filterCol=colmap['filter'], seeingCol=colmap['seeingGeom']) sql = sqlConstraint bundle = metricBundles.MetricBundle(metric, slicer, sql, plotFuncs=subsetPlots, displayDict=displayDict, stackerList=stackerList, plotDict=plotDict) bundleList.append(bundle) displayDict[ 'caption'] = r'Proper motion precision of an $r=20$ flat SED star' metric = metrics.ProperMotionMetric(m5Col=colmap['fiveSigmaDepth'], mjdCol=colmap['mjd'], filterCol=colmap['filter'], seeingCol=colmap['seeingGeom']) bundle = metricBundles.MetricBundle(metric, slicer, sql, plotFuncs=subsetPlots, displayDict=displayDict, plotDict=plotDict) bundleList.append(bundle) # Solar system stuff displayDict['caption'] = 'Fraction of observations that are in pairs' displayDict['subgroup'] = 'Solar System' sql = '%s (filter="g" or filter="r" or filter="i")' % sqlC pairSlicer = slicers.HealpixSlicer(nside=pairnside, latCol=colmap['dec'], lonCol=colmap['ra'], latLonDeg=colmap['raDecDeg']) metric = metrics.PairFractionMetric(mjdCol=colmap['mjd']) bundle = metricBundles.MetricBundle(metric, pairSlicer, sql, plotFuncs=subsetPlots, displayDict=displayDict) bundleList.append(bundle) # stats from the note column if 'note' in colmap.keys(): displayDict = {'group': 'Basic Stats', 'subgroup': 'Percent stats'} metric = metrics.StringCountMetric(col=colmap['note'], percent=True, metricName='Percents') sql = '' slicer = slicers.UniSlicer() bundle = metricBundles.MetricBundle(metric, slicer, sql, displayDict=displayDict) bundleList.append(bundle) displayDict['subgroup'] = 'Count Stats' metric = metrics.StringCountMetric(col=colmap['note'], metricName='Counts') bundle = metricBundles.MetricBundle(metric, slicer, sql, displayDict=displayDict) bundleList.append(bundle) for b in bundleList: b.setRunName(runName) # Add hourglass plots. hrDict = hourglassBatch(colmap=colmap, runName=runName, nyears=nyears, extraSql=sqlConstraint) # Add basic slew stats. try: slewDict = slewBasics(colmap=colmap, runName=runName) except KeyError as e: warnings.warn( 'Could not add slew stats: missing required key %s from colmap' % (e)) bd = metricBundles.makeBundlesDictFromList(bundleList) bd.update(slewDict) bd.update(hrDict) return bd