Coverage for python/lsst/analysis/tools/tasks/sourceObjectTableAnalysis.py: 38%

47 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-10 11:03 +0000

1# This file is part of analysis_tools. 

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/>. 

21from __future__ import annotations 

22 

23__all__ = ("SourceObjectTableAnalysisConfig", "SourceObjectTableAnalysisTask") 

24 

25import lsst.pex.config as pexConfig 

26import numpy as np 

27import pandas as pd 

28from astropy.table import vstack 

29from lsst.pipe.base import connectionTypes as ct 

30from smatch import Matcher 

31 

32from ..interfaces import AnalysisBaseConfig, AnalysisBaseConnections, AnalysisPipelineTask 

33 

34 

35class SourceObjectTableAnalysisConnections( 

36 AnalysisBaseConnections, 

37 dimensions=("visit",), 

38 defaultTemplates={ 

39 "inputName": "sourceTable_visit", 

40 "inputCoaddName": "deep", 

41 "associatedSourcesInputName": "isolated_star_sources", 

42 "outputName": "sourceObjectTable", 

43 }, 

44): 

45 data = ct.Input( 

46 doc="Visit based source table to load from the butler", 

47 name="sourceTable_visit", 

48 storageClass="ArrowAstropy", 

49 dimensions=("visit", "band"), 

50 deferLoad=True, 

51 ) 

52 

53 associatedSources = ct.Input( 

54 doc="Table of associated sources", 

55 name="{associatedSourcesInputName}", 

56 storageClass="ArrowAstropy", 

57 multiple=True, 

58 deferLoad=True, 

59 dimensions=("instrument", "skymap", "tract"), 

60 ) 

61 

62 refCat = ct.Input( 

63 doc="Catalog of positions to use as reference.", 

64 name="objectTable", 

65 storageClass="DataFrame", 

66 dimensions=["skymap", "tract", "patch"], 

67 multiple=True, 

68 deferLoad=True, 

69 ) 

70 

71 

72class SourceObjectTableAnalysisConfig( 

73 AnalysisBaseConfig, pipelineConnections=SourceObjectTableAnalysisConnections 

74): 

75 ra_column = pexConfig.Field( 

76 doc="Name of column in refCat to use for right ascension.", 

77 dtype=str, 

78 default="r_ra", 

79 ) 

80 dec_column = pexConfig.Field( 

81 doc="Name of column in refCat to use for declination.", 

82 dtype=str, 

83 default="r_dec", 

84 ) 

85 

86 

87class SourceObjectTableAnalysisTask(AnalysisPipelineTask): 

88 ConfigClass = SourceObjectTableAnalysisConfig 

89 _DefaultName = "sourceTableVisitAnalysis" 

90 

91 def runQuantum(self, butlerQC, inputRefs, outputRefs): 

92 inputs = butlerQC.get(inputRefs) 

93 

94 # Get isolated sources: 

95 visit = inputs["data"].dataId["visit"] 

96 band = inputs["data"].dataId["band"] 

97 names = self.collectInputNames() 

98 names -= {self.config.ra_column, self.config.dec_column} 

99 data = inputs["data"].get(parameters={"columns": names}) 

100 

101 dataId = butlerQC.quantum.dataId 

102 plotInfo = self.parsePlotInfo(inputs, dataId) 

103 

104 isolatedSources = [] 

105 for associatedSourcesRef in inputs["associatedSources"]: 

106 associatedSources = associatedSourcesRef.get(parameters={"columns": ["visit", "source_row"]}) 

107 visit_sources = associatedSources[associatedSources["visit"] == visit] 

108 isolatedSources.append(data[visit_sources["source_row"]]) 

109 isolatedSources = vstack(isolatedSources) 

110 

111 # Get objects: 

112 allRefCats = [] 

113 for refCatRef in inputs["refCat"]: 

114 refCat = refCatRef.get( 

115 parameters={"columns": ["detect_isPrimary", self.config.ra_column, self.config.dec_column]} 

116 ) 

117 goodInds = ( 

118 refCat["detect_isPrimary"] 

119 & np.isfinite(refCat[self.config.ra_column]) 

120 & np.isfinite(refCat[self.config.dec_column]) 

121 ) 

122 allRefCats.append(refCat[goodInds]) 

123 

124 refCat = pd.concat(allRefCats) 

125 

126 with Matcher(isolatedSources["coord_ra"], isolatedSources["coord_dec"]) as m: 

127 idx, isolatedMatchIndices, refMatchIndices, dists = m.query_radius( 

128 refCat[self.config.ra_column].values, 

129 refCat[self.config.dec_column].values, 

130 1 / 3600.0, 

131 return_indices=True, 

132 ) 

133 

134 matchRef = refCat.iloc[refMatchIndices] 

135 matchIS = isolatedSources[isolatedMatchIndices].to_pandas() 

136 

137 allCat = pd.concat([matchRef.reset_index(), matchIS.reset_index()], axis=1) 

138 outputs = self.run(data=allCat, bands=band, plotInfo=plotInfo) 

139 butlerQC.put(outputs, outputRefs)