In [11]:
%matplotlib inline
import matplotlib.pyplot as plt

In [1]:
import numpy as np
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.metricBundles as metricBundles
import lsst.sims.maf.plots as plots

In [2]:
from lsst.sims.maf.metrics import BaseMetric

In [3]:
help(BaseMetric)


Help on class BaseMetric in module lsst.sims.maf.metrics.baseMetric:

class BaseMetric(__builtin__.object)
 |  Base class for the metrics.
 |  
 |  Methods defined here:
 |  
 |  __init__(self, col=None, metricName=None, maps=None, units=None, metricDtype=None, badval=-666)
 |      Instantiate metric.
 |      
 |      'col' is a kwarg for purposes of the MAF driver; when actually using a metric, it must be set to
 |      the names of the data columns that the metric will operate on. This can be a single string or a list.
 |      
 |      'maps' is a list of any maps that the metric will need, accessed via slicePoint that is passed from the slicer.
 |      
 |      After inheriting from this base metric :
 |        * every metric object will have metricDtype (the type of data it calculates) set according to:
 |             -- kwarg (metricDtype='float', 'int', etc)
 |             -- 'float' (assumes float if not specified in kwarg)
 |             -- 'object' (if reduce functions are present and value not set in kwarg)
 |        * every metric object will have the data columns it requires added to the column registry
 |          (so the driver can know which columns to pull from the database)
 |  
 |  run(self, dataSlice, slicePoint=None)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __metaclass__ = <class 'lsst.sims.maf.metrics.baseMetric.MetricRegistr...
 |      Meta class for metrics, to build a registry of metric classes.
 |  
 |  colInfo = <lsst.sims.maf.utils.getColInfo.ColInfo object>
 |  
 |  colRegistry = <lsst.sims.maf.metrics.baseMetric.ColRegistry object>
 |  
 |  registry = {'AveSlewFracMetric': <class 'lsst.sims.maf.metrics.slewMet...


In [4]:
class visits(BaseMetric):
    def __init__(self, mjdcol='expMJD', m5Col='fiveSigmaDepth', filterCol='filter',**kwargs):
        # Set the values we want to keep for our class.
        self.mjdcol = mjdcol
        self.m5col = m5Col
        ###self.percentile = percentile
        # Now we have to call the BaseMetric's __init__ method, to get the "framework" part set up.
        # We currently do this using 'super', which just calls BaseMetric's method.
        # The call to super just basically looks like this .. you must pass the columns you need, and the kwargs.
        super(visits, self).__init__(metricName='visits', col=[self.mjdcol, self.m5col, filterCol], 
                                     badval=np.nan, metricDtype=object, **kwargs)
        
    # Now write out "run" method, the part that does the metric calculation.
    def run(self, dataSlice, slicePoint=None):
        # for this calculation, I'll just call numpy's percentile function.
        result = (dataSlice['obsHistID'], dataSlice['expMJD'], dataSlice['fiveSigmaDepth'], dataSlice['filter'])
        return result

In [6]:
from __future__ import division

In [25]:
# UserPointsSlicer
numSamples = 100
ra = np.random.random(size=numSamples)/ numSamples*np.pi
dec = np.random.random(size=numSamples)/ numSamples*(-np.pi)
slicer = slicers.UserPointsSlicer(ra=ra,dec=dec)
metric = visits()

In [26]:
plt.plot(ra, dec, 'o')


Out[26]:
[<matplotlib.lines.Line2D at 0x11a6330d0>]

In [7]:
opsdb = db.OpsimDatabase('sqlite:////Users/rbiswas/data/LSST/OpSimData/enigma_1189_sqlite.db')

In [8]:
sqlconstraint = 'night<365'

In [9]:
bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint)

In [10]:
bgroup = metricBundles.MetricBundleGroup({0: bundle}, opsdb, outDir='newmetric_test', resultsDb=None)
bgroup.runAll()


Querying database with constraint night<365
Found 271946 visits
Running:  [0]
Completed metric generation.
Running reduce methods.
Running summary statistics.
Completed.

In [13]:
%timeit bundle.metricValues


10000000 loops, best of 3: 72 ns per loop

In [15]:
v = visits()

In [16]:
v.metricDtype


Out[16]:
object

In [17]:
v.badval


Out[17]:
nan

In [18]:
metric = visits()
slicer = slicers.userPointsSlicer.UserPointsSlicer(ra=np.array([23.,25.]), dec=np.array([23.,25.]))
sqlconstraint = 'night<365'
myBundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint)

In [19]:
!pwd


/Users/rbiswas/doc/projects/supernovae/SNsims/snsims

In [ ]:
myBundle.r

In [ ]:
bgroup = metricBundles.MetricBundleGroup({0: myBundle}, opsdb, outDir='newmetric_test', resultsDb=None)
bgroup.runAll()

In [ ]:
l = [1.2, 3.]

In [ ]:
type(l)

In [ ]: