diff --git a/src/chexus/validators.py b/src/chexus/validators.py index 96c4f22..210c732 100644 --- a/src/chexus/validators.py +++ b/src/chexus/validators.py @@ -307,14 +307,13 @@ def validate(self, node: Dataset | Group) -> Violation | None: return Violation(node.name) -class detector_numbers_unique_in_all_detectors(Validator): +class detector_numbers_unique_in_detector(Validator): def __init__(self) -> None: super().__init__( "detector_numbers are not unique", "The values in all detector_numbers fields in all " "detectors should be unique.", ) - self._seen_detector_numbers = np.array([], dtype='int') def applies_to(self, node: Dataset | Group) -> bool: return ( @@ -324,14 +323,11 @@ def applies_to(self, node: Dataset | Group) -> bool: ) def validate(self, node: Dataset | Group) -> Violation | None: - detector_numbers = np.asarray(node.children['detector_number'].value).ravel() + detector_numbers = np.asarray(node.children['detector_number'].value) if not hasattr(detector_numbers, '__len__'): return - if np.isin(detector_numbers, self._seen_detector_numbers).any(): + if not len(detector_numbers.ravel()) == len(np.unique(detector_numbers)): return Violation(node.name) - self._seen_detector_numbers = np.concatenate( - (self._seen_detector_numbers, detector_numbers) - ) class event_id_subset_of_detector_number(Validator): @@ -480,7 +476,7 @@ def base_validators(*, has_scipp=True): transformation_offset_units_missing(), units_invalid(), NXlog_has_value(), - detector_numbers_unique_in_all_detectors(), + detector_numbers_unique_in_detector(), event_id_subset_of_detector_number(), NXdetector_pixel_offsets_are_unambiguous(), ] diff --git a/tests/validators_test.py b/tests/validators_test.py index 94b38ff..d5bb7e6 100644 --- a/tests/validators_test.py +++ b/tests/validators_test.py @@ -589,7 +589,9 @@ def test_NXlog_nested_top_dead_center_has_no_value(): assert result.name == "chopper/top_dead_center" -@pytest.mark.parametrize('value', [(1, 2, 3), np.array([1, 2, 3]), [[0, 1], [2, 3]]]) +@pytest.mark.parametrize( + 'value', [[1, 2, 3], np.array([1, 2, 3]), np.array([[0, 1], [2, 3]])] +) def test_duplicate_detector_number(value): det = chexus.Group(name="detector1", attrs={"NX_class": "NXdetector"}) det.children = { @@ -601,18 +603,12 @@ def test_duplicate_detector_number(value): parent=det, ) } - det2 = chexus.Group(name="detector2", attrs={"NX_class": "NXdetector"}) - det2.children = { - 'detector_number': chexus.Dataset( - name="detector_number", value=[4, 5, 6], shape=(3,), dtype=int, parent=det2 - ) - } - assert chexus.validators.detector_numbers_unique_in_all_detectors().applies_to(det) - validator = chexus.validators.detector_numbers_unique_in_all_detectors() + assert chexus.validators.detector_numbers_unique_in_detector().applies_to(det) + validator = chexus.validators.detector_numbers_unique_in_detector() assert validator.validate(det) is None - assert validator.validate(det2) is None - # Second time the same detector numbers are seen, we expect a violation - assert validator.validate(det) is not None + # Make one entry duplicate + det.children['detector_number'].value[-1] = 1 + assert isinstance(validator.validate(det), chexus.Violation) def test_event_id_not_in_detector_number():