Coverage for python/lsst/faro/measurement/TractTableMeasurementTasks.py: 45%

56 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-05 13:37 +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/>. 

21 

22import lsst.afw.math as afwMath 

23from lsst.pex.config import Field, DictField 

24from lsst.pipe.base import Struct, Task 

25from lsst.verify import Measurement, Datum 

26 

27from lsst.faro.base.ConfigBase import MeasurementTaskConfig 

28import lsst.faro.utils.selectors as selectors 

29from lsst.faro.utils.tex_table import calculateTEx 

30 

31import astropy.units as u 

32import numpy as np 

33 

34__all__ = ("TExTableConfig", "TExTableTask", "FluxStatisticConfig", "FluxStatisticTask") 

35 

36 

37class TExTableConfig(MeasurementTaskConfig): 

38 """Class to organize the yaml configuration parameters to be passed to 

39 TExTableTask when using a parquet table input. All values needed to perform 

40 TExTableTask have default values set below. 

41 

42 Optional Input (yaml file) 

43 ---------- 

44 Column names specified as str in yaml configuration for TeX task. These are 

45 the desired column names to be passed to the calcuation. If you wish to use 

46 values other than the default values specified below, add the following e.g. 

47 line to the yaml file: 

48 

49 config.measure.raColumn = "coord_ra_new" 

50 """ 

51 

52 minSep = Field( 

53 doc="Inner radius of the annulus in arcmin", dtype=float, default=0.25 

54 ) 

55 maxSep = Field( 

56 doc="Outer radius of the annulus in arcmin", dtype=float, default=1.0 

57 ) 

58 nbins = Field(doc="Number of log-spaced angular bins", dtype=int, default=10) 

59 rhoStat = Field(doc="Rho statistic to be computed", dtype=int, default=1) 

60 shearConvention = Field( 

61 doc="Use shear ellipticity convention rather than distortion", 

62 dtype=bool, 

63 default=True, 

64 ) 

65 columns = DictField( 

66 doc="""Columns required for metric calculation. Should be all columns in SourceTable contexts, 

67 and columns that do not change name with band in ObjectTable contexts""", 

68 keytype=str, 

69 itemtype=str, 

70 default={"ra": "coord_ra", 

71 "dec": "coord_dec", 

72 "ixx": "ixx", 

73 "ixy": "ixx", 

74 "iyy": "ixx", 

75 "ixxPsf": "ixx", 

76 "ixyPsf": "ixx", 

77 "iyyPsf": "iyy", 

78 "deblend_nChild": "deblend_nChild" 

79 } 

80 ) 

81 columnsBand = DictField( 

82 doc="""Columns required for metric calculation that change with band in ObjectTable contexts""", 

83 keytype=str, 

84 itemtype=str, 

85 default={} 

86 ) 

87 

88 

89class TExTableTask(Task): 

90 """Class to perform the tex_table calculation on a parquet table data 

91 object. 

92 

93 Parameters 

94 ---------- 

95 None 

96 

97 Output: 

98 ---------- 

99 TEx metric with defined configuration. 

100 """ 

101 

102 ConfigClass = TExTableConfig 

103 _DefaultName = "TExTableTask" 

104 

105 def run( 

106 self, metricName, catalog, currentBands, **kwargs 

107 ): 

108 

109 self.log.info("Measuring %s", metricName) 

110 

111 # If accessing objectTable_tract, we need to append the band name at 

112 # the beginning of the column name. 

113 if currentBands is not None: 

114 prependString = currentBands 

115 else: 

116 prependString = None 

117 

118 # filter catalog 

119 catalog = selectors.applySelectors(catalog, 

120 self.config.selectorActions, 

121 currentBands=currentBands) 

122 

123 result = calculateTEx(catalog, self.config, prependString) 

124 if "corr" not in result.keys(): 

125 return Struct(measurement=Measurement(metricName, np.nan * u.Unit(""))) 

126 

127 writeExtras = True 

128 if writeExtras: 

129 extras = {} 

130 extras["radius"] = Datum( 

131 result["radius"], label="radius", description="Separation (arcmin)." 

132 ) 

133 extras["corr"] = Datum( 

134 result["corr"], label="Correlation", description="Correlation." 

135 ) 

136 extras["corrErr"] = Datum( 

137 result["corrErr"], 

138 label="Correlation Uncertainty", 

139 description="Correlation Uncertainty.", 

140 ) 

141 else: 

142 extras = None 

143 return Struct( 

144 measurement=Measurement( 

145 metricName, np.mean(np.abs(result["corr"])), extras=extras 

146 ) 

147 ) 

148 

149 

150class FluxStatisticConfig(MeasurementTaskConfig): 

151 """Class to configure the flux statistic measurement task. 

152 """ 

153 

154 statistic = Field( 

155 doc="Statistic name to use to generate the metric (from `~lsst.afw.math.Property`).", 

156 dtype=str, 

157 default="MEAN", 

158 ) 

159 numSigmaClip = Field( 

160 doc="Rejection threshold (sigma) for statistics clipping.", 

161 dtype=float, 

162 default=5.0, 

163 ) 

164 clipMaxIter = Field( 

165 doc="Maximum number of clipping iterations to apply.", 

166 dtype=int, 

167 default=3, 

168 ) 

169 

170 

171class FluxStatisticTask(Task): 

172 """Class to perform flux statistic calculations on parquet table data. 

173 

174 Output 

175 ------ 

176 Flux metric with defined configuration. 

177 """ 

178 

179 ConfigClass = FluxStatisticConfig 

180 _DefaultName = "FluxStatisticTask" 

181 

182 def run( 

183 self, metricName, catalog, currentBands, **kwargs 

184 ): 

185 

186 self.log.info("Measuring %s", metricName) 

187 

188 # filter catalog using selectors 

189 catalog = selectors.applySelectors(catalog, 

190 self.config.selectorActions, 

191 currentBands=currentBands) 

192 

193 # extract flux value column 

194 all_columns = [x for x in self.config.columns.values()] 

195 for col in self.config.columnsBand.values(): 

196 all_columns.append(f"{currentBands}_{col}") 

197 fluxes = catalog[all_columns].iloc[:, 0].to_numpy() 

198 

199 # calculate statistic 

200 statisticToRun = afwMath.stringToStatisticsProperty(self.config.statistic) 

201 statControl = afwMath.StatisticsControl(self.config.numSigmaClip, 

202 self.config.clipMaxIter,) 

203 result = afwMath.makeStatistics(fluxes, statisticToRun, statControl).getValue() 

204 

205 # return result 

206 return Struct( 

207 measurement=Measurement( 

208 metricName, result*u.nanojansky, extras=None 

209 ) 

210 )