Coverage for python/lsst/analysis/tools/tasks/associatedSourcesTractAnalysis.py: 46%

59 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-05 12:16 +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__ = ("AssociatedSourcesTractAnalysisConfig", "AssociatedSourcesTractAnalysisTask") 

24 

25import numpy as np 

26from astropy.table import join, vstack 

27from lsst.geom import Box2D 

28from lsst.pipe.base import NoWorkFound 

29from lsst.pipe.base import connectionTypes as ct 

30from lsst.skymap import BaseSkyMap 

31 

32from ..interfaces import AnalysisBaseConfig, AnalysisBaseConnections, AnalysisPipelineTask 

33 

34 

35class AssociatedSourcesTractAnalysisConnections( 

36 AnalysisBaseConnections, 

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

38 defaultTemplates={ 

39 "outputName": "isolated_star_sources", 

40 "associatedSourcesInputName": "isolated_star_sources", 

41 }, 

42): 

43 sourceCatalogs = ct.Input( 

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

45 name="sourceTable_visit", 

46 storageClass="ArrowAstropy", 

47 deferLoad=True, 

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

49 multiple=True, 

50 ) 

51 

52 associatedSources = ct.Input( 

53 doc="Table of associated sources", 

54 name="{associatedSourcesInputName}", 

55 storageClass="ArrowAstropy", 

56 deferLoad=True, 

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

58 ) 

59 

60 skyMap = ct.Input( 

61 doc="Input definition of geometry/bbox and projection/wcs for warped exposures", 

62 name=BaseSkyMap.SKYMAP_DATASET_TYPE_NAME, 

63 storageClass="SkyMap", 

64 dimensions=("skymap",), 

65 ) 

66 camera = ct.PrerequisiteInput( 

67 doc="Input camera to use for focal plane geometry.", 

68 name="camera", 

69 storageClass="Camera", 

70 dimensions=("instrument",), 

71 isCalibration=True, 

72 ) 

73 

74 

75class AssociatedSourcesTractAnalysisConfig( 

76 AnalysisBaseConfig, pipelineConnections=AssociatedSourcesTractAnalysisConnections 

77): 

78 def setDefaults(self): 

79 super().setDefaults() 

80 

81 

82class AssociatedSourcesTractAnalysisTask(AnalysisPipelineTask): 

83 ConfigClass = AssociatedSourcesTractAnalysisConfig 

84 _DefaultName = "associatedSourcesTractAnalysis" 

85 

86 @staticmethod 

87 def getBoxWcs(skymap, tract): 

88 """Get box that defines tract boundaries.""" 

89 tractInfo = skymap.generateTract(tract) 

90 wcs = tractInfo.getWcs() 

91 tractBox = tractInfo.getBBox() 

92 return tractBox, wcs 

93 

94 @classmethod 

95 def callback(cls, inputs, dataId): 

96 """Callback function to be used with reconstructor.""" 

97 return cls.prepareAssociatedSources( 

98 inputs["skyMap"], 

99 dataId["tract"], 

100 inputs["sourceCatalogs"], 

101 inputs["associatedSources"], 

102 ) 

103 

104 @classmethod 

105 def prepareAssociatedSources(cls, skymap, tract, sourceCatalogs, associatedSources): 

106 """Concatenate source catalogs and join on associated object index.""" 

107 

108 # Keep only sources with associations 

109 sourceCatalogStack = vstack(sourceCatalogs) 

110 dataJoined = join(sourceCatalogStack, associatedSources, keys="sourceId", join_type="inner") 

111 

112 # Determine which sources are contained in tract 

113 ra = np.radians(dataJoined["coord_ra"]) 

114 dec = np.radians(dataJoined["coord_dec"]) 

115 box, wcs = cls.getBoxWcs(skymap, tract) 

116 box = Box2D(box) 

117 x, y = wcs.skyToPixelArray(ra, dec) 

118 boxSelection = box.contains(x, y) 

119 

120 # Keep only the sources in groups that are fully contained within the 

121 # tract 

122 dataFiltered = dataJoined[boxSelection] 

123 

124 return dataFiltered 

125 

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

127 inputs = butlerQC.get(inputRefs) 

128 

129 # Load specified columns from source catalogs 

130 names = self.collectInputNames() 

131 names |= {"sourceId", "coord_ra", "coord_dec"} 

132 names.remove("obj_index") 

133 sourceCatalogs = [] 

134 for handle in inputs["sourceCatalogs"]: 

135 sourceCatalogs.append(self.loadData(handle, names)) 

136 inputs["sourceCatalogs"] = sourceCatalogs 

137 

138 dataId = butlerQC.quantum.dataId 

139 plotInfo = self.parsePlotInfo(inputs, dataId, connectionName="associatedSources") 

140 

141 # TODO: make key used for object index configurable 

142 inputs["associatedSources"] = self.loadData(inputs["associatedSources"], ["obj_index", "sourceId"]) 

143 

144 if len(inputs["associatedSources"]) == 0: 

145 raise NoWorkFound(f"No associated sources in tract {dataId.tract.id}") 

146 

147 data = self.callback(inputs, dataId) 

148 

149 kwargs = {"data": data, "plotInfo": plotInfo, "skymap": inputs["skyMap"], "camera": inputs["camera"]} 

150 outputs = self.run(**kwargs) 

151 butlerQC.put(outputs, outputRefs)