Exemplo n.º 1
0
 def testOpenShutterFractionMetric(self):
     """
     Test the open shutter fraction metric.
     """
     nvisit = 10
     exptime = 30.
     slewtime = 30.
     visitExpTime = np.ones(nvisit, dtype='float')*exptime
     visitTime = np.ones(nvisit, dtype='float')*(exptime+0.0)
     slewTime = np.ones(nvisit, dtype='float')*slewtime
     data = np.core.records.fromarrays([visitExpTime, visitTime, slewTime],
                                       names=['visitExposureTime', 'visitTime', 'slewTime'])
     metric = metrics.OpenShutterFractionMetric()
     result = metric.run(data)
     self.assertEqual(result, .5)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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
Exemplo n.º 4
0
def openshutterFractions(colmap=None,
                         runName='opsim',
                         extraSql=None,
                         extraMetadata=None):
    """Evaluate open shutter fraction over whole survey and per night.

    Parameters
    ----------
    colmap : dict, opt
        A dictionary with a mapping of column names. Default will use OpsimV4 column names.
    runName : str, opt
        The name of the simulated survey. Default is "opsim".
    extraSql : str, opt
        Additional constraint to add to any sql constraints (e.g. 'night<365')
        Default None, for no additional constraints.
    extraMetadata : str, opt
        Additional metadata to add before any below (i.e. "WFD").  Default is None.
    """
    if colmap is None:
        colmap = ColMapDict('opsimV4')
    bundleList = []

    group = 'Open Shutter Fraction'

    subgroup = 'All visits'
    if extraMetadata is not None:
        subgroup = extraMetadata + ' ' + subgroup.lower()
    elif extraSql is not None and extraMetadata is None:
        subgroup = subgroup + ' ' + extraSql

    # Open Shutter fraction over whole survey.
    displayDict = {'group': group, 'subgroup': subgroup, 'order': 0}
    displayDict[
        'caption'] = 'Total open shutter fraction over %s. ' % subgroup.lower(
        )
    displayDict['caption'] += 'Does not include downtime due to weather.'
    metric = metrics.OpenShutterFractionMetric(
        slewTimeCol=colmap['slewtime'],
        expTimeCol=colmap['exptime'],
        visitTimeCol=colmap['visittime'])
    slicer = slicers.UniSlicer()
    bundle = mb.MetricBundle(metric,
                             slicer,
                             extraSql,
                             metadata=subgroup,
                             displayDict=displayDict)
    bundleList.append(bundle)
    # Count the number of nights on-sky in the survey.
    displayDict[
        'caption'] = 'Number of nights on the sky during the survey, %s.' % subgroup.lower(
        )
    metric = metrics.CountUniqueMetric(colmap['night'])
    slicer = slicers.UniSlicer()
    bundle = mb.MetricBundle(metric,
                             slicer,
                             extraSql,
                             metadata=subgroup,
                             displayDict=displayDict)
    bundleList.append(bundle)
    # Count the number of nights total in the survey (start to finish of observations).
    displayDict[
        'caption'] = 'Number of nights from start to finish of survey, %s.' % subgroup.lower(
        )
    metric = metrics.FullRangeMetric(colmap['night'])
    slicer = slicers.UniSlicer()
    bundle = mb.MetricBundle(metric,
                             slicer,
                             extraSql,
                             metadata=subgroup,
                             displayDict=displayDict)
    bundleList.append(bundle)

    # Open shutter fraction per night.
    subgroup = 'Per night'
    if extraMetadata is not None:
        subgroup = extraMetadata + ' ' + subgroup.lower()
    elif extraSql is not None and extraMetadata is None:
        subgroup = subgroup + ' ' + extraSql
    displayDict = {'group': group, 'subgroup': subgroup, 'order': 0}
    displayDict['caption'] = 'Open shutter fraction %s.' % (subgroup.lower())
    displayDict['caption'] += ' This compares on-sky image time against on-sky time + slews + filter ' \
                              'changes + readout, but does not include downtime due to weather.'
    metric = metrics.OpenShutterFractionMetric(
        slewTimeCol=colmap['slewtime'],
        expTimeCol=colmap['exptime'],
        visitTimeCol=colmap['visittime'])
    slicer = slicers.OneDSlicer(sliceColName=colmap['night'], binsize=1)
    bundle = mb.MetricBundle(metric,
                             slicer,
                             extraSql,
                             metadata=subgroup,
                             summaryMetrics=standardSummary(),
                             displayDict=displayDict)
    bundleList.append(bundle)

    # Set the runName for all bundles and return the bundleDict.
    for b in bundleList:
        b.setRunName(runName)
    return mb.makeBundlesDictFromList(bundleList)