Coverage for tests/test_localBackground.py: 34%

75 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-02 06:51 -0800

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 

22import unittest 

23 

24import numpy as np 

25 

26import lsst.geom 

27import lsst.afw.image 

28import lsst.utils.tests 

29from lsst.meas.base import LocalBackgroundAlgorithm 

30from lsst.meas.base.tests import (AlgorithmTestCase, FluxTransformTestCase, 

31 SingleFramePluginTransformSetupHelper) 

32 

33 

34class LocalBackgroundTestCase(AlgorithmTestCase, lsst.utils.tests.TestCase): 

35 """Test case for the CircularApertureFlux algorithm/plugin. 

36 """ 

37 

38 def setUp(self): 

39 self.algName = "base_LocalBackground" 

40 self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), 

41 lsst.geom.Extent2I(100, 100)) 

42 self.dataset = lsst.meas.base.tests.TestDataset(self.bbox) 

43 self.dataset.addSource(100000.0, lsst.geom.Point2D(49.5, 49.5)) 

44 

45 self.bgValue = 1234.56789 

46 self.bgStdev = 12.3456789 

47 self.annulusInner = 5 

48 self.annulusOuter = 15 

49 psfSize = self.dataset.psfShape.getDeterminantRadius() 

50 self.numPix = np.pi*(self.annulusOuter**2 - self.annulusInner**2)*psfSize**2 

51 

52 def tearDown(self): 

53 del self.bbox 

54 del self.dataset 

55 

56 def checkCatalog(self, task, catalog, exposure, *args): 

57 exposure.maskedImage.image.array[:] += self.bgValue 

58 task.run(catalog, exposure, *args) 

59 self.assertEqual(len(catalog), 1) 

60 src = catalog[0] 

61 

62 # Check the background measurements 

63 bgValue = src.get(self.algName + "_instFlux") 

64 bgStdev = src.get(self.algName + "_instFluxErr") 

65 self.assertFalse(src.get(self.algName + "_flag")) 

66 self.assertFalse(src.get(self.algName + "_flag_noGoodPixels")) 

67 self.assertFalse(src.get(self.algName + "_flag_noPsf")) 

68 self.assertFloatsAlmostEqual(bgValue, self.bgValue, 

69 atol=3.0*self.bgStdev/np.sqrt(self.numPix)) # Within 3 sigma 

70 self.assertFloatsAlmostEqual(bgStdev, self.bgStdev, rtol=0.1) 

71 

72 # Check that the background value is useful, and it's what we'd get if 

73 # the background wasn't there. 

74 src = catalog[0] 

75 psfFlux = src.get("base_PsfFlux_instFlux") - \ 

76 src.get("base_PsfFlux_area")*src.get(self.algName + "_instFlux") 

77 exposure.maskedImage.image.array[:] -= self.bgValue 

78 task.run(catalog, exposure, *args) 

79 self.assertFloatsAlmostEqual(src.get("base_PsfFlux_instFlux"), psfFlux, rtol=3.0e-4) 

80 

81 def setConfig(self, config): 

82 config.plugins[self.algName].annulusInner = self.annulusInner 

83 config.plugins[self.algName].annulusOuter = self.annulusOuter 

84 config.plugins.names.add("base_PsfFlux") 

85 

86 def testSingleFramePlugin(self): 

87 config = self.makeSingleFrameMeasurementConfig(self.algName) 

88 self.setConfig(config) 

89 task = self.makeSingleFrameMeasurementTask(config=config) 

90 exposure, catalog = self.dataset.realize(self.bgStdev, task.schema, randomSeed=12345) 

91 self.checkCatalog(task, catalog, exposure) 

92 

93 def testForcedPlugin(self): 

94 config = self.makeForcedMeasurementConfig(self.algName) 

95 self.setConfig(config) 

96 task = self.makeForcedMeasurementTask(self.algName, config=config) 

97 measWcs = self.dataset.makePerturbedWcs(self.dataset.exposure.getWcs(), randomSeed=12345) 

98 measDataset = self.dataset.transform(measWcs) 

99 exposure, truthCatalog = measDataset.realize(self.bgStdev, measDataset.makeMinimalSchema(), 

100 randomSeed=12345) 

101 refCat = self.dataset.catalog 

102 refWcs = self.dataset.exposure.getWcs() 

103 measCat = task.generateMeasCat(exposure, refCat, refWcs) 

104 task.attachTransformedFootprints(measCat, refCat, exposure, refWcs) 

105 self.checkCatalog(task, measCat, exposure, refCat, refWcs) 

106 

107 

108class LocalBackgroundTransformTestCase(FluxTransformTestCase, SingleFramePluginTransformSetupHelper, 

109 lsst.utils.tests.TestCase): 

110 controlClass = lsst.meas.base.LocalBackgroundAlgorithm.Control 

111 algorithmClass = LocalBackgroundAlgorithm 

112 transformClass = lsst.meas.base.LocalBackgroundTransform 

113 flagNames = ('flag', 'flag_noGoodPixels') 

114 singleFramePlugins = ('base_LocalBackground',) 

115 forcedPlugins = ('base_LocalBackground',) 

116 

117 

118class TestMemory(lsst.utils.tests.MemoryTestCase): 

119 pass 

120 

121 

122def setup_module(module): 

123 lsst.utils.tests.init() 

124 

125 

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

127 lsst.utils.tests.init() 

128 unittest.main()