8000 convert int-like values silently into float-like for NX_FLOAT by rettigl · Pull Request #637 · FAIRmat-NFDI/pynxtools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

convert int-like values silently into float-like for NX_FLOAT #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2025
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
30 changes: 30 additions & 0 deletions src/pynxtools/dataconverter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,30 @@ def convert_str_to_bool_safe(value: str) -> Optional[bool]:
raise ValueError(f"Could not interpret string '{value}' as boolean.")


def convert_int_to_float(value):
"""
Converts int-like values to float, including values in arrays, and lists

Args:
value: The input value, which can be a single value, list, or numpy array.

Returns:
The input value with all int-like values converted to float.
"""
if isinstance(value, int):
return float(value)
elif isinstance(value, list):
return [convert_int_to_float(v) for v in value]
elif isinstance(value, tuple):
return tuple(convert_int_to_float(v) for v in value)
elif isinstance(value, set):
return {convert_int_to_float(v) for v in value}
elif isinstance(value, np.ndarray) and np.issubdtype(value.dtype, np.integer):
return value.astype(float)
else:
return value


def is_valid_data_field(
value: Any, nxdl_type: str, nxdl_enum: list, nxdl_enum_open: bool, path: str
) -> Any:
Expand All @@ -683,6 +707,12 @@ def is_valid_data_field(
collector.collect_and_log(
path, ValidationProblem.InvalidType, accepted_types, nxdl_type
)
elif accepted_types[0] is float:
value = convert_int_to_float(value)
if not is_valid_data_type(value, accepted_types):
collector.collect_and_log(
path, ValidationProblem.InvalidType, accepted_types, nxdl_type
)
else:
collector.collect_and_log(
path, ValidationProblem.InvalidType, accepted_types, nxdl_type
Expand Down
26 changes: 20 additions & 6 deletions tests/dataconverter/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,19 @@ def listify_template(data_dict: Template):
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value" BBD4 ;,
0,
),
[],
id="int-instead-of-float",
),
pytest.param(
alter_dict(
TEMPLATE,
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value",
np.complex128(0),
),
[
"The value at /ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value should be one of the following Python types: (<class 'float'>, <class 'numpy.floating'>), as defined in the NXDL as NX_FLOAT."
],
id="int-instead-of-float",
id="complex-instead-of-float",
),
pytest.param(
alter_dict(
Expand Down Expand Up @@ -534,13 +543,18 @@ def listify_template(data_dict: Template):
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value",
[2], # pylint: disable=E1126
),
[
"The value at /ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value should be "
"one of the following Python types: (<class 'float'>, <class 'numpy.floating'>), as defined in the NXDL "
"as NX_FLOAT."
],
[],
id="list-of-int-instead-of-float",
),
pytest.param(
alter_dict(
TEMPLATE,
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value",
np.array([2]), # pylint: disable=E1126
),
[],
id="array-of-int-instead-of-float",
),
pytest.param(
set_to_none_in_dict(
TEMPLATE,
Expand Down
Loading
0