Coverage for tests/test_utils.py: 18%
53 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-19 02:14 -0800
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-19 02:14 -0800
1# This file is part of ap_association.
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 os
23import unittest
25import pandas as pd
27import lsst.daf.butler as dafButler
29from lsst.ap.association.utils import readSdmSchemaFile, make_empty_catalog, convertDataFrameToSdmSchema, \
30 getMidpointFromTimespan
31from utils_tests import makeExposure, makeRegionTime
34class TestUtils(unittest.TestCase):
36 def test_make_empty_catalog(self):
37 """Check that an empty catalog has the correct format.
38 """
39 schemaFile = os.path.join("${SDM_SCHEMAS_DIR}", "yml", "apdb.yaml")
40 schemaName = "ApdbSchema"
41 schema = readSdmSchemaFile(schemaFile, schemaName)
43 tableNames = ["DiaObject", "DiaSource", "DiaForcedSource"]
44 for tableName in tableNames:
45 emptyDiaObjects = make_empty_catalog(schema, tableName=tableName)
46 self.assertTrue(emptyDiaObjects.empty)
48 emptyColumns = set(emptyDiaObjects.columns)
49 self.assertIn("ra", emptyColumns)
50 self.assertIn("dec", emptyColumns)
51 self.assertIn("diaObjectId", emptyColumns)
53 emptyDf = pd.DataFrame(columns=["diaObjectId",])
54 emptyDf.set_index("diaObjectId")
55 convertedEmptyDiaObjects = convertDataFrameToSdmSchema(schema, emptyDf, tableName=tableName)
56 # TODO: we have no tests of convertDataFrameToSdmSchema, so it's dangerous to use it as an oracle.
57 emptyTypes = dict(zip(emptyDiaObjects.columns, emptyDiaObjects.dtypes))
58 convertedEmptyTypes = dict(zip(convertedEmptyDiaObjects.columns, convertedEmptyDiaObjects.dtypes))
59 self.assertEqual(emptyTypes, convertedEmptyTypes)
61 def test_regionTime_timespan(self):
62 """Check that the midpoint from a RegionTimeInfo matches the time from
63 the visitInfo of the exposure.
64 """
65 exposure = makeExposure()
66 regionTime = makeRegionTime(exposure)
67 visitTime = exposure.visitInfo.date.toAstropy()
68 midpoint = getMidpointFromTimespan(regionTime.timespan)
69 self.assertEqual(visitTime.jd, midpoint.jd)
71 def test_invalidTimespan(self):
72 """Test the error handling of getMidpointFromTimespan.
73 """
74 exposure = makeExposure()
75 visitTime = exposure.visitInfo.date.toAstropy()
76 timespan_no_end = dafButler.Timespan(begin=visitTime, end=dafButler.Timespan.EMPTY)
77 timespan_no_begin = dafButler.Timespan(begin=dafButler.Timespan.EMPTY, end=visitTime)
78 with self.assertRaises(ValueError):
79 getMidpointFromTimespan(timespan_no_end)
80 with self.assertRaises(ValueError):
81 getMidpointFromTimespan(timespan_no_begin)
83 timespan_none_end = dafButler.Timespan(begin=visitTime, end=None)
84 timespan_none_begin = dafButler.Timespan(begin=None, end=visitTime)
85 with self.assertRaises(ValueError):
86 getMidpointFromTimespan(timespan_none_end, allowUnbounded=False)
87 with self.assertRaises(ValueError):
88 getMidpointFromTimespan(timespan_none_begin, allowUnbounded=False)
89 self.assertEqual(getMidpointFromTimespan(timespan_none_begin).jd, visitTime.jd)
90 self.assertEqual(getMidpointFromTimespan(timespan_none_end).jd, visitTime.jd)
92 timespan_none_both = dafButler.Timespan(begin=None, end=None)
93 with self.assertRaises(ValueError):
94 getMidpointFromTimespan(timespan_none_both, allowUnbounded=True)
95 with self.assertRaises(ValueError):
96 getMidpointFromTimespan(timespan_none_both, allowUnbounded=False)