Coverage for tests/test_forcedPhot.py: 38%

54 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-10-02 08:15 +0000

1# This file is part of meas_base. 

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/>. 

21 

22"""Tests of the various forced photometry tasks. 

23 

24These tests primarily confirm that their respective Tasks can be configured and 

25run without errors, but do not check anything about their algorithmic quality. 

26""" 

27 

28import unittest 

29 

30import numpy as np 

31 

32import lsst.afw.image 

33from lsst.afw.math import ChebyshevBoundedField 

34from lsst.afw.table import CoordKey 

35from lsst.meas.base import ForcedPhotCcdTask, ForcedPhotCcdFromDataFrameTask 

36import lsst.meas.base.tests 

37import lsst.utils.tests 

38 

39skyCenter = lsst.geom.SpherePoint(245.0, -45.0, lsst.geom.degrees) 

40 

41 

42class ForcedPhotometryTests: 

43 """Base class for tests of forced photometry tasks. 

44 

45 Creates a simple test image and catalog to run forced photometry on. 

46 """ 

47 def setUp(self): 

48 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Point2I(100, 100)) 

49 dataset = lsst.meas.base.tests.TestDataset(bbox, crval=skyCenter) 

50 dataset.addSource(instFlux=1000, centroid=lsst.geom.Point2D(30, 30)) 

51 dataset.addSource(instFlux=10000, centroid=lsst.geom.Point2D(60, 70)) 

52 

53 schema = dataset.makeMinimalSchema() 

54 # Add coordinate error fields needed by updateSourceCoords below: 

55 CoordKey.addErrorFields(schema) 

56 self.exposure, self.refCat = dataset.realize(noise=10, schema=schema) 

57 lsst.afw.table.updateSourceCoords(self.exposure.wcs, self.refCat) 

58 # Simple aperture correction map in case the task needs it. 

59 apCorrMap = lsst.afw.image.ApCorrMap() 

60 apCorrMap["base_PsfFlux_instFlux"] = ChebyshevBoundedField(bbox, np.array([[2.0]])) 

61 apCorrMap["base_PsfFlux_instFluxErr"] = ChebyshevBoundedField(bbox, np.array([[3.0]])) 

62 self.exposure.info.setApCorrMap(apCorrMap) 

63 

64 # Offset WCS so that the forced coordinates don't match the truth. 

65 self.offsetWcs = dataset.makePerturbedWcs(self.exposure.wcs) 

66 

67 

68class ForcedPhotCcdTaskTestCase(ForcedPhotometryTests, lsst.utils.tests.TestCase): 

69 def testRun(self): 

70 config = ForcedPhotCcdTask.ConfigClass() 

71 task = ForcedPhotCcdTask(refSchema=self.refCat.schema, config=config) 

72 measCat = task.measurement.generateMeasCat(self.exposure, self.refCat, self.exposure.wcs) 

73 

74 task.run(measCat, self.exposure, self.refCat, self.offsetWcs) 

75 

76 # Check that something was measured. 

77 self.assertTrue(np.isfinite(measCat["base_TransformedCentroid_x"]).all()) 

78 self.assertTrue(np.isfinite(measCat["base_TransformedCentroid_y"]).all()) 

79 self.assertTrue(np.isfinite(measCat["base_PsfFlux_instFlux"]).all()) 

80 # We use an offset WCS, so the transformed centroids should not exactly 

81 # match the original positions. 

82 self.assertFloatsNotEqual(measCat["base_TransformedCentroid_x"], self.refCat['truth_x']) 

83 self.assertFloatsNotEqual(measCat["base_TransformedCentroid_y"], self.refCat['truth_y']) 

84 

85 

86class ForcedPhotCcdFromDataFrameTaskTestCase(ForcedPhotometryTests, lsst.utils.tests.TestCase): 

87 def testRun(self): 

88 """Testing run() for this task ignores the dataframe->SourceCatalog 

89 conversion that happens in runQuantum, but that should be tested 

90 separately. 

91 """ 

92 config = ForcedPhotCcdFromDataFrameTask.ConfigClass() 

93 task = ForcedPhotCcdFromDataFrameTask(refSchema=self.refCat.schema, config=config) 

94 measCat = task.measurement.generateMeasCat(self.exposure, self.refCat, self.exposure.wcs) 

95 

96 task.run(measCat, self.exposure, self.refCat, self.offsetWcs) 

97 

98 # Check that something was measured. 

99 self.assertTrue(np.isfinite(measCat["base_TransformedCentroidFromCoord_x"]).all()) 

100 self.assertTrue(np.isfinite(measCat["base_TransformedCentroidFromCoord_y"]).all()) 

101 self.assertTrue(np.isfinite(measCat["base_PsfFlux_instFlux"]).all()) 

102 # We use an offset WCS, so the transformed centroids should not exactly 

103 # match the original positions. 

104 self.assertFloatsNotEqual(measCat["base_TransformedCentroidFromCoord_x"], self.refCat['truth_x']) 

105 self.assertFloatsNotEqual(measCat["base_TransformedCentroidFromCoord_y"], self.refCat['truth_y']) 

106 

107 

108class MemoryTester(lsst.utils.tests.MemoryTestCase): 

109 pass 

110 

111 

112def setup_module(module): 

113 lsst.utils.tests.init() 

114 

115 

116if __name__ == "__main__": 116 ↛ 117line 116 didn't jump to line 117, because the condition on line 116 was never true

117 lsst.utils.tests.init() 

118 unittest.main()