Hide keyboard shortcuts

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

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22import unittest 

23import itertools 

24import warnings 

25 

26import astropy.time 

27import astropy.utils.exceptions 

28 

29# As of astropy 4.2, the erfa interface is shipped independently and 

30# ErfaWarning is no longer an AstropyWarning 

31try: 

32 import erfa 

33except ImportError: 

34 erfa = None 

35 

36from lsst.daf.butler import Timespan 

37 

38 

39class TimespanTestCase(unittest.TestCase): 

40 """Tests for the `Timespan` class. 

41 

42 Test coverage for the `TimespanDatabaseRepresentation` classes is handled 

43 by the tests for `Database` and its subclasses. 

44 """ 

45 

46 def setUp(self): 

47 start = astropy.time.Time('2020-01-01T00:00:00', format="isot", scale="tai") 

48 offset = astropy.time.TimeDelta(60, format="sec") 

49 self.timestamps = [start + offset*n for n in range(3)] 

50 self.timespans = [Timespan(begin=None, end=None)] 

51 self.timespans.extend(Timespan(begin=None, end=t) for t in self.timestamps) 

52 self.timespans.extend(Timespan(begin=t, end=None) for t in self.timestamps) 

53 self.timespans.extend(Timespan(begin=a, end=b) for a, b in itertools.combinations(self.timestamps, 2)) 

54 

55 def testStrings(self): 

56 """Test __str__ against expected values and __repr__ with eval 

57 round-tripping. 

58 """ 

59 for ts in self.timespans: 

60 # Uncomment the next line and run this test directly for the most 

61 # important test: human inspection. 

62 # print(str(ts), repr(ts)) 

63 self.assertIn(", ", str(ts)) 

64 self.assertTrue(str(ts).endswith(")")) 

65 if ts.begin is None: 

66 self.assertTrue(str(ts).startswith("(-∞, ")) 

67 else: 

68 self.assertTrue(str(ts).startswith(f"[{ts.begin}, ")) 

69 if ts.end is None: 

70 self.assertTrue(str(ts).endswith(", ∞)")) 

71 else: 

72 self.assertTrue(str(ts).endswith(f", {ts.end})")) 

73 self.assertEqual(eval(repr(ts)), ts) 

74 

75 def testOperationConsistency(self): 

76 """Test that overlaps, intersection, and difference are consistent. 

77 """ 

78 for a, b in itertools.combinations_with_replacement(self.timespans, 2): 

79 with self.subTest(a=str(a), b=str(b)): 

80 c1 = a.intersection(b) 

81 c2 = b.intersection(a) 

82 diffs1 = tuple(a.difference(b)) 

83 diffs2 = tuple(b.difference(a)) 

84 if a == b: 

85 self.assertFalse(diffs1) 

86 self.assertFalse(diffs2) 

87 else: 

88 for t in diffs1: 

89 self.assertTrue(a.overlaps(t)) 

90 self.assertFalse(b.overlaps(t)) 

91 for t in diffs2: 

92 self.assertTrue(b.overlaps(t)) 

93 self.assertFalse(a.overlaps(t)) 

94 self.assertEqual(c1, c2) 

95 if a.overlaps(b): 

96 self.assertTrue(b.overlaps(a)) 

97 self.assertIsNotNone(c1) 

98 else: 

99 self.assertFalse(b.overlaps(a)) 

100 self.assertIsNone(c1) 

101 self.assertEqual(diffs1, (a,)) 

102 self.assertEqual(diffs2, (b,)) 

103 

104 def testPrecision(self): 

105 """Test that we only use nanosecond precision for equality.""" 

106 ts1 = self.timespans[-1] 

107 ts2 = Timespan(begin=ts1.begin + astropy.time.TimeDelta(1e-10, format="sec"), end=ts1.end) 

108 self.assertEqual(ts1, ts2) 

109 

110 self.assertEqual(Timespan(begin=None, end=None), Timespan(begin=None, end=None)) 

111 self.assertEqual(Timespan(begin=None, end=ts1.end), Timespan(begin=None, end=ts1.end)) 

112 

113 ts2 = Timespan(begin=ts1.begin + astropy.time.TimeDelta(1e-8, format="sec"), end=ts1.end) 

114 self.assertNotEqual(ts1, ts2) 

115 

116 ts2 = Timespan(begin=None, end=ts1.end) 

117 self.assertNotEqual(ts1, ts2) 

118 

119 t1 = Timespan(begin=astropy.time.Time(2456461.0, val2=0.06580758101851847, format="jd", scale="tai"), 

120 end=astropy.time.Time(2456461.0, val2=0.06617994212962963, format="jd", scale="tai")) 

121 t2 = Timespan(begin=astropy.time.Time(2456461.0, val2=0.06580758101851858, format="jd", scale="tai"), 

122 end=astropy.time.Time(2456461.0, val2=0.06617994212962963, format="jd", scale="tai")) 

123 self.assertEqual(t1, t2) 

124 

125 # Ensure that == and != work properly 

126 self.assertTrue(t1 == t2, f"Equality of {t1} and {t2}") 

127 self.assertFalse(t1 != t2, f"Check != is false for {t1} and {t2}") 

128 

129 def testTimescales(self): 

130 """Test time scale conversion occurs on comparison.""" 

131 ts1 = Timespan(begin=astropy.time.Time('2013-06-17 13:34:45.775000', scale='tai', format='iso'), 

132 end=astropy.time.Time('2013-06-17 13:35:17.947000', scale='tai', format='iso')) 

133 ts2 = Timespan(begin=astropy.time.Time('2013-06-17T13:34:10.775', scale='utc', format='isot'), 

134 end=astropy.time.Time('2013-06-17T13:34:42.947', scale='utc', format='isot')) 

135 self.assertEqual(ts1, ts2, f"Compare {ts1} with {ts2}") 

136 

137 def testFuture(self): 

138 """Check that we do not get warnings from future dates.""" 

139 

140 # Astropy will give "dubious year" for UTC five years in the future 

141 # so hide these expected warnings from the test output 

142 with warnings.catch_warnings(): 

143 warnings.simplefilter("ignore", category=astropy.utils.exceptions.AstropyWarning) 

144 if erfa is not None: 

145 warnings.simplefilter("ignore", category=erfa.ErfaWarning) 

146 ts1 = Timespan(begin=astropy.time.Time('2213-06-17 13:34:45.775000', scale='utc', format='iso'), 

147 end=astropy.time.Time('2213-06-17 13:35:17.947000', scale='utc', format='iso')) 

148 ts2 = Timespan(begin=astropy.time.Time('2213-06-17 13:34:45.775000', scale='utc', format='iso'), 

149 end=astropy.time.Time('2213-06-17 13:35:17.947000', scale='utc', format='iso')) 

150 

151 # unittest can't test for no warnings so we run the test and 

152 # trigger our own warning and count all the warnings 

153 with self.assertWarns(Warning) as cm: 

154 self.assertEqual(ts1, ts2) 

155 warnings.warn("deliberate") 

156 self.assertEqual(str(cm.warning), "deliberate") 

157 

158 

159if __name__ == "__main__": 159 ↛ 160line 159 didn't jump to line 160, because the condition on line 159 was never true

160 unittest.main()