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 

26from scarlet_extensions.initialization.source import initSource 

27 

28import lsst.meas.extensions.scarlet as mes 

29import lsst.utils.tests 

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 = scarlet.Frame(shape, channels=np.arange(B)) 

60 observation = scarlet.Observation(images, channels=np.arange(B)) 

61 

62 # init stack objects 

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

64 # init source 

65 xmin = bbox.getMinX() 

66 ymin = bbox.getMinY() 

67 center = np.array([peak.getIy()-ymin, peak.getIx()-xmin], dtype=int) 

68 src = initSource(frame=frame, center=center, observation=observation, thresh=0, downgrade=False) 

69 

70 # scarlet has more flexible models now, 

71 # so `sed` and `morph` are no longer attributes, 

72 # meaning we have to extract them ourselves. 

73 sed = src.children[0].parameters[0]._data 

74 morph = src.children[1].parameters[0]._data 

75 

76 self.assertFloatsAlmostEqual(sed/3, trueSed) 

77 src_morph = np.zeros(frame.shape[1:], dtype=morph.dtype) 

78 src_morph[src._model_frame_slices[1:]] = (morph*3)[src._model_slices[1:]] 

79 self.assertFloatsAlmostEqual(src_morph, trueMorph, rtol=1e-7) 

80 self.assertFloatsEqual(src.center, center) 

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

82 

83 def test_to_heavy(self): 

84 shape = (5, 31, 55) 

85 B, Ny, Nx = shape 

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

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

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

89 images = images.astype(np.float32) 

90 seds = seds.astype(np.float32) 

91 

92 frame = scarlet.Frame(shape, psfs=targetPsfImage[None], channels=np.arange(B)) 

93 observation = scarlet.Observation(images, psfs=psfImages, channels=np.arange(B)).match(frame) 

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

95 xmin = bbox.getMinX() 

96 ymin = bbox.getMinY() 

97 center = np.array([peak.getIy()-ymin, peak.getIx()-xmin], dtype=int) 

98 src = initSource(frame=frame, center=center, observation=observation, thresh=0, downgrade=False) 

99 

100 # Convolve the model with the observed PSF 

101 model = src.get_model(frame=src.frame) 

102 model = observation.render(model) 

103 

104 # Test Model to Heavy 

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

106 src.detectedPeak = peak 

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

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

109 

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

111 self.assertFloatsAlmostEqual(hModel, model, rtol=1e-4, atol=1e-4) 

112 

113 # Test the peak in each band 

114 for single in hFoot: 

115 peaks = single.getPeaks() 

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

117 hPeak = peaks[0] 

118 self.assertEqual(hPeak.getIx()-xmin, coords[0][1]) 

119 self.assertEqual(hPeak.getIy()-ymin, coords[0][0]) 

120 

121 

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

123 pass 

124 

125 

126def setup_module(module): 

127 lsst.utils.tests.init() 

128 

129 

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

131 lsst.utils.tests.init() 

132 unittest.main()