Coverage for tests/test_nightReport.py: 22%
137 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-04-01 11:22 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-04-01 11:22 +0000
1# This file is part of summit_utils.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://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 <https://www.gnu.org/licenses/>.
22import unittest
23import tempfile
24import itertools
25import os
26import datetime
27from unittest import mock
28from numpy.random import shuffle
29from astro_metadata_translator import ObservationInfo
31import lsst.utils.tests
33import matplotlib as mpl
34mpl.use('Agg')
36from lsst.summit.utils.nightReport import NightReport, ColorAndMarker # noqa: E402
37import lsst.summit.utils.butlerUtils as butlerUtils # noqa: E402
40class NightReportTestCase(lsst.utils.tests.TestCase):
42 @classmethod
43 def setUpClass(cls):
44 try:
45 cls.butler = butlerUtils.makeDefaultLatissButler()
46 except FileNotFoundError:
47 raise unittest.SkipTest("Skipping tests that require the LATISS butler repo.")
49 cls.dayObs = 20200314 # has 377 images and data also exists on the TTS & summit
51 # Do the init in setUpClass because this takes about 35s for 20200314
52 cls.report = NightReport(cls.butler, cls.dayObs)
53 # number of images isn't necessarily the same as the number for the
54 # the dayObs in the registry becacuse of the test stands/summit
55 # having partial data, so get the number of images from the length
56 # of the scraped data. Not ideal, but best that can be done due to
57 # only having partial days in the test datasets.
58 cls.nImages = len(cls.report.data.keys())
59 cls.seqNums = list(cls.report.data.keys())
61 def test_saveAndLoad(self):
62 """Test that a NightReport can save itself, and be loaded back.
63 """
64 writeDir = tempfile.mkdtemp()
65 saveFile = os.path.join(writeDir, f'testNightReport_{self.dayObs}.pickle')
66 self.report.save(saveFile)
67 self.assertTrue(os.path.exists(saveFile))
69 loaded = NightReport(self.butler, self.dayObs, saveFile)
70 self.assertIsInstance(loaded, lsst.summit.utils.nightReport.NightReport)
71 self.assertGreaterEqual(len(loaded.data), 1)
72 self.assertEqual(loaded.dayObs, self.dayObs)
74 # TODO: add a self.assertRaises on a mismatched dayObs
76 def test_getSortedData(self):
77 """Test the _getSortedData returns the seqNums in order.
78 """
79 shuffledKeys = list(self.report.data.keys())
80 shuffle(shuffledKeys)
81 shuffledData = {k: self.report.data[k] for k in shuffledKeys}
83 sortedData = self.report._getSortedData(shuffledData)
84 sortedKeys = sorted(list(sortedData.keys()))
85 self.assertEqual(sortedKeys, list(self.report.data.keys()))
86 return
88 def test_getExpRecordDictForDayObs(self):
89 """Test getExpRecordDictForDayObs.
91 Test it returns a dict of dicts, keyed by integer seqNums.
92 """
93 expRecDict = self.report.getExpRecordDictForDayObs(self.dayObs)
94 self.assertIsInstance(expRecDict, dict)
95 self.assertGreaterEqual(len(expRecDict), 1)
97 # check all the keys are ints
98 seqNums = list(expRecDict.keys())
99 self.assertTrue(all(isinstance(s, int) for s in seqNums))
101 # check all the values are dicts
102 self.assertTrue(all(isinstance(expRecDict[s], dict) for s in seqNums))
103 return
105 def test_getObsInfoAndMetadataForSeqNum(self):
106 """Test that getObsInfoAndMetadataForSeqNum returns the correct types.
107 """
108 seqNum = self.seqNums[0]
109 obsInfo, md = self.report.getObsInfoAndMetadataForSeqNum(seqNum)
110 self.assertIsInstance(obsInfo, ObservationInfo)
111 self.assertIsInstance(md, dict)
112 return
114 def test_rebuild(self):
115 """Test that rebuild does nothing, as no data will be being added.
117 NB Do not call full=True on this, as it will double the length of the
118 tests and they're already extremely slow.
119 """
120 lenBefore = len(self.report.data)
121 self.report.rebuild()
122 self.assertEqual(len(self.report.data), lenBefore)
123 return
125 def test_getExposureMidpoint(self):
126 """Test the exposure midpoint calculation
127 """
128 # we would like a non-zero exptime exposure really
129 seqNumToUse = 0
130 for seqNum in self.report.data.keys():
131 expTime = self.report.data[seqNum]['exposure_time']
132 if expTime > 0:
133 seqNumToUse = seqNum
134 break
136 midPoint = self.report.getExposureMidpoint(seqNumToUse)
137 record = self.report.data[seqNumToUse]
139 if expTime == 0:
140 self.assertGreaterEqual(midPoint, record['datetime_begin'].to_datetime())
141 self.assertLessEqual(midPoint, record['datetime_end'].to_datetime())
142 else:
143 self.assertGreater(midPoint, record['datetime_begin'].to_datetime())
144 self.assertLess(midPoint, record['datetime_end'].to_datetime())
145 return
147 def test_getTimeDeltas(self):
148 """Test the time delta calculation returns a dict.
149 """
150 dts = self.report.getTimeDeltas()
151 self.assertIsInstance(dts, dict)
152 return
154 def test_makeStarColorAndMarkerMap(self):
155 """Test the color map maker returns a dict of ColorAndMarker objects.
156 """
157 cMap = self.report.makeStarColorAndMarkerMap(self.report.stars)
158 self.assertEqual(len(cMap), len(self.report.stars))
159 self.assertIsInstance(cMap, dict)
160 values = list(cMap.values())
161 self.assertTrue(all(isinstance(value, ColorAndMarker) for value in values))
162 return
164 def test_printObsTable(self):
165 """Test that a the printObsTable() method prints out the correct
166 number of lines.
167 """
168 with mock.patch('sys.stdout') as fake_stdout:
169 self.report.printObsTable()
171 # newline for each row plus header line, plus the line with dashes
172 self.assertEqual(len(fake_stdout.mock_calls), 2*(self.nImages + 2))
174 def test_plotPerObjectAirMass(self):
175 """Test that a the per-object airmass plots runs.
176 """
177 # We assume matplotlib is making plots, so just check that these
178 # don't crash.
180 # Default plotting:
181 self.report.plotPerObjectAirMass()
182 # plot with only one object as a str not a list of str
183 self.report.plotPerObjectAirMass(objects=self.report.stars[0])
184 # plot with first two objects as a list
185 self.report.plotPerObjectAirMass(objects=self.report.stars[0:2])
186 # flip y axis option
187 self.report.plotPerObjectAirMass(airmassOneAtTop=True)
188 # flip and select stars
189 self.report.plotPerObjectAirMass(objects=self.report.stars[0], airmassOneAtTop=True) # both
191 def test_makeAltAzCoveragePlot(self):
192 """Test that a the polar coverage plotting code runs.
193 """
194 # We assume matplotlib is making plots, so just check that these
195 # don't crash.
197 # test the default case
198 self.report.makeAltAzCoveragePlot()
199 # plot with only one object as a str not a list of str
200 self.report.makeAltAzCoveragePlot(objects=self.report.stars[0])
201 # plot with first two objects as a list
202 self.report.makeAltAzCoveragePlot(objects=self.report.stars[0:2])
203 # test turning lines off
204 self.report.makeAltAzCoveragePlot(objects=self.report.stars[0:2], withLines=False)
206 def test_calcShutterTimes(self):
207 timings = self.report.calcShutterTimes()
208 if not timings:
209 return # if the day has no on-sky observations, this returns None
210 efficiency = 100*(timings['scienceTimeTotal']/timings['nightLength'])
211 self.assertGreater(efficiency, 0)
212 self.assertLessEqual(efficiency, 100)
214 def test_getDatesForSeqNums(self):
215 dateTimeDict = self.report.getDatesForSeqNums()
216 self.assertIsInstance(dateTimeDict, dict)
217 self.assertTrue(all(isinstance(seqNum, int) for seqNum in dateTimeDict.keys()))
218 self.assertTrue(all(isinstance(seqNum, datetime.datetime) for seqNum in dateTimeDict.values()))
220 def test_doesNotRaise(self):
221 """Tests for things which are hard to test, so just make sure they run.
222 """
223 self.report.printShutterTimes()
224 for sample, includeRaw in itertools.product((True, False), (True, False)):
225 self.report.printAvailableKeys(sample=sample, includeRaw=includeRaw)
226 self.report.printObsTable()
227 for threshold, includeCalibs in itertools.product((0, 1, 10), (True, False)):
228 self.report.printObsGaps(threshold=threshold, includeCalibs=includeCalibs)
230 def test_internals(self):
231 startNum = self.report.getObservingStartSeqNum()
232 self.assertIsInstance(startNum, int)
233 self.assertGreater(startNum, 0) # the day starts at 1, so zero would be an error of some sort
235 starsFromGetter = self.report.getObservedObjects()
236 self.assertIsInstance(starsFromGetter, list)
237 self.assertSetEqual(set(starsFromGetter), set(self.report.stars))
239 starsFromGetter = self.report.getObservedObjects(ignoreTileNum=True)
240 self.assertLessEqual(len(starsFromGetter), len(self.report.stars))
242 # check the internal color map has the right number of items
243 self.assertEqual(len(self.report.cMap), len(starsFromGetter))
246class TestMemory(lsst.utils.tests.MemoryTestCase):
247 pass
250def setup_module(module):
251 lsst.utils.tests.init()
254if __name__ == "__main__": 254 ↛ 255line 254 didn't jump to line 255, because the condition on line 254 was never true
255 lsst.utils.tests.init()
256 unittest.main()