Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-42088 Add a check for invalid characters in measurement names #219

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions python/lsst/analysis/tools/atools/photometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ def setDefaults(self):
self.produce.metric.units = {"median": "mmag", "sigmaMad": "mmag", "mean": "mmag"}

self.produce.metric.newNames = {
"median": "{band}_ap12-psf_median",
"mean": "{band}_ap12-psf_mean",
"sigmaMad": "{band}_ap12-psf_sigmaMad",
"median": "{band}_ap12_psf_diff_median",
"mean": "{band}_ap12_psf_diff_mean",
"sigmaMad": "{band}_ap12_psf_diff_sigmaMad",
}


Expand Down
18 changes: 17 additions & 1 deletion python/lsst/analysis/tools/interfaces/_stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

__all__ = ("BasePrep", "BaseProcess", "BaseMetricAction", "BaseProduce")

import logging
from collections import abc
from typing import Any, cast

Expand All @@ -44,6 +45,8 @@
)
from ._interfaces import KeyedData, KeyedDataSchema, KeyedDataTypes, Scalar, Vector

_LOG = logging.getLogger(__name__)


class BasePrep(KeyedDataAction):
"""Base class for actions which prepare data for processing."""
Expand Down Expand Up @@ -182,13 +185,26 @@ def __call__(self, data: KeyedData, **kwargs) -> KeyedData:
return results


def _newNameChecker(value: str) -> bool:
if "-" in value:
# Yes this should be a log here, as pex config provides no other
# useful way to get info into the exception.
_LOG.error("Remapped metric names must not have a - character in them.")
return False
return True


class BaseMetricAction(MetricAction):
"""Base class for actions which compute metrics."""

units = DictField[str, str](doc="Mapping of scalar key to astropy unit string", default={})
newNames = DictField[str, str](
doc="Mapping of key to new name if needed prior to creating metric",
doc=(
"Mapping of key to new name if needed prior to creating metric, "
"cannot contain a minus character in the name."
),
default={},
itemCheck=_newNameChecker,
)

def getInputSchema(self) -> KeyedDataSchema:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_analysisTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ def testFinalize(self):
self.assertEqual(self.e.name, "BE_final")


class TestMetricNameValidation(TestCase):
"""Test that disallowed characters in measurement names are filtered."""

def testMinusNameFails(self):
tool = AnalysisTool()
with self.assertRaises(pexConfig.FieldValidationError):
tool.produce.metric.newNames = {"something": "not-allowed"}


class LoadFromPipelineTestCase(TestCase):
"""Test that analysis tools can be loaded from a pipeline"""

Expand Down