Exemplo n.º 1
0
def calcCompleteness(orbitfile, obsfile, outDir):
    """
    Calculate a basic set of metrics on the NEOs, including completeness.
    Saves the plots to disk.
    """
    # Read data back from disk.
    mos = MoSlicer(orbitfile, Hrange=np.arange(13, 26, 0.5))
    mos.readObs(obsfile)

    # Nobs
    metric = MoMetrics.NObsMetric()
    slicer = mos
    pandasConstraint = None
    plotDict = {'nxbins':100, 'nybins':100}
    nobs = mmb.MoMetricBundle(metric, slicer, pandasConstraint,
                          runName=runName, metadata=metadata, plotDict=plotDict)

    # Calculate completeness. First we must calculate "DiscoveryChances".
    # Set up an example metric bundle.
    metric = MoMetrics.DiscoveryChancesMetric()
    slicer = mos
    pandasConstraint = None
    discovery = mmb.MoMetricBundle(metric, slicer, pandasConstraint,
                                    runName=runName, metadata=metadata, plotDict=plotDict)


    bdict = {'nobs':nobs, 'discovery':discovery}
    bg = mmb.MoMetricBundleGroup(bdict, outDir=outDir)
    bg.runAll()
    bg.plotAll()

    ph = plots.PlotHandler(outDir=outDir)
    # Then calculate 'completeness' as function of H, as a secondary metric.
    completeness = discovery.reduceMetric(discovery.metric.reduceFuncs['Completeness'])

    # And we can make an 'integrated over H distribution' version.
    completenessInt = completeness.reduceMetric(completeness.metric.reduceFuncs['CumulativeH'])

    Hmark = 21.0
    for c in [completeness, completenessInt]:
        summaryMetric = ValueAtHMetric(Hmark=Hmark)
        c.setSummaryMetrics(summaryMetric)
        c.computeSummaryStats()
        label = "Completeness at H=%.1f: %.2f" %(Hmark, c.summaryValues['Value At H=%.1f' %Hmark])
        c.setPlotDict({'label':label})
        c.plot(plotFunc = moPlots.MetricVsH())
        plt.axvline(Hmark, color='r', linestyle=':')
        plt.axhline(c.summaryValues['Value At H=%.1f' %(Hmark)], color='r', linestyle='-')
        plt.legend(loc=(0.9, 0.2))
        plt.savefig(os.path.join(outDir, c.fileRoot + '_vsH' + '.png'), format='png')
Exemplo n.º 2
0
 def reduceMetric(self,
                  reduceFunc,
                  reducePlotDict=None,
                  reduceDisplayDict=None):
     """
     Run reduce methods on the metric bundle, such as completeness or integrate over H distribution.
     These return a new metric bundle.
     """
     rName = reduceFunc.__name__.replace('reduce', '')
     reduceName = self.metric.name + '_' + rName
     newmetric = deepcopy(self.metric)
     newmetric.name = reduceName
     newBundle = MoMetricBundle(metric=newmetric,
                                slicer=self.slicer,
                                constraint=self.constraint,
                                runName=self.runName,
                                metadata=self.metadata,
                                plotDict=self.plotDict,
                                plotFuncs=self.plotFuncs,
                                summaryMetrics=self.summaryMetrics)
     if rName == 'Completeness':
         newBundle.metric.name = 'Completeness'
         newBundle.plotFuncs = [moPlots.MetricVsH()]
         newBundle.setPlotDict({'ylabel': 'Completeness @H'})
     if rName == 'CumulativeH':
         newBundle.metric.name = self.metric.name + ' (cumulative H)'
         newBundle.setPlotDict({'units': '<=H'})
         if 'ylabel' in newBundle.plotDict:
             newBundle.plotDict['ylabel'] = newBundle.plotDict[
                 'ylabel'].replace('@H', '<=H')
     newBundle._buildFileRoot()
     # Calculate new metric values.
     newBundle.metricValues, Hvals = reduceFunc(
         self.metricValues, self.slicer.slicePoints['H'])
     if newBundle.metricValues.shape[1] != self.slicer.slicerShape[1]:
         # Then something happened with reduceFunction --
         #  .. usually, this would be with 'completeness'. It can reshape the metric values so that
         #  (a) if we cloned H, we now go from multiple metricvalues per H value to a single value per H [(nSso, nHrange) -> (1, nHrange)]
         #  (b) if we did not clone H, we now have a binned metricvalues - one per H bin, instead of one per nsso [(nSso, 1) -> (nHrange, nHrange)]
         # and we don't really care (in general) because we won't be re-slicing the observations. But we do need to update the slicePoints['H'],
         #  and for that we need a new slicer. Rereading the orbit file is a bit of a pain, but not too bad.
         newslicer = MoSlicer(orbitfile=self.slicer.orbitfile, Hrange=Hvals)
         newBundle.slicer = newslicer
     return newBundle
