Coverage for python/lsst/analysis/tools/tasks/base.py: 22%

127 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-08-26 03:20 -0700

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"""Base class implementation for the classes needed in creating `PipelineTasks` 

24which execute `AnalysisTools`. 

25 

26The classes defined in this module have all the required behaviors for 

27defining, introspecting, and executing `AnalysisTools` against an input dataset 

28type. 

29 

30Subclasses of these tasks should specify specific datasets to consume in their 

31connection classes and should specify a unique name 

32""" 

33 

34from collections import abc 

35from typing import TYPE_CHECKING, Any, Iterable, Mapping, cast 

36 

37if TYPE_CHECKING: 37 ↛ 38line 37 didn't jump to line 38, because the condition on line 37 was never true

38 from lsst.daf.butler import DeferredDatasetHandle 

39 

40from lsst.daf.butler import DataCoordinate 

41from lsst.pex.config import ListField 

42from lsst.pipe.base import PipelineTask, PipelineTaskConfig, PipelineTaskConnections, Struct 

43from lsst.pipe.base import connectionTypes as ct 

44from lsst.pipe.base.butlerQuantumContext import ButlerQuantumContext 

45from lsst.pipe.base.connections import InputQuantizedConnection, OutputQuantizedConnection 

46from lsst.pipe.tasks.configurableActions import ConfigurableActionStructField 

47 

48from ..analysisMetrics.metricMeasurementBundle import MetricMeasurementBundle 

49from ..interfaces import AnalysisMetric, AnalysisPlot, KeyedData 

50 

51 

52class AnalysisBaseConnections( 

53 PipelineTaskConnections, dimensions={}, defaultTemplates={"outputName": "Placeholder"} 

54): 

55 r"""Base class for Connections used for AnalysisTools PipelineTasks. 

56 

57 This class has a pre-defined output connection for the 

58 MetricMeasurementMapping. The dataset type name for this connection is 

59 determined by the template ``outputName``. 

60 

61 Output connections for plots created by `AnalysisPlot`\ s are created 

62 dynamically when an instance of the class is created. The init method 

63 examines all the `AnalysisPlot` actions specified in the associated 

64 `AnalysisBaseConfig` subclass accumulating all the info needed to 

65 create the output connections. 

66 

67 The dimensions for all of the output connections (metric and plot) will 

68 be the same as the dimensions specified for the AnalysisBaseConnections 

69 subclass (i.e. quantum dimensions). 

70 """ 

71 

72 metrics = ct.Output( 

73 doc="Metrics calculated on input dataset type", 

74 name="{outputName}_metrics", 

75 storageClass="MetricMeasurementBundle", 

76 ) 

77 

78 def __init__(self, *, config: AnalysisBaseConfig = None): # type: ignore 

79 # Validate that the outputName template has been set in config. This 

80 # should have been checked early with the configs validate method, but 

81 # it is possible for someone to manually create everything in a script 

82 # without running validate, so also check it late here. 

83 if (outputName := config.connections.outputName) == "Placeholder": # type: ignore 

84 raise RuntimeError( 

85 "Subclasses must specify an alternative value for the defaultTemplate `outputName`" 

86 ) 

87 super().__init__(config=config) 

88 

89 # All arguments must be passed by kw, but python has not method to do 

90 # that without specifying a default, so None is used. Validate that 

91 # it is not None. This is largely for typing reasons, as in the normal 

92 # course of operation code execution paths ensure this will not be None 

93 assert config is not None 

94 

95 # Set the dimensions for the metric 

96 self.metrics = ct.Output( 

97 name=self.metrics.name, 

98 doc=self.metrics.doc, 

99 storageClass=self.metrics.storageClass, 

100 dimensions=self.dimensions, 

101 multiple=False, 

102 isCalibration=False, 

103 ) 

104 

105 # Look for any conflicting names, creating a set of them, as these 

106 # will be added to the instance as well as recorded in the outputs 

107 # set. 

108 existingNames = set(dir(self)) 

109 

110 # Accumulate all the names to be used from all of the defined 

111 # AnalysisPlots. 

112 names: list[str] = [] 

113 for plotAction in config.plots: 

114 if plotAction.parameterizedBand: 

115 for band in config.bands: 

116 names.extend(name.format(band=band) for name in plotAction.getOutputNames()) 

117 else: 

118 names.extend(plotAction.getOutputNames()) 

119 

120 # For each of the names found, create output connections. 

121 for name in names: 

122 name = f"{outputName}_{name}" 

123 if name in self.outputs or name in existingNames: 

124 raise NameError( 

125 f"Plot with name {name} conflicts with existing connection" 

126 " are two plots named the same?" 

127 ) 

128 outConnection = ct.Output( 

129 name=name, 

130 storageClass="Plot", 

131 doc="Dynamic connection for plotting", 

132 dimensions=self.dimensions, 

133 ) 

