def nvisitsPerProp(opsdb, colmap=None, runName='opsim', binNights=1): """Set up a group of all and per-proposal nvisits metrics. Parameters ---------- opsdb : lsst.sims.maf.db.Database or lsst.sims.maf.db.OpsimDatabase* object colmap : dict or None, 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". binNights : int, opt Number of nights to count in each bin. Default = 1, count number of visits in each night. Returns ------- metricBundle """ if colmap is None: colmap = getColMap(opsdb) propids, proptags = opsdb.fetchPropInfo() # Calculate the total number of visits per proposal, and their fraction compared to total. totvisits = opsdb.fetchNVisits() metric = metrics.CountMetric(colmap['mjd'], metricName='Nvisits') slicer = slicers.UniSlicer() summaryMetrics = [ metrics.IdentityMetric(metricName='Count'), metrics.NormalizeMetric(normVal=totvisits, metricName='Fraction of total') ] bundleList = [] displayDict = { 'group': 'Visit Summary', 'subgroup': 'Proposal distribution', 'order': -1 } bdict = {} bdict.update( nvisitsPerNight(colmap=colmap, runName=runName, binNights=binNights, sql=None, metadata='All visits')) # Look for any multi-proposal groups that we should include. for tag in proptags: if len(proptags[tag]) > 1: pids = proptags[tag] sql = '(' for pid in pids[:-1]: sql += 'proposalId=%d or ' % pid sql += ' proposalId=%d)' % pids[-1] metadata = '%s' % (tag) bdict.update( nvisitsPerNight(colmap=colmap, runName=runName, binNights=binNights, sql=sql, metadata=metadata)) displayDict['order'] += 1 displayDict[ 'caption'] = 'Number of visits and fraction of total visits, for %s.' % metadata bundle = mb.MetricBundle(metric, slicer, sql=sql, metadata=metadata, summaryMetrics=summaryMetrics, displayDict=displayDict) bundleList.append(bundle) # And then just run each proposal separately. for propid in propids: sql = 'proposalId=%d' % (propid) metadata = '%s' % (propids[propid]) bdict.update( nvisitsPerNight(colmap=colmap, runName=runName, binNights=binNights, sql=sql, metadata=metadata)) displayDict['order'] += 1 displayDict[ 'caption'] = 'Number of visits and fraction of total visits, for %s.' % metadata bundle = mb.MetricBundle(metric, slicer, constraint=sql, metadata=metadata, summaryMetrics=summaryMetrics, displayDict=displayDict) bundleList.append(bundle) for b in bundleList: b.setRunName(runName) bdict.update(mb.makeBundlesDictFromList(bundleList)) return bdict
def nvisitsPerProp(opsdb, colmap=None, runName='opsim', binNights=1, extraSql=None): """Set up a group of all and per-proposal nvisits metrics. Parameters ---------- opsdb : lsst.sims.maf.db.Database or lsst.sims.maf.db.OpsimDatabase* object colmap : dict or None, 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". binNights : int, opt Number of nights to count in each bin. Default = 1, count number of visits in each night. sqlConstraint : str or None, opt SQL constraint to add to all metrics. Returns ------- metricBundle """ if colmap is None: colmap = getColMap(opsdb) propids, proptags = opsdb.fetchPropInfo() bdict = {} bundleList = [] totvisits = opsdb.fetchNVisits() metadata = 'All props' if extraSql is not None and len(extraSql) > 0: metadata += ' %s' % extraSql # Nvisits per night, all proposals. bdict.update( nvisitsPerNight(colmap=colmap, runName=runName, binNights=binNights, extraSql=extraSql, extraMetadata=metadata, subgroup='All proposals')) # Nvisits total, all proposals. metric = metrics.CountMetric(colmap['mjd'], metricName='Nvisits') slicer = slicers.UniSlicer() summaryMetrics = [ metrics.IdentityMetric(metricName='Count'), metrics.NormalizeMetric(normVal=totvisits, metricName='Fraction of total') ] displayDict = { 'group': 'Nvisit Summary', 'subgroup': 'Proposal distribution', 'order': -1 } displayDict['caption'] = 'Total number of visits for all proposals.' if extraSql is not None and len(extraSql) > 0: displayDict['caption'] += ' (with constraint %s.)' % extraSql bundle = mb.MetricBundle(metric, slicer, extraSql, metadata=metadata, displayDict=displayDict, summaryMetrics=summaryMetrics) bundleList.append(bundle) # Look for any multi-proposal groups that we should include. for tag in proptags: if len(proptags[tag]) > 1 or tag in ('WFD', 'DD'): pids = proptags[tag] sql = '(' for pid in pids[:-1]: sql += '%s=%d or ' % (colmap['proposalId'], pid) sql += ' %s=%d)' % (colmap['proposalId'], pids[-1]) metadata = '%s' % tag if extraSql is not None: sql = '(%s) and (%s)' % (sql, extraSql) metadata += ' %s' % (extraSql) bdict.update( nvisitsPerNight(colmap=colmap, runName=runName, binNights=binNights, extraSql=sql, extraMetadata=metadata, subgroup=tag)) displayDict['order'] += 1 displayDict[ 'caption'] = 'Number of visits and fraction of total visits, for %s.' % metadata bundle = mb.MetricBundle(metric, slicer, sql, metadata=metadata, summaryMetrics=summaryMetrics, displayDict=displayDict) bundleList.append(bundle) # And each proposal separately. for propid in propids: sql = '%s=%d' % (colmap['proposalId'], propid) metadata = '%s' % (propids[propid]) if extraSql is not None: sql += ' and (%s)' % (extraSql) metadata += ' %s' % extraSql bdict.update( nvisitsPerNight(colmap=colmap, runName=runName, binNights=binNights, extraSql=sql, extraMetadata=metadata, subgroup='Per proposal')) displayDict['order'] += 1 displayDict[ 'caption'] = 'Number of visits and fraction of total visits, for %s.' % metadata bundle = mb.MetricBundle(metric, slicer, constraint=sql, metadata=metadata, summaryMetrics=summaryMetrics, displayDict=displayDict) bundleList.append(bundle) for b in bundleList: b.setRunName(runName) bdict.update(mb.makeBundlesDictFromList(bundleList)) return bdict
def testNormalizeMetric(self): """Test normalize metric.""" data = np.ones(10, dtype=zip(['testcol'], ['float'])) metric = metrics.NormalizeMetric(col='testcol', normVal=5.5) result = metric.run(data) np.testing.assert_equal(result, np.ones(10, float) / 5.5)