Exemplo n.º 3
0
def calcCompleteness(orbitfile, obsfile, outDir, runName, metadata=None):
    """
    Calculate a basic set of metrics on the NEOs, including completeness.
    Saves the plots to disk.
    """
    # Read data back from disk.
    mos = MoSlicer(orbitfile, Hrange=np.arange(15, 27, 0.25))
    mos.readObs(obsfile)

    # Nobs
    metric = moMetrics.NObsMetric()
    slicer = mos
    pandasConstraint = None
    plotDict = {'nxbins': 200, 'nybins': 200}
    nobs = mmb.MoMetricBundle(metric,
                              slicer,
                              pandasConstraint,
                              runName=runName,
                              metadata=metadata,
                              plotDict=plotDict)

    # Calculate completeness. First we must calculate "DiscoveryChances".
    # Set up an example metric bundle.
    discovery = {}
    nObsList = [2, 3, 4]
    nyears = [3, 5, 10, 12, 15]
    for yr in nyears:
        discovery[yr] = {}
        for nObs in nObsList:
            md = metadata + ' %d visits/night after %d years' % (nObs, yr)
            #metric = moMetrics.DiscoveryMetric(tMin=0, tMax=90./60/24., nObsPerNight=nObs, nNightsPerWindow=3, tWindow=30)
            metric = moMetrics.DiscoveryChancesMetric(tNight=90. / 60. / 24.,
                                                      nObsPerNight=nObs,
                                                      nNightsPerWindow=3,
                                                      tWindow=15)
            slicer = mos
            pandasConstraint = 'night <= %d' % (yr * 365)
            discovery[yr][nObs] = mmb.MoMetricBundle(
                metric,
                slicer,
                pandasConstraint,
                runName=runName,
                metadata=md,
                plotDict=plotDict,
                plotFuncs=[moPlots.MetricVsH()])

        #metric = moMetrics.DiscoveryMetric(tMin=0, tMax=90./60/24., nObsPerNight=2, nNightsPerWindow=4, tWindow=30)
        metric = moMetrics.DiscoveryChancesMetric(tNight=90. / 60. / 24.,
                                                  nObsPerNight=2,
                                                  nNightsPerWindow=4,
                                                  tWindow=30)
        discovery[yr]['4nights'] = mmb.MoMetricBundle(
            metric=metric,
            slicer=mos,
            constraint=pandasConstraint,
            runName=runName,
            metadata=metadata + '4 nights/track after %d years' % (yr),
            plotDict=plotDict,
            plotFuncs=[moPlots.MetricVsH()])

    bdict = {}
    for yr in nyears:
        for nObs in nObsList:
            bdict['discovery_%s_%d' % (nObs, yr)] = discovery[yr][nObs]
        bdict['4nights_%d' % (yr)] = discovery[yr]['4nights']
    bdict['nobs'] = nobs

    bg = mmb.MoMetricBundleGroup(bdict, outDir=outDir)
    bg.runAll()
    bg.plotAll()

    Hmark = 22

    completeness = {}
    completenessInt = {}
    hVals = mos.Hrange
    for yr in nyears:
        completeness[yr] = {}
        completenessInt[yr] = {}
        for nObs in nObsList:
            #discChances = discovery[nObs].childBundles['N_Chances']
            discChances = discovery[yr][nObs]
            discChances.setSummaryMetrics([
                moSummaryMetrics.CompletenessMetric(),
                moSummaryMetrics.CumulativeCompletenessMetric()
            ])
            discChances.computeSummaryStats()
            completeness[yr][nObs] = discChances.summaryValues['Completeness'][
                0]
            completenessInt[yr][nObs] = discChances.summaryValues[
                'CumulativeCompleteness'][0]
        for nObs in ['4nights']:
            #discChances = discovery[nObs].childBundles['N_Chances']
            discChances = discovery[yr][nObs]
            discChances.setSummaryMetrics([
                moSummaryMetrics.CompletenessMetric(),
                moSummaryMetrics.CumulativeCompletenessMetric()
            ])
            discChances.computeSummaryStats()
            completeness[yr][nObs] = discChances.summaryValues['Completeness'][
                0]
            completenessInt[yr][nObs] = discChances.summaryValues[
                'CumulativeCompleteness'][0]

    # Make a figure of completeness with the different discovery patterns, for one year.
    yr = 12
    Hidx = np.where(hVals == Hmark)[0]
    plt.figure()
    plt.title('Completeness %s' % (runName))
    for nObs in nObsList:
        cval = completeness[yr][nObs][Hidx]
        plt.plot(hVals,
                 completeness[yr][nObs],
                 label='%d obs/tracklet, %.0f%s H @ %.1f' %
                 (nObs, cval * 100, '%', Hmark))
    for nObs in ['4nights']:
        cval = completeness[yr][nObs][Hidx]
        plt.plot(hVals,
                 completeness[yr][nObs],
                 label='4 pairs/track, %.0f%s H @ %.1f' %
                 (cval * 100, '%', Hmark))
    plt.axvline(Hmark, color='r', linestyle=':')
    plt.xlabel('H')
    plt.ylabel('Completeness @ H')
    plt.legend(loc='upper right',
               fancybox=True,
               numpoints=1,
               fontsize='smaller')
    plt.savefig(os.path.join(outDir, 'completeness.pdf'), format='pdf')

    plt.figure()
    plt.title('Cumulative completeness %s' % (runName))
    for nObs in nObsList:
        cval = completenessInt[yr][nObs][Hidx]
        plt.plot(hVals,
                 completenessInt[yr][nObs],
                 label='%d obs/tracklet, %.0f%s H <= %.1f' %
                 (nObs, cval * 100, '%', Hmark))
    for nObs in ['4nights']:
        cval = completenessInt[yr][nObs][Hidx]
        plt.plot(hVals,
                 completenessInt[yr][nObs],
                 label='4 pairs/track, %.0f%s H <= %.1f' %
                 (cval * 100, '%', Hmark))
    plt.axvline(Hmark, color='r', linestyle=':')
    plt.xlabel('H')
    plt.ylabel('Completeness <= H')
    plt.legend(loc='upper right',
               fancybox=True,
               numpoints=1,
               fontsize='smaller')
    plt.savefig(os.path.join(outDir, 'completenessInt.pdf'), format='pdf')

    # And make a figure for one set of discovery criterion, over time.
    nObs = 2
    plt.figure()
    plt.title('Completeness over time %s' % (runName))
    for yr in nyears:
        cval = completeness[yr][nObs][Hidx]
        plt.plot(hVals,
                 completeness[yr][nObs],
                 label='Year %d: %.0f @ H=%.1f' % (yr, cval * 100, Hmark))
    plt.axvline(Hmark, color='r', linestyle=':')
    plt.xlabel('H')
    plt.ylabel('Completeness @ H')
    plt.legend(loc='upper right',
               fancybox=True,
               numpoints=1,
               fontsize='smaller')
    plt.savefig(os.path.join(outDir, 'completenessTime.pdf'), format='pdf')

    nObs = 2
    plt.figure()
    plt.title('Completeness over time %s' % (runName))
    for yr in nyears:
        cval = completenessInt[yr][nObs][Hidx]
        plt.plot(hVals,
                 completenessInt[yr][nObs],
                 label='Year %d: %.0f <= H=%.1f' % (yr, cval * 100, Hmark))
    plt.axvline(Hmark, color='r', linestyle=':')
    plt.xlabel('H')
    plt.ylabel('Completeness <= H')
    plt.legend(loc='upper right',
               fancybox=True,
               numpoints=1,
               fontsize='smaller')
    plt.savefig(os.path.join(outDir, 'completenessIntTime.pdf'), format='pdf')
