Coverage for tests/test_loadDiaCatalogs.py: 31%
65 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-19 05:12 -0700
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-19 05:12 -0700
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 numpy as np
24import tempfile
25import unittest
26import yaml
28from lsst.ap.association import LoadDiaCatalogsTask, LoadDiaCatalogsConfig
29from lsst.dax.apdb import Apdb, ApdbSql, ApdbTables
30from lsst.utils import getPackageDir
31import lsst.utils.tests
32from utils_tests import makeExposure, makeDiaObjects, makeDiaSources, makeDiaForcedSources
35def _data_file_name(basename, module_name):
36 """Return path name of a data file.
38 Parameters
39 ----------
40 basename : `str`
41 Name of the file to add to the path string.
42 module_name : `str`
43 Name of lsst stack package environment variable.
45 Returns
46 -------
47 data_file_path : `str`
48 Full path of the file to load from the "data" directory in a given
49 repository.
50 """
51 return os.path.join(getPackageDir(module_name), "data", basename)
54class TestLoadDiaCatalogs(unittest.TestCase):
56 def setUp(self):
57 np.random.seed(1234)
59 self.db_file_fd, self.db_file = tempfile.mkstemp(
60 dir=os.path.dirname(__file__))
61 self.addCleanup(os.remove, self.db_file)
62 self.addCleanup(os.close, self.db_file_fd)
64 self.apdbConfig = ApdbSql.init_database(db_url="sqlite:///" + self.db_file)
65 self.apdb = Apdb.from_config(self.apdbConfig)
67 self.exposure = makeExposure(False, False)
69 self.diaObjects = makeDiaObjects(20, self.exposure)
70 self.diaSources = makeDiaSources(
71 100,
72 self.diaObjects["diaObjectId"].to_numpy(),
73 self.exposure)
74 self.diaForcedSources = makeDiaForcedSources(
75 200,
76 self.diaObjects["diaObjectId"].to_numpy(),
77 self.exposure)
79 self.dateTime = self.exposure.visitInfo.date
80 self.apdb.store(self.dateTime.toAstropy(),
81 self.diaObjects,
82 self.diaSources,
83 self.diaForcedSources)
85 # These columns are not in the DPDD, yet do appear in DiaSource.yaml.
86 # We don't need to check them against the default APDB schema.
87 self.ignoreColumns = ["band", "bboxSize", "isDipole"]
89 def testRun(self):
90 """Test the full run method for the loader.
91 """
92 diaLoader = LoadDiaCatalogsTask()
93 result = diaLoader.run(self.exposure, self.apdb)
95 self.assertEqual(len(result.diaObjects), len(self.diaObjects))
96 self.assertEqual(len(result.diaSources), len(self.diaSources))
97 self.assertEqual(len(result.diaForcedSources),
98 len(self.diaForcedSources))
100 def testLoadDiaObjects(self):
101 """Test that the correct number of diaObjects are loaded.
102 """
103 diaLoader = LoadDiaCatalogsTask()
104 region = diaLoader._getRegion(self.exposure)
105 diaObjects = diaLoader.loadDiaObjects(region,
106 self.apdb)
107 self.assertEqual(len(diaObjects), len(self.diaObjects))
109 def testLoadDiaForcedSources(self):
110 """Test that the correct number of diaForcedSources are loaded.
111 """
112 diaLoader = LoadDiaCatalogsTask()
113 region = diaLoader._getRegion(self.exposure)
114 diaForcedSources = diaLoader.loadDiaForcedSources(
115 self.diaObjects,
116 region,
117 self.dateTime,
118 self.apdb)
119 self.assertEqual(len(diaForcedSources), len(self.diaForcedSources))
121 def testLoadDiaSources(self):
122 """Test that the correct number of diaSources are loaded.
124 Also check that they can be properly loaded both by location and
125 ``diaObjectId``.
126 """
127 diaConfig = LoadDiaCatalogsConfig()
128 diaLoader = LoadDiaCatalogsTask(config=diaConfig)
130 region = diaLoader._getRegion(self.exposure)
131 diaSources = diaLoader.loadDiaSources(self.diaObjects,
132 region,
133 self.dateTime,
134 self.apdb)
135 self.assertEqual(len(diaSources), len(self.diaSources))
137 def test_apdbSchema(self):
138 """Test that the default DiaSource schema from dax_apdb agrees with the
139 column names defined here in ap_association/data/DiaSource.yaml.
140 """
141 tableDef = self.apdb.tableDef(ApdbTables.DiaSource)
142 apdbSchemaColumns = [column.name for column in tableDef.columns]
144 functorFile = _data_file_name("DiaSource.yaml", "ap_association")
145 with open(functorFile) as yaml_stream:
146 diaSourceFunctor = yaml.safe_load_all(yaml_stream)
147 for functor in diaSourceFunctor:
148 diaSourceColumns = [column for column in list(functor['funcs'].keys())
149 if column not in self.ignoreColumns]
150 self.assertLess(set(diaSourceColumns), set(apdbSchemaColumns))
153class MemoryTester(lsst.utils.tests.MemoryTestCase):
154 pass
157def setup_module(module):
158 lsst.utils.tests.init()
161if __name__ == "__main__": 161 ↛ 162line 161 didn't jump to line 162, because the condition on line 161 was never true
162 lsst.utils.tests.init()
163 unittest.main()