Coverage for python/lsst/faro/base/CatalogMeasurementBase.py: 36%

59 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-09-15 11:05 +0000

1# This file is part of faro. 

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 astropy.table import Table, hstack 

22import astropy.units as u 

23 

24import lsst.pipe.base as pipeBase 

25import lsst.pex.config as pexConfig 

26from lsst.verify.tasks import MetricTask, MetricConfig, MetricConnections 

27from lsst.pipe.tasks.loadReferenceCatalog import LoadReferenceCatalogTask 

28import lsst.geom 

29from .BaseSubTasks import NumSourcesTask 

30 

31__all__ = ( 

32 "CatalogMeasurementBaseConnections", 

33 "CatalogMeasurementBaseConfig", 

34 "CatalogMeasurementBaseTask", 

35) 

36 

37 

38class CatalogMeasurementBaseConnections( 

39 MetricConnections, defaultTemplates={"refDataset": ""} 

40): 

41 

42 refCat = pipeBase.connectionTypes.PrerequisiteInput( 

43 doc="Reference catalog", 

44 name="{refDataset}", 

45 storageClass="SimpleCatalog", 

46 dimensions=("skypix",), 

47 deferLoad=True, 

48 multiple=True, 

49 ) 

50 

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

52 super().__init__(config=config) 

53 if config.connections.refDataset == "": 

54 self.prerequisiteInputs.remove("refCat") 

55 

56 

57class CatalogMeasurementBaseConfig( 

58 MetricConfig, pipelineConnections=CatalogMeasurementBaseConnections 

59): 

60 """Configuration for CatalogMeasurementBaseTask.""" 

61 

62 measure = pexConfig.ConfigurableField( 

63 # This task is meant to make measurements of various types. 

64 # The default task is, therefore, a bit of a place holder. 

65 # It is expected that this will be overridden in the pipeline 

66 # definition in most cases. 

67 target=NumSourcesTask, 

68 doc="Measure task", 

69 ) 

70 

71 referenceCatalogLoader = pexConfig.ConfigurableField( 

72 target=LoadReferenceCatalogTask, doc="Reference catalog loader", 

73 ) 

74 

75 def setDefaults(self): 

76 self.referenceCatalogLoader.doApplyColorTerms = False 

77 

78 

79class CatalogMeasurementBaseTask(MetricTask): 

80 """Base class for science performance metrics measured from source/object catalogs.""" 

81 

82 ConfigClass = CatalogMeasurementBaseConfig 

83 _DefaultName = "catalogMeasurementBaseTask" 

84 

85 def __init__(self, config, *args, **kwargs): 

86 super().__init__(*args, config=config, **kwargs) 

87 self.makeSubtask("measure") 

88 

89 def run(self, **kwargs): 

90 if 'shelveName' in self.measure.config.keys(): 

91 if self.measure.config.shelveName: 

92 # Persist in-memory objects for development and testing 

93 self._persistMeasurementInputs(self.measure.config, self.measure.config.shelveName, **kwargs) 

94 return self.measure.run(self.config.connections.metric, **kwargs) 

95 

96 def _getTableColumnsSelectors(self, columns, currentBands=None): 

97 """given a list of selectors return columns required to apply these 

98 selectors. 

99 Parameters 

100 ---------- 

101 columns: `list` [`str`] 

102 a list of columns required to calculate a metric. This list 

103 is appended with any addditional columns required for the selectorActions. 

104 

105 currentBands: `list` [`str`] 

106 The filter band(s) associated with the observations. 

107 

108 Returns 

109 ------- 

110 columnNames: `list` [`str`] the set of columns required to compute a 

111 metric with any addditional columns required for the selectorActions 

112 appended to the set. 

113 

114 """ 

115 columnNames = set(columns) 

116 for actionStruct in [self.config.measure.selectorActions]: 

117 for action in actionStruct: 

118 for col in action.columns(currentBands): 

119 columnNames.add(col) 

120 

121 return columnNames 

122 

