Skip to content
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
0f46c34
Fix the nx_char type for numpy to and .
RubelMozumder Feb 18, 2025
dd8beb4
Still char instead of the int is being validated which is wrong.
RubelMozumder Feb 19, 2025
351f377
Remove auto conversion for datatype.
RubelMozumder Feb 20, 2025
00b78aa
extends tests.
RubelMozumder Feb 20, 2025
3ad40a0
resolve PR comments
RubelMozumder Feb 27, 2025
6cf6f6d
Remove unnecessary returned value.
RubelMozumder Feb 27, 2025
539bdea
fix np integer and float.
RubelMozumder Feb 27, 2025
cde7a6e
minor change.
RubelMozumder Feb 28, 2025
4ab4b53
check enums for documented fields, and don't return False if any issu…
rettigl Feb 28, 2025
ae10916
add enum checking for attributes
rettigl Feb 18, 2025
8f4291e
Adds parsing code for enumeration tree generation during validation
sherjeelshabih Feb 24, 2025
099b965
Fix typos in old test
sherjeelshabih Feb 24, 2025
4da5735
always check data types and enums, and check NXdata attributes separa…
rettigl Feb 25, 2025
d4f4c2f
fix typos
rettigl Feb 26, 2025
1925d5d
move enum checking into is_valid_data_field, and proper bool conversion
rettigl Feb 28, 2025
ecb4914
satisfy mypy
rettigl Feb 28, 2025
2fae4de
Merge pull request #565 from FAIRmat-NFDI/fix_check_undocumented
rettigl Mar 4, 2025
4a29251
add tests from branch fix_attribute_enum_check
rettigl Mar 4, 2025
31cd131
add review suggestion
rettigl Mar 5, 2025
ca1ef4f
Merge pull request #573 from FAIRmat-NFDI/add_more_tests
sherjeelshabih Mar 5, 2025
3aabe0b
Fixes the types and removes bytes from NX_char as that creates failures
sherjeelshabih Mar 5, 2025
bd478e8
Fixes for arrays in an array
sherjeelshabih Mar 5, 2025
b336ca5
fix mypy error
rettigl Mar 5, 2025
e9b025e
ruff
rettigl Mar 5, 2025
8c89eef
Applies suggested fix
sherjeelshabih Mar 11, 2025
1c5538d
Update src/pynxtools/dataconverter/helpers.py
sherjeelshabih Mar 11, 2025
365f061
Applies fixes from suggestions
sherjeelshabih Mar 11, 2025
bd250cc
Updates
sherjeelshabih Mar 11, 2025
84426b7
Ruff
sherjeelshabih Mar 11, 2025
5a63c3b
Update src/pynxtools/dataconverter/helpers.py
sherjeelshabih Mar 11, 2025
0d056b7
ruff
sherjeelshabih Mar 11, 2025
70a457c
remove empty string
rettigl Mar 11, 2025
b6b112b
Fixes
sherjeelshabih Mar 11, 2025
7892971
move validations tests to test_validation
rettigl Mar 11, 2025
ea22d42
fix converted tests
rettigl Mar 11, 2025
8ffd63b
add additional tests for base class elements
rettigl Mar 11, 2025
02f2721
fix validation issues and add further tests
rettigl Mar 12, 2025
3c28c1f
add case and tests for undocumented units
rettigl Mar 12, 2025
ae22931
Merge pull request #581 from FAIRmat-NFDI/reorder_tests
sherjeelshabih Mar 13, 2025
ed78c74
Merge pull request #577 from FAIRmat-NFDI/more-fixes
sherjeelshabih Mar 13, 2025
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
123 changes: 65 additions & 58 deletions src/pynxtools/dataconverter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,83 +579,88 @@ def is_value_valid_element_of_enum(value, elist) -> Tuple[bool, list]:
NUMPY_INT_TYPES = (np.short, np.intc, np.int_)
NUMPY_UINT_TYPES = (np.ushort, np.uintc, np.uint)
# np int for np version 1.26.0
np_int = (
np.intc,
np.int_,
np.intp,
np.int8,
np.int16,
np.int32,
np.int64,
np.uint8,
np.uint16,
np.uint32,
np.uint64,
np.unsignedinteger,
np.signedinteger,
)
np_float = (np.float16, np.float32, np.float64, np.floating)
np_bytes = (np.bytes_, np.byte, np.ubyte)
np_char = (np.str_, np.char.chararray, *np_bytes)
np_int = (np.integer,)
np_float = (np.floating,)
# Not to be confused with `np.byte` and `np.ubyte`, these store
# an integer of `8bit` and `unsigned 8bit` respectively.
np_bytes = (np.bytes_,)
np_char = (np.str_, np.bytes_) # Only numpy Unicode string and Byte string
np_bool = (np.bool_,)
np_complex = (np.complex64, np.complex128, np.cdouble, np.csingle)
np_complex = (np.complex64, np.complex128, np.cdouble, np.csingle, np.complex_)
NEXUS_TO_PYTHON_DATA_TYPES = {
"ISO8601": (str,),
"NX_BINARY": (
bytes,
bytearray,
np.ndarray,
*np_bytes,
),
"NX_BOOLEAN": (bool, np.ndarray, *np_bool),
"NX_CHAR": (str, np.ndarray, *np_char),
"NX_BOOLEAN": (bool, *np_bool),
"NX_CHAR": (str, *np_char),
"NX_DATE_TIME": (str,),
"NX_FLOAT": (float, np.ndarray, *np_float),
"NX_INT": (int, np.ndarray, *np_int),
"NX_UINT": (np.ndarray, np.unsignedinteger),
"NX_FLOAT": (float, *np_float),
"NX_INT": (int, *np_int),
"NX_UINT": (
np.unsignedinteger,
np.uint,
),
"NX_NUMBER": (
int,
float,
np.ndarray,
*np_int,
*np_float,
dict,
),
"NX_POSINT": (
int,
np.ndarray,
np.signedinteger,
), # > 0 is checked in is_valid_data_field()
"NX_COMPLEX": (complex, np.ndarray, *np_complex),
"NXDL_TYPE_UNAVAILABLE": (str,), # Defaults to a string if a type is not provided.
"NX_COMPLEX": (complex, *np_complex),
"NXDL_TYPE_UNAVAILABLE": (
str,
*np_char,
), # Defaults to a string if a type is not provided.
"NX_CHAR_OR_NUMBER": (
str,
int,
float,
np.ndarray,
*np_char,
*np_int,
*np_float,
dict,
),
}


