From 74282d6b76e8efbbceee7fc8823c3d7ae15c916c Mon Sep 17 00:00:00 2001 From: Johannes Kasimir Date: Thu, 23 Apr 2026 10:05:41 +0200 Subject: [PATCH] feat: verify type of event_index has 8 bytes --- src/chexus/validators.py | 18 ++++++++++++++++++ tests/validators_test.py | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/chexus/validators.py b/src/chexus/validators.py index 77ee418..dd7dc2c 100644 --- a/src/chexus/validators.py +++ b/src/chexus/validators.py @@ -469,6 +469,24 @@ def validate(self, node: Dataset | Group) -> Violation | None: return Violation(node.name, "NXlog must have a value") +class event_index_is_eight_bytes(Validator): + def __init__(self) -> None: + super().__init__( + "event_index_too_small", "event_index needs to be an 8 byte type" + ) + + def applies_to(self, node: Dataset | Group) -> bool: + return ( + isinstance(node, Dataset) + and node.parent.attrs.get("NX_class") == "NXevent_data" + and node.name == 'event_index' + ) + + def validate(self, node: Dataset | Group) -> Violation | None: + if np.dtype(node.dtype).itemsize < 8: + return Violation(node.name, "event_index type too small") + + def base_validators(*, has_scipp=True): validators = [ depends_on_missing(), diff --git a/tests/validators_test.py b/tests/validators_test.py index deb3104..8cfe612 100644 --- a/tests/validators_test.py +++ b/tests/validators_test.py @@ -798,3 +798,27 @@ def test_NXdetector_pixel_offsets_are_unambiguous_2d_ids_allow_axis_attr() -> No chexus.validators.NXdetector_pixel_offsets_are_unambiguous().validate(det) is None ) + + +def test_event_index_is_eight_bytes() -> None: + parent = chexus.Group(name="event_data", attrs={"NX_class": "NXevent_data"}) + good = chexus.Dataset( + name='event_index', + value=[1, 2, 3], + shape=(3,), + dtype='int64', + parent=parent, + ) + bad = chexus.Dataset( + name='event_index', + value=[1, 2, 3], + shape=(3,), + dtype='int32', + parent=parent, + ) + assert chexus.validators.event_index_is_eight_bytes().applies_to(good) + assert chexus.validators.event_index_is_eight_bytes().applies_to(bad) + assert chexus.validators.event_index_is_eight_bytes().validate(good) is None + assert isinstance( + chexus.validators.event_index_is_eight_bytes().validate(bad), chexus.Violation + )