Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/chexus/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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):
Expand Down Expand Up @@ -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(),
]
Expand Down
20 changes: 8 additions & 12 deletions tests/validators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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():
Expand Down