Coverage for tests/test_hips.py: 20%
92 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-28 04:54 -0700
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-28 04:54 -0700
1# This file is part of pipe_tasks.
2#
3# LSST Data Management System
4# This product includes software developed by the
5# LSST Project (http://www.lsst.org/).
6# See COPYRIGHT file at the top of the source tree.
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <https://www.lsstcorp.org/LegalNotices/>.
21#
22"""Test HIPS code."""
23import unittest
24import numpy as np
25import hpgeom as hpg
27import lsst.utils.tests
28import lsst.pipe.base
29import lsst.afw.image
30import lsst.skymap
31import lsst.geom
33from lsst.pipe.tasks.hips import (
34 HighResolutionHipsTask,
35 HighResolutionHipsConfig,
36 HighResolutionHipsConnections
37)
40class HipsTestCase(unittest.TestCase):
41 def test_hips_single(self):
42 """Test creating a single HIPS image."""
43 np.random.seed(12345)
45 config = HighResolutionHipsConfig()
47 skymap = self._make_skymap()
49 tract = 9597
50 patch = 50
51 tract_info = skymap[tract]
52 patch_info = tract_info[patch]
54 exposure = self._make_noise_exposure(patch_info)
55 handles = [lsst.pipe.base.InMemoryDatasetHandle(exposure)]
57 center = patch_info.wcs.pixelToSky(patch_info.inner_bbox.getCenter())
58 pixel = self._get_pixel(2**config.hips_order, center)
60 hips_task = HighResolutionHipsTask(config=config)
61 output = hips_task.run([pixel], handles)
63 # Check that all the pixels are filled.
64 npix = (np.isfinite(output.hips_exposures[pixel].image.array.ravel()).sum())
65 self.assertEqual(npix, output.hips_exposures[pixel].image.array.size)
67 # Check that metadata is correct
68 self.assertEqual(output.hips_exposures[pixel].getPhotoCalib(), exposure.getPhotoCalib())
69 self.assertEqual(output.hips_exposures[pixel].getFilter(), exposure.getFilter())
71 def test_hips_double(self):
72 """Test creating a HIPS image from two neighboring patches."""
73 np.random.seed(12345)
75 config = HighResolutionHipsConfig()
77 skymap = self._make_skymap()
79 tract = 9597
80 patches = [50, 51]
81 tract_info = skymap[tract]
83 handles = []
84 centers = []
85 for patch in patches:
86 patch_info = tract_info[patch]
87 exposure = self._make_noise_exposure(patch_info)
88 handles.append(lsst.pipe.base.InMemoryDatasetHandle(exposure))
89 centers.append(patch_info.wcs.pixelToSky(patch_info.inner_bbox.getCenter()))
91 center = lsst.geom.SpherePoint(
92 (centers[0].getRa().asDegrees() + centers[1].getRa().asDegrees())/2.*lsst.geom.degrees,
93 (centers[0].getDec().asDegrees() + centers[1].getDec().asDegrees())/2.*lsst.geom.degrees
94 )
95 pixel = self._get_pixel(2**config.hips_order, center)
97 # Just transform one, make sure it falls off the edge.
98 hips_task = HighResolutionHipsTask(config=config)
99 output = hips_task.run([pixel], [handles[0]])
101 # Check that not all the pixels are filled.
102 npix = (np.isfinite(output.hips_exposures[pixel].image.array.ravel()).sum())
103 self.assertLess(npix, output.hips_exposures[pixel].image.array.size)
105 # Transform both.
106 hips_task = HighResolutionHipsTask(config=config)
107 output = hips_task.run([pixel], handles)
109 # Check that all the pixels are filled.
110 npix = (np.isfinite(output.hips_exposures[pixel].image.array.ravel()).sum())
111 self.assertEqual(npix, output.hips_exposures[pixel].image.array.size)
113 def test_hips_none(self):
114 """Test making a HIPS image with no overlapping inputs."""
115 np.random.seed(12345)
117 config = HighResolutionHipsConfig()
119 skymap = self._make_skymap()
121 tract = 9597
122 patch = 50
123 tract_info = skymap[tract]
124 patch_info = tract_info[patch]
126 exposure = self._make_noise_exposure(patch_info)
127 handles = [lsst.pipe.base.InMemoryDatasetHandle(exposure)]
129 pixel = 0
131 hips_task = HighResolutionHipsTask(config=config)
132 output = hips_task.run([pixel], handles)
134 # Check that there is no returned image
135 self.assertEqual(len(output.hips_exposures), 0)
137 def test_hips_connections(self):
138 """Test that the HIPS connections validate properly."""
139 config = HighResolutionHipsConfig()
141 # Test that the connections validate
142 _ = HighResolutionHipsConnections(config=config)
144 # Test that changing hips_order will break things because of the
145 # dimensions mismatch.
146 config.hips_order = 5
147 with self.assertRaises(ValueError):
148 _ = HighResolutionHipsConnections(config=config)
150 # I'd like to change the dimensions but I don't know how to do that.
152 def _make_noise_exposure(self, patch_info):
153 """Make a simple noise exposure.
155 Parameters
156 ----------
157 patch_info : `lsst.skymap.PatchInfo`
158 Patch info to use to make the exposure.
160 Returns
161 -------
162 exposure : `lsst.afw.image.ExposureF`
163 Noise exposure.
164 """
165 exposure = lsst.afw.image.ExposureF(patch_info.outer_bbox)
166 exposure.image.array[:, :] = np.random.normal(scale=1.0, size=exposure.image.array.shape)
167 exposure.setWcs(patch_info.wcs)
168 exposure.setPhotoCalib(lsst.afw.image.PhotoCalib(calibrationMean=1.0))
169 exposure.setFilter(lsst.afw.image.FilterLabel(band='i'))
171 return exposure
173 def _make_skymap(self):
174 """Make a testing skymap.
176 Returns
177 -------
178 skymap : `lsst.skymap.RingsSkyMap`
179 """
181 # Generate a skymap
182 skymap_config = lsst.skymap.ringsSkyMap.RingsSkyMapConfig()
183 skymap_config.numRings = 120
184 skymap_config.projection = "TAN"
185 skymap_config.tractOverlap = 1.0/60
186 skymap_config.pixelScale = 0.168
187 return lsst.skymap.ringsSkyMap.RingsSkyMap(skymap_config)
189 def _get_pixel(self, nside, sphpoint):
190 """Get the pixel value from a spherepoint.
192 Parameters
193 ----------
194 nside : `int`
195 Healpix nside
196 sphpoint : `lsst.geom.SpherePoint`
197 Point to compute pixel value.
199 Returns
200 -------
201 pixel : `int`
202 Healpix pixel (nest ordering)
203 """
204 pixel = hpg.angle_to_pixel(
205 nside,
206 sphpoint.getRa().asDegrees(),
207 sphpoint.getDec().asDegrees(),
208 )
209 return pixel
212if __name__ == "__main__": 212 ↛ 213line 212 didn't jump to line 213, because the condition on line 212 was never true
213 unittest.main()