|
42 | 42 | generate_tree_from, |
43 | 43 | ) |
44 | 44 | from pynxtools.definitions.dev_tools.utils.nxdl_utils import get_nx_namefit |
| 45 | +from pynxtools.units import NXUnitSet, ureg |
45 | 46 |
|
46 | 47 |
|
47 | 48 | def validate_hdf_group_against(appdef: str, data: h5py.Group): |
@@ -216,6 +217,49 @@ def best_namefit_of( |
216 | 217 | return best_match |
217 | 218 |
|
218 | 219 |
|
| 220 | +def is_valid_unit_for_node( |
| 221 | + node: NexusNode, unit: str, unit_path: str, hints: dict[str, Any] |
| 222 | +) -> None: |
| 223 | + """ |
| 224 | + Validate whether a unit string is compatible with the expected unit category for a given NeXus node. |
| 225 | +
|
| 226 | + This function checks if the provided `unit` string matches the expected unit dimensionality |
| 227 | + defined in the node's `unit` field. Special logic is applied for "NX_TRANSFORMATION", where |
| 228 | + the dimensionality depends on the `transformation_type` hint. |
| 229 | +
|
| 230 | + If the unit does not match the expected dimensionality, a validation problem is logged. |
| 231 | +
|
| 232 | + Args: |
| 233 | + node (NexusNode): The node containing unit metadata to validate against. |
| 234 | + unit (str): The unit string to validate (e.g., "m", "eV", "1", ""). |
| 235 | + unit_path (str): The path to the unit in the NeXus template, used for logging. |
| 236 | + hints (dict[str, Any]): Additional metadata used during validation. For example, |
| 237 | + hints["transformation_type"] may be used to determine the expected unit category |
| 238 | + if the node represents a transformation. |
| 239 | + """ |
| 240 | + # Need to use a list as `NXtransformation` is a special use case |
| 241 | + if node.unit == "NX_TRANSFORMATION": |
| 242 | + if (transformation_type := hints.get("transformation_type")) is not None: |
| 243 | + category_map: dict[str, str] = { |
| 244 | + "translation": "NX_LENGTH", |
| 245 | + "rotation": "NX_ANGLE", |
| 246 | + } |
| 247 | + node_unit_category = category_map.get(transformation_type, "NX_UNITLESS") |
| 248 | + else: |
| 249 | + node_unit_category = "NX_UNITLESS" |
| 250 | + log_input = node_unit_category |
| 251 | + else: |
| 252 | + node_unit_category = node.unit |
| 253 | + log_input = None |
| 254 | + |
| 255 | + if NXUnitSet.matches(node_unit_category, unit): |
| 256 | + return |
| 257 | + |
| 258 | + collector.collect_and_log( |
| 259 | + unit_path, ValidationProblem.InvalidUnit, node, unit, log_input |
| 260 | + ) |
| 261 | + |
| 262 | + |
219 | 263 | def validate_dict_against( |
220 | 264 | appdef: str, mapping: MutableMapping[str, Any], ignore_undocumented: bool = False |
221 | 265 | ) -> bool: |
@@ -607,14 +651,30 @@ def handle_field(node: NexusNode, keys: Mapping[str, Any], prev_path: str): |
607 | 651 |
|
608 | 652 | # Check unit category |
609 | 653 | if node.unit is not None: |
610 | | - remove_from_not_visited(f"{prev_path}/{variant}/@units") |
611 | | - if f"{variant}@units" not in keys: |
612 | | - collector.collect_and_log( |
613 | | - variant_path, |
614 | | - ValidationProblem.MissingUnit, |
615 | | - node.unit, |
616 | | - ) |
617 | | - # TODO: Check unit with pint |
| 654 | + unit_path = f"{variant_path}/@units" |
| 655 | + if node.unit != "NX_UNITLESS": |
| 656 | + remove_from_not_visited(unit_path) |
| 657 | + if f"{variant}@units" not in keys and ( |
| 658 | + node.unit != "NX_TRANSFORMATION" |
| 659 | + or mapping.get(f"{variant_path}/@transformation_type") |
| 660 | + in ("translation", "rotation") |
| 661 | + ): |
| 662 | + collector.collect_and_log( |
| 663 | + variant_path, |
| 664 | + ValidationProblem.MissingUnit, |
| 665 | + node.unit, |
| 666 | + ) |
| 667 | + break |
| 668 | + |
| 669 | + unit = keys.get(f"{variant}@units") |
| 670 | + # Special case: NX_TRANSFORMATION unit depends on `@transformation_type` attribute |
| 671 | + if ( |
| 672 | + transformation_type := keys.get(f"{variant}@transformation_type") |
| 673 | + ) is not None: |
| 674 | + hints = {"transformation_type": transformation_type} |
| 675 | + else: |
| 676 | + hints = {} |
| 677 | + is_valid_unit_for_node(node, unit, unit_path, hints) |
618 | 678 |
|
619 | 679 | field_attributes = get_field_attributes(variant, keys) |
620 | 680 | field_attributes = _follow_link(field_attributes, variant_path) |
@@ -820,9 +880,13 @@ def is_documented(key: str, tree: NexusNode) -> bool: |
820 | 880 | and node.unit is not None |
821 | 881 | and f"{key}/@units" not in mapping |
822 | 882 | ): |
823 | | - collector.collect_and_log( |
824 | | - f"{key}", ValidationProblem.MissingUnit, node.unit |
825 | | - ) |
| 883 | + # Workaround for NX_UNITLESS of NX_TRANSFORMATION unit category |
| 884 | + if node.unit != "NX_TRANSFORMATION" or mapping.get( |
| 885 | + f"{key}/@transformation_type" |
| 886 | + ) in ("translation", "rotation"): |
| 887 | + collector.collect_and_log( |
| 888 | + f"{key}", ValidationProblem.MissingUnit, node.unit |
| 889 | + ) |
826 | 890 |
|
827 | 891 | return True |
828 | 892 |
|
@@ -1298,6 +1362,10 @@ def check_reserved_prefix( |
1298 | 1362 | check_attributes_of_nonexisting_field(tree) |
1299 | 1363 |
|
1300 | 1364 | for not_visited_key in not_visited: |
| 1365 | + if mapping.get(not_visited_key) is None: |
| 1366 | + # This value is not really set. Skip checking its validity. |
| 1367 | + continue |
| 1368 | + |
1301 | 1369 | # TODO: remove again if "@target"/"@reference" is sorted out by NIAC |
1302 | 1370 | always_allowed_attributes = ("@target", "@reference") |
1303 | 1371 | if not_visited_key.endswith(always_allowed_attributes): |
@@ -1344,6 +1412,20 @@ def check_reserved_prefix( |
1344 | 1412 | mapping[not_visited_key], |
1345 | 1413 | ) |
1346 | 1414 |
|
| 1415 | + if node.unit is not None: |
| 1416 | + # Special case: NX_TRANSFORMATION unit depends on `@transformation_type` attribute |
| 1417 | + if ( |
| 1418 | + transformation_type := mapping.get( |
| 1419 | + not_visited_key.replace("/@units", "/@transformation_type") |
| 1420 | + ) |
| 1421 | + ) is not None: |
| 1422 | + hints = {"transformation_type": transformation_type} |
| 1423 | + else: |
| 1424 | + hints = {} |
| 1425 | + is_valid_unit_for_node( |
| 1426 | + node, mapping[not_visited_key], not_visited_key, hints |
| 1427 | + ) |
| 1428 | + |
1347 | 1429 | # parent key will be checked on its own if it exists, because it is in the list |
1348 | 1430 | continue |
1349 | 1431 |
|
|
0 commit comments