Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of meas_extensions_scarlet. 

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 

25import scarlet 

26 

27import lsst.meas.extensions.scarlet as mes 

28import lsst.utils.tests 

29from lsst.afw.detection import PeakTable, multiband 

30 

31from utils import numpyToStack, initData 

32 

33 

34class TestLsstSource(lsst.utils.tests.TestCase): 

35 def test_init(self): 

36 # Initialize the model 

37 shape = (5, 31, 55) 

38 B, Ny, Nx = shape 

39 

40 x = np.linspace(-2, 2, 5) 

41 y = np.linspace(-2, 2, 5) 

42 x, y = np.meshgrid(x, y) 

43 r = np.sqrt(x**2 + y**2) 

44 

45 trueSed = np.arange(B) 

46 trueMorph = np.zeros(shape[1:]) 

47 

48 center = (np.array(trueMorph.shape) - 1) // 2 

49 cy, cx = center 

50 trueMorph[cy-2:cy+3, cx-2:cx+3] = 3-r 

51 

52 morph = trueMorph.copy() 

53 # Make a point that is not monotonic or symmetric to ensure 

54 # that it is supressed. 

55 morph[5, 3] = 10 

56 

57 # Create the scarlet objects 

58 images = trueSed[:, None, None] * morph[None, :, :] 

59 frame = mes.LsstFrame(shape) 

60 observation = mes.LsstObservation(images) 

61 

62 # init stack objects 

63 foot, peak, bbox = numpyToStack(images, center, (15, 3)) 

64 # init source 

65 src = mes.source.initSource(frame=frame, peak=peak, observation=observation, bbox=bbox, thresh=0) 

66 

67 self.assertFloatsAlmostEqual(src.sed/3, trueSed) 

68 self.assertFloatsAlmostEqual(src.morph*3, trueMorph, rtol=1e-7) 

69 self.assertEqual(src.detectedPeak, peak) 

70 self.assertEqual(foot.getBBox(), bbox) 

71 

72 def test_to_heavy(self): 

73 shape = (5, 31, 55) 

74 B, Ny, Nx = shape 

75 coords = [(20, 10), (10, 30), (17, 42)] 

76 result = initData(shape, coords, [3, 2, 1]) 

77 targetPsfImage, psfImages, images, channels, seds, morphs, targetPsf, psfs = result 

78 images = images.astype(np.float32) 

79 seds = seds.astype(np.float32) 

80 

81 frame = mes.LsstFrame(shape, psfs=targetPsfImage[None]) 

82 observation = mes.LsstObservation(images, psfs=psfImages).match(frame) 

83 foot, peak, bbox = numpyToStack(images, coords[0], (15, 3)) 

84 src = mes.source.initSource(frame=frame, peak=peak, observation=observation, bbox=bbox, thresh=0) 

85 # Get the HeavyFootprint 

86 peakSchema = PeakTable.makeMinimalSchema() 

87 hFoot = mes.morphToHeavy(src, peakSchema=peakSchema) 

88 hBBox = hFoot.getBBox() 

89 

90 hMorph = multiband.heavyFootprintToImage(hFoot, fill=0).image.array 

91 sBbox = scarlet.bbox.Box.from_data(src.morph) 

92 self.assertFloatsAlmostEqual(hMorph, sBbox.extract_from(src.morph.astype(np.float32))) 

93 self.assertEqual(hBBox.getMinX(), sBbox.start[-1]) 

94 self.assertEqual(hBBox.getMinY(), sBbox.start[-2]) 

95 self.assertEqual(hBBox.getMaxX(), sBbox.stop[-1]-1) 

96 self.assertEqual(hBBox.getMaxY(), sBbox.stop[-2]-1) 

97 

98 peaks = hFoot.getPeaks() 

99 self.assertEqual(len(peaks), 1) 

100 hPeak = peaks[0] 

101 self.assertEqual(hPeak.getIx(), coords[0][1]) 

102 self.assertEqual(hPeak.getIy(), coords[0][0]) 

103 

104 # Test Model to Heavy 

105 filters = [f for f in "grizy"] 

106 hFoot = mes.modelToHeavy(src, filters, bbox.getMin(), observation) 

107 hModel = hFoot.getImage(fill=0).image.array 

108 

109 self.assertEqual(bbox, hFoot.getBBox()) 

110 self.assertFloatsAlmostEqual(hModel, observation.render(src.get_model()), rtol=1e-4, atol=1e-4) 

111 

112 

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

114 pass 

115 

116 

117def setup_module(module): 

118 lsst.utils.tests.init() 

119 

120 

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

122 lsst.utils.tests.init() 

123 unittest.main()