Coverage for python / lsst / faro / measurement / TractTableValueMeasurement.py: 36%

58 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-24 08:40 +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 astropy.units as u 

23 

24import lsst.pex.config as pexConfig 

25import lsst.pipe.base.connectionTypes as cT 

26from lsst.pex.config.configurableActions import ConfigurableActionField 

27from lsst.pipe.base import Struct 

28from lsst.pipe.tasks.dataFrameActions import SingleColumnAction 

29from lsst.verify import Measurement 

30from lsst.verify.tasks import MetricTask, MetricConfig, MetricConnections, MetricComputationError 

31 

32__all__ = ( 

33 "TractTableValueMeasurementConnections", 

34 "TractTableValueMeasurementConfig", 

35 "TractTableValueMeasurementTask", 

36) 

37 

38 

39class TractTableValueMeasurementConnections( 

40 MetricConnections, 

41 defaultTemplates={"package": None, "metric": None, "name_table": None}, 

42 dimensions=("tract", "skymap"), 

43): 

44 columns = cT.Input( 

45 doc="Table columns to read", 

46 name="{name_table}.columns", 

47 storageClass="DataFrameIndex", 

48 dimensions=("tract", "skymap"), 

49 ) 

50 measurement = cT.Output( 

51 name="metricvalue_{package}_{metric}", 

52 doc="The metric value computed by this task.", 

53 storageClass="MetricValue", 

54 dimensions=("tract", "skymap", "band"), 

55 multiple=True, 

56 ) 

57 table = cT.Input( 

58 doc="Table to read value from", 

59 name="{name_table}", 

60 storageClass="DataFrame", 

61 dimensions=("tract", "skymap"), 

62 deferLoad=True, 

63 ) 

64 

65 

66class TractTableValueMeasurementConfig( 

67 MetricConfig, pipelineConnections=TractTableValueMeasurementConnections 

68): 

69 """Configuration for TractTableValueMeasurementTask.""" 

70 action = ConfigurableActionField( 

71 doc="Action to compute the value with", 

72 default=SingleColumnAction, 

73 ) 

74 band_order = pexConfig.ListField( 

75 dtype=str, 

76 doc="Standard (usually wavelength-based) ordering for possible bands" 

77 " to determine standard colors", 

78 default=('u', 'g', 'r', 'i', 'z', 'y'), 

79 ) 

80 format_column = pexConfig.Field( 

81 dtype=str, 

82 doc="Format of the full column names including the band", 

83 default="{band}_{column}", 

84 ) 

85 prefixes_column = pexConfig.ListField( 

86 dtype=str, 

87 doc="Column name prefixes to ignore when applying special formatting rules", 

88 default=['all_', 'resolved_', 'unresolved_'], 

89 ) 

90 row = pexConfig.Field( 

91 dtype=int, 

92 doc="Index of the row to retrieve the value from", 

93 optional=False, 

94 ) 

95 unit = pexConfig.Field( 

96 dtype=str, 

97 doc="The astropy unit of the metric value", 

98 default='', 

99 ) 

100 

101 def _format_column(self, band: str, column: str): 

102 prefix = '' 

103 for prefix_column in self.prefixes_column: 

104 if column.startswith(prefix_column): 

105 prefix = prefix_column 

106 column = column[len(prefix):] 

107 break 

108 if column.startswith('color_'): 

109 column = f'color_{self.band_order[self.band_order.index(band) - 1]}_m_{band}_{band}_{column[6:]}' 

110 if column.startswith('flux_'): 

111 column = f'flux_{band}_{column[5:]}' 

112 elif column.startswith('mag_'): 

113 column = f'mag_{band}_{column[4:]}' 

114 return self.format_column.format(band=band, column=f'{prefix}{column}') 

115 

116 

117class TractTableValueMeasurementTask(MetricTask): 

118 """Measure a metric from a single row and combination of columns in a table.""" 

119 

120 ConfigClass = TractTableValueMeasurementConfig 

121 _DefaultName = "TractTableValueMeasurementTask" 

122 

123 def run(self, table, bands, name_metric): 

124 unit = u.Unit(self.config.unit) 

125 measurements = [None]*len(bands) 

126 columns = list(self.config.action.columns) 

127 for idx, band in enumerate(bands): 

128 row = table.iloc[[self.config.row]].rename( 

129 columns={self.config._format_column(band, column): column 

130 for column in columns} 

131 ) 

132 value = self.config.action(row).iloc[0] 

133 measurements[idx] = Measurement(name_metric, value*unit) 

134 return Struct(measurement=measurements) 

135 

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

137 try: 

138 inputs = butlerQC.get(inputRefs) 

139 bands = [x.dataId['band'] for x in [y for y in outputRefs][0][1]] 

140 columns_base = list(self.config.action.columns) 

141 columns_in = [] 

142 for band in bands: 

143 columns_in.extend(self.config._format_column(band, column) 

144 for column in columns_base) 

145 

146 # If columns_in contains non-existent columns, the get call will fail 

147 outputs = self.run( 

148 table=inputs['table'].get(parameters={'columns': columns_in}), 

149 bands=bands, 

150 name_metric=self.config.connections.metric, 

151 ) 

152 butlerQC.put(outputs, outputRefs) 

153 except MetricComputationError: 

154 self.log.error( 

155 "Measurement of %r failed on %s->%s", 

156 self, inputRefs, outputRefs, exc_info=True)