Coverage for tests/test_scarlet.py : 18%

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/>.
22import unittest
24import numpy as np
25import scarlet
27import lsst.meas.extensions.scarlet as mes
28import lsst.utils.tests
29from lsst.afw.detection import PeakTable, multiband
31from utils import numpyToStack, initData
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
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)
45 trueSed = np.arange(B)
46 trueMorph = np.zeros(shape[1:])
48 center = (np.array(trueMorph.shape) - 1) // 2
49 cy, cx = center
50 trueMorph[cy-2:cy+3, cx-2:cx+3] = 3-r
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
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))
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 = mes.source.initSource(frame=frame, center=center, observation=observation, thresh=0)
70 self.assertFloatsAlmostEqual(src.sed/3, trueSed)
71 src_morph = np.zeros(frame.shape[1:], dtype=src.morph.dtype)
72 src_morph[src.model_frame_slices[1:]] = (src.morph*3)[src.model_slices[1:]]
73 self.assertFloatsAlmostEqual(src_morph, trueMorph, rtol=1e-7)
74 self.assertFloatsEqual(src.center, center)
75 self.assertEqual(foot.getBBox(), bbox)
77 def test_to_heavy(self):
78 shape = (5, 31, 55)
79 B, Ny, Nx = shape
80 coords = [(20, 10), (10, 30), (17, 42)]
81 result = initData(shape, coords, [3, 2, 1])
82 targetPsfImage, psfImages, images, channels, seds, morphs, targetPsf, psfs = result
83 images = images.astype(np.float32)
84 seds = seds.astype(np.float32)
86 frame = scarlet.Frame(shape, psfs=targetPsfImage[None], channels=np.arange(B))
87 observation = scarlet.Observation(images, psfs=psfImages, channels=np.arange(B)).match(frame)
88 foot, peak, bbox = numpyToStack(images, coords[0], (15, 3))
89 xmin = bbox.getMinX()
90 ymin = bbox.getMinY()
91 center = np.array([peak.getIy()-ymin, peak.getIx()-xmin], dtype=int)
92 src = mes.source.initSource(frame=frame, center=center, observation=observation, thresh=0)
93 # Get the HeavyFootprint
94 peakSchema = PeakTable.makeMinimalSchema()
95 hFoot = mes.source.morphToHeavy(src, peakSchema=peakSchema)
96 hBBox = hFoot.getBBox()
98 hMorph = multiband.heavyFootprintToImage(hFoot, fill=0).image.array
99 sBbox = scarlet.bbox.Box.from_data(src.morph)
100 self.assertFloatsAlmostEqual(hMorph, sBbox.extract_from(src.morph.astype(np.float32)))
101 self.assertEqual(hBBox.getMinX(), sBbox.start[-1])
102 self.assertEqual(hBBox.getMinY(), sBbox.start[-2])
103 self.assertEqual(hBBox.getMaxX(), sBbox.stop[-1]-1)
104 self.assertEqual(hBBox.getMaxY(), sBbox.stop[-2]-1)
106 peaks = hFoot.getPeaks()
107 self.assertEqual(len(peaks), 1)
108 hPeak = peaks[0]
109 self.assertEqual(hPeak.getIx(), coords[0][1])
110 self.assertEqual(hPeak.getIy(), coords[0][0])
112 # Convolve the model with the observed PSF
113 model = src.get_model(frame=src.model_frame)
114 model = observation.render(model)
116 # Test Model to Heavy
117 filters = [f for f in "grizy"]
118 src.detectedPeak = peak
119 hFoot = mes.source.modelToHeavy(src, filters, bbox.getMin(), observation)
120 hModel = hFoot.getImage(fill=0).image.array
122 self.assertEqual(bbox, hFoot.getBBox())
123 self.assertFloatsAlmostEqual(hModel, model, rtol=1e-4, atol=1e-4)
126class MemoryTester(lsst.utils.tests.MemoryTestCase):
127 pass
130def setup_module(module):
131 lsst.utils.tests.init()
134if __name__ == "__main__": 134 ↛ 135line 134 didn't jump to line 135, because the condition on line 134 was never true
135 lsst.utils.tests.init()
136 unittest.main()