Coverage for tests/test_diaPipe.py: 34%
64 statements
« prev ^ index » next coverage.py v6.4.1, created at 2022-07-11 08:03 +0000
« prev ^ index » next coverage.py v6.4.1, created at 2022-07-11 08:03 +0000
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 unittest
23import numpy as np
24import pandas as pd
26import lsst.afw.image as afwImage
27import lsst.afw.table as afwTable
28import lsst.pipe.base as pipeBase
29import lsst.utils.tests
30from unittest.mock import patch, Mock, DEFAULT
32from lsst.ap.association import DiaPipelineTask
35class TestDiaPipelineTask(unittest.TestCase):
37 @classmethod
38 def _makeDefaultConfig(cls, doPackageAlerts=False):
39 config = DiaPipelineTask.ConfigClass()
40 config.apdb.db_url = "sqlite://"
41 config.apdb.isolation_level = "READ_UNCOMMITTED"
42 config.doPackageAlerts = doPackageAlerts
43 return config
45 def setUp(self):
46 # schemas are persisted in both Gen 2 and Gen 3 butler as prototypical catalogs
47 srcSchema = afwTable.SourceTable.makeMinimalSchema()
48 srcSchema.addField("base_PixelFlags_flag", type="Flag")
49 srcSchema.addField("base_PixelFlags_flag_offimage", type="Flag")
50 self.srcSchema = afwTable.SourceCatalog(srcSchema)
52 def tearDown(self):
53 pass
55 def testRunWithAlerts(self):
56 """Test running while creating and packaging alerts.
57 """
58 self._testRun(True)
60 def testRunWithoutAlerts(self):
61 """Test running without creating and packaging alerts.
62 """
63 self._testRun(False)
65 def _testRun(self, doPackageAlerts=False):
66 """Test the normal workflow of each ap_pipe step.
67 """
68 config = self._makeDefaultConfig(doPackageAlerts=doPackageAlerts)
69 task = DiaPipelineTask(config=config)
70 # Set DataFrame index testing to always return False. Mocks return
71 # true for this check otherwise.
72 task.testDataFrameIndex = lambda x: False
73 diffIm = Mock(spec=afwImage.ExposureF)
74 exposure = Mock(spec=afwImage.ExposureF)
75 template = Mock(spec=afwImage.ExposureF)
76 diaSrc = Mock(sepc=pd.DataFrame)
77 ccdExposureIdBits = 32
79 # Each of these subtasks should be called once during diaPipe
80 # execution. We use mocks here to check they are being executed
81 # appropriately.
82 subtasksToMock = [
83 "diaCatalogLoader",
84 "associator",
85 "diaCalculation",
86 "diaForcedSource",
87 ]
88 if doPackageAlerts:
89 subtasksToMock.append("alertPackager")
90 else:
91 self.assertFalse(hasattr(task, "alertPackager"))
93 # apdb isn't a subtask, but still needs to be mocked out for correct
94 # execution in the test environment.
95 with patch.multiple(
96 task, **{task: DEFAULT for task in subtasksToMock + ["apdb"]}
97 ):
98 result = task.run(diaSrc,
99 diffIm,
100 exposure,
101 template,
102 ccdExposureIdBits,
103 "g")
104 for subtaskName in subtasksToMock:
105 getattr(task, subtaskName).run.assert_called_once()
106 pipeBase.testUtils.assertValidOutput(task, result)
107 self.assertEqual(result.apdbMarker.db_url, "sqlite://")
108 self.assertEqual(result.apdbMarker.isolation_level,
109 "READ_UNCOMMITTED")
111 def test_createDiaObjects(self):
112 """Test that creating new DiaObjects works as expected.
113 """
114 nSources = 5
115 diaSources = pd.DataFrame(data=[
116 {"ra": 0.04*idx, "decl": 0.04*idx,
117 "diaSourceId": idx + 1 + nSources, "diaObjectId": 0,
118 "ssObjectId": 0}
119 for idx in range(nSources)])
121 config = self._makeDefaultConfig(doPackageAlerts=False)
122 task = DiaPipelineTask(config=config)
123 result = task.createNewDiaObjects(diaSources)
124 self.assertEqual(nSources, len(result.newDiaObjects))
125 self.assertTrue(np.all(np.equal(
126 result.diaSources["diaObjectId"].to_numpy(),
127 result.diaSources["diaSourceId"].to_numpy())))
128 self.assertTrue(np.all(np.equal(
129 result.newDiaObjects["diaObjectId"].to_numpy(),
130 result.diaSources["diaSourceId"].to_numpy())))
133class MemoryTester(lsst.utils.tests.MemoryTestCase):
134 pass
137def setup_module(module):
138 lsst.utils.tests.init()
141if __name__ == "__main__": 141 ↛ 142line 141 didn't jump to line 142, because the condition on line 141 was never true
142 lsst.utils.tests.init()
143 unittest.main()