Skip to content

Commit

Permalink
Add config access to getOutputNames()
Browse files Browse the repository at this point in the history
  • Loading branch information
enourbakhsh committed Sep 21, 2023
1 parent 1887775 commit 5267e5a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
27 changes: 23 additions & 4 deletions python/lsst/analysis/tools/interfaces/_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from dataclasses import dataclass
from typing import Iterable

import lsst.pex.config as pexConfig
from lsst.pex.config.configurableActions import ConfigurableAction, ConfigurableActionField

from ..contexts import ContextApplier
Expand Down Expand Up @@ -193,10 +194,16 @@ class PlotAction(AnalysisAction):
a `Mapping` of `str` to `PlotType` when called.
"""

def getOutputNames(self) -> Iterable[str]:
def getOutputNames(self, config: pexConfig.Config | None = None) -> Iterable[str]:
"""Returns a list of names that will be used as keys if this action's
call method returns a mapping. Otherwise return an empty Iterable
Parameters
----------
config : `lsst.pex.config.Config`, optional
Configuration of the task. This is only used if the output naming
needs to be config-aware.
Returns
-------
result : `Iterable` of `str`
Expand Down Expand Up @@ -251,14 +258,26 @@ def getInputSchema(self) -> KeyedDataSchema:
yield from self.metric.getInputSchema()
yield from self.plot.getInputSchema()

def getOutputNames(self) -> Iterable[str]:
def getOutputNames(self, config: pexConfig.Config | None = None) -> Iterable[str]:
"""Returns a list of names that will be used as keys if this action's
call method returns a mapping. Otherwise return an empty Iterable
call method returns a mapping. Otherwise return an empty Iterable.
Parameters
----------
config : `lsst.pex.config.Config`, optional
Configuration of the task. This is only used if the output naming
needs to be config-aware.
Returns
-------
result : `Iterable` of `str`
If a `PlotAction` produces more than one plot, this should be the
keys the action will use in the returned `Mapping`.
"""
return self.plot.getOutputNames()
if config is None:
# `dynamicOutputNames`` is set to False.
outNames = self.plot.getOutputNames()
else:
# `dynamicOutputNames`` is set to True.
outNames = self.plot.getOutputNames(config=config)
return outNames
18 changes: 15 additions & 3 deletions python/lsst/analysis/tools/interfaces/_analysisTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from functools import wraps
from typing import Callable, Iterable, Protocol, runtime_checkable

import lsst.pex.config as pexConfig
from lsst.pex.config import Field, ListField
from lsst.pex.config.configurableActions import ConfigurableActionField
from lsst.verify import Measurement
Expand All @@ -38,7 +39,7 @@

@runtime_checkable
class _HasOutputNames(Protocol):
def getOutputNames(self) -> Iterable[str]:
def getOutputNames(self, config: pexConfig.Config | None = None) -> Iterable[str]:
...


Expand Down Expand Up @@ -133,6 +134,11 @@ def __init_subclass__(cls: type[AnalysisTool], **kwargs):
if "finalize" in vars(cls):
cls.finalize = _finalizeWrapper(cls.finalize, cls)

dynamicOutputNames: bool | Field[bool] = False
"""Determines whether to grant the getOutputNames() method access to config
parameters.
"""

parameterizedBand: bool | Field[bool] = True
"""Specifies if an `AnalysisTool` may parameterize a band within any field
in any stage, or if the set of bands is already uniquely determined though
Expand Down Expand Up @@ -234,14 +240,20 @@ def populatePrepFromProcess(self):
"""
self.prep.addInputSchema(self.process.getInputSchema())

def getOutputNames(self) -> Iterable[str]:
def getOutputNames(self, config: pexConfig.Config | None = None) -> Iterable[str]:
"""Return the names of the plots produced by this analysis tool.
If there is a `PlotAction` defined in the produce action, these names
will either come from the `PlotAction` if it defines a
``getOutputNames`` method (likely if it returns a mapping of figures),
or a default value is used and a single figure is assumed.
Parameters
----------
config : `lsst.pex.config.Config`, optional
Configuration of the task. This is only used if the output naming
needs to be config-aware.
Returns
-------
result : `tuple` of `str`
Expand All @@ -251,7 +263,7 @@ def getOutputNames(self) -> Iterable[str]:
case JointAction(plot=NoPlot()):
return tuple()
case _HasOutputNames():
outNames = tuple(self.produce.getOutputNames())
outNames = tuple(self.produce.getOutputNames(config=config))
case _:
raise ValueError(f"Unsupported Action type {type(self.produce)} for getting output names")

Expand Down
8 changes: 6 additions & 2 deletions python/lsst/analysis/tools/interfaces/_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,15 @@ def __init__(self, *, config: AnalysisBaseConfig = None): # type: ignore
# AnalysisPlots.
names: list[str] = []
for action in config.atools:
if action.dynamicOutputNames:
outputNames = action.getOutputNames(config=config)
else:
outputNames = action.getOutputNames()
if action.parameterizedBand:
for band in config.bands:
names.extend(name.format(band=band) for name in action.getOutputNames())
names.extend(name.format(band=band) for name in outputNames)
else:
names.extend(action.getOutputNames())
names.extend(outputNames)

# For each of the names found, create output connections.
for name in names:
Expand Down

0 comments on commit 5267e5a

Please sign in to comment.