Coverage for python/lsst/afw/detection/utils.py: 18%

27 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-05-29 01:21 -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 

22__all__ = ["writeFootprintAsDefects", "footprintsToNumpy"] 

23 

24import numpy as np 

25 

26import lsst.geom as geom 

27from lsst.afw.table import SourceCatalog 

28 

29from . import footprintToBBoxList 

30 

31 

32def writeFootprintAsDefects(fd, foot): 

33 """ 

34 Write foot as a set of Defects to fd 

35 

36 Given a detection footprint, convert it to a BBoxList and write the output to the file object fd. 

37 

38 Parameters 

39 ---------- 

40 fd : `typing.TextIO` 

41 foot : `lsst.afw.detection.Footprint` 

42 

43 See Also 

44 -------- 

45 lsst.afw.detection.footprintToBBoxList 

46 """ 

47 

48 bboxes = footprintToBBoxList(foot) 

49 for bbox in bboxes: 

50 print("""\ 

51Defects: { 

52 x0: %4d # Starting column 

53 width: %4d # number of columns 

54 y0: %4d # Starting row 

55 height: %4d # number of rows 

56}""" % (bbox.getMinX(), bbox.getWidth(), bbox.getMinY(), bbox.getHeight()), file=fd) 

57 

58 

59def footprintsToNumpy( 

60 catalog: SourceCatalog, 

61 bbox: geom.Box2I | None = None, 

62 shape: tuple[int, int] | None = None, 

63 xy0: tuple[int, int] | None = None, 

64 asBool: bool = True, 

65) -> np.ndarray: 

66 """Convert all of the footprints in a catalog into a boolean array. 

67 

68 Parameters 

69 ---------- 

70 catalog: 

71 The source catalog containing the footprints. 

72 This is typically a mergeDet catalog, or a full source catalog 

73 with the parents removed. 

74 shape: 

75 The final shape of the output array. 

76 xy0: 

77 The lower-left corner of the array that will contain the spans. 

78 

79 Returns 

80 ------- 

81 result: 

82 The array with pixels contained in `spans` marked as `True`. 

83 """ 

84 if bbox is None and shape is None: 

85 raise RuntimeError("Must provide either bbox or shape") 

86 

87 if bbox is not None: 

88 width, height = bbox.getDimensions() 

89 shape = (height, width) 

90 xy0 = (bbox.getMinX(), bbox.getMinY()) 

91 

92 if xy0 is None: 

93 offset = (0, 0) 

94 else: 

95 offset = (-xy0[0], -xy0[1]) 

96 

97 result = np.zeros(shape, dtype=int) 

98 for src in catalog: 

99 spans = src.getFootprint().spans 

100 yidx, xidx = spans.shiftedBy(*offset).indices() 

101 result[yidx, xidx] = src.getId() 

102 if asBool: 

103 result = result != 0 

104 return result