134 object.__setattr__(self, name, outConnection) 

135 self.outputs.add(name) 

136 

137 

138class AnalysisBaseConfig(PipelineTaskConfig, pipelineConnections=AnalysisBaseConnections): 

139 """Base class for all configs used to define an `AnalysisPipelineTask` 

140 

141 This base class defines three fields that should be used in all subclasses, 

142 plots, metrics, and bands. 

143 

144 The ``plots`` field is where a user configures which `AnalysisPlots` will 

145 be run in this `PipelineTask`. 

146 

147 Likewise ``metrics`` defines which `AnalysisMetrics` will be run. 

148 

149 The bands field specifies which bands will be looped over for 

150 `AnalysisTools` which support parameterized bands. I.e. called once for 

151 each band in the list. 

152 """ 

153 

154 plots = ConfigurableActionStructField[AnalysisPlot](doc="AnalysisPlots to run with this Task") 

155 metrics = ConfigurableActionStructField[AnalysisMetric](doc="AnalysisMetrics to run with this Task") 

156 bands = ListField[str]( 

157 doc="Filter bands on which to run all of the actions", default=["g", "r", "i", "z", "y"] 

158 ) 

159 

160 def validate(self): 

161 super().validate() 

162 # Validate that the required connections template is set. 

163 if self.connections.outputName == "Placeholder": # type: ignore 

164 raise RuntimeError("Connections class 'outputName' must have a config explicitly set") 

165 

166 

167class _StandinPlotInfo(dict): 

168 """This class is an implementation detail to support plots in the instance 

169 no PlotInfo object is present in the call to run. 

170 """ 

171 

172 def __missing__(self, key): 

173 return "" 

174 

175 

176class AnalysisPipelineTask(PipelineTask): 

177 """Base class for `PipelineTasks` intended to run `AnalysisTools`. 

178 

179 The run method will run all of the `AnalysisMetrics` and `AnalysisPlots` 

180 defined in the config class. 

181 

182 To support interactive investigations, the actual work is done in 

183 ``runMetrics`` and ``runPlots`` methods. These can be called interactively 

184 with the same arguments as ``run`` but only the corresponding outputs will 

185 be produced. 

186 """ 

187 

188 # Typing config because type checkers dont know about our Task magic 

189 config: AnalysisBaseConfig 

190 ConfigClass = AnalysisBaseConfig 

191 

192 def runPlots(self, data: KeyedData, **kwargs) -> Struct: 

193 results = Struct() 

194 # allow not sending in plot info 

195 if "plotInfo" not in kwargs: 

196 kwargs["plotInfo"] = _StandinPlotInfo() 

197 for name, action in self.config.plots.items(): 

198 for selector in action.prep.selectors: 

199 if "threshold" in selector.keys(): 

200 kwargs["plotInfo"]["SN"] = selector.threshold 

201 kwargs["plotInfo"]["plotName"] = name 

202 match action(data, **kwargs): 

203 case abc.Mapping() as val: 

204 for n, v in val.items(): 

205 setattr(results, n, v) 

206 case value: 

207 setattr(results, name, value) 

208 return results 

209 

210 def runMetrics(self, data: KeyedData, **kwargs) -> Struct: 

211 metricsMapping = MetricMeasurementBundle() 

212 for name, action in self.config.metrics.items(): 

213 match action(data, **kwargs): 

214 case abc.Mapping() as val: 

215 results = list(val.values()) 

216 case val: 

217 results = [val] 

218 metricsMapping[name] = results # type: ignore 

219 return Struct(metrics=metricsMapping) 

220 

221 def run(self, *, data: KeyedData | None = None, **kwargs) -> Struct: 

222 """Produce the outputs associated with this `PipelineTask` 

223 

224 Parameters 

225 ---------- 

226 data : `KeyedData` 

227 The input data from which all `AnalysisTools` will run and produce 

228 outputs. A side note, the python typing specifies that this can be 

229 None, but this is only due to a limitation in python where in order 

230 to specify that all arguments be passed only as keywords the 

231 argument must be given a default. This argument most not actually 

232 be None. 

233 **kwargs 

234 Additional arguments that are passed through to the `AnalysisTools` 

235 specified in the configuration. 

236 

237 Returns 

238 ------- 

239 results : `~lsst.pipe.base.Struct` 

240 The accumulated results of all the plots and metrics produced by 

241 this `PipelineTask`. 

242 

243 Raises 

244 ------ 

245 ValueError 

246 Raised if the supplied data argument is `None` 

247 """ 

248 if data is None: 

249 raise ValueError("data must not be none") 

250 results = Struct() 

251 plotKey = f"{self.config.connections.outputName}_{{name}}" # type: ignore 

252 if "bands" not in kwargs: 

253 kwargs["bands"] = list(self.config.bands) 

254 kwargs["plotInfo"]["bands"] = kwargs["bands"] 

