Coverage for tests/test_timespan.py: 9%

145 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-04-14 09:22 +0000

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 itertools 

23import unittest 

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 

37from lsst.daf.butler.core.time_utils import TimeConverter 

38 

39 

40class TimespanTestCase(unittest.TestCase): 

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

42 

43 Test coverage for the `TimespanDatabaseRepresentation` classes is handled 

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

45 """ 

46 

47 def setUp(self): 

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

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

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

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

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

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

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

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

56 

57 def testEmpty(self): 

58 """Test various ways to construct an empty timespan, and that 

59 operations on empty timespans yield the expected behavior. 

60 """ 

61 self.assertEqual( 

62 Timespan.makeEmpty(), 

63 Timespan(Timespan.EMPTY, Timespan.EMPTY), 

64 ) 

65 self.assertEqual( 

66 Timespan.makeEmpty(), 

67 Timespan(self.timestamps[1], self.timestamps[0]), 

68 ) 

69 self.assertEqual( 

70 Timespan.makeEmpty(), 

71 Timespan(Timespan.EMPTY, self.timestamps[0]), 

72 ) 

73 self.assertEqual( 

74 Timespan.makeEmpty(), 

75 Timespan(self.timestamps[0], Timespan.EMPTY), 

76 ) 

77 self.assertEqual( 

78 Timespan.makeEmpty(), Timespan(self.timestamps[0], self.timestamps[0], padInstantaneous=False) 

79 ) 

80 empty = Timespan.makeEmpty() 

81 for t in self.timestamps: 

82 with self.subTest(t=str(t)): 

83 self.assertFalse(empty < t) 

84 self.assertFalse(empty > t) 

85 self.assertFalse(t < empty) 

86 self.assertFalse(t > empty) 

87 self.assertFalse(empty.contains(t)) 

88 for t in self.timespans: 

89 with self.subTest(t=str(t)): 

90 self.assertTrue(t.contains(empty)) 

91 self.assertFalse(t.overlaps(empty)) 

92 self.assertFalse(empty.overlaps(t)) 

93 self.assertEqual(empty.contains(t), t.isEmpty()) 

94 self.assertFalse(empty < t) 

95 self.assertFalse(t < empty) 

96 self.assertFalse(empty > t) 

97 self.assertFalse(t > empty) 

98 

99 def testFromInstant(self): 

100 """Test construction of instantaneous timespans.""" 

101 self.assertEqual( 

102 Timespan.fromInstant(self.timestamps[0]), Timespan(self.timestamps[0], self.timestamps[0]) 

103 ) 

104 

105 def testInvalid(self): 

106 """Test that we reject timespans that should not exist.""" 

107 with self.assertRaises(ValueError): 

108 Timespan(TimeConverter().max_time, None) 

109 with self.assertRaises(ValueError): 

110 Timespan(TimeConverter().max_time, TimeConverter().max_time) 

111 with self.assertRaises(ValueError): 

112 Timespan(None, TimeConverter().epoch) 

113 with self.assertRaises(ValueError): 

114 Timespan(TimeConverter().epoch, TimeConverter().epoch) 

115 t = TimeConverter().nsec_to_astropy(TimeConverter().max_nsec - 1) 

116 with self.assertRaises(ValueError): 

117 Timespan(t, t) 

118 with self.assertRaises(ValueError): 

119 Timespan.fromInstant(t) 

120 

121 def testStrings(self): 

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

123 round-tripping. 

124 """ 

125 for ts in self.timespans: 

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

127 # important test: human inspection. 

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

129 if ts.isEmpty(): 

130 self.assertEqual("(empty)", str(ts)) 

131 else: 

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

133 if ts.begin is None: 

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

135 else: 

136 self.assertTrue(str(ts).startswith(f"[{ts.begin.tai.isot}, ")) 

137 if ts.end is None: 

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

139 else: 

140 self.assertTrue(str(ts).endswith(f", {ts.end.tai.isot})")) 

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

142 

143 def testOperationConsistency(self): 

144 """Test that overlaps, contains, intersection, and difference are 

145 consistent. 

146 """ 

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

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

149 c1 = a.intersection(b) 

150 c2 = b.intersection(a) 

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

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

153 if a == b: 

154 self.assertFalse(diffs1) 

155 self.assertFalse(diffs2) 

156 self.assertTrue(a.contains(b)) 

157 self.assertTrue(b.contains(a)) 

158 if a.contains(b): 

159 self.assertTrue(a.overlaps(b) or b.isEmpty()) 

160 self.assertFalse(diffs2) 

161 if b.contains(a): 

162 self.assertTrue(b.overlaps(a) or a.isEmpty()) 

163 self.assertFalse(diffs1) 

164 if diffs1 is not None: 

165 for t in diffs1: 

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

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

168 if diffs2 is not None: 

169 for t in diffs2: 

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

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

172 self.assertEqual(c1, c2) 

173 if a.overlaps(b): 

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

175 self.assertFalse(c1.isEmpty()) 

176 else: 

177 self.assertTrue(a < b or a > b or a.isEmpty() or b.isEmpty()) 

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

179 self.assertTrue(c1.isEmpty()) 

180 if diffs1 is not None: 

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

182 if diffs2 is not None: 

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

184 

185 def testPrecision(self): 

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

187 ts1 = self.timespans[-1] 

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

189 self.assertEqual(ts1, ts2) 

190 

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

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

193 

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

195 self.assertNotEqual(ts1, ts2) 

196 

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

198 self.assertNotEqual(ts1, ts2) 

199 

200 t1 = Timespan( 

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

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

203 ) 

204 t2 = Timespan( 

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

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

207 ) 

208 self.assertEqual(t1, t2) 

209 

210 # Ensure that == and != work properly 

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

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

213 

214 def testTimescales(self): 

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

216 ts1 = Timespan( 

217 begin=astropy.time.Time("2013-06-17 13:34:45.775000", scale="tai", format="iso"), 

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

219 ) 

220 ts2 = Timespan( 

221 begin=astropy.time.Time("2013-06-17T13:34:10.775", scale="utc", format="isot"), 

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

223 ) 

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

225 

226 def testFuture(self): 

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

228 

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

230 # so hide these expected warnings from the test output 

231 with warnings.catch_warnings(): 

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

233 if erfa is not None: 

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

235 ts1 = Timespan( 

236 begin=astropy.time.Time(self.timestamps[0], scale="utc", format="iso"), 

237 end=astropy.time.Time("2099-06-17 13:35:17.947000", scale="utc", format="iso"), 

238 ) 

239 ts2 = Timespan( 

240 begin=astropy.time.Time(self.timestamps[0], scale="utc", format="iso"), 

241 end=astropy.time.Time("2099-06-17 13:35:17.947000", scale="utc", format="iso"), 

242 ) 

243 

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

245 # trigger our own warning and count all the warnings 

246 with self.assertWarns(Warning) as cm: 

247 self.assertEqual(ts1, ts2) 

248 warnings.warn("deliberate") 

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

250 

251 def testJson(self): 

252 ts1 = Timespan( 

253 begin=astropy.time.Time("2013-06-17 13:34:45.775000", scale="tai", format="iso"), 

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

255 ) 

256 json_str = ts1.to_json() 

257 ts_json = Timespan.from_json(json_str) 

258 self.assertEqual(ts_json, ts1) 

259 

260 

261if __name__ == "__main__": 

262 unittest.main()