123 def _getReferenceCatalog(self, butlerQC, dataIds, refCats, filterList, epoch=None): 

124 """Load reference catalog in sky region of interest and optionally applies proper 

125 motion correction and color terms. 

126 

127 Loads the `lsst.afw.table.SimpleCatalog` reference catalog, computes ra and dec 

128 (optionally) applying a proper motion correction. Also, color terms 

129 are (optionally) applied to the reference magnitudes in order to transform 

130 them to the data's photometric system. 

131 

132 returns a refCat with both the original loaded reference catalog and 

133 the coorected coordinates (ra,dec) and transformed reference magnitudes 

134 (refMag-/refMagErr-) 

135 

136 Parameters 

137 ---------- 

138 butlerQC : `lsst.pipe.base.butlerQuantumContext.ButlerQuantumContext` 

139 Butler quantum context for a Gen3 repository. 

140 dataIds: interable of `lsst.daf.butler.dataId` 

141 An iterable object of dataIds that point to reference catalogs 

142 in a Gen3 repository. 

143 refCats : iterable of `lsst.daf.butler.DeferredDatasetHandle` 

144 An iterable object of dataset refs for reference catalogs in 

145 a Gen3 repository. 

146 filterList : `list` [`str`] 

147 List of camera physicalFilter names to apply color terms. 

148 epoch : `astropy.time.Time`, optional 

149 Epoch to which to correct proper motion and parallax 

150 (if available), or `None` to not apply such corrections. 

151 

152 Returns 

153 ------- 

154 refCat: pandas.dataframe 

155 a reference catalog with original columns and corrected 

156 coordinates (ra,dec) and reference magnitudes (refMag-/refMagErr-) 

157 """ 

158 center = lsst.geom.SpherePoint( 

159 butlerQC.quantum.dataId.region.getBoundingCircle().getCenter() 

160 ) 

161 radius = butlerQC.quantum.dataId.region.getBoundingCircle().getOpeningAngle() 

162 

163 loaderTask = LoadReferenceCatalogTask( 

164 config=self.config.referenceCatalogLoader, dataIds=dataIds, refCats=refCats, 

165 name=self.config.connections.refCat 

166 ) 

167 

168 # Get catalog with proper motion and color terms applied 

169 refCatCorrected = loaderTask.getSkyCircleCatalog( 

170 center, radius, filterList, epoch=epoch 

171 ) 

172 

173 # Get unformatted catalog w/ all columns 

174 skyCircle = loaderTask.refObjLoader.loadSkyCircle( 

175 center, radius, loaderTask._referenceFilter, epoch=epoch 

176 ) 

177 refCat = skyCircle.refCat 

178 

179 refCatTable = Table() 

180 refCatTable['ra'] = refCatCorrected['ra']*u.deg 

181 refCatTable['dec'] = refCatCorrected['dec']*u.deg 

182 for n, filterName in enumerate(filterList): 

183 refCatTable['refMag-' + filterName] = refCatCorrected["refMag"][:, n]*u.ABmag 

184 refCatTable['refMagErr-' + filterName] = refCatCorrected["refMagErr"][:, n]*u.ABmag 

185 refCatFrame = hstack([refCatTable, refCat.asAstropy()]).to_pandas() 

186 

187 return refCatFrame 

188 

189 def _persistMeasurementInputs(self, config, shelveName, **kwargs): 

190 """Persist in-memory objects sent as inputs to metric measurement run method. 

191 

192 This function is intended to be used for development and testing purposes 

193 as a debug tool and is NOT to be used in routine operation. 

194 

195 Parameters 

196 ---------- 

197 config : `lsst.pex.config.Config` 

198 Config to be saved. 

199 shelveName : `str` 

200 Filename for output shelve. 

201 """ 

202 

203 import shelve 

204 

205 with shelve.open(shelveName, 'n') as shelf: 

206 shelf['config'] = config 

207 for key in kwargs.keys(): 

208 shelf[key] = kwargs[key]