255 for name, value in self.runPlots(data, **kwargs).getDict().items(): 

256 setattr(results, plotKey.format(name=name), value) 

257 for name, value in self.runMetrics(data, **kwargs).getDict().items(): 

258 setattr(results, name, value) 

259 

260 return results 

261 

262 def runQuantum( 

263 self, 

264 butlerQC: ButlerQuantumContext, 

265 inputRefs: InputQuantizedConnection, 

266 outputRefs: OutputQuantizedConnection, 

267 ) -> None: 

268 """Override default runQuantum to load the minimal columns necessary 

269 to complete the action. 

270 

271 Parameters 

272 ---------- 

273 butlerQC : `ButlerQuantumContext` 

274 A butler which is specialized to operate in the context of a 

275 `lsst.daf.butler.Quantum`. 

276 inputRefs : `InputQuantizedConnection` 

277 Datastructure whose attribute names are the names that identify 

278 connections defined in corresponding `PipelineTaskConnections` 

279 class. The values of these attributes are the 

280 `lsst.daf.butler.DatasetRef` objects associated with the defined 

281 input/prerequisite connections. 

282 outputRefs : `OutputQuantizedConnection` 

283 Datastructure whose attribute names are the names that identify 

284 connections defined in corresponding `PipelineTaskConnections` 

285 class. The values of these attributes are the 

286 `lsst.daf.butler.DatasetRef` objects associated with the defined 

287 output connections. 

288 """ 

289 inputs = butlerQC.get(inputRefs) 

290 dataId = butlerQC.quantum.dataId 

291 if dataId is not None: 

292 dataId = DataCoordinate.standardize(dataId, universe=butlerQC.registry.dimensions) 

293 plotInfo = self.parsePlotInfo(inputs, dataId) 

294 data = self.loadData(inputs["data"]) 

295 if "skymap" in inputs.keys(): 

296 skymap = inputs["skymap"] 

297 else: 

298 skymap = None 

299 outputs = self.run(data=data, plotInfo=plotInfo, skymap=skymap) 

300 butlerQC.put(outputs, outputRefs) 

301 

302 def parsePlotInfo( 

303 self, inputs: Mapping[str, Any], dataId: DataCoordinate | None, connectionName: str = "data" 

304 ) -> Mapping[str, str]: 

305 """Parse the inputs and dataId to get the information needed to 

306 to add to the figure. 

307 

308 Parameters 

309 ---------- 

310 inputs: `dict` 

311 The inputs to the task 

312 dataCoordinate: `lsst.daf.butler.DataCoordinate` 

313 The dataId that the task is being run on. 

314 connectionName: `str` 

315 Name of the input connection to use for determining table name. 

316 

317 Returns 

318 ------- 

319 plotInfo : `dict` 

320 """ 

321 

322 if inputs is None: 

323 tableName = "" 

324 run = "" 

325 else: 

326 tableName = inputs[connectionName].ref.datasetType.name 

327 run = inputs[connectionName].ref.run 

328 

329 plotInfo = {"tableName": tableName, "run": run} 

330 

331 if dataId is not None: 

332 for dataInfo in dataId: 

333 plotInfo[dataInfo.name] = dataId[dataInfo.name] 

334 

335 return plotInfo 

336 

337 def loadData(self, handle: DeferredDatasetHandle, names: Iterable[str] | None = None) -> KeyedData: 

338 """Load the minimal set of keyed data from the input dataset. 

339 

340 Parameters 

341 ---------- 

342 handle : `DeferredDatasetHandle` 

343 Handle to load the dataset with only the specified columns. 

344 names : `Iterable` of `str` 

345 The names of keys to extract from the dataset. 

346 If `names` is `None` then the `collectInputNames` method 

347 is called to generate the names. 

348 For most purposes these are the names of columns to load from 

349 a catalog or data frame. 

350 

351 Returns 

352 ------- 

353 result: `KeyedData` 

354 The dataset with only the specified keys loaded. 

355 """ 

356 if names is None: 

357 names = self.collectInputNames() 

358 return cast(KeyedData, handle.get(parameters={"columns": names})) 

359 

360 def collectInputNames(self) -> Iterable[str]: 

361 """Get the names of the inputs. 

362 

363 If using the default `loadData` method this will gather the names 

364 of the keys to be loaded from an input dataset. 

365 

366 Returns 

367 ------- 

368 inputs : `Iterable` of `str` 

369 The names of the keys in the `KeyedData` object to extract. 

370 

371 """ 

372 inputs = set() 

373 for band in self.config.bands: 

374 for name, action in self.config.plots.items(): 

375 for column, dataType in action.getFormattedInputSchema(band=band): 

376 inputs.add(column) 

377 for name, action in self.config.metrics.items(): 

378 for column, dataType in action.getFormattedInputSchema(band=band): 

379 inputs.add(column) 

380 return inputs