Coverage for python/lsst/ap/association/filterDiaSourceCatalog.py: 52%

34 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-02 04:31 -0700

1# This file is part of ap_association 

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__ = ( 

23 "FilterDiaSourceCatalogConfig", 

24 "FilterDiaSourceCatalogTask", 

25) 

26 

27import numpy as np 

28 

29from lsst.afw.table import SourceCatalog 

30import lsst.pex.config as pexConfig 

31import lsst.pipe.base as pipeBase 

32import lsst.pipe.base.connectionTypes as connTypes 

33from lsst.utils.timer import timeMethod 

34 

35 

36class FilterDiaSourceCatalogConnections( 

37 pipeBase.PipelineTaskConnections, 

38 dimensions=("instrument", "visit", "detector"), 

39 defaultTemplates={"coaddName": "deep", "fakesType": ""}, 

40): 

41 """Connections class for FilterDiaSourceCatalogTask.""" 

42 

43 diaSourceCat = connTypes.Input( 

44 doc="Catalog of DiaSources produced during image differencing.", 

45 name="{fakesType}{coaddName}Diff_diaSrc", 

46 storageClass="SourceCatalog", 

47 dimensions=("instrument", "visit", "detector"), 

48 ) 

49 

50 filteredDiaSourceCat = connTypes.Output( 

51 doc="Output catalog of DiaSources after filtering.", 

52 name="{fakesType}{coaddName}Diff_candidateDiaSrc", 

53 storageClass="SourceCatalog", 

54 dimensions=("instrument", "visit", "detector"), 

55 ) 

56 

57 rejectedDiaSources = connTypes.Output( 

58 doc="Optional output storing all the rejected DiaSources.", 

59 name="{fakesType}{coaddName}Diff_rejectedDiaSrc", 

60 storageClass="SourceCatalog", 

61 dimensions={"instrument", "visit", "detector"}, 

62 ) 

63 

64 def __init__(self, *, config=None): 

65 super().__init__(config=config) 

66 if not self.config.doWriteRejectedSources: 

67 self.outputs.remove("rejectedDiaSources") 

68 

69 

70class FilterDiaSourceCatalogConfig( 

71 pipeBase.PipelineTaskConfig, pipelineConnections=FilterDiaSourceCatalogConnections 

72): 

73 """Config class for FilterDiaSourceCatalogTask.""" 

74 

75 doRemoveSkySources = pexConfig.Field( 

76 dtype=bool, 

77 default=False, 

78 doc="Input DiaSource catalog contains SkySources that should be " 

79 "removed before storing the output DiaSource catalog.", 

80 ) 

81 

82 doWriteRejectedSources = pexConfig.Field( 

83 dtype=bool, 

84 default=True, 

85 doc="Store the output DiaSource catalog containing all the rejected " 

86 "sky sources." 

87 ) 

88 

89 

90class FilterDiaSourceCatalogTask(pipeBase.PipelineTask): 

91 """Filter out sky sources from a DiaSource catalog.""" 

92 

93 ConfigClass = FilterDiaSourceCatalogConfig 

94 _DefaultName = "filterDiaSourceCatalog" 

95 

96 @timeMethod 

97 def run(self, diaSourceCat): 

98 """Filter sky sources from the supplied DiaSource catalog. 

99 

100 Parameters 

101 ---------- 

102 diaSourceCat : `lsst.afw.table.SourceCatalog` 

103 Catalog of sources measured on the difference image. 

104 

105 Returns 

106 ------- 

107 filterResults : `lsst.pipe.base.Struct` 

108 

109 ``filteredDiaSourceCat`` : `lsst.afw.table.SourceCatalog` 

110 The catalog of filtered sources. 

111 ``rejectedDiaSources`` : `lsst.afw.table.SourceCatalog` 

112 The catalog of rejected sources. 

113 """ 

114 rejectedSkySources = None 

115 if self.config.doRemoveSkySources: 

116 sky_source_column = diaSourceCat["sky_source"] 

117 num_sky_sources = np.sum(sky_source_column) 

118 rejectedSkySources = diaSourceCat[sky_source_column].copy(deep=True) 

119 diaSourceCat = diaSourceCat[~sky_source_column].copy(deep=True) 

120 self.log.info(f"Filtered {num_sky_sources} sky sources.") 

121 if not rejectedSkySources: 

122 rejectedSkySources = SourceCatalog(diaSourceCat.getSchema()) 

123 filterResults = pipeBase.Struct(filteredDiaSourceCat=diaSourceCat, 

124 rejectedDiaSources=rejectedSkySources) 

125 return filterResults