Exemplo n.º 4
0
def calcCompleteness(orbitfile, obsfile, outDir, runName, metadata=None):
    """
    Calculate a basic set of metrics on the NEOs, including completeness.
    Saves the plots to disk.
    """
    # Read data back from disk.
    mos = MoSlicer(orbitfile, Hrange=np.arange(15, 27, 0.25))
    mos.readObs(obsfile)

    # Nobs
    metric = moMetrics.NObsMetric()
    slicer = mos
    pandasConstraint = None
    plotDict = {'nxbins':200, 'nybins':200}
    nobs = mmb.MoMetricBundle(metric, slicer, pandasConstraint,
                          runName=runName, metadata=metadata, plotDict=plotDict)

    # Calculate completeness. First we must calculate "DiscoveryChances".
    # Set up an example metric bundle.
    discovery = {}
    nObsList = [2, 3, 4]
    nyears = [3, 5, 10, 12, 15]
    for yr in nyears:
        discovery[yr] = {}
        for nObs in nObsList:
            md = metadata + ' %d visits/night after %d years' %(nObs, yr)
            #metric = moMetrics.DiscoveryMetric(tMin=0, tMax=90./60/24., nObsPerNight=nObs, nNightsPerWindow=3, tWindow=30)
            metric = moMetrics.DiscoveryChancesMetric(tNight=90./60./24., nObsPerNight=nObs, nNightsPerWindow=3, tWindow=15)
            slicer = mos
            pandasConstraint = 'night <= %d' %(yr*365)
            discovery[yr][nObs] = mmb.MoMetricBundle(metric, slicer, pandasConstraint,
                                                    runName=runName, metadata=md,
                                                    plotDict=plotDict, plotFuncs=[moPlots.MetricVsH()])

        #metric = moMetrics.DiscoveryMetric(tMin=0, tMax=90./60/24., nObsPerNight=2, nNightsPerWindow=4, tWindow=30)
        metric = moMetrics.DiscoveryChancesMetric(tNight=90./60./24., nObsPerNight=2, nNightsPerWindow=4, tWindow=30)
        discovery[yr]['4nights'] = mmb.MoMetricBundle(metric=metric,
                                                slicer=mos, constraint=pandasConstraint, runName=runName,
                                                metadata = metadata+'4 nights/track after %d years' %(yr),
                                                plotDict=plotDict, plotFuncs=[moPlots.MetricVsH()])

    bdict = {}
    for yr in nyears:
        for nObs in nObsList:
            bdict['discovery_%s_%d' %(nObs, yr)] = discovery[yr][nObs]
        bdict['4nights_%d' %(yr)] = discovery[yr]['4nights']
    bdict['nobs'] = nobs

    bg = mmb.MoMetricBundleGroup(bdict, outDir=outDir)
    bg.runAll()
    bg.plotAll()

    Hmark = 22

    completeness = {}
    completenessInt = {}
    hVals = mos.Hrange
    for yr in nyears:
        completeness[yr] = {}
        completenessInt[yr] = {}
        for nObs in nObsList:
        #discChances = discovery[nObs].childBundles['N_Chances']
            discChances = discovery[yr][nObs]
            discChances.setSummaryMetrics([moSummaryMetrics.CompletenessMetric(), moSummaryMetrics.CumulativeCompletenessMetric()])
            discChances.computeSummaryStats()
            completeness[yr][nObs] = discChances.summaryValues['Completeness'][0]
            completenessInt[yr][nObs] = discChances.summaryValues['CumulativeCompleteness'][0]
        for nObs in ['4nights']:
        #discChances = discovery[nObs].childBundles['N_Chances']
            discChances = discovery[yr][nObs]
            discChances.setSummaryMetrics([moSummaryMetrics.CompletenessMetric(), moSummaryMetrics.CumulativeCompletenessMetric()])
            discChances.computeSummaryStats()
            completeness[yr][nObs] = discChances.summaryValues['Completeness'][0]
            completenessInt[yr][nObs] = discChances.summaryValues['CumulativeCompleteness'][0]

    # Make a figure of completeness with the different discovery patterns, for one year.
    yr = 12
    Hidx = np.where(hVals == Hmark)[0]
    plt.figure()
    plt.title('Completeness %s' %(runName))
    for nObs in nObsList:
        cval = completeness[yr][nObs][Hidx]
        plt.plot(hVals, completeness[yr][nObs], label='%d obs/tracklet, %.0f%s H @ %.1f' %(nObs, cval*100, '%', Hmark))
    for nObs in ['4nights']:
        cval = completeness[yr][nObs][Hidx]
        plt.plot(hVals, completeness[yr][nObs], label='4 pairs/track, %.0f%s H @ %.1f' %(cval*100, '%', Hmark))
    plt.axvline(Hmark, color='r', linestyle=':')
    plt.xlabel('H')
    plt.ylabel('Completeness @ H')
    plt.legend(loc='upper right', fancybox=True, numpoints=1, fontsize='smaller')
    plt.savefig(os.path.join(outDir, 'completeness.pdf'), format='pdf')

    plt.figure()
    plt.title('Cumulative completeness %s' %(runName))
    for nObs in nObsList:
        cval = completenessInt[yr][nObs][Hidx]
        plt.plot(hVals, completenessInt[yr][nObs], label='%d obs/tracklet, %.0f%s H <= %.1f' %(nObs, cval*100, '%', Hmark))
    for nObs in ['4nights']:
        cval = completenessInt[yr][nObs][Hidx]
        plt.plot(hVals, completenessInt[yr][nObs], label='4 pairs/track, %.0f%s H <= %.1f' %(cval*100, '%', Hmark))
    plt.axvline(Hmark, color='r', linestyle=':')
    plt.xlabel('H')
    plt.ylabel('Completeness <= H')
    plt.legend(loc='upper right', fancybox=True, numpoints=1, fontsize='smaller')
    plt.savefig(os.path.join(outDir, 'completenessInt.pdf'), format='pdf')

    # And make a figure for one set of discovery criterion, over time.
    nObs = 2
    plt.figure()
    plt.title('Completeness over time %s' %(runName))
    for yr in nyears:
        cval = completeness[yr][nObs][Hidx]
        plt.plot(hVals, completeness[yr][nObs], label='Year %d: %.0f @ H=%.1f' %(yr, cval*100, Hmark))
    plt.axvline(Hmark, color='r', linestyle=':')
    plt.xlabel('H')
    plt.ylabel('Completeness @ H')
    plt.legend(loc='upper right', fancybox=True, numpoints=1, fontsize='smaller')
    plt.savefig(os.path.join(outDir, 'completenessTime.pdf'), format='pdf')

    nObs = 2
    plt.figure()
    plt.title('Completeness over time %s' %(runName))
    for yr in nyears:
        cval = completenessInt[yr][nObs][Hidx]
        plt.plot(hVals, completenessInt[yr][nObs], label='Year %d: %.0f <= H=%.1f' %(yr, cval*100, Hmark))
    plt.axvline(Hmark, color='r', linestyle=':')
    plt.xlabel('H')
    plt.ylabel('Completeness <= H')
    plt.legend(loc='upper right', fancybox=True, numpoints=1, fontsize='smaller')
    plt.savefig(os.path.join(outDir, 'completenessIntTime.pdf'), format='pdf')