Skip to content

Commit

Permalink
Add Instrument.group_name_to_group_id method
Browse files Browse the repository at this point in the history
This supports visit definition from group dimension.
  • Loading branch information
timj committed Feb 1, 2024
1 parent ccf7cc9 commit 01084ba
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/changes/DM-42636.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``Instrument.group_name_to_group_id`` method to convert a group name string to an integer suitable for use as a visit ID.
25 changes: 25 additions & 0 deletions python/lsst/pipe/base/_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import contextlib
import datetime
import os.path
import re
from abc import ABCMeta, abstractmethod
from collections.abc import Sequence
from typing import TYPE_CHECKING, Any, cast, final
Expand Down Expand Up @@ -735,3 +736,27 @@ def _make_default_dimension_packer(
IDs into integers.
"""
return config_dict.apply_with(default, data_id, is_exposure=is_exposure)

@classmethod
def group_name_to_group_id(cls, group_name: str) -> int:
"""Translate the exposure group name to an integer.
Parameters
----------
group_name : `str`
The name of the exposure group.
Returns
-------
id : `int`
The exposure group name in integer form. This integer might be
used as an ID to uniquely identify the group in contexts where
a string can not be used.
Notes
-----
The default implementation removes all non numeric characters and casts
to an integer.
"""
cleaned = re.sub(r"\D", "", group_name)
return int(cleaned)
6 changes: 6 additions & 0 deletions tests/test_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ def check_dimension_packers(
with self.assertRaisesRegex(ValueError, f"Exposure ID {n_exposures} is out of bounds"):
packer.pack(instrument=self.name, detector=1, exposure=n_exposures)

def test_group_name(self):
"""Test group name to ID conversion."""
self.assertEqual(self.instrument.group_name_to_group_id("1:234-5.6"), 123456)
with self.assertRaises(ValueError):
self.instrument.group_name_to_group_id("no_int")


if __name__ == "__main__":
unittest.main()

0 comments on commit 01084ba

Please sign in to comment.