Coverage for tests/test_RBTransiNetTask.py: 31%

51 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-04 10:22 +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 setUp(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 self.config = RBTransiNetTask.ConfigClass() 

45 self.config.modelPackageName = "dummy" 

46 self.config.modelPackageStorageMode = "local" 

47 

48 def test_make_cutouts(self): 

49 task = RBTransiNetTask(config=self.config) 

50 for record in self.catalog: 

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

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

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

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

55 

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

57 self._check_empty_cutout(result.science) 

58 self._check_empty_cutout(result.template) 

59 self._check_empty_cutout(result.difference) 

60 

61 def _check_cutout(self, image, size): 

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

63 

64 Parameters 

65 ---------- 

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

67 Square cutout made from image. 

68 size : `int` 

69 Expected size of the cutout. 

70 """ 

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

72 return 

73 

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

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

76 # very specific configurations. 

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

78 

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

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

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

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

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

84 

85 def _check_empty_cutout(self, cutout): 

86 """Test that the cutout is empty. 

87 

88 Parameters 

89 ---------- 

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

91 Square cutout made from image. 

92 """ 

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

94 

95 def test_run(self): 

96 """Test that run passes an appropriate object to the interface, 

97 mocking the interface infer step so we don't need to use pytorch. 

98 """ 

99 scores = np.array([0.0, 1.0, 0.0]) 

100 task = RBTransiNetTask(config=self.config) 

101 task.interface.infer = unittest.mock.Mock(task.interface.infer) 

102 with unittest.mock.patch.object(task.interface, "infer") as mock_infer: 

103 mock_infer.return_value = scores 

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 np.testing.assert_array_equal(scores, result.classifications["score"]) 

108 

109 

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

111 pass 

112 

113 

114def setup_module(module): 

115 lsst.utils.tests.init() 

116 

117 

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

119 lsst.utils.tests.init() 

120 unittest.main()