Coverage for python/lsst/afw/display/_images_compat.py: 38%

16 statements  

« prev     ^ index     » next       coverage.py v7.14.3, created at 2026-07-09 02:01 -0700

1# This file is part of afw. 

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 

22from __future__ import annotations 

23 

24__all__ = ["normalize_lsst_images"] 

25 

26import sys 

27import lsst.afw.image 

28 

29 

30def normalize_lsst_images(data, wcs=None): 

31 """Convert an `lsst.images` object to its afw equivalent for display. 

32 

33 Parameters 

34 ---------- 

35 data : `~typing.Any` 

36 Data to display. If this is an `lsst.images.MaskedImage` (including 

37 subclasses such as `~lsst.images.VisitImage` and 

38 `~lsst.images.cells.CellCoadd`), `lsst.images.Image`, or 

39 `lsst.images.Mask`, it is converted to the corresponding 

40 `lsst.afw.image` type; anything else is returned unchanged. 

41 wcs : `lsst.afw.geom.SkyWcs` or `None`, optional 

42 WCS supplied by the caller. 

43 

44 Returns 

45 ------- 

46 data : `~typing.Any` 

47 The converted afw object, or the input unchanged. 

48 wcs : `lsst.afw.geom.SkyWcs` or `None` 

49 The WCS converted from ``data.sky_projection`` if present, else the 

50 caller-supplied ``wcs``. 

51 

52 Raises 

53 ------ 

54 RuntimeError 

55 Raised if ``wcs`` is not `None` and ``data`` has its own 

56 ``sky_projection``. 

57 

58 Notes 

59 ----- 

60 ``lsst.images`` is deliberately never imported here: it optionally 

61 depends on afw, so the dependency cannot go the other way. If the 

62 module has never been imported, the caller cannot be holding one of 

63 its objects, so looking it up in `sys.modules` is sufficient. 

64 

65 Only the image and mask planes are ever forwarded to display backends, 

66 so PSFs, variance, and other components are never serialized for 

67 display. Mask schemas with more than 31 named planes cannot be 

68 represented as an `lsst.afw.image.Mask` and fail conversion. 

69 """ 

70 images = sys.modules.get("lsst.images") 

71 if images is None or not isinstance(data, (images.MaskedImage, images.Image, images.Mask)): 71 ↛ 74line 71 didn't jump to line 74 because the condition on line 71 was always true

72 return data, wcs 

73 

74 if data.sky_projection is not None: 

75 if wcs is not None: 

76 raise RuntimeError( 

77 "You may not specify a wcs with an lsst.images object that has its own sky_projection" 

78 ) 

79 wcs = data.sky_projection.to_legacy() 

80 

81 if isinstance(data, images.MaskedImage): 

82 data = lsst.afw.image.MaskedImage( 

83 data.image.to_legacy(), 

84 mask=data.mask.to_legacy(), 

85 variance=data.variance.to_legacy(), 

86 dtype=data.image.array.dtype, 

87 ) 

88 else: 

89 # Image and Mask convert directly (Image as a zero-copy view). 

90 data = data.to_legacy() 

91 

92 return data, wcs