Source code for rubin_sim.maf.metrics.base_lynx_metrics

# Base class for all metrics that use LightCurveLynx models. The can either
# take a precomputed light curve (saved in the slicepoint) or compute it
# dynamically using LightCurveLynx.

from .base_metric import BaseMetric


[docs] class BaseLynxMetric(BaseMetric): """ Base class for LightCurveLynx metrics, extending the BaseMetric class. This class can either use from the loaded "lightcurve" data (in the data_slice) via the `LynxResultsSlicer` or compute it dynamically using LightCurveLynx via the data by the `LynxSamplerSlicer`. Parameters ---------- from_results : `bool` or `None` Whether to use precomputed results or compute them dynamically. This is a required keyword argument that must be specified as True or False. Default is None, which will raise an error. **kwargs Additional keyword arguments passed to the parent class (BaseMetric). """ # The columns from the Opsim database that are required for simulations. _simulate_cols = [ "fieldRA", "fieldDec", "band", "visitExposureTime", "numExposures", "seeingFwhmEff", "skyBrightness", "observationStartMJD", "airmass", ] def __init__(self, *args, from_results=None, **kwargs): if from_results is None: raise ValueError("from_results must be specified as True or False.") self.from_results = from_results if not self.from_results: # Update the columns that we need to fetch from the Opsim database # to ensure we have all the necessary columns for the simulation. kwargs["col"] = list(set(kwargs.get("col", []) + self._simulate_cols)) else: # All we need is the "lightcurve" column from the result data. kwargs["col"] = "lightcurve" # Initialize the parent class with the updated keyword arguments. super().__init__(*args, **kwargs)
[docs] def get_lightcurve(self, data_slice, slice_point): """ Get the light curve for the given data slice and slice point. This may mean running a new LightCurveLynx simulation. The data is returned in a pandas DataFrame with columns such as mjd, flux, and fluxerr. Parameters ---------- data_slice : `numpy.ndarray`, (N,) Values passed to metric by the slicer, which the metric will use to calculate metric values at each slice_point. slice_point : `dict` or None Dictionary of slice_point metadata passed to each metric. E.g. the ra/dec of the healpix pixel. Returns ------- lightcurve : `Pandas DataFrame` A pandas data frame with the light curve information for the object. """ # If twe are computing metrics from the precomputed results, return the # lightcurve from the data slice. if self.from_results: return data_slice[0][0] # Check that we have the model and the parameters. if "lynx_model" not in slice_point or "lynx_params" not in slice_point: raise ValueError("Slice point must contain 'lynx_model' and 'lynx_params'.") # Make sure we can use the LightCurveLynx libraries. try: from lightcurvelynx.utils.maf_api import MAFQueryTable, execute_maf_query except ImportError: raise ImportError( "LightCurveLynx is needed to run the LynxDetectionMetric. " "It is not installed by default. Install it with " "`pip install lightcurvelynx`." ) # Convert the numpy table into a dictionary and use that to # initialize the MAF query table. data_dict = {col: data_slice[col] for col in self._simulate_cols} maf_query_table = MAFQueryTable(data_dict) # Execute the MAF query to retrieve the lightcurve data for # this slice point. lightcurve_data, _ = execute_maf_query( slice_point["lynx_model"], maf_query_table, graph_state=slice_point["lynx_params"], ) return lightcurve_data
[docs] class LynxObsCountMetric(BaseLynxMetric): """ A simple test metric that will probably be removed later. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)
[docs] def run(self, data_slice, slice_point): """Calculate metric values. Parameters ---------- data_slice : `numpy.ndarray`, (N,) Values passed to metric by the slicer, which the metric will use to calculate metric values at each slice_point. slice_point : `dict` or None Dictionary of slice_point metadata passed to each metric. E.g. the ra/dec of the healpix pixel. Returns ------- metricValue : `int` `float` or `object` The metric value at each slice_point.""" lc = self.get_lightcurve(data_slice, slice_point) if lc is None: return 0 return len(lc["flux"])