def check_all_children_for_callable(objects: list, check: Callable, *args) -> bool:
"""Checks whether all objects in list are validated by given callable."""
for obj in objects:
if not check(obj, *args):
return False
def check_all_children_for_callable(
objects: Union[list, np.ndarray],
Comment thread
rettigl marked this conversation as resolved.
Outdated
checker: Optional[Callable] = None,
Comment thread
rettigl marked this conversation as resolved.
Outdated
accepted_types: Optional[tuple] = None,
) -> bool:
"""Checks whether all objects in list or numpy array are validated
by given callable and types.
"""

return True
if checker is not None:
for obj in objects:
args = (obj, accepted_types) if accepted_types is not None else (obj,)
if not checker(*args):
return False
return True
if isinstance(objects, tuple):
return False
if isinstance(objects, list):
# Handles list and list of list
return all([type(elem) in accepted_types for elem in objects])
if isinstance(objects, np.ndarray):
return any([np.issubdtype(objects.dtype, type_) for type_ in accepted_types])

return False


def is_valid_data_type(value, accepted_types):
"""Checks whether the given value or its children are of an accepted type."""
if not isinstance(value, list):

if not isinstance(value, (list, np.ndarray)):
return isinstance(value, accepted_types)

return check_all_children_for_callable(value, isinstance, accepted_types)
return check_all_children_for_callable(objects=value, accepted_types=accepted_types)


