Skip to content

Commit

Permalink
use snake case throughout. apply black and isort.
Browse files Browse the repository at this point in the history
  • Loading branch information
ebellm committed Jan 20, 2024
1 parent dabe0b1 commit 78851ce
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 109 deletions.
2 changes: 1 addition & 1 deletion rubin_sim/maf/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .galactic_plane_metrics import *
from .galplane_time_sampling_metrics import *
from .hourglass_metric import *
from .incremental_template_metric import *
from .kuiper_metrics import *
from .long_gap_agn_metric import *
from .mo_metrics import *
Expand Down Expand Up @@ -46,4 +47,3 @@
from .vector_metrics import *
from .visit_groups_metric import *
from .weak_lensing_systematics_metric import *
from .incremental_template_metric import *
238 changes: 130 additions & 108 deletions rubin_sim/maf/metrics/incremental_template_metric.py
Original file line number Diff line number Diff line change
@@ -1,161 +1,183 @@
import numpy as np

from .base_metric import BaseMetric

__all__ = ["TemplateTime"]


class TemplateTime(BaseMetric):
"""Find the time at which we expect to hit incremental template availability.
Note that there are some complications to real template generation that make this an
approximation and not an exact answer -- one aspect is that templates are generated in
`patches` and not per pixel. However, it may be possible to generate parts of these patches
at about 5arcsecond scales, which implies running with a healpix slicer at nside=512 or 1024.
"""Find the time at which we expect to hit incremental template
availability.
Note that there are some complications to real template generation
that make this an approximation and not an exact answer -- one aspect is
that templates are generated in `patches` and not per pixel. However, it
may be possible to generate parts of these patches at about 5 arcsecond
scales, which implies running with a healpix slicer at nside=512 or 1024.
Parameters
----------
n_images_in_template : `int`, opt
Number of qualified visits required for incremental template generation.
Default 3.
Number of qualified visits required for template generation.
Default 3.
seeing_percentile : `float`, opt
Maximum percentile seeing to allow in the qualified images (0 - 100).
Maximum percentile seeing to allow in the qualified images (0 - 100).
Default 50.
m5_percentile : `float`, opt
Maximum percentile m5 to allow in the qualified images (0 - 100).
Maximum percentile m5 to allow in the qualified images (0 - 100).
Default 50.
seeingCol : `str`, opt
seeing_col : `str`, opt
Name of the seeing column to use.
m5Col : `str`, opt
m5_col : `str`, opt
Name of the five sigma depth columns.
nightCol : `str`, opt
night_col : `str`, opt
Name of the column describing the night of the visit.
mjd_col : `str`, opt
Name of column describing time of the visit
filter_col : `str`, opt
Name of column describing filter
"""

def __init__(self, n_images_in_template=3, seeing_percentile=50, m5_percentile=50,
seeingCol='seeingFwhmEff', m5Col='fiveSigmaDepth',
nightCol = 'night', mjd_col = 'observationStartMJD', filter_col = 'filter', **kwargs):

def __init__(
self,
n_images_in_template=3,
seeing_percentile=50,
m5_percentile=50,
seeing_col="seeingFwhmEff",
m5_col="fiveSigmaDepth",
night_col="night",
mjd_col="observationStartMJD",
filter_col="filter",
**kwargs,
):
self.n_images_in_template = n_images_in_template
self.seeing_percentile = seeing_percentile
self.m5_percentile = m5_percentile
self.seeingCol = seeingCol
self.m5Col = m5Col
self.nightCol = nightCol
self.seeing_col = seeing_col
self.m5_col = m5_col
self.night_col = night_col
self.mjd_col = mjd_col
self.filter_col = filter_col

Check warning on line 62 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L55-L62

Added lines #L55 - L62 were not covered by tests
if 'metric_name' in kwargs:
self.metric_name = kwargs['metric_name']
del kwargs['metric_name']
if "metric_name" in kwargs:
self.metric_name = kwargs["metric_name"]
del kwargs["metric_name"]

Check warning on line 65 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L64-L65

Added lines #L64 - L65 were not covered by tests
else:
self.metric_name = 'TemplateTime'
super().__init__(col=[self.seeingCol, self.m5Col, self.nightCol, self.mjd_col, self.filter_col],
metric_name=self.metric_name, units="days", **kwargs)
# Looking at names of seeing columns, five sigma depth, and nights associated with template visits
self.metric_name = "TemplateTime"
super().__init__(

Check warning on line 68 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L67-L68

Added lines #L67 - L68 were not covered by tests
col=[self.seeing_col, self.m5_col, self.night_col, self.mjd_col, self.filter_col],
metric_name=self.metric_name,
units="days",
**kwargs,
)

def run(self, dataSlice, slice_point=None):
def run(self, data_slice, slice_point=None):
result = {}

Check warning on line 76 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L76

Added line #L76 was not covered by tests

# Bail if not enough visits at all
if len(dataSlice) < self.n_images_in_template:
if len(data_slice) < self.n_images_in_template:
return self.badval

Check warning on line 80 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L80

Added line #L80 was not covered by tests

# Check that the visits are sorted in time
dataSlice.sort(order=self.mjd_col)
data_slice.sort(order=self.mjd_col)

Check warning on line 83 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L83

Added line #L83 was not covered by tests

# Find the threshold seeing and m5

bench_seeing = np.percentile(dataSlice[self.seeingCol], self.seeing_percentile)
bench_m5 = np.percentile(dataSlice[self.m5Col], 100 - self.m5_percentile)

#Checks whether data was collected on night where seeing conditions were preferable
seeing_ok = np.where(dataSlice[self.seeingCol] < bench_seeing,
True, False)

#Checks whether data was collected where seeing range was preferable
m5_ok = np.where(dataSlice[self.m5Col] > bench_m5,
True, False)
bench_seeing = np.percentile(data_slice[self.seeing_col], self.seeing_percentile)
bench_m5 = np.percentile(data_slice[self.m5_col], 100 - self.m5_percentile)

Check warning on line 87 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L86-L87

Added lines #L86 - L87 were not covered by tests

seeing_ok = np.where(data_slice[self.seeing_col] < bench_seeing, True, False)

Check warning on line 89 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L89

Added line #L89 was not covered by tests

m5_ok = np.where(data_slice[self.m5_col] > bench_m5, True, False)

Check warning on line 91 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L91

Added line #L91 was not covered by tests

ok_template_input = np.where(seeing_ok & m5_ok)[0]

Check warning on line 93 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L93

Added line #L93 was not covered by tests
if len(ok_template_input) < self.n_images_in_template: # If seeing_ok and/or m5_ok are "false", returned as bad value
if (
len(ok_template_input) < self.n_images_in_template
): # If seeing_ok and/or m5_ok are "false", returned as bad value
return self.badval

Check warning on line 97 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L97

Added line #L97 was not covered by tests

idx_template_created = ok_template_input[self.n_images_in_template - 1] # Last image needed for template
idx_template_inputs = ok_template_input[:self.n_images_in_template] # Images included in template

Night_template_created = dataSlice[self.nightCol][idx_template_created]
N_nights_without_template = Night_template_created - dataSlice[self.nightCol][0]

where_template = dataSlice[self.nightCol] > Night_template_created # of later images where we have a template

idx_template_created = ok_template_input[

Check warning on line 99 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L99

Added line #L99 was not covered by tests
self.n_images_in_template - 1
] # Last image needed for template
idx_template_inputs = ok_template_input[: self.n_images_in_template] # Images included in template

Check warning on line 102 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L102

Added line #L102 was not covered by tests

Night_template_created = data_slice[self.night_col][idx_template_created]
N_nights_without_template = Night_template_created - data_slice[self.night_col][0]

Check warning on line 105 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L104-L105

Added lines #L104 - L105 were not covered by tests

where_template = (

Check warning on line 107 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L107

Added line #L107 was not covered by tests
data_slice[self.night_col] > Night_template_created
) # of later images where we have a template
N_images_with_template = np.sum(where_template)

Check warning on line 110 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L110

Added line #L110 was not covered by tests
template_m5 = 1.25 * np.log10(np.sum(10.0 ** (0.8 * dataSlice[self.m5Col][idx_template_inputs])))

template_m5 = 1.25 * np.log10(np.sum(10.0 ** (0.8 * data_slice[self.m5_col][idx_template_inputs])))

Check warning on line 112 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L112

Added line #L112 was not covered by tests

# derive variance-weighted PSF width
# 1. normalize the weights
template_f0 = np.sqrt(np.sum(25 * 10 ** (0.8 * dataSlice[self.m5Col][idx_template_inputs])))
# 1. normalize the weights
template_f0 = np.sqrt(np.sum(25 * 10 ** (0.8 * data_slice[self.m5_col][idx_template_inputs])))

Check warning on line 116 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L116

Added line #L116 was not covered by tests
# 2. compute per-input noise
image_noise = template_f0 / 5 * 10 ** (-0.4 * dataSlice[self.m5Col])
image_noise = template_f0 / 5 * 10 ** (-0.4 * data_slice[self.m5_col])

Check warning on line 118 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L118

Added line #L118 was not covered by tests
# 3. used variance-weighted sum of squares to derive template seeing
template_seeing = np.sqrt(np.sum(
(dataSlice[self.seeingCol][idx_template_inputs]/
image_noise[idx_template_inputs])**2.))


delta_m5 = -2.5 * np.log10(np.sqrt(1.0 + 10 ** (-0.8 * (template_m5 - dataSlice[self.m5Col]))))
diff_m5s = dataSlice[self.m5Col] + delta_m5

n_alerts_per_diffim = 1e4 * 10**(0.6 * (diff_m5s - 24.7))

template_seeing = np.sqrt(

Check warning on line 120 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L120

Added line #L120 was not covered by tests
np.sum(
(data_slice[self.seeing_col][idx_template_inputs] / image_noise[idx_template_inputs]) ** 2.0
)
)

delta_m5 = -2.5 * np.log10(np.sqrt(1.0 + 10 ** (-0.8 * (template_m5 - data_slice[self.m5_col]))))
diff_m5s = data_slice[self.m5_col] + delta_m5

Check warning on line 127 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L126-L127

Added lines #L126 - L127 were not covered by tests

n_alerts_per_diffim = 1e4 * 10 ** (0.6 * (diff_m5s - 24.7))

Check warning on line 129 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L129

Added line #L129 was not covered by tests

result["Night_template_created"] = Night_template_created
result["N_nights_without_template"] = N_nights_without_template
result["N_images_until_template"] = idx_template_created + 1
result["N_images_with_template"] = N_images_with_template
result["Template_m5"] = template_m5
result["Template_seeing"] = template_seeing
result["Total_alerts"] = np.sum(n_alerts_per_diffim[where_template])
result["Template_input_m5s"] = dataSlice[self.m5Col][idx_template_inputs]
result["Template_input_seeing"] = dataSlice[self.seeingCol][idx_template_inputs]
result["Fraction_better_template_seeing"] = np.sum((dataSlice[self.seeingCol][where_template] > template_seeing)) / np.sum(where_template)
result["Template_input_m5s"] = data_slice[self.m5_col][idx_template_inputs]
result["Template_input_seeing"] = data_slice[self.seeing_col][idx_template_inputs]
result["Fraction_better_template_seeing"] = np.sum(

Check warning on line 140 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L131-L140

Added lines #L131 - L140 were not covered by tests
(data_slice[self.seeing_col][where_template] > template_seeing)
) / np.sum(where_template)
result["Diffim_lc"] = {

Check warning on line 143 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L143

Added line #L143 was not covered by tests
"mjd":dataSlice[self.mjd_col][where_template],
"night":dataSlice[self.nightCol][where_template],
"band":dataSlice[self.filter_col][where_template],
"diff_m5":diff_m5s[where_template],
"science_m5":dataSlice[self.m5Col][where_template],
"template_m5":template_m5 * np.ones(np.sum(where_template)),
"science_seeing":dataSlice[self.seeingCol][where_template],
"template_seeing":template_seeing * np.ones(np.sum(where_template)),
"n_alerts":n_alerts_per_diffim[where_template]
"mjd": data_slice[self.mjd_col][where_template],
"night": data_slice[self.night_col][where_template],
"band": data_slice[self.filter_col][where_template],
"diff_m5": diff_m5s[where_template],
"science_m5": data_slice[self.m5_col][where_template],
"template_m5": template_m5 * np.ones(np.sum(where_template)),
"science_seeing": data_slice[self.seeing_col][where_template],
"template_seeing": template_seeing * np.ones(np.sum(where_template)),
"n_alerts": n_alerts_per_diffim[where_template],
}

return result

Check warning on line 155 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L155

Added line #L155 was not covered by tests


def reduce_Night_template_created(self, metricVal): # returns night of template creation
return metricVal["Night_template_created"]

def reduce_N_nights_without_template(self, metricVal): # returns number of nights needed to complete template
return metricVal["N_nights_without_template"]

def reduce_N_images_until_template(self, metricVal): # returns number of images needed to complete template
return metricVal["N_images_until_template"]

def reduce_N_images_with_template(self, metricVal): # returns number of images with a template
return metricVal["N_images_with_template"]


def reduce_Template_m5(self, metricVal): # calculated coadded m5 of resulting template
return metricVal["Template_m5"]

def reduce_Template_seeing(self, metricVal): # calculated seeing of resulting template
return metricVal["Template_seeing"]

def reduce_fraction_better_template_seeing(self, metricVal): # calculated seeing of resulting template
return metricVal["Fraction_better_template_seeing"]

def reduce_Total_alerts(self, metricVal):
return metricVal["Total_alerts"]

def reduce_Night_template_created(self, metric_val): # returns night of template creation
return metric_val["Night_template_created"]

Check warning on line 158 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L158

Added line #L158 was not covered by tests

def reduce_N_nights_without_template(
self, metric_val
): # returns number of nights needed to complete template
return metric_val["N_nights_without_template"]

Check warning on line 163 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L163

Added line #L163 was not covered by tests

def reduce_N_images_until_template(
self, metric_val
): # returns number of images needed to complete template
return metric_val["N_images_until_template"]

Check warning on line 168 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L168

Added line #L168 was not covered by tests

def reduce_N_images_with_template(self, metric_val): # returns number of images with a template
return metric_val["N_images_with_template"]

Check warning on line 171 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L171

Added line #L171 was not covered by tests

def reduce_Template_m5(self, metric_val): # calculated coadded m5 of resulting template
return metric_val["Template_m5"]

Check warning on line 174 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L174

Added line #L174 was not covered by tests

def reduce_Template_seeing(self, metric_val): # calculated seeing of resulting template
return metric_val["Template_seeing"]

Check warning on line 177 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L177

Added line #L177 was not covered by tests

def reduce_fraction_better_template_seeing(self, metric_val): # calculated seeing of resulting template
return metric_val["Fraction_better_template_seeing"]

Check warning on line 180 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L180

Added line #L180 was not covered by tests

def reduce_Total_alerts(self, metric_val):
return metric_val["Total_alerts"]

Check warning on line 183 in rubin_sim/maf/metrics/incremental_template_metric.py

View check run for this annotation

Codecov / codecov/patch

rubin_sim/maf/metrics/incremental_template_metric.py#L183

Added line #L183 was not covered by tests

0 comments on commit 78851ce

Please sign in to comment.