Skip to content

Commit

Permalink
Add check on measurement names
Browse files Browse the repository at this point in the history
Add a check when specifying measurement names that they do not contain
characters that will be a problem for avro schemas.
  • Loading branch information
natelust committed Mar 14, 2024
1 parent c6946d0 commit d8a7716
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
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

0 comments on commit d8a7716

Please sign in to comment.