Coverage for tests/testVisitGroupsMetric.py : 19%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import matplotlib
2matplotlib.use("Agg")
3import numpy as np
4import unittest
5import lsst.sims.maf.metrics as metrics
6import lsst.utils.tests
9class TestVisitGroupsMetric(unittest.TestCase):
11 def testPairFractionMetric(self):
12 metric = metrics.PairFractionMetric(mjdCol='mjd')
13 times = np.arange(0, 200, 30) / 60. / 24.
14 data = np.core.records.fromarrays([times], names='mjd')
15 # These should all have pairs
16 result = metric.run(data)
17 self.assertEqual(result, 1.)
19 # These should have none
20 times = np.arange(0, 400, 100) / 60. / 24.
21 data = np.core.records.fromarrays([times], names='mjd')
22 result = metric.run(data)
23 self.assertEqual(result, 0.)
25 # If those are right, then this should be 50%
26 t1 = np.arange(0, 200, 30) / 60. / 24.
27 t2 = np.arange(0, 400, 100) / 60. / 24. + 1000
29 times = np.append(t1, t2)
30 data = np.core.records.fromarrays([times], names='mjd')
31 result = metric.run(data)
32 expected = np.size(t1)/float(np.size(t1)+np.size(t2))
33 self.assertEqual(result, expected)
35 def testVisitGroups(self):
36 """Test visit groups (solar system groups) metric."""
37 # Set up some simple test data.
38 tmin = 15.0/60./24.0
39 tmax = 90./60./24.0
40 tstart = 49406.00
41 # The test data needs to have times for all observations: set up the times in two stages .. the night
42 night = np.array([0, 0, 0, 0, 0, 0,
43 1, 1,
44 2,
45 3, 3,
46 31, 31,
47 32, 32,
48 33, 33,
49 34, 34, 34, 34,
50 35, 35, 35,
51 36, 36, 36,
52 37, 37, 37,
53 38, 38, 38], 'int')
54 # .. and the time within the night.
55 expmjd = np.array([tstart, tstart+tmin/10.0, tstart+tmin+tmin/2.0, tstart+tmin*2+tmax,
56 tstart+2*tmax, tstart+2*tmax+tmin/2.0, # n0
57 tstart, tstart+tmax, # n1 .. only 2 obs but should make a pair
58 tstart, # n2 - only 1 obs, should not make a pair
59 tstart, tstart+tmin, # n3 .. only 2 obs but should make a pair
60 tstart, tstart+tmin, tstart, tstart+tmin,
61 tstart, tstart+tmin, # n31/32/33 - a pair on each night
62 tstart, tstart+tmin/10.0, tstart+tmax*2,
63 tstart+tmax*2+tmin/10.0, # n34 .. should make no pairs
64 tstart, tstart+tmin/10.0, tstart+tmax, # n35 should make 2.5 (too close at start)
65 # n36 should make 2.5 pairs (too close at end)
66 tstart, tstart+tmax, tstart+tmax+tmin/10.0,
67 tstart, tstart+tmin, tstart+tmax, # n37 - 3 (regular 3)
68 tstart, tstart+tmax, tstart+tmax*2 # n38 - 3 (three, but long spacing)
69 ], 'float')
70 expmjd = expmjd + night
71 testdata = np.core.records.fromarrays([expmjd, night], names=['expmjd', 'night'])
72 # Set up metric.
73 testmetric = metrics.VisitGroupsMetric(timeCol='expmjd', nightsCol='night',
74 deltaTmin=tmin, deltaTmax=tmax, minNVisits=2,
75 window=5, minNNights=3)
76 # and set up a copy, with a higher number of min visits per night
77 testmetric2 = metrics.VisitGroupsMetric(timeCol='expmjd', nightsCol='night',
78 deltaTmin=tmin, deltaTmax=tmax,
79 minNVisits=3, window=5, minNNights=3)
80 # Run metric for expected results.
81 metricval = testmetric.run(testdata)
82 # These are the expected results, based on the times above.
83 expected_nights = np.array([0, 1, 3, 31, 32, 33, 35, 36, 37, 38])
84 expected_numvisits = np.array([5.0, 2, 2, 2, 2, 2, 2.5, 2.5, 3, 3])
85 np.testing.assert_equal(metricval['visits'], expected_numvisits)
86 np.testing.assert_equal(metricval['nights'], expected_nights)
87 # Test reduce methods.
88 self.assertEqual(testmetric.reduceMedian(metricval), np.median(expected_numvisits))
89 self.assertEqual(testmetric.reduceNNightsWithNVisits(metricval), len(expected_nights))
90 self.assertEqual(testmetric2.reduceNNightsWithNVisits(metricval), 3)
91 self.assertEqual(testmetric.reduceNVisitsInWindow(metricval), 11)
92 self.assertEqual(testmetric2.reduceNNightsInWindow(metricval), 2)
93 self.assertEqual(testmetric.reduceNLunations(metricval), 2)
94 # Test with a longer (but simpler) date range.
95 indnight = np.array([0, 1, 2, 3, 4, 5, 31, 32, 33, 34, 61, 62, 63, 121, 122, 123], 'int')
96 indtimes = np.array([tstart, tstart+tmin, tstart+tmax], 'float')
97 expmjd = []
98 night = []
99 for n in indnight:
100 for t in indtimes:
101 expmjd.append(float(n + t))
102 night.append(n)
103 expmjd = np.array(expmjd)
104 night = np.array(night)
105 testdata = np.core.records.fromarrays([expmjd, night], names=['expmjd', 'night'])
106 metricval = testmetric.run(testdata)
107 self.assertEqual(testmetric.reduceNLunations(metricval), 4)
108 self.assertEqual(testmetric.reduceMaxSeqLunations(metricval), 3)
111class TestMemory(lsst.utils.tests.MemoryTestCase):
112 pass
115def setup_module(module):
116 lsst.utils.tests.init()
119if __name__ == "__main__": 119 ↛ 120line 119 didn't jump to line 120, because the condition on line 119 was never true
120 lsst.utils.tests.init()
121 unittest.main()