Coverage for tests / test_imageCutoutsBackend.py: 26%

56 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-28 09:20 +0000

1# This file is part of dax_images_cutout. 

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 os.path 

23import tempfile 

24import unittest 

25 

26import astropy.io.fits 

27 

28import lsst.afw.image 

29import lsst.daf.butler 

30import lsst.geom 

31import lsst.resources 

32import lsst.utils.tests 

33from lsst.dax.images.cutout import CutoutMode, ImageCutoutFactory, projection_finders, stencils 

34 

35 

36class TestImageCutoutsBackend(lsst.utils.tests.TestCase): 

37 """Tests for ImageCutoutsBackend.""" 

38 

39 @classmethod 

40 def setUpClass(cls): 

41 try: 

42 cls.data_dir = lsst.utils.getPackageDir("testdata_image_cutouts") 

43 except LookupError: 

44 raise unittest.SkipTest("testdata_image_cutouts not setup.") from None 

45 

46 def setUp(self): 

47 collection = "2.2i/runs/test-med-1/w_2022_03/DM-33223/20220118T193330Z" 

48 self.butler = lsst.daf.butler.Butler(os.path.join(self.data_dir, "repo"), collections=collection) 

49 

50 # Centered on a galaxy 

51 point = lsst.geom.SpherePoint(56.6400770 * lsst.geom.degrees, -36.4492250 * lsst.geom.degrees) 

52 radius = 10 * lsst.geom.arcseconds 

53 self.stencil = stencils.SkyCircle(point, radius) 

54 

55 self.projectionFinders = ( 

56 projection_finders.ReadComponents(), 

57 projection_finders.ReadComponentsAstropyFits(), 

58 ) 

59 

60 self.dataId = {"patch": 24, "tract": 3828, "band": "r", "skymap": "DC2"} 

61 

62 def test_extract_ref(self): 

63 """Test that extract_ref produces a reasonable cutout.""" 

64 dataRef = self.butler.registry.findDataset("deepCoadd_calexp", dataId=self.dataId) 

65 

66 with tempfile.TemporaryDirectory() as tempdir: 

67 for cutout_mode in CutoutMode: 

68 # Try each available projection finder at least once. 

69 proj_finder = self.projectionFinders[cutout_mode.value % len(self.projectionFinders)] 

70 cutoutBackend = ImageCutoutFactory(self.butler, proj_finder, tempdir) 

71 result = cutoutBackend.extract_ref(self.stencil, dataRef, cutout_mode=cutout_mode) 

72 # The galaxy should be near the center of the image. 

73 match result.cutout: 

74 case lsst.afw.image.Exposure() | lsst.afw.image.MaskedImage(): 

75 box = result.cutout.getBBox() 

76 array = result.cutout.image.array 

77 case lsst.afw.image.Image(): 

78 array = result.cutout.array 

79 box = result.cutout.getBBox() 

80 case astropy.io.fits.HDUList(): 

81 hdu = result.cutout[1] 

82 array = hdu.data 

83 # Only checks the shape. 

84 box = lsst.geom.Box2I(lsst.geom.Point2I([1, 1]), lsst.geom.Extent2I(hdu.shape)) 

85 case _: 

86 raise RuntimeError(f"Unexpected cutout type: {type(result.cutout)}") 

87 self.assertEqual(box.width, 101) 

88 self.assertEqual(box.height, 101) 

89 self.assertFloatsAlmostEqual(array[50, 49], 2.083247661590576) 

90 self.assertFloatsAlmostEqual(array[1, 1], -0.14575983583927155) 

91 self.assertFloatsAlmostEqual(array[100, 100], 0.08674515783786774) 

92 

93 output = cutoutBackend.process_ref(self.stencil, dataRef, cutout_mode=cutout_mode) 

94 self.assertTrue(output.exists()) 

95 

96 output = cutoutBackend.process_uuid(self.stencil, dataRef.id, cutout_mode=cutout_mode) 

97 self.assertTrue(output.exists()) 

98 

99 

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

101 unittest.main()