def is_positive_int(value):
Expand All @@ -665,7 +670,7 @@ def is_greater_than(num):
return num.flat[0] > 0 if isinstance(num, np.ndarray) else num > 0

if isinstance(value, list):
return check_all_children_for_callable(value, is_greater_than)
return check_all_children_for_callable(objects=value, checker=is_greater_than)

return value.flat[0] > 0 if isinstance(value, np.ndarray) else value > 0

Expand All @@ -685,36 +690,38 @@ def convert_str_to_bool_safe(value):
def is_valid_data_field(value, nxdl_type, path):
# todo: Check this funciton and wtire test for it. It seems the funciton is not
# working as expected.
"""Checks whether a given value is valid according to what is defined in the NXDL.

This function will also try to convert typical types, for example int to float,
and return the successful conversion.
"""Checks whether a given value is valid according to the type defined in the NXDL.

If it fails to convert, it raises an Exception.
This function only tries to convert boolean value in str format (e.g. "true" ) to
python Boolean (True). In case, it fails to convert, it raises an Exception.

Returns two values: first, boolean (True if the the value corresponds to nxdl_type,
False otherwise) and second, result of attempted conversion or the original value
(if conversion is not needed or impossible)
Return:
Bool: (True if the the value corresponds to nxdl_type, False otherwise)
"""
accepted_types = NEXUS_TO_PYTHON_DATA_TYPES[nxdl_type]
output_value = value

accepted_types = NEXUS_TO_PYTHON_DATA_TYPES[nxdl_type]
# Do not count the dict as it represents a link value
if not isinstance(value, dict) and not is_valid_data_type(value, accepted_types):
try:
if accepted_types[0] is bool and isinstance(value, str):
value = convert_str_to_bool_safe(value)
if value is None:
raise ValueError
output_value = accepted_types[0](value)
except ValueError:
return True

collector.collect_and_log(
path, ValidationProblem.InvalidType, accepted_types, nxdl_type
)
return False
except (ValueError, TypeError):
collector.collect_and_log(
path, ValidationProblem.InvalidType, accepted_types, nxdl_type
)
return False, value
return False

if nxdl_type == "NX_POSINT" and not is_positive_int(value):
collector.collect_and_log(path, ValidationProblem.IsNotPosInt, value)
return False, value
return False

if nxdl_type in ("ISO8601", "NX_DATE_TIME"):
iso8601 = re.compile(
Expand All @@ -724,9 +731,9 @@ def is_valid_data_field(value, nxdl_type, path):
results = iso8601.search(value)
if results is None:
collector.collect_and_log(path, ValidationProblem.InvalidDatetime, value)
return False, value
return False

return True, output_value
return True


@lru_cache(maxsize=None)
Expand Down
8 changes: 4 additions & 4 deletions src/pynxtools/dataconverter/validation.py
Comment thread
rettigl marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def handle_field(node: NexusNode, keys: Mapping[str, Any], prev_path: str):
continue

# Check general validity
is_valid_data_field(
_ = is_valid_data_field(
mapping[f"{prev_path}/{variant}"], node.dtype, f"{prev_path}/{variant}"
)

Expand Down Expand Up @@ -468,7 +468,7 @@ def handle_attribute(node: NexusNode, keys: Mapping[str, Any], prev_path: str):
return

for variant in variants:
is_valid_data_field(
_ = is_valid_data_field(
mapping[
f"{prev_path}/{variant if variant.startswith('@') else f'@{variant}'}"
],
Expand Down Expand Up @@ -534,8 +534,8 @@ def is_documented(key: str, node: NexusNode) -> bool:
collector.collect_and_log(
f"{key}", ValidationProblem.MissingUnit, node.unit
)

return is_valid_data_field(mapping[key], node.dtype, key)[0]
is_documented_flag = is_valid_data_field(mapping[key], node.dtype, key)
return is_documented_flag
Comment thread
rettigl marked this conversation as resolved.
Outdated

def recurse_tree(
node: NexusNode,
Expand Down
Loading