Coverage for tests / test_RBTransiNetTask.py: 31%

66 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-01 08:28 +0000

1# This file is part of meas_transiNet. 

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 

23import numpy as np 

24 

25import lsst.afw.table 

26from lsst.geom import Point2I, Point2D, Box2I 

27import lsst.meas.base.tests 

28import lsst.utils.tests 

29 

30from lsst.meas.transiNet import RBTransiNetTask 

31 

32 

33class TestRBTransiNetTask(lsst.utils.tests.TestCase): 

34 def create_sample_datasets(self): 

35 bbox = Box2I(Point2I(0, 0), Point2I(400, 400)) 

36 dataset = lsst.meas.base.tests.TestDataset(bbox) 

37 dataset.addSource(5000, Point2D(50, 50.)) 

38 # TODO: make one of these centered in a different corner of the pixel, 

39 # to test that the cutout is properly centered. 

40 dataset.addSource(10000, Point2D(100, 50.)) 

41 dataset.addSource(20000, Point2D(1, 1)) # close-to-border source 

42 self.exposure, self.catalog = dataset.realize(10.0, dataset.makeMinimalSchema()) 

43 

44 def setUp(self): 

45 self.create_sample_datasets() 

46 

47 # Task configuration 

48 self.config = RBTransiNetTask.ConfigClass() 

49 self.config.modelPackageName = "dummy" 

50 self.config.modelPackageStorageMode = "local" 

51 self.config.cutoutSize = 256 

52 

53 def test_make_cutouts(self): 

54 task = RBTransiNetTask(config=self.config) 

55 for record in self.catalog: 

56 result = task._make_cutouts(self.exposure, self.exposure, self.exposure, record) 

57 self._check_cutout(result.science, task.config.cutoutSize) 

58 self._check_cutout(result.template, task.config.cutoutSize) 

59 self._check_cutout(result.difference, task.config.cutoutSize) 

60 

61 if record.getX() == 1 and record.getY() == 1: # This is the "border"-source 

62 self._check_empty_cutout(result.science) 

63 self._check_empty_cutout(result.template) 

64 self._check_empty_cutout(result.difference) 

65 

66 def _check_cutout(self, image, size): 

67 """Test that the image cutout was made correctly. 

68 

69 Parameters 

70 ---------- 

71 image : `np.ndarray` (N, 2) 

72 Square cutout made from image. 

73 size : `int` 

74 Expected size of the cutout. 

75 """ 

76 self.assertEqual(image.shape, (size, size)) 

77 return 

78 

79 # TODO: below test should be removed/fixed -- disabled for now. 

80 # It only passes with very specific cutout dimensions and in 

81 # very specific configurations. 

82 # See https://jira.lsstcorp.org/browse/DM-35635 for more info. 

83 

84 max_index = np.unravel_index(image.argmax(), image.shape) 

85 # TODO: I'm not comfortable with this particular test: the exact 

86 # position of the max pixel depends on where in that pixel the 

87 # centroid is. We can assume Box2I.makeCenteredBox works correctly... 

88 self.assertEqual(max_index, ((size/2)-1, (size/2)-1)) 

89 

90 def _check_empty_cutout(self, cutout): 

91 """Test that the cutout is empty. 

92 

93 Parameters 

94 ---------- 

95 cutout : `np.ndarray` (N, 2) 

96 Square cutout made from image. 

97 """ 

98 np.testing.assert_array_equal(cutout, np.zeros_like(cutout)) 

99 

100 def test_run(self): 

101 """Test that run passes an appropriate object to the interface. 

102 """ 

103 task = RBTransiNetTask(config=self.config) 

104 result = task.run(self.exposure, self.exposure, self.exposure, self.catalog) 

105 self.assertIsInstance(result.classifications, lsst.afw.table.BaseCatalog) 

106 np.testing.assert_array_equal(self.catalog["id"], result.classifications["id"]) 

107 

108 def test_config_butlerblock(self): 

109 config = RBTransiNetTask.ConfigClass() 

110 config.modelPackageName = "dummy" 

111 config.modelPackageStorageMode = "butler" 

112 with self.assertRaises(ValueError): 

113 config.validate() 

114 

115 def test_config_butlerpass_none(self): 

116 config = RBTransiNetTask.ConfigClass() 

117 config.modelPackageName = None 

118 config.modelPackageStorageMode = "butler" 

119 # Should not raise 

120 config.validate() 

121 

122 def test_config_butlerblock_empty(self): 

123 config = RBTransiNetTask.ConfigClass() 

124 config.modelPackageName = "" # Want *only* None for butler packages 

125 config.modelPackageStorageMode = "butler" 

126 with self.assertRaises(ValueError): 

127 config.validate() 

128 

129 

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

131 pass 

132 

133 

134def setup_module(module): 

135 lsst.utils.tests.init() 

136 

137 

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

139 lsst.utils.tests.init() 

140 unittest.main()