Skip to content

Commit

Permalink
Merge pull request #379 from lsst/u/lynnej/fix_test
Browse files Browse the repository at this point in the history
  • Loading branch information
rhiannonlynne authored Nov 2, 2023
2 parents cbecfc0 + a66de69 commit 9a5eb91
Show file tree
Hide file tree
Showing 34 changed files with 234 additions and 199 deletions.
4 changes: 2 additions & 2 deletions tests/maf/test_3x2fom.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def setUp(self):
"Skipping 3x3 metric test because no dust maps.",
)
def test_3x2(self):
# Only testing that the metric successfully runs, not checking that the
# output values are valid.
# Only testing that the metric successfully runs, not checking that
# the output values are valid.
bundle_list = []
nside = 64
colmap = maf.batches.col_map_dict("fbs")
Expand Down
9 changes: 4 additions & 5 deletions tests/maf/test_archive.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# imports
import os
import sys
import unittest
from os import path
Expand Down Expand Up @@ -128,12 +127,12 @@ def test_get_family_descriptions(self):

def test_describe_families(self):
if "IPython" in sys.modules:
with patch("IPython.display.display_markdown") as display_markdown, patch(
"IPython.display.HTML"
) as display_html, patch("IPython.display") as ip_display:
with patch("IPython.display.display_markdown") as _, patch("IPython.display.HTML") as _, patch(
"IPython.display"
) as _:
self.perform_describe_families_test()
else:
with patch("builtins.print") as print_mock:
with patch("builtins.print") as _:
self.perform_describe_families_test()

def perform_describe_families_test(self):
Expand Down
10 changes: 5 additions & 5 deletions tests/maf/test_basemetrics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import matplotlib

matplotlib.use("Agg")
import unittest

import rubin_sim.maf.metrics as metrics
Expand All @@ -13,7 +10,9 @@ def test_reduce_dict(self):
self.assertEqual(list(testmetric.reduce_funcs.keys()), [])

def test_metric_name(self):
"""Test that metric name is set appropriately automatically and explicitly"""
"""Test that metric name is set appropriately automatically
and when explicitly passed.
"""
# Test automatic setting of metric name
testmetric = metrics.BaseMetric("testcol")
self.assertEqual(testmetric.name, "Base testcol")
Expand All @@ -33,7 +32,8 @@ def test_col_registry(self):
colset = set()
colset.add(cols)
testmetric = metrics.BaseMetric(cols)
# Class registry should have dictionary with values = set of columns for metric class
# Class registry should have dictionary with values =
# set of columns for metric class
for item in colset:
self.assertIn(item, testmetric.col_registry.col_set)
cols = ["onecolumn", "twocolumn"]
Expand Down
38 changes: 29 additions & 9 deletions tests/maf/test_batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,31 @@ def setUp(self):
)
def testload_them_all(self):
ack = batches.altazHealpix()
assert ack is not None
ack = batches.altazLambert()
assert ack is not None
ack = batches.standard_summary()
assert ack is not None
ack = batches.standard_metrics("night")
assert ack is not None
ack = batches.filtersPerNight()
assert ack is not None
ack = batches.filtersWholeSurvey()
assert ack is not None
ack = batches.glanceBatch()
assert ack is not None
ack = batches.hourglassPlots()
assert ack is not None
ack = batches.slewBasics()
assert ack is not None
ack = batches.timeGaps()
assert ack is not None
ack = batches.metadataBasics("airmass")
assert ack is not None
ack = batches.metadataBasicsAngle("rotskyPos")
assert ack is not None
ack = batches.metadataMaps("fiveSigmaDepth")
assert ack is not None

@unittest.skipUnless(
os.path.isdir(os.path.join(get_data_dir(), "maf")),
Expand All @@ -47,9 +60,13 @@ def testload_them_all(self):
def test_moving_objects_batches(self):
slicer = MoObjSlicer()
ack = batches.quick_discovery_batch(slicer)
assert ack is not None
ack = batches.discovery_batch(slicer)
assert ack is not None
ack = batches.characterization_inner_batch(slicer)
assert ack is not None
ack = batches.characterization_outer_batch(slicer)
assert ack is not None

def test_moving_fractions(self):
bdict = {}
Expand All @@ -58,24 +75,27 @@ def test_moving_fractions(self):
bdict["days"] = bundle
bdict["days"].slicer = MoObjSlicer()
bdict["days"].slicer.slice_points["H"] = np.arange(50)
# Should probably fill in some data as well to make sure
# more parts of the code get exercised, but this is a start.
ack = batches.run_completeness_summary(bdict, 0.0, [10, 20], None, None)
ack = batches.run_fraction_summary(bdict, 0.0, None, None)
ack = batches.plot_fractions(bdict, out_dir=self.out_dir)
# A mystery why this batch takes a bundle while the rest take dicts.
ack = batches.plot_single(bundle, out_dir=self.out_dir)
ack = batches.plot_activity(bdict)
# This is a start, but the methods below aren't really doing anything
# without appropriately named/values bundles in the bdict.
batches.run_completeness_summary(bdict, 0.0, [10, 20], None, None)
batches.run_fraction_summary(bdict, 0.0, None, None)
batches.plot_fractions(bdict, out_dir=self.out_dir)
# This batch takes a single bundle because it plots that single
# bundle with multiple summary interpretations of the metric
batches.plot_single(bundle, out_dir=self.out_dir)
batches.plot_activity(bdict)

@unittest.skipUnless(
os.path.isdir(os.path.join(get_data_dir(), "maf")),
"Skipping scienceRadarBatch test because operating without full MAF test data",
)
def test_science_radar(self):
# Loading the science radar batch requires reading a significant set of input files
# Loading the science radar batch requires reading a significant
# set of input files
# This test is skipped if running with the lighter set of test data.
# batch requires reading a lot of input files for lightcurves
ack = batches.science_radar_batch()
assert ack is not None

@unittest.skipUnless(
os.path.isdir(os.path.join(get_data_dir(), "maf")),
Expand Down
15 changes: 8 additions & 7 deletions tests/maf/test_cadencemetrics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import matplotlib

matplotlib.use("Agg")
import unittest

import numpy as np
Expand Down Expand Up @@ -101,7 +98,8 @@ def test_general_uniformity_metric(self):
np.testing.assert_almost_equal(result3, 0.0)
# Test setup with max_value > 1
metric = metrics.GeneralUniformityMetric(col="observationStartMJD", min_value=0, max_value=10)
# Make a perfectly uniform dist covering limited range (0-1 out of 0-10 range)
# Make a perfectly uniform dist covering limited range
# (0-1 out of 0-10 range)
data["observationStartMJD"] = np.arange(0.0, 1 + step / 2, step)
result5 = metric.run(data, slice_point)
self.assertEqual(result5, 0.9)
Expand Down Expand Up @@ -163,7 +161,8 @@ def test_t_gaps_percent_metric(self):
data["observationStartMJD"] = [1, 2, 3, 4]
metric = metrics.TgapsPercentMetric(min_time=0.5, max_time=1.5, all_gaps=True)
result3 = metric.run(data)
# This should be 50% -- 3 gaps of 1 day, 2 gaps of 2 days, 1 gap of 3 days
# This should be 50% -- 3 gaps of 1 day, 2 gaps of 2 days, and
# 1 gap of 3 days
self.assertEqual(result3, 50)

def test_night_gap_metric(self):
Expand Down Expand Up @@ -248,7 +247,8 @@ def test_rapid_revisit_uniformity_metric(self):
for i in range(10000):
dtimes = rng.rand(100)
data['observationStartMJD'] = dtimes.cumsum()
metric = metrics.RapidRevisitUniformityMetric(d_tmin=0.1, d_tmax=0.8, min_nvisits=50)
metric = metrics.RapidRevisitUniformityMetric(d_tmin=0.1,
d_tmax=0.8, min_nvisits=50)
result = metric.run(data)
resmin = np.min([resmin, result])
resmax = np.max([resmax, result])
Expand Down Expand Up @@ -314,7 +314,8 @@ def test_transient_metric(self):
metric = metrics.TransientMetric(survey_duration=ndata / 365.25 * 2)
self.assertEqual(metric.run(data_slice), 0.5)

# Set half of the m5 of the observations very bright, so kill another half.
# Set half of the m5 of the observations very bright,
# so kill another half.
data_slice["fiveSigmaDepth"][0:50] = 20
self.assertEqual(metric.run(data_slice), 0.25)

Expand Down
19 changes: 10 additions & 9 deletions tests/maf/test_calibrationmetrics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import matplotlib

matplotlib.use("Agg")
import unittest

import numpy as np
Expand Down Expand Up @@ -50,8 +47,9 @@ def test_parallax_metric(self):
worse4 = metrics.ParallaxMetric(normalize=flag, rmag=22.0, seeing_col="finSeeing").run(
data[0:300], slice_point
)
# Make sure the RMS increases as seeing increases, the star gets fainter,
# the background gets brighter, or the baseline decreases.
# Make sure the RMS increases as seeing increases,
# the star gets fainter, the background gets brighter,
# or the baseline decreases.
if flag:
pass
else:
Expand Down Expand Up @@ -102,10 +100,12 @@ def test_proper_motion_metric(self):
worse4 = metrics.ProperMotionMetric(normalize=flag, rmag=22.0, seeing_col="finSeeing").run(
data[0:300], slice_point
)
# Make sure the RMS increases as seeing increases, the star gets fainter,
# the background gets brighter, or the baseline decreases.
# Make sure the RMS increases as seeing increases,
# the star gets fainter, the background gets brighter,
# or the baseline decreases.
if flag:
# When normalized, mag of star and m5 don't matter (just scheduling).
# When normalized, mag of star and m5 don't matter
# (just scheduling).
self.assertAlmostEqual(worse2, worse1)
self.assertAlmostEqual(worse4, worse3)
# But using fewer points should make proper motion worse.
Expand Down Expand Up @@ -204,7 +204,8 @@ def test_parallax_dcr_degen_metric(self):
val = metric.run(data)
np.testing.assert_almost_equal(val, 0.0, decimal=2)

# Generate a random distribution that should have little or no correlation
# Generate a random distribution that should have
# little or no correlation
rng = np.random.RandomState(42)

data["ra_pi_amp"] = rng.rand(100) * 2 - 1.0
Expand Down
39 changes: 24 additions & 15 deletions tests/maf/test_healpixslicer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import matplotlib

matplotlib.use("Agg")
import os
import unittest

Expand Down Expand Up @@ -37,7 +34,8 @@ def make_data_values(
datavalues = datavalues[randind]
datavalues = np.array(list(zip(datavalues)), dtype=[("testdata", "float")])
data.append(datavalues)
# Generate RA/Dec values equally spaces on sphere between ramin/max, decmin/max.
# Generate RA/Dec values equally spaces on sphere between
# ramin/max, decmin/max.
ra = np.arange(0, size, dtype="float")
ra *= (float(ramax) - float(ramin)) / (ra.max() - ra.min())
randorder = rng.rand(size)
Expand Down Expand Up @@ -65,7 +63,8 @@ def make_data_values(

def calc_dist_vincenty(ra1, dec1, ra2, dec2):
"""Calculates distance on a sphere using the Vincenty formula.
Give this function RA/Dec values in radians. Returns angular distance(s), in radians.
Give this function RA/Dec values in radians.
Returns angular distance(s), in radians.
Note that since this is all numpy, you could input arrays of RA/Decs."""
d1 = (np.cos(dec2) * np.sin(ra2 - ra1)) ** 2 + (
np.cos(dec1) * np.sin(dec2) - np.sin(dec1) * np.cos(dec2) * np.cos(ra2 - ra1)
Expand All @@ -87,7 +86,8 @@ def test_slicertype(self):
self.assertEqual(testslicer.slicer_name, "HealpixSlicer")

def test_nsides_nbins(self):
"""Test that number of sides passed to slicer produces expected number of bins."""
"""Test that number of sides passed to slicer produces expected
number of bins."""
nsides = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
npixx = [12, 48, 192, 768, 3072, 12288, 49152, 196608, 786432, 3145728]
for nside, npix in zip(nsides, npixx):
Expand Down Expand Up @@ -129,8 +129,10 @@ def tearDown(self):
self.testslicer = None

def test_slicer_equivalence(self):
"""Test that slicers are marked equal when appropriate, and unequal when appropriate."""
# Note that they are judged equal based on nsides (not on data in ra/dec spatial tree).
"""Test that slicers are marked equal when appropriate,
and unequal when appropriate."""
# Note that they are judged equal based on nsides
# (not on data in ra/dec spatial tree).
testslicer2 = HealpixSlicer(
nside=self.nside,
verbose=False,
Expand Down Expand Up @@ -180,7 +182,8 @@ def tearDown(self):
self.testslicer = None

def test_iteration(self):
"""Test iteration goes through expected range and ra/dec are in expected range (radians)."""
"""Test iteration goes through expected range and ra/dec
are in expected range (radians)."""
npix = hp.nside2npix(self.nside)
for i, s in enumerate(self.testslicer):
self.assertEqual(i, s["slice_point"]["sid"])
Expand All @@ -201,7 +204,8 @@ def test_get_item(self):


class TestHealpixSlicerSlicing(unittest.TestCase):
# Note that this is really testing baseSpatialSlicer, as slicing is done there for healpix grid
# Note that this is really testing baseSpatialSlicer,
# as slicing is done there for healpix grid

def setUp(self):
self.camera_footprint_file = os.path.join(get_data_dir(), "tests", "fov_map.npz")
Expand Down Expand Up @@ -233,7 +237,8 @@ def tearDown(self):
self.testslicer = None

def test_slicing(self):
"""Test slicing returns (all) data points which are within 'radius' of bin point."""
"""Test slicing returns (all) data points which are within
'radius' of bin point."""
# Test that slicing fails before setup_slicer
self.assertRaises(NotImplementedError, self.testslicer._slice_sim_data, 0)
# Set up and test actual slicing.
Expand All @@ -252,7 +257,8 @@ def test_slicing(self):


class TestHealpixChipGap(unittest.TestCase):
# Note that this is really testing baseSpatialSlicer, as slicing is done there for healpix grid
# Note that this is really testing baseSpatialSlicer,
# as slicing is done there for healpix grid

def setUp(self):
self.camera_footprint_file = os.path.join(get_data_dir(), "tests", "fov_map.npz")
Expand Down Expand Up @@ -285,18 +291,21 @@ def tearDown(self):
self.testslicer = None

def test_slicing(self):
"""Test slicing returns (most) data points which are within 'radius' of bin point."""
"""Test slicing returns (most) data points which are
within 'radius' of bin point."""
# Test that slicing fails before setup_slicer
self.assertRaises(NotImplementedError, self.testslicer._slice_sim_data, 0)
# Set up and test actual slicing.
self.testslicer.setup_slicer(self.dv)
for s in self.testslicer:
ra = s["slice_point"]["ra"]
dec = s["slice_point"]["dec"]
# Find the points of 'dv' which are within self.radius of this slice_point
# Find the points of 'dv' which are within self.radius of
# this slice_point
distances = calc_dist_vincenty(ra, dec, self.dv["ra"], self.dv["dec"])
didxs = np.where(distances <= np.radians(self.radius))
# find the indexes of dv which the slicer says are in the camera footprint
# find the indexes of dv which the slicer says are in the
# camera footprint
sidxs = s["idxs"]
self.assertLessEqual(len(sidxs), len(didxs[0]))
if len(sidxs) > 0:
Expand Down
Loading

0 comments on commit 9a5eb91

Please